Last active
August 29, 2015 14:07
-
-
Save svanscho/bfce22997852e80cdc61 to your computer and use it in GitHub Desktop.
ArduinoAmbisphere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Ethernet.h> | |
//******************************************************************************** | |
// Webserver section | |
//******************************************************************************** | |
// MAC address from Ethernet shield sticker under board | |
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x6D, 0x7E }; | |
IPAddress ip(10, 26, 12, 200); // IP address, may need to change depending on network | |
EthernetServer server(80); // create a server at port 80 | |
//url parsing variables | |
String readString = ""; //string for fetching data from url | |
String sensorString = ""; //string for sensor number (name) | |
String stateString = ""; //string for sensor state | |
const String putSensorPath = "PUT /sensor/"; //url path to filter | |
const String getSensorPath = "GET /sensor/"; //url path to filter | |
int ind1 = 0; //helper variable for parsing url string | |
int ind2 = 0; //helper variable for parsing url string | |
//******************************************************************************** | |
// Sensor section | |
//******************************************************************************** | |
//var to store sensor states -- at startup set all sensors to off | |
int sensorState[]={0,0,0,0}; //first index (0) is not used | |
//sensor configuration | |
const int antennaPin = 8; | |
const int sendRetries = 5; | |
char sensor1_on[101] = "1000100010001110100011101000100010001110100011101000111010001110100011101000111010001110100011101000"; | |
char sensor1_off[101] = "1000100010001110100011101000100010001110100011101000111010001110100011101000111010001110100010001000"; | |
char sensor2_on[101] = "1000100010001110100011101000111010001000100011101000111010001110100011101000111010001110100011101000"; | |
char sensor2_off[101] = "1000100010001110100011101000111010001000100011101000111010001110100011101000111010001110100010001000"; | |
char sensor3_on[101] = "1000100010001110100011101000111010001110100010001000111010001110100011101000111010001110100011101000"; | |
char sensor3_off[101] = "1000100010001110100011101000111010001110100010001000111010001110100011101000111010001110100010001000"; | |
//******************************************************************************** | |
// Helper functions | |
//******************************************************************************** | |
// Function to send the output code to the RF transmitter connected to GPIO. | |
void sendCode(char* szCode) | |
{ | |
int t=422; //422us period | |
for (int iSend = 0 ; iSend < sendRetries ; iSend++) //send the code X times to make sure it is received | |
{ | |
for (int i = 0 ; i < strlen(szCode) ; i++) | |
{ | |
if (szCode[i] == '1') | |
{ | |
digitalWrite(antennaPin, HIGH); | |
delayMicroseconds(t); | |
} | |
else | |
{ | |
digitalWrite(antennaPin, LOW); | |
delayMicroseconds(t); | |
} | |
} | |
delay(12); | |
} | |
} | |
void setSwitch(int sensor, int state){ | |
char codeToSend[101]; | |
switch(sensor){ | |
case 1: | |
if (state == 0){ | |
strncpy(codeToSend,sensor1_off,101); | |
} | |
else{ | |
strncpy(codeToSend,sensor1_on,101); | |
} | |
break; | |
case 2: | |
if (state == 0){ | |
strncpy(codeToSend,sensor2_off,101); | |
} | |
else{ | |
strncpy(codeToSend,sensor2_on,101); | |
} | |
break; | |
case 3: | |
if (state == 0){ | |
strncpy(codeToSend,sensor3_off,101); | |
} | |
else{ | |
strncpy(codeToSend,sensor3_on,101); | |
} | |
break; | |
} | |
sendCode(codeToSend); | |
} | |
/*void kerstboom(){ | |
int toggle = 2000; | |
//on and off | |
for (int j=0;j<=1;j++){ | |
//for all sensors | |
for (int i=1;i<=3;i++){ | |
setSwitch(i,j); | |
delay(100); | |
} | |
delay(toggle); | |
} | |
} | |
*/ | |
String getJsonSensorState(int sensor){ | |
String response = ""; | |
response+="{\"sensor\": \""; | |
response+=String(sensor); | |
response+="\",\"state\": "; | |
response+=String(getSensorState(sensor)); | |
response+="}"; | |
return response; | |
} | |
int getSensorState(int sensor){ | |
int state = sensorState[sensor]; | |
setSwitch(sensor,state); //when requesting, force the state again just to be sure | |
return state; | |
} | |
void setSensorState(int sensor, int state){ | |
sensorState[sensor]=state; | |
setSwitch(sensor,state); | |
} | |
//******************************************************************************** | |
// Main code block | |
//******************************************************************************** | |
void setup() | |
{ | |
// disable the SD card by switching pin 4 high | |
// not using the SD card in this program, but if an SD card is left in the socket, | |
// it may cause a problem with accessing the Ethernet chip, unless disabled | |
pinMode(4, OUTPUT); | |
digitalWrite(4, HIGH); | |
//setup ethernet shield | |
Ethernet.begin(mac, ip); // initialize Ethernet device | |
server.begin(); // start to listen for clients | |
//setup antenna | |
pinMode(antennaPin, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
EthernetClient client = server.available(); // try to get client | |
if (client) { // got client? | |
readString=""; | |
sensorString=""; | |
stateString=""; | |
boolean validRequest = false; | |
while (client.connected()) { | |
if (client.available()) { // client data available to read | |
char c = client.read(); // read 1 byte (character) from client | |
if (readString.length() < 100) { | |
//store characters to string | |
readString+=(c); | |
} | |
if (c == '\n' && readString.charAt(0)=='\r'){ | |
//Serial.println("end message detected"); | |
//the full http request is parsed, did we receive a valid GET or PUT request? | |
if (validRequest && sensorString.toInt()>=1 && sensorString.toInt()<4){ | |
//headers | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Connection: close"); | |
client.println("Content-Type: application/json"); | |
client.println(); | |
//body | |
client.println(getJsonSensorState(sensorString.toInt())); | |
} | |
//we couldn't found the requested path, send HTTP 404 | |
else{ | |
client.println("HTTP/1.1 404 Not Found"); | |
client.println("Connection: close"); | |
} | |
break; | |
} | |
if (c == '\n') { | |
//Serial.println("end line detected"); | |
if(readString.startsWith(putSensorPath)) { //PUT request for sensor sring | |
ind1 = readString.lastIndexOf("/sensor/")+8; | |
ind2 = readString.lastIndexOf(" HTTP"); | |
//capture front part of command string | |
sensorString = readString.substring(ind1, ind2); | |
ind1 = sensorString.lastIndexOf("/"); | |
ind2 = sensorString.length(); | |
stateString = sensorString.substring(ind1+1, ind2); | |
sensorString = sensorString.substring(0,ind1); | |
if (sensorString.toInt() != 0){ | |
setSensorState(sensorString.toInt(),stateString.toInt()); | |
} | |
//set vars for response data | |
validRequest = true; | |
} | |
if(readString.startsWith(getSensorPath)) { //GET request for sensor sring | |
ind1 = readString.lastIndexOf("/sensor/")+8; | |
ind2 = readString.lastIndexOf(" HTTP"); | |
//capture front part of command string | |
sensorString = readString.substring(ind1, ind2); | |
ind1 = sensorString.indexOf("/"); | |
ind2 = sensorString.length(); | |
sensorString = sensorString.substring(ind1+1, ind2); | |
//set vars for response data | |
validRequest = true; | |
} | |
//Serial.println(readString); | |
//one line of the http header is read, reset readString var new lines | |
readString=""; | |
} | |
} // end if (client.available()) | |
} // end while (client.connected()) | |
delay(1); // give the web browser time to receive the data | |
client.stop(); // close the connection | |
} // end if (client) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment