PushBox - Arduino


PushingBox este un site intermediar care direcționează notificările venite de la o placă de dezvoltare, spre un dispozitiv smart sau un cont de mail, cu ajutorul unor servicii plătite sau gratuite. Aici se pot face diferite scenarii a căror identități (deviceID) se vor introduce în codul prezentat mai jos.

PushingBox Sketch for Arduino

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   // Be sure address is unique in network
IPAddress ip(192, 168, 1, 110); // IP address, may need to change depending on network

//Your secret DevID from PushingBox.com. You can use multiple DevID  on multiple Pin if you want
char DEVID1[] = "v9DD50EF7F2E6671";        //Scenario : "The mailbox is open"

//Numeric Pin where you connect your switch
uint8_t pinDevid1 = 3; // Example : the mailbox switch is connect to the Pin 3

char serverName[] = "api.pushingbox.com";

boolean DEBUG = true;  // Debug mode
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

EthernetClient client;  // Initialize the Ethernet client library

void setup() {
 
  // disable Ethernet chip
    pinMode(10, OUTPUT);
    digitalWrite(10, HIGH);
   
  Serial.begin(9600);       // for debugging
   
  pinMode(pinDevid1, INPUT);
     
  Ethernet.begin(mac, ip);  // initialize Ethernet device
}

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 other scenario by creating a DEVID2
      }
     
     
      //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");}

    if(DEBUG){Serial.println("sendind request");}
    client.print("GET /pushingbox?devid=");
    client.print(devid);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(serverName);
    client.println("User-Agent: Arduino");
    client.println();
  }
  else {
    if(DEBUG){Serial.println("connection failed");}
  }
}

0 comentarii:

Trimiteți un comentariu