Create variables for Pushing Box

Hi Guys,

maybe someone can help me out here. I’ve got a few cores running to send each other emails upon proximity to each other given a few parameters match up. I’m using Pushing BOx and all is well. Now I want the email which is send out to vary in a few instances, which according to Pushing Box should be easy I just have to define my actions something like this:

The $room$ temperature is $temperature$ degree

now where and how would that fit into my firmware?

I’m using the code from github:

any pointers in the right direction are greatly appreciated :slight_smile:

you will need to add

&room=kitchen&temperature=23"

to the end of the get request…

so in this part of the code, we can add a few lines

client.print("GET /pushingbox?devid=");
client.print(devid);
client.println(" HTTP/1.1");

So try and change to this,

client.print("GET /pushingbox?devid=");
client.print(devid);
client.print("&room=");
client.print(room);
client.print("&temperature=");
client.print(temp);
client.println(" HTTP/1.1");

you will need to create the variables room and temp, add them in before setup somewhere.

char room[] = "kitchen";
uint8_t temp = 24;

this should give you a static room and temp to start with, as i haven’t tested any of the above, I’m still waiting for my new cores to arrive :slight_smile:

If you get this working could you post the code?

1 Like

Your example code said:

const char * DEVID1 = "Your_DEVID_here";         // Scenario: "The mailbox is open"

The above Your_DEVID_here should be something like this according to your example given:
v0123456789ABCDE&room=kitchen&temperature=23

In Pushingbox.com, you add an action by choosing "Add an action to this service" and in the Message box that says "My push description", you fill in something like The $room$ temperature is now $temperature$ degree Celsius

That would do.

Also, you may want (need) to change this little bit (which doesn’t tell us much at all)

client.flush();
if(DEBUG){
        Serial.print("sent! - RSSI:");
        Serial.println(Network.RSSI());
        Serial.println("");
    }

To this, which will print the response from pushingbox, which will be very helpful in faultfinding.

if (DEBUG) {
        while (!client.available()) SPARK_WLAN_Loop();
        while (client.available()) {
            char c = client.read();
            Serial.print(c);
        }
        Serial.println();
}
delay(150);
while(client.available()) client.read(); 

let me explain this a bit, the client.flush() just throws away the response from PushingBox
with DEBUG turned on (boolean DEBUG = true; at the top of the code) it will enter the if loop, then say sent etc. even if the pushingbox server didn’t have a clue what the spark said…

the updated code doesn’t throw away the response straight away, it checks DEBUG first, if its true then it wait’s for a response, once it starts getting the response it will print it on the serial terminal. then it prints a blank line to keep the output neat, then waits a bit longer and does a “client.flush()” but in a different way (that may help fix a bug)
here is an example of a good response:

HTTP/1.1 200 OK
Set-Cookie: 60gpBAK=R1225225159; path=/; expires=Fri, 27-Jun-2014 18:20:25 GMT
Date: Fri, 27 Jun 2014 17:21:58 GMT
Content-Type: text/html
Content-Length: 0
Connection: close
Set-Cookie: 60gp=R475267376; path=/; expires=Fri, 27-Jun-2014 18:37:01 GMT
Server: Apache
Content-Location: pushingbox.php
Vary: negotiate,Accept-Encoding
TCN: choice
X-Powered-By: PHP/5.2.17

When I get bored i may update the example code and see if pushingbox want to update the Git

ok cool i’ll give it that a shot and post what i can get out of it - will have to wait till monday though - just if you do happen to get bored I should mention that “network” in the sample code should be changed to “WiFi”:slight_smile:

That was one on the list!

1 Like

works great :slight_smile: thx - code below:

////
//
// General code from http://www.pushingbox.com for Spark Core v1.0
//
////

//Example code for sending dynamic emails from the Core
//When creating Pushing Box scenario format email body somethinglike this: The $room$ temperature is $temperature$ degree

/////////////////
// MODIFY HERE //
/////////////////
// Your secret DevID from PushingBox.com. You can use multiple DevID on multiple Pin if you want
const char * DEVID1 = “insert-devid-from-pushingbox-here”; // Scenario: “The mailbox is open”

