I am not allowed to upload a file so I am pasting my code here, please let me know if there is any better way to share it:
#include <Arduino.h>
#include <Ethernet.h>
#include <TimeLib.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <EEPROM-Storage.h>
// Network
EthernetServer server(80);
IPAddress defaultIP({192, 168, 0, 10});
#define STRING_BUFFER_SIZE 128
byte mac[] = {0x13, 0x03, 0x0C, 0x0C, 0x0C, 0x0C};
// ++++++++++++++++++++ +++ ++++++++++++ Setup ++++++++++++++++++++ +++ ++++++++++++
void setup() {
Serial.begin(9600);
server.begin();
delay(1000);
Ethernet.begin(mac, defaultIP);
delay(1000);
Serial.print(F("server is at "));
Serial.println(Ethernet.localIP());
setTime(0);
}
// ++++++++++++++++++++ +++ ++++++++++++ Loop ++++++++++++++++++++ +++ ++++++++++++
void loop() {
delay(500);
handleTraffic();
}
// ++++++++++++++++++++ +++ ++++++++++++ Network ++++++++++++++++++++ +++ ++++++++++++
void handleCommand(EthernetClient client, char* cmd, char* param) {
Serial.println("Handle command cmd and param: ");
Serial.println(cmd);
Serial.println(param);
if (strcmp(cmd, "state") == 0) {
Serial.println(F("### Computing: state"));
sendStatusJSON(client, false);
}
else if (strcmp(cmd, "setPower") == 0) {
Serial.println(F("### Computing: power"));
if(strcmp(param, "on")==0){
//setOn();
sendJSON (client, F("statePower"), F("\"On\""));
}
else if(strcmp(param, "off")==0){
//setOff();
sendJSON (client, F("statePower"), F("\"Off\""));
}
else if(strcmp(param, "standby")==0){
//setStandBy();
sendJSON (client, F("statePower"), F("\"standby\""));
}
}
else if (strcmp(cmd, "resetIP") == 0) {
//resetIP();
sendJSON (client, F("cmd"), F("\"Trying to reset IP address\""));
}
else if (strcmp(cmd, "setIP") == 0) {
Serial.println(F("### Computing: setIP"));
sendJSON (client, F("cmd"), F("\"Trying to change IP address\""));
//storedIP = parseIP(param);
//Ethernet.setLocalIP(storedIP);
Serial.print(F("server is at "));
Serial.println(Ethernet.localIP());
}
else if (strcmp(cmd, "setIPProj") == 0) {
Serial.println(F("### Computing: setIP Projector"));
sendJSON (client, F("cmd"), F("\"Changing my record of IP address of Projector\""));
//storedIP_Projector = parseIP(param);
}
else {
send404(client);
}
}
// ++++++++++++++++++++ +++ ++++++++++++ Send Answers ++++++++++++++++++++ +++ ++++++++++++
void send404(EthernetClient client) {
client.println(F("HTTP/1.1 404 OK"));
client.println(F("Content-Type: text/html"));
client.println(F("Access-Control-Allow-Headers: *"));
client.println(F("Access-Control-Allow-Origin: *"));
client.println(F("Access-Control-Allow-Methods: *"));
client.println(F("Connnection: close"));
client.println();
client.println(F("<!DOCTYPE HTML>"));
client.println(F("<html><body>404</body></html>"));
}
void sendJSON(EthernetClient client, String description, String value) {
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: application/json"));
client.println(F("Access-Control-Allow-Headers: *"));
client.println(F("Access-Control-Allow-Origin: *"));
client.println(F("Access-Control-Allow-Methods: *"));
client.println(F("Connnection: close"));
client.println();
client.println(F("{"));
client.print(F("\t\"")); client.print(description); client.print(F("\": "));
client.print(value);
client.println();
client.println(F("}"));
client.println(F("\n"));
return;
}
void sendStatusJSON(EthernetClient client, boolean expert) {
client.println(F("HTTP/1.1 200 OK"));
client.println(F("Content-Type: application/json"));
client.println(F("Access-Control-Allow-Headers: *"));
client.println(F("Access-Control-Allow-Origin: *"));
client.println(F("Access-Control-Allow-Methods: *"));
client.println(F("Connnection: close"));
client.println();
client.println(F("{"));
client.print(F("\t\"state-.-.-.-.-.\": "));
client.println(F(","));
client.println();
client.println(F("}"));
client.println(F("\n"));
return;
}
// ++++++++++++++++++++ +++ ++++++++++++ Webserver ++++++++++++++++++++ +++ ++++++++++++
char** parse(char* str) {
char ** messages;
messages = (char**)malloc(sizeof(char *));
char *p;
p = strtok(str, " ");
unsigned int i = 0;
while (p != NULL) {
p = strtok(NULL, "/");
char *sp;
boolean last = false;
sp = strchr(p, ' ');
if (sp != NULL) {
*sp++ = '\0';
last = true;
}
messages[i] = p;
i++;
if (last) {
break;
}
messages = (char**)realloc(messages, sizeof(char *) * i + 1);
}
messages[i] = '\0';
return messages;
}
int countSegments(char* str) {
int p = 0;
int count = 0;
while (str[p] != '\0') {
if (str[p] == '/') {
count++;
}
p++;
}
count--;
return count;
}
void handleTraffic() {
char buffer[STRING_BUFFER_SIZE];
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c;
int bufindex = 0; // reset buffer
buffer[0] = client.read();
buffer[1] = client.read();
bufindex = 2;
while (buffer[bufindex - 2] != '\r' && buffer[bufindex - 1] != '\n') {
c = client.read();
if (bufindex < STRING_BUFFER_SIZE) {
buffer[bufindex] = c;
}
bufindex++;
}
bufindex = 0;
int nSegments = countSegments(buffer);
char **pathsegments = parse(buffer);
if (c == '\n' && currentLineIsBlank) {
handleCommand(client, pathsegments[0], pathsegments[1]);
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(100);
client.stop();
Serial.println(F("Request answered"));
}
}