Create variables for Pushing Box

@raune if you highlight the code in your post and press the “</>” button it should put it in a easy to read font in a text box with a scroll slider etc… it sometimes doesn’t work if there are empty spaces or tabs before the text but im sure you will work it out.

Could you test the code below for me, its another example with door status and a timestamp. I have added a couple of extra inputs too, D0, D1 and D2. I think it should compile OK, but I’m not sure if the line termination on the Time.timeStr() will mess things up with the get request. Also i added a couple of timeouts to stop it hanging if it doesn’t get a response of if the terminal window doesn’t get opened within 20seconds.

I would test it myself but I blew up my core last week so I’m waiting for new ones to arrive!

/*******************************************************************************
 *
 * Example code for http://www.pushingbox.com for Spark Core 
 * 
 * Modified from original version here:
 *  https://github.com/Clement87/PushingBox-for-Spark-Core
 *
 *     Example code for sending dynamic emails from the Core
 * When creating Pushing Box scenario format email body something like this: 
 *    The $item$ Door was $state$ on $time$
 *
 *******************************************************************************/

const char * DEVID1 = "yourDevID"; // Scenario: "Door monitor"

const char * item1 = "Front";  //change the name of "items" here
const char * item2 = "Back";   //could be letterbox or tank etc.
const char * item3 = "Side";

const char * state1 = "Closed";  //change the state names here
const char * state2 = "Opened";  //could be empty-Full etc.

// Numeric Pin where you connect your switch
uint8_t pin1 = D0; // item1
uint8_t pin2 = D1; // item2
uint8_t pin3 = D2; // item3

//declare variables to be sent, size must be 1 more than longest string
char state[7];
char item[6];

// Debug mode enables the serial port output
boolean DEBUG = true;

int LED = D7; // Built in LED
const char * serverName = "api.pushingbox.com"; // PushingBox API URL

// Remember last state of pin
boolean pin1State = false;
boolean pin2State = false;
boolean pin3State = false;

TCPClient client;
unsigned long timer; // used for timeouts 

void setup() {
    if (DEBUG) {
        Serial.begin(9600); // Start the USB Serial
        timer = millis();
        // Wait for user to open a terminal and press a key, with 20 sec timeout
        while (!Serial.available() && millis() <= timer + 20000) SPARK_WLAN_Loop();
    }
    // set input pins as Input PullUp, using the internal resistor. Connect PIN to GND.
    pinMode(pin1, INPUT_PULLUP);
    pinMode(pin2, INPUT_PULLUP);
    pinMode(pin3, INPUT_PULLUP);

    pinMode(LED, OUTPUT); // sets LED as output
    delay(1000);

    // Prints out the network parameters over Serial
    if (DEBUG) {
        Serial.print("SSID: ");
        Serial.println(WiFi.SSID());
        Serial.print("Core IP: ");
        Serial.println(WiFi.localIP());
        Serial.print("Gateway: ");
        Serial.println(WiFi.gatewayIP());
        Serial.print("Mask: ");
        Serial.println(WiFi.subnetMask());
        Serial.print("WiFi RSSI: ");
        Serial.println(WiFi.RSSI());
    }

}

void loop() {

    if (digitalRead(pin1) == HIGH && pin1State == false) {// Door Opened
        if (DEBUG) Serial.println("pin1 is HIGH");
        pin1State = true;
        strcpy( item, item1);
        strcpy( state, state1);
        sendToPushingBox(DEVID1);
    }
    if (digitalRead(pin1) == LOW && pin1State == true) {// Door Closed
        if (DEBUG) Serial.println("pin1 is LOW");
        pin1State = false;
        strcpy( item, item1);
        strcpy( state, state2);
        sendToPushingBox(DEVID1);
    }
    if (digitalRead(pin2) == HIGH && pin2State == false) {// Door Opened
        if (DEBUG) Serial.println("pin2 is HIGH");
        pin2State = true;
        strcpy( item, item2);
        strcpy( state, state1);
        sendToPushingBox(DEVID1);
    }
    if (digitalRead(pin2) == LOW && pin2State == true) {// Door Closed
        if (DEBUG) Serial.println("pin2 is LOW");
        pin2State = false;
        strcpy( item, item2);
        strcpy( state, state2);
        sendToPushingBox(DEVID1);
    }
    if (digitalRead(pin3) == HIGH && pin3State == false) {// Door Opened 
        if (DEBUG) Serial.println("pin3 is HIGH");
        pin3State = true;
        strcpy( item, item3);
        strcpy( state, state1);
        sendToPushingBox(DEVID1);
    }
    if (digitalRead(pin3) == LOW && pin3State == true) {// Door Closed
        if (DEBUG) Serial.println("pin3 is LOW");
        pin3State = false;
        strcpy( item, item3);
        strcpy( state, state2);
        sendToPushingBox(DEVID1);
    }

}

void sendToPushingBox(const char * devid) {
    digitalWrite(LED, HIGH); // sets the LED on
    if (DEBUG) Serial.print("closing... ");
    client.stop();
    if (DEBUG) Serial.print("connecting... ");
    if (client.connect(serverName, 80)) {
        if (DEBUG) Serial.println("connected");
        client.print("GET /pushingbox?devid=");
        client.print(devid);
        //variables to be sent:
        client.print("&item=");
        client.print(item);
        client.print("&state=");
        client.print(state);
        client.print("&time=");
        client.print(Time.timeStr());
        //---------------------
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.println(serverName);
        client.println("User-Agent: Spark");
        //client.println("Connection: close");
        client.println();
        if (DEBUG) {
            Serial.println("Sent! Response from server:");
            timer = millis(); // used for a timeout
            while (!client.available() && millis() <= timer + 10000) SPARK_WLAN_Loop(); // wait for a response for upto 10seconds
            while (client.available()) {
                char c = client.read();
                Serial.print(c); //print the response 
            }
            Serial.println("Done!");
        }
        delay(150);
        while (client.available()) client.read(); // makeshift client.flush
        delay(20);
        client.stop();

        digitalWrite(LED, LOW); // sets the LED off
    } else {
        digitalWrite(LED, HIGH); // sets the LED on. If always on, last connection was failed
        if (DEBUG) Serial.println("connection failed");
        delay(20);
        client.stop();
    }
}