char serverName[] = "api.pushingbox.com";
boolean pinDevid1State = false; // Save the last state of the Pin for DEVID1
boolean lastConnected = false; // State of the connection last time through the main loop
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac,ip,dnServer,gateway,subnet);
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
delay(1000);
}
void loop()
{
////
// Listening for the pinDevid1 state
////
if (digitalRead(pinDevid1) == HIGH && pinDevid1State == false) // switch on pinDevid1 is ON
{
if(DEBUG){Serial.println("pinDevid1 is HIGH");}
pinDevid1State = true;
//Sending request to PushingBox when the pin is HIGHT
sendToPushingBox(DEVID1);
}
if (digitalRead(pinDevid1) == LOW && pinDevid1State == true) // switch on pinDevid1 is OFF
{
if(DEBUG){Serial.println("pinDevid1 is LOW");}
pinDevid1State = false;
//Sending request to PushingBox when the pin is LOW
//sendToPushingBox(DEVID1); //Here you can run an other scenario by creating a DEVID2 variable
}
//DEBUG part
// this write the respons from PushingBox Server.
// You should see a "200 OK"
if (client.available()) {
char c = client.read();
if(DEBUG){Serial.print(c);}
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
if(DEBUG){Serial.println();}
if(DEBUG){Serial.println("disconnecting.");}
client.stop();
}
lastConnected = client.connected();
}
//Function for sending the request to PushingBox
void sendToPushingBox(char devid[]){
client.stop();
if(DEBUG){Serial.println("connecting...");}
if (client.connect(serverName, 80)) {
if(DEBUG){Serial.println("connected");}