-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathardo-rest-server.ino
138 lines (105 loc) · 3.05 KB
/
ardo-rest-server.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// include Libraries
#include <SPI.h>
#include <Ethernet.h>
#include <aREST.h>
// Enter a MAC address for your controller below.
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xFE, 0x40 };
// IP address in case DHCP fails
IPAddress ip(10, 0, 0, 3);
// Ethernet server
EthernetServer server(80);
// Create aREST instance
aREST rest = aREST();
/* SET PINS */
int speakerPin = 9;
int temperaturePin = 0;
int lightPin = 1;
void setup(void)
{
pinMode(speakerPin, OUTPUT);
// Start Serial
Serial.begin(9600);
// Function to be exposed
rest.function("play", playSong);
rest.function("temp", temperature);
rest.function("light", light);
// Give name and ID to device
rest.set_id("001");
rest.set_name("Ardo");
// Start the Ethernet connection and the server
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
rest.handle(client);
}
/* Route: /play */
// Custom function accessible by the API
int playSong(String command) {
int songSize = command.length() + 1;
int songLength = command.length();
int tempo = 150;
char song[songSize];
command.toCharArray(song, songSize); // a space represents a rest
for (int i = 0; i < songLength; i++) {
if (song[i] == ' ') {
delay(1 * tempo); // rest
} else {
playNote(song[i], 1 * tempo);
}
// pause between notes
delay(tempo / 2);
}
return 1;
}
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
/* Route: temp */
int temperature(String command) {
Serial.println("Starting temperature");
//getting the voltage reading from the tem
float temperature = getVoltage(temperaturePin);
// perature sensor
temperature = (temperature - .5) * 100;
return (int)temperature;
}
float getVoltage(int pin) {
//converting from a 0 to 1023 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
return (analogRead(pin) * .004882814);
}
/* Route: light */
int light(String command) {
// Read the lightlevel
int lightLevel = analogRead(lightPin);
//adjust the value 0 to 900 to span 0 to 255
lightLevel = map(lightLevel, 0, 900, 0, 255);
// make sure the value is between 0 and 255
//lightLevel = constrain(lightLevel, 0, 255);
return lightLevel;
}