//declare variables to be sent here fx:-----------------
char room[] = “kitchen”;
uint8_t temp[] = 24;
//-----------------------------------------------------
// Numeric Pin where you connect your switch
uint8_t pinDevid1 = D0; // Example: the mailbox switch is connect to the Pin D0

// Debug mode
boolean DEBUG = true;
//////////////
// End //
//////////////

int LED = D7; // Built in LED
const char * serverName = “api.pushingbox.com”; // PushingBox API URL
boolean pinDevid1State = false; // Save the last state of the Pin for DEVID1
TCPClient client;

void setup() {
Serial.begin(9600); // Start the USB Serial
pinMode(pinDevid1, INPUT_PULLUP); // sets pin as Input PullUp, using the internal resistor. Connect PIN to GND.
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() {

////
// 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 HIGH
    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(DEVID2);                               // Here you can run an other scenario by creating a DEVID2 variable
}

}

void sendToPushingBox(const char * devid)
{
digitalWrite(LED, HIGH); // sets the LED on
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);
//put variables to be sent here:
client.print(”&room=”);
client.print(room);
client.print(”&temperature=”);
client.print(temp);
//-----------------------------
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.print(“sent! - RSSI:”);
Serial.println(WiFi.RSSI());
Serial.println(””);
while (!client.available()) SPARK_WLAN_Loop();
while (client.available()) {
char c = client.read();
Serial.print©;
}
Serial.println();
}
delay(150);
while(client.available()) {client.read();
}
digitalWrite(LED, LOW); // sets the LED off
}
else{
digitalWrite(LED, HIGH); // sets the LED on. If always on, last connexion was failed
if(DEBUG){Serial.println(“connection failed”);}
}
}

@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();
    }
}

ah ok i’ll post that code again: yours seem to have some syntax errors:

In file included from ../inc/spark_wiring.h:30:0,
from ../inc/application.h:29,
from hooties_pb.cpp:13:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
hooties_pb.cpp: In function 'void loop()':
hooties_pb.cpp:75:14: error: incompatible types in assignment of 'const char [6]' to 'char [5]'

^
hooties_pb.cpp:76:15: error: incompatible types in assignment of 'const char [7]' to 'char [6]'
void loop() {
^
hooties_pb.cpp:82:14: error: incompatible types in assignment of 'const char [6]' to 'char [5]'
state = "Opened";
^
hooties_pb.cpp:83:15: error: incompatible types in assignment of 'const char [7]' to 'char [6]'
sendToPushingBox(DEVID1);
^
hooties_pb.cpp:89:14: error: invalid array assignment
state = "Closed";
^
hooties_pb.cpp:90:15: error: incompatible types in assignment of 'const char [7]' to 'char [6]'
sendToPushingBox(DEVID1);
^
hooties_pb.cpp:96:14: error: invalid array assignment
state = "Opened";
^
hooties_pb.cpp:97:15: error: incompatible types in assignment of 'const char [7]' to 'char [6]'
sendToPushingBox(DEVID1);
^
hooties_pb.cpp:103:14: error: invalid array assignment
state = "Closed";
^
hooties_pb.cpp:104:15: error: incompatible types in assignment of 'const char [7]' to 'char [6]'
sendToPushingBox(DEVID1);
^
hooties_pb.cpp:110:14: error: invalid array assignment
state = "Opened";
^
hooties_pb.cpp:111:15: error: incompatible types in assignment of 'const char [7]' to 'char [6]'
sendToPushingBox(DEVID1);
^
make: *** [hooties_pb.o] Error 1
////
    //
    // General code from http://www.pushingbox.com for Spark Core v1.0
    //
    ////
    
      /////////////////
     // MODIFY HERE //
    /////////////////
    // Your secret DevID from PushingBox.com. You can use multiple DevID  on multiple Pin if you want
    const char * DEVID1 = "your-devid-here";         // Scenario: "The mailbox is open"
    char * room[] = {"kitchen", "bath", "lounge"};
    uint8_t temp[] = {24, 20, 18};
    
    // Numeric Pin where you connect your switch
    uint8_t pinDevid1 = D0;                     // Example: the mailbox switch is connect to the Pin D0
    
    // Debug mode
    boolean DEBUG = true;
      //////////////
     //   End    //
    //////////////
    
    int LED = D7;                               // Built in LED
    const char * serverName = "api.pushingbox.com";   // PushingBox API URL
    boolean pinDevid1State = false;             // Save the last state of the Pin for DEVID1
    TCPClient client;
    
    void setup() {
        Serial.begin(9600);                     // Start the USB Serial
        pinMode(pinDevid1, INPUT_PULLUP);       // sets pin as Input PullUp, using the internal resistor. Connect PIN to GND.
        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() {
    
        ////
        // 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 HIGH
            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(DEVID2);                               // Here you can run an other scenario by creating a DEVID2 variable
        }
        
    }
    
    void sendToPushingBox(const char * devid)
    {
        digitalWrite(LED, HIGH);          // sets the LED on
        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);
            client.print("&room=");
            client.print(room[2]);
            client.print("&temperature=");
            client.print(temp[1]);
            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.print("sent! - RSSI:");
            Serial.println(WiFi.RSSI());
            Serial.println("");
            while (!client.available()) SPARK_WLAN_Loop();
            while (client.available()) {
                char c = client.read();
                Serial.print(c);
            }
            Serial.println();
    }
    delay(150);
    while(client.available()) {client.read();
            }
            digitalWrite(LED, LOW);           // sets the LED off
        } 
        else{
            digitalWrite(LED, HIGH);          // sets the LED on. If always on, last connexion was failed
            if(DEBUG){Serial.println("connection failed");}
        }
    }

Haha schoolboy errors! I must have been sleep-typing again… @raune I’ve edited the post above so it compiles now, could you please test again for me

SSID: …
Core IP: 192.168.2.5
Gateway: 192.168.2.1
Mask: 255.255.255.0
WiFi RSSI: -44
pin2 is HIGH
closing… connecting… connected
Sent! Response from server:
Done!
pin3 is HIGH
closing… connecting… connection failed
pin1 is HIGH
closing… connecting… connected
Sent! Response from server:
Done!
pin2 is LOW
closing… connecting… connected
Sent! Response from server:
Done!
pin2 is HIGH
closing… connecting… connected
Sent! Response from server:
Done!

thats what your looking for?

hmmm, there should be a reply from Pushingbox… normally starting with “HTTP/1.1 200 OK” or “400 bad request” something… it should be between “response from server:” and “Done!”. did you get emails from pushingbox each time?

Could you try commenting out the 2 lines

client.print("&time=");
client.print(Time.timeStr());

I think the issue may be that Time.timestr() has a newline termination at the end (0x0a) that will probably confuse the server

SSID: …
Core IP: 192.168.2.5
Gateway: 192.1Connection: keep-alive
Set-Cookie: 60gp=R2337315551; path=/; expires=Wed, 03-Sep-2014 12:41:06 GMT
Server: Apache
Content-Location: pushingbox.php
Vary: negotiate,Accept-Encoding
TCN: choice
X-Powered-By: PHP/5.2.17

Done!

no sorry - mail coming through tho

hmmm thats very strange, might have to wait until i get my new core to test…

Is that all that came up? strange that it started printing the second half of pushingbox’s reply halfway through the gateway IP

yes thats it… :/…
(btw i’m runnning the core over my macs WiFi / if that makes any difference)…

I do remember something about iPhone Hotspots.. it may be something to watch out for?

How has your project come along that you wanted variables for? what was it you were doing?

not sure - works fine for me though i’ve just got a few arrays that identify the individual cores and if and what they have in common and then send each other a mail accordingly… it’s jackets to be precise so you wear one and if you meet someone likeminded you exchange contact details … its my master thesis :smile:

@Hootie81 and @raune I am using pushingbox too and I dont’ see any response to my GET request (just blank). The request action goes ahead correctly. Are you able to see any response as yet?

hi,

this thread is about a year old (and I frankly don’t remember the ins and outs of it but yes works well) you might want to start a new one specifying your issue to get some help.

cheers and good luck,
raune