I did have the code do polling at first, but after reading on attachinterrupts i thought that was the better way.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include<stdio.h>
#include<stdlib.h>
#include <ArduinoJson.h>
#include <WiFiManager.h>
#include <ESP8266HTTPClient.h>
#include <string.h>;
ESP8266WebServer server(80);
int relayPin = D2;
bool relaystate = LOW;
int heartbeatcheck = 0;
int myheartbeatcount = 0;
int sentcount =0;
int intervalChk =5; //from getVariables to change how often it should send data in by default every 5 min
int sense_Pin = 0; // sensor input at Analog pin A0
int value = 0;
int sendNoteChk =61;
int buttonPin = D5; // the number of the pushbutton pin
int buttonState = 0;
volatile byte interruptFlag = 0;
char clientkey[] = "";
char taskkey[] = "";
void setup()
{
// put your setup code here, to run once:
pinMode(relayPin, OUTPUT);
Serial.begin(115200);
WiFiManager wifiManager;
wifiManager.autoConnect("ShieldOneAP");
attachInterrupt(digitalPinToInterrupt(buttonPin), InterruptHandler, FALLING);
server.on("/on", turnOn); //Associate the handler function to the path
server.on("/off", turnOff); //Associate the handler function to the path
server.on("/toggle", toggle); //Associate the handler function to the path
server.begin(); //Start the server
Serial.println("Server listening");
setvariables();
}
void loop()
{
// for web server
server.handleClient();
if(interruptFlag > 0)
{
senddata(1,"Shop Lights has been toggled");
toggle();
delay(2000);
interruptFlag = 0;
}
if(heartbeatcheck > 18000000)
{
Serial.println("heartbeat ");
senddata(99999,"HeartBeat"); //if using senddata function five 9s means heartbeat
delay( 1000 );
Serial.println("Variable check ");
getvariables();
delay( 1000 );
heartbeatcheck = 0;
}
else
{
heartbeatcheck++;
}
/* works, commented out in favor of attachInterrupt
buttonState = digitalRead( buttonPin );
// Now do stuff that should be triggered by the button press
if ( buttonState == HIGH )
{
delay( 250 ); // This prevents the button state from being read again for half a second.
}
else
{
Serial.println("switch has been pushed");
senddata(buttonState,"1");
delay( 500 );
toggle();
delay(500);
}
*/
}
void InterruptHandler()
{
interruptFlag++;
}
void turnOn()
{
Serial.println("turn on");
relaystate = HIGH;
digitalWrite(relayPin, relaystate);
server.send(200, "text/plain", "Lights turned on");
sendnotification("Shop Lights Turned On");
}
void turnOff()
{
Serial.println("turn off");
relaystate = LOW;
digitalWrite(relayPin, relaystate);
server.send(200, "text/plain", "Lights turned off");
sendnotification("Shop Lights Turned Off");
}
void toggle()
{
bool x = relaystate;
relaystate = !relaystate;
digitalWrite(relayPin, relaystate);
server.send(200, "text/plain", "Lights toggled");
if(x == LOW)
{
sendnotification("Shop Lights Toggled Off");
}
else
{
sendnotification("Shop Lights Toggled On");
}
}
void senddata(int myreading,char mybody[1200])
{
HTTPClient myhttp;
myhttp.begin("http://yousvc.azurewebsites.net/api/SendReading");
myhttp.addHeader("Content-Type", "application/json");
const size_t bufferSize = JSON_OBJECT_SIZE(4) + 140;
DynamicJsonBuffer jsonBuffer(bufferSize);
char wrkjson[500] = "{"ClientKey":"";
strcat(wrkjson, clientkey);
strcat(wrkjson,"","TaskId":"");
strcat(wrkjson, taskkey);
strcat(wrkjson,"","MsgBody":"");
strcat(wrkjson, mybody);
strcat(wrkjson, "","Reading":");
char wrk[7];
sprintf(wrk,"%ld", myreading);
strcat(wrkjson, wrk);
strcat(wrkjson, "}");
Serial.print(wrkjson);
const char* json = wrkjson;
int httpCode = myhttp.POST((uint8_t *)json,strlen(json));
if(httpCode == HTTP_CODE_OK)
{
Serial.print("HTTP response code ");
Serial.println(httpCode);
String response = myhttp.getString();
Serial.println("send reading " + response);
}
else
{
Serial.println("Error in HTTP request");
Serial.println(httpCode);
}
myhttp.end();
}
void getvariables()
{
int result;
int result2;
HTTPClient myhttp;
myhttp.begin("http://yousvc.azurewebsites.net/api/TaskVariable");
myhttp.addHeader("Content-Type", "application/json");
char wrkjson[500] = "{"ClientKey":"";
strcat(wrkjson, clientkey);
strcat(wrkjson,"","TaskKey":"");
strcat(wrkjson, taskkey);
strcat(wrkjson,""}");
Serial.print(wrkjson);
const char* json = wrkjson;
int httpCode = myhttp.POST((uint8_t *)json,strlen(json));
if(httpCode == HTTP_CODE_OK)
{
Serial.print("HTTP response code ");
Serial.println(httpCode);
String response = myhttp.getString();
Serial.println("get var " + response);
char myresponse[975];
response.toCharArray(myresponse,975);
StaticJsonBuffer<975> JSONBuffer; //Memory pool
JsonObject& parsed = JSONBuffer.parseObject(myresponse); //Parse message
if (!parsed.success())
{
Serial.println("Parsing failed");
delay(5000);
return;
}
const char * sensorType = parsed["TheSwitchValue"]; //Get sensor type value
Serial.println(sensorType);
result = strcmp(sensorType, "true");
if (result == 0)
{
//Serial.print("entering");
turnOn();
setToggleSwitch();
}
const char * sensorType2 = parsed["TheSwitchValue2"]; //Get sensor type value
Serial.println(sensorType2);
result2 = strcmp(sensorType2, "true");
if (result2 == 0)
{
//Serial.print("entering");
turnOff();
setToggleSwitch();
}
}
else
{
Serial.println("Error in HTTP request");
Serial.println(httpCode);
}
myhttp.end();
}
void setvariables()
{
char buffer[100];
sprintf(buffer, WiFi.localIP().toString().c_str());
HTTPClient myhttp;
myhttp.begin("http://yousvc.azurewebsites.net/api/SetTaskVariable");
myhttp.addHeader("Content-Type", "application/json");
const size_t bufferSize = JSON_OBJECT_SIZE(4) + 140;
DynamicJsonBuffer jsonBuffer(bufferSize);
char wrkjson[800] = "{"ClientKey":"";
strcat(wrkjson, clientkey);
strcat(wrkjson,"","TaskKey":"");
strcat(wrkjson, taskkey);
strcat(wrkjson,"","IpAddress":"");
strcat(wrkjson, buffer);
strcat(wrkjson,""}");
Serial.print(wrkjson);
const char* json = wrkjson;
int httpCode = myhttp.POST((uint8_t *)json,strlen(json));
if(httpCode == HTTP_CODE_OK)
{
Serial.print("HTTP response code ");
Serial.println(httpCode);
String response = myhttp.getString();
Serial.println("set the var " + response);
}
else
{
Serial.println("Error in HTTP request");
Serial.println(httpCode);
}
myhttp.end();
}
void setToggleSwitch()
{
char buffer[100];
sprintf(buffer, WiFi.localIP().toString().c_str());
Serial.print("In Set Toggle Switch On");
HTTPClient myhttp;
myhttp.begin("http://yousvc.azurewebsites.net/api/SetTaskVariable");
myhttp.addHeader("Content-Type", "application/json");
const size_t bufferSize = JSON_OBJECT_SIZE(4) + 175;
DynamicJsonBuffer jsonBuffer(bufferSize);
const char* json = "{"ClientKey":"f239","TaskKey":"6b4a676","TheSwitchValue":"false","TheSwitchValue2":"false"}";
Serial.print(json);
int httpCode = myhttp.POST((uint8_t *)json,strlen(json));
if(httpCode == HTTP_CODE_OK)
{
Serial.print("HTTP response code ");
Serial.println(httpCode);
String response = myhttp.getString();
Serial.println("set the var " + response);
}
else
{
Serial.println("Error in HTTP request");
Serial.println(httpCode);
}
myhttp.end();
}
void sendnotification(char mybody[200])
{
}