Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By digitalkiwi
#30924 I'm trying to modify some code I found to add a button press versus the constant connect and post cycle/loop using the Arduino IDE.

The raw code is here: https://github.com/Astu04/Pushetta_ESP8266

I figure you'd just add in an "if " statement then if button state high/true run through the connection process.



I couldn't find any reference to using button or triggers to connect and using the Arduino IDE, ideally I'd love to have some sort of input trigger the connection versus the connection being available all the time.
Also some sort of sleep mode to cut down on power consumption (that's another story I guess)

My code is similar to this - I get the impression that the TCP connection is creating issues.

Code: Select allvoid loop()
{

if (buttonState = =HIGH)  {

  Serial.println("Connecting to Pushetta");

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect("api.pushetta.com", httpPort)) {
    Serial.println("Connection failed");
    return;
  }

  sendToPushetta(CHANNEL, "Hello world!");
  delay(60000);
}
}

else {
//do nothing
}


User avatar
By David Alexis
#31200 I hope I got your question correctly.

There should be lots of examples available for hooking up a button with Arduino. But here are the basics. Let's say you have the button hooked up to pin 2 on the ESP8266 (like I did on my ESP-01). First you have to set up pin 2 for input. Then you read the button state in loop() using the digitalRead() function.

Code: Select allconst int button = 2;              // This makes it easy to refer to the button in code and to change things later, if needed.

void setup() {
    // Set up the button pin for input
    pinMode( button, INPUT_PULLUP );

    // Your other setup code, if any, goes here...
}

void loop() {
    // Read the button pin to see if it was pressed.  If it is, the value will be LOW.
    int buttonState = digitalRead( button );

    // Now do stuff that should be triggered by the button press
    if ( buttonState == LOW ) {
        // Put your code here.  e.g. connect, send, disconnect.

        // Add a delay at the end of your code block to handle debouncing the button. 
        // (That's an interesting, but necessary, thing to prevent the button press from being detected multiple times.)
        // A delay is just one approach to doing this.
        delay( 500 );            // This prevents the button state from being read again for half a second.
    }
}
User avatar
By David Alexis
#31292 Excellent post, torntrousers. I'm working on a little project that needs to wake up on a button press. My only issue with the approach is that the ESP8266 takes about 5 seconds on powerup to get fully connected to my network and ready to send. I'm going to test various network options to see if I can bring that time down. Then the power down / wake and send approach would work perfectly for me.