Pushing Box email event via ethernet Shield

Hi…

Im currently working on a project that will notify me if my front door is opened or house alarm goes off…

I’m using pushingbox… and the code below, with two device instances… and I must say it is amazing!!

////
//
// General code from http://www.pushingbox.com for Arduino + Ethernet Shield (official) v1.2
//
////

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

  /////////////////
 // MODIFY HERE //
/////////////////
byte mac[] = { 0x00, 0x13, 0x02, 0xCC, 0xD1, 0x19 };   // Be sure this address is unique in your network

//Your secret DevID from PushingBox.com. You can use multiple DevID  on multiple Pin if you want
char DEVID1[] = "v1CC65E36D8Cxxxx";        //Scenario : "House alarm"
char DEVID2[] = "v9565F5FAA75xxxx";        //Scenario : "front Door"

//Numeric Pin where you connect your switch
#define pinDevid1 3  // 
#define pinDevid2 2  // 

// Debug mode
#define DEBUG true
  //////////////
 //   End    //
//////////////


char serverName[] = "api.pushingbox.com";
boolean pinDevid1State = false;
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);
  pinMode(pinDevid1, INPUT);
  pinMode(pinDevid2, INPUT);
  
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  else{
    Serial.println("Ethernet ready");
    // print the Ethernet board/shield's IP address:
    Serial.print("My IP address: ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  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
      }
      if (digitalRead(pinDevid2) == HIGH && pinDevid1State == false) // switch on pinDevid2 is ON 
      {
        if(DEBUG){Serial.println("pinDevid2 is HIGH");}
        pinDevid1State = true;
        //Sending request to PushingBox when the pin is HIGHT
        sendToPushingBox(DEVID2);
      }
       if (digitalRead(pinDevid2) == LOW && pinDevid1State == true) // switch on pinDevid2 is OFF
      {
        if(DEBUG){Serial.println("pinDevid2 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");}

    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");}
  }
}

the problem I have is that when the front door, or alarm goes off , instead of getting one notification(email), i get constant notifications, until the ip goes low…

Is there anyway to detect the change once… ie a rising edge… to stop this…

David

Yes, just add another variable that you set on the instance your digital input is triggered. And then use that as part of the check when you cycle around in your main loop. You then reset that variable when the input goes low again. You could make your code nicer and create a function for your checks, instead of two similar blocks, and that you simply pass the trigger information to it.

Also in your code just above the '// Debug Part ' you have the following;

//sendToPushingBox(DEVID1);    //Here you can run an other scenario by creating a DEVID2 variable

I am assuming the DEVID1 will need to be DEVID2 when you go to un-comment that code?

Paul

please can you give an example of the variable? can i just detect the change in state..how? Im really new to this.. :-(

do I need to uncomment that bit of code? as its working...

Re-looking at the code you have, you do have a variable that takes into account the state of the digital input. And its purpose is to stop further triggers of calling the code to connect to pushingbox.

When the input is set you say that it keeps sending you a constant stream of email notifications? Use your debugging statements to see what is happening.

Do you get a continuous stream of debug serial prints?

Look at modifying the following line;

if (digitalRead(pinDevid1) == HIGH && pinDevid1State == false)

to

if ((digitalRead(pinDevid1) == HIGH) && (pinDevid1State == false))

Do this for all those similar conditional statements, it defines better what you are if'ing about, if (this) && (that) is better than if this && that.

Another thing to be aware of, do you know your switch contacts are bounce free, that is, that they provide a good clean transition from off to on to off? If not, you can either put some RC on your input or employ some input switch debounce in your code, check on the playground for examples.

do I need to uncomment that bit of code? as its working...

No, you don't need to uncomment it, but when or if you do, you might want to make that alteration. Remember it is a comment line only, it has no impact on your code at present while it has the '//' in front.

Paul

Further, it might be more useful if you have a separate variable to hold the state of each digital input. You only have one, that is 'pinDevid1State'.

The situation will arise where input one is activated and will send out an connect to pushingbox, then if input to is activated while input one is still activated then you will get two notifications, again one for input one, and also one for input two.

Also, my preference would be not to use the define statement as you have for defining the digital input pin numbers. Read up in the reference section about this, 'Define' as opposed to declaring a variable, for example I would do the following,

uint8_t    pinDevid1    =  2;

You can shorten your delay waiting for the ethernet to come out of reset, typically 150mSec should be ok.

Also, do you know that your inputs are not ever floating? What I mean, is that if your inputs are just connected to a relay contact, it could be that when the contact is open, your input might float about as it is not tied either to 0 volts or to 5 volts.

In this case look into using either a pull-up or a pull-down resister as appropriate for your circuit. You can use the internal pull-up of in the Adruino if you wish, by setting it on, again details in the reference section under pinMode.

 pinMode(pin, HIGH)

Paul

Still going continous :frowning:

////
//
// General code from http://www.pushingbox.com for Arduino + Ethernet Shield (official) v1.2
//
////

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

  /////////////////
 // MODIFY HERE //
/////////////////
byte mac[] = { 0x00, 0x13, 0x02, 0xCC, 0xD1, 0x19 };   // Be sure this address is unique in your network

//Your secret DevID from PushingBox.com. You can use multiple DevID  on multiple Pin if you want
char DEVID1[] = "v1CC65E36xxxxxx00";        //Scenario : "House alarm"
char DEVID2[] = "v9565F5FAxxxxxx1";        //Scenario : "front Door"

//Numeric Pin where you connect your switch
#define pinDevid1 3  // 
#define pinDevid2 2  // 

// Debug mode
#define DEBUG true
  //////////////
 //   End    //
//////////////


char serverName[] = "api.pushingbox.com";
boolean pinDevid1State = false;
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);
  pinMode(pinDevid1, INPUT);
  pinMode(pinDevid2, INPUT);
  
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  else{
    Serial.println("Ethernet ready");
    // print the Ethernet board/shield's IP address:
    Serial.print("My IP address: ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  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
      }
      if ((digitalRead(pinDevid2) == HIGH) && (pinDevid1State == false)) // switch on pinDevid2 is ON 
      {
        if(DEBUG){Serial.println("pinDevid2 is HIGH");}
        pinDevid1State = true;
        //Sending request to PushingBox when the pin is HIGHT
        sendToPushingBox(DEVID2);
      }
       if ((digitalRead(pinDevid2) == LOW) && (pinDevid1State == true)) // switch on pinDevid2 is OFF
      {
        if(DEBUG){Serial.println("pinDevid2 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");}

    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");}
  }
}

im using pull down resistors… and pulling inputs constantly(latched high) via a breadboard link…

Ok… now just to push my luck…if i can get it to do its job …

i want to change one of the inputs to analogue and detect light via an LDR…

SO IM LOOKING TO ADD

int LDR = 0;                                //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDRValue = 0;                    //that’s a variable to store LDR values
int light_sensitivity = 1000;

and in loop

LDRValue = analogRead(LDR);          //reads the ldr’s value through LDR which we have set to Analog input 0 “A0?
    Serial.println(LDRValue);                  //prints the LDR values to serial monitor
    delay(1);                                                //This is the speed by which LDR sends value to arduino
 
    if (LDRValue < light_sensitivity)

Please can you look at how I can swap out my digital read of dig2 with this analogue read of A0… I know im close…

I hope this makes sense.

so setup will be analogue level on a0 triggers deviceid2

and dig ip 3 triggers deviceid3

would really appreciate help.
cheers
David.

Before adding more, please just get this section to operate correctly first.

One question, is this code from an proven working code or have you built this code up?

Suggestions are; Create extra variable for second input state, for example, declare 'pinDevid2State' That or remove the possibility of triggering input two, try commenting out the code for the extra input.

By just having a simple piece of code that just watches out for one input only will help you.

To help you further, you can disable the code to call pushingbox, just get the code that triggers on an input to work correctly first. Once that works fine, move to the next stage, call the code to pushingbox. Once that works fine, you can start to elaborate more on your code, doing the analog sensor.

Yes, you are nearly there, and I guess you are eager and keen, but you will learn a lot more and gain a better understanding by following simple rules to start with at this point in time.

When you say

Still going continous :-(

Can you please tell us 'Do you get a continuous stream of debug serial prints?' from the routine?

Paul

Ok Paul. Will look at that..

How do I view the debug?

How do I view the debug?

Using the serial monitor in your Arduino IDE. So anywhere you have the statement 'Serial.print(.......)' will be displayed on your computer screen. You have a number of these sprinkled in your program, look where you have 'if (debug) {Serial.print(......);}

Placing these statements in certain points in your program can help you determine what is happening at the that point in the program flow, hence why it is generally called debug points, even though we allow the program to continue to flow and we just print out to the computer that is connected.

Paul

Ok will look at this.. When I get 5 today... Will also look at getting just the one input to work. Will report back later. Got family things today.

Agreed I'm keen. But need to learn the blocks first. Really appreciate your assistance and patience.

David.

rockwallaby: One question, is this code from an proven working code or have you built this code up?

Can you please tell us 'Do you get a continuous stream of debug serial prints?' from the routine?

Paul

Yes it is proven code from .. https://github.com/PushingBox/PushingBox-for-Arduino/tree/master/PushingBox_Arduino_Ethernet_Official

char serverName[] = "api.pushingbox.com";
[b][font=Verdana]boolean pinDevid1State = false;                // Save the last state of the Pin for DEVID1
boolean lastConnected = false;  [/font] [/b]                // State of the connection last time through the main loop

apparnetly this bold text should only let it mail once,, but as isay it sends mail continuously when held hi.. I do get a continous stream looking at the serial debug. any help wpuld be much appreciated.

Hi, I am using this sketch for my outside lights. I have the same problem with the doorbell sketch. when the switch is high(ON), the Pushingbox (Prowl) keeps sending notifications to myphone. Did you find a variable to solve this issue? if yes, please let me know. Best regards, Nader

Hi,

In order to get one notification per connection, you need to change the following line.
pinMode(pindevid,INPUT) to pinMode(pindevid, INPUT_PULLUP). the PULLUP will tell Arduino board the switch is off.
This way (pindevid) is connected to ground and it shows HIGH.
When the switch is on or button is pushed you will get one notification .
Good luck,
Naderjazz

When the switch is on or button is pushed you will get one notification .

No, that is not correct.

OP: You need to look at the state change detection example. You need to detect when the switch BECOMES pressed, not IS pressed. Do what you need to when the switch BECOMES pressed. No nothing when the switch BECOMES released. Do nothing except when the state changes.

hi .. am doing the same project and i have the same problem + i cant find my ip camera URL do u have any idea how to find it ?? . . thank u