Unable to connect to PushingBox using example

Hello,

I’m a new user. I’m not that familiar with Arduino like programming as I’ve been using MikrobasicPro for Pic32. My main reason for purchasing the core/Photon, is to find a way to send a notification email from my projects. (A surprisingly hard thing to do)

I’ve tried the different PushingBox examples found on the forum with disappointment.

My core is connected, breathing cyan and I’m able to flash the programs both from Spark Dev and from the Web.
I’m able to connect the serial window in SparkDev to debug this program:

/*
  kid-summoner.ino

    On pressing a button, a tweet is sent via Pushingbox
    Written for the Spark Core
*/

int inputPin = D1; //push-button pin
int val = 0; //variable for push-button status
int state = 1; //variable for on/off state

const char * DEVID = "vC1efg12334567";         // Scenario: "Tweet"
const char * serverName = "[api.pushingbox.com](http://api.pushingbox.com)";   // Pushingbox API URL

TCPClient client;

void setup() {

    pinMode(inputPin, INPUT);
    Serial.begin(9600);
}


void loop() {
    val = digitalRead(inputPin); //read the state of the push-button

    if (val == LOW) { //if push-button pressed
        state = !state; //reverse on/off state
        delay(250); //primitive button debounce
        Serial.println("button pushed!");
        sendToPushingBox(DEVID);

    }
}

void sendToPushingBox(const char * devid)
{
    client.stop();
    if (client.connect(serverName, 80)) {
        client.print("GET /pushingbox?devid=");
        client.print(devid);
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.println(serverName);
        client.println("User-Agent: Spark");
        client.println();
        client.flush();

    }
    else{
        Serial.println("connection failed");
    }
} 

I always get:

button pushed!
Connection failed

I appreciate any help. Thanks,

Stuart

No solution to the current issue, but definitely an alternative worth looking into: IFTTT. Recently Spark OS started supporting IFTTT, opening a lot more possibilities. You can use this to send mails for you on a certain trigger. You can also use it to trigger one of many other triggers. Pushing box officially isn’t one of them, but you can use it regardless. Follow this guide, provided by pushingbox, and you should be able to use it with IFTTT. If you’re only goal is to send an email, you can skip the pushingbox part, and have it mail you directly.
If using IFTTT, the code on your Core can also be simplified quite a bit, since you’d only need a Spark.publish, and no external libraries.

Anyhow, give it a try, and let me know if that worked for you. Perhaps some other folks can take a look at your current code so see why it might not be working.

at the suggestion of @Hootie81, I have been using these functions to handle the return of an HTTP call and it works to send to PushingBox. I have it like this:

in the header like this::

TCPClient client;
char reply[512];

and the functions:

void notification()
{
  if (client.connect("api.pushingbox.com", 80))
  {
    out("GET /pushingbox?devid=<MyDevID>  HTTP/1.1\r\n");
    out("Host: api.pushingbox.com\r\n");
    out("User-Agent: Spark/1.0\r\n");
    out("Content-Type: text/html\r\n");
    out("Connection: close\r\n\r\n");
    //DEBUG_PRINTLN("Closing Connection...");
    in(reply, 3000);
  }
  //
}

void out(const char *s)
{
  client.write( (const uint8_t*)s, strlen(s) );
  //Serial.write( (const uint8_t*)s, strlen(s) );
}

void in(char *ptr, uint8_t timeout)
{
  int pos = 0;
  unsigned long lastTime = millis();
  while( client.available()==0 && millis()-lastTime<timeout)
  {
  }  //do nothing
  lastTime = millis();
  unsigned long lastdata = millis();
  while ( client.available() || (millis()-lastdata < 500))
  {
    if (client.available())
    {
      char c = client.read();
      //DEBUG_PRINTLN(c);
      lastdata = millis();
      ptr[pos] = c;
      pos++;
    }
    if (pos >= 512 - 1)
      break;
  }
  ptr[pos] = '\0'; //end the char array
  while (client.available()) client.read(); // makeshift client.flush()
  client.flush();  //for safety
  delay(400);
  client.stop();
}
1 Like

