Ethernet Push Notifications

Hi Folks

I'm using the below code with an uno+ethernet shield. It uses a simple push button to activate the pushingbox scenairos to send emails and 'push notifications' to smart phones.

What i want to do is surely qutie simple. But i can't figure it out.

I only want the void loop to be activated if the button is depressed for a certain amount of time say 5 seconds.

i.e. one push of the button does nothing but holding it down for 5 seconds activates the whole thing.

Thanks alot.

FTL

////
//
// 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, 0xAA, 0xBB, 0xCC, 0xDE, 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[] = "Your_DevID_Here";        //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

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


char serverName[] = "api.pushingbox.com";
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


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

Any help would be greatly appreciated.

FTL

Check out the "blink without delay" example. Every iteration through the loop, check the button status. If it has been released, restart the timer. If it is still pressed and the 5 seconds has elapsed, then run your code.

Thanks ST. I've looked at 'blink with out delay' I can't really figure it out.

Isn't the someway to do something like...

-Button state= high

-delay 5000

-if button state still high, run code?

I'm not so good at this... i'm sorry but any help would be wonderful...

FTL

To simplify things you might start with writing code that lights the arduino on board LED. Below is some very simple button code in which you might put your delay inside the first if statement fillowed by a second if statement checking to see if the button is still pressed. Millies would be better to allow continuous checking to see if the button has been released with in the delay period.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

Try this.

#define delayMillis 5000UL
#define buttonPin 8

unsigned long currentMillis,lastMillis;
bool buttonDone = false;

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin,INPUT_PULLUP);  
  Serial.println("ready");
  lastMillis = millis();
}

void loop() {
  if(digitalRead(buttonPin) == LOW) {
    currentMillis = millis();
    if(!buttonDone && (currentMillis - lastMillis > delayMillis)) {
      doYourThing();
      buttonDone = true;      
    }
  }
  else {
    if(buttonDone) Serial.println("Button released");
    lastMillis = millis();
    buttonDone = false;
  }
}

void doYourThing() {
  Serial.println("Doing my thing");  
}

It only calls the doYourThing function once for each button press, no matter how long you hold the button.

This is great thanks guys... i will test asap.

FTL

I wasn't very clear on the button or switch connection. Connect the button or switch from the buttonPin (D8) to GND. Pressing the button or closing the switch will pull D8 LOW. Five seconds with D8 LOW, it will call the doYourThing function.

edit: By using the "blink without delay" algorithm, it makes this non blocking, so you can do other things while waiting for the 5 second button press.

Hi

Surfer Tim. Your code works great... exactly what i needed.. But after much experimentation i just can't figure out how to implement it into the pushing box sketch.

This i'm afraid is where my limits are reached.

Would someone be so kind as to point me in the right direction?

Thanks in advance

FTL

I don't know about the pushing box sketch, but the code should go in the doYourThing function.

Either post the sketch or a link to it.

Here it is -

////
//
// 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, 0xAA, 0xBB, 0xCC, 0xDE, 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[] = "Your_DevID_Here";        //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

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


char serverName[] = "api.pushingbox.com";
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


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

Put the stuff in loop into doYourThing. Add the sendToPushingBox function.

edit: That function may take a second or two to process. If the button is released and repressed while in the pushingbox function, the sketch will not detect it. In that case, you might want to use an interrupt to catch the button changes.

I'm sorry. I just don't follow.

What do you mean by add it to doyourthing?

And where do i ad the sendtopushingboxfunction?

Sorry for being such a noob.

Hi,

I just put two pieces of code together

tested it 20 times it all worked with pushingbox.com

Look at the comments in the code(you have to delete some of the CODE) and put in your ID

in your original code the buttonpin is pulled down

in this code you just connect a button to pin8 to the ground (no resistors needed)

#include <SPI.h>
#include <EthernetV2_0.h>  // use your ethernet library here
#define delayMillis 5000UL
#define buttonPin 8

unsigned long currentMillis,lastMillis;
bool buttonDone = false;
  /////////////////
 // MODIFY HERE //
/////////////////
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 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[] = "PUT YOUR ID HERE";        //Scenario : "The mailbox is open"

//Numeric Pin where you connect your switch
 // Example : the mailbox switch is connect to the Pin 3

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


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

#define W5200_CS  10  // you do not need this code
// SD card SPI CS pin
#define SDCARD_CS 4   // you do not need this code
// 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(SDCARD_CS,OUTPUT);  // you do not need this code
  digitalWrite(SDCARD_CS,HIGH); // you do not need this code
pinMode(buttonPin,INPUT_PULLUP);  
lastMillis = millis();
  
  // 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(buttonPin) == LOW) {
    currentMillis = millis();
    if(!buttonDone && (currentMillis - lastMillis > delayMillis)) {
     sendToPushingBox(DEVID1);
      buttonDone = true;     
    }
  }
  else if (buttonDone) 
    {Serial.println("Button released");
    lastMillis = millis();
    buttonDone = false;
  }

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

This is really good of you... thank you...

Only thing is the code works great... but, it doesn't quite do what i need. I need to to 'run' only if the button is held down for 5s or more.

As it is now it sendstopushingbox as soon as i connect to gnd.

Would you be able to help me again?

FTl

It shouldn't send right away. It should wait 5 seconds. Did the original code I posted work ok? Did it wait the 5 seconds? It did here.

Your doyourthing code does that surfertim...

The new code sends right away...