Hello Bulldog,

I’d like to verify that your code works. Here’s the complete program if anyone wants it:

Tomorrow I will try IFTTT

This forum is wonderful!. Thanks for the great help.

Stuart

int inputPin = D1; //push-button pin
int val = 0; //variable for push-button status
int state = 1; //variable for on/off state
int led = D7; // integrated LED

TCPClient client;
char reply[512];

void setup() {

    pinMode(inputPin, INPUT);
    pinMode(led, OUTPUT);
    Serial.begin(9600);
}


void loop() {
    val = digitalRead(inputPin); //read the state of the push-button

    if (val == LOW) { //if push-button pressed
        state = !state; //reverse on/off state
        delay(250); //primitive button debounce
        Serial.println("button pushed!");
        digitalWrite(led, HIGH);   // Turn ON the LED pins
        notification();
    }
}


void notification()
{
  if (client.connect("api.pushingbox.com", 80))
  {
    out("GET /pushingbox?devid=vC1234567891023  HTTP/1.1\r\n");
    out("Host: api.pushingbox.com\r\n");
    out("User-Agent: Spark/1.0\r\n");
    out("Content-Type: text/html\r\n");
    out("Connection: close\r\n\r\n");
    //DEBUG_PRINTLN("Closing Connection...");
    in(reply, 3000);
    Serial.println("Complete");
    //
    digitalWrite(led, LOW);    // Blink LED
    delay(250);               
    digitalWrite(led, HIGH);   
    delay(250); 
    digitalWrite(led, LOW);  
  }
      else{
        Serial.println("connection failed");
        digitalWrite(led, LOW);    // Pulse LED Quickly
        delay(100);               
        digitalWrite(led, HIGH);    
        delay(100); 
        digitalWrite(led, LOW);    
        delay(100);               
        digitalWrite(led, HIGH);    
        delay(100); 
        digitalWrite(led, LOW);    
        delay(100);               
        digitalWrite(led, HIGH);    
        delay(100); 
        digitalWrite(led, LOW);   
    }
  //
}

void out(const char *s)
{
  client.write( (const uint8_t*)s, strlen(s) );
  //Serial.println("Writing");
}

void in(char *ptr, uint8_t timeout)
{
  int pos = 0;
  unsigned long lastTime = millis();
  while( client.available()==0 && millis()-lastTime<timeout)
  {
  }  //do nothing
  lastTime = millis();
  unsigned long lastdata = millis();
  while ( client.available() || (millis()-lastdata < 500))
  {
    if (client.available())
    {
      char c = client.read();
      //DEBUG_PRINTLN(c);
      lastdata = millis();
      ptr[pos] = c;
      pos++;
    }
    if (pos >= 512 - 1)
      break;
  }
  ptr[pos] = '\0'; //end the char array
  while (client.available()) client.read(); // makeshift client.flush()
  client.flush();  //for safety
  delay(400);
  client.stop();
}
3 Likes

fyi, these are in there for the way I do debugging, so you can nix them! :wink:

Now the same example using IFTTT:

int inputPin = D1; //push-button pin
int val = 0; //variable for push-button status
int state = 1; //variable for on/off state
int led = D7; // integrated LED


void setup() {

    pinMode(inputPin, INPUT);
    pinMode(led, OUTPUT);
    Serial.begin(9600);
}


void loop() {
    val = digitalRead(inputPin); //read the state of the push-button

    if (val == LOW) { //if push-button pressed
        state = !state; //reverse on/off state
        delay(250); //primitive button debounce
        Serial.println("button pushed!");
        digitalWrite(led, HIGH);   // Turn ON the LED pins
        notification();
    }
}


void notification()
{
    delay(1000); 
    Spark.publish("button1",NULL, 60, PRIVATE);
   //
    Serial.println("Complete");
    //
    digitalWrite(led, LOW);    // Blink LED
    delay(250);               
    digitalWrite(led, HIGH);   
    delay(250); 
    digitalWrite(led, LOW);  
  }

Out of curiosity,

how does one detect if an IFTTT request does not get published?

Thanks very much,

Stuart


I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy