Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By dmpotter
#25255 I still think the below still falls under this topic, however it has an IFTTT hook for turning stuff on and off from the web. In this case, I added buttons which will control various items that I have connected on Wink's Genius power strips. So, I use the Maker channel (https://ifttt.com/maker) as my "THIS" and the Wink's Genius Power Strip (https://ifttt.com/pivot_power_genius) function as the "THAT".

Using Code which I got from Piotr Bagniewski's Channel on youtube (https://www.youtube.com/watch?v=gp6FN0A20qA) and just adding different things, this is what it looks like when running.



This is the code

Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "...YOURROUTERSSSID...";          // Our Wifi SSID
const char* password = "...YOURROUTERSSISPASS...";       // Our Wifi Password
const int buttonPin1 = 13;                // GPIO13 which would be connected to a button
const int buttonPin2 = 12;                // GPIO12 which would be connected to a button
const int buttonPin3 = 14;                // GPIO14 which would be connected to a button
const int buttonPin4 = 16;                // GPIO16 which would be connected to a button
const char* host = "maker.ifttt.com";     //IFTTT channel address
const char* url = "ASTRING";               // Based on which button was pressed this would be the rest of the IFTTT URL Needed
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int led = 4;                                // LED to Signal that the command was sent to IFTTT
int ledError = 5;                           // LED which would represent problem connecting to Router

void setup() {
  Serial.begin(115200);                   
  pinMode(led, OUTPUT);
  pinMode(ledError,OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  }

int value = 1;
void loop() {
   digitalWrite(led,0);
   digitalWrite(ledError,0);
   buttonState1 = digitalRead(buttonPin1);
   buttonState2 = digitalRead(buttonPin2);
   buttonState3 = digitalRead(buttonPin3);
   buttonState4 = digitalRead(buttonPin4);

    if (buttonState1 == HIGH || buttonState2 == HIGH || buttonState3 == HIGH || buttonState4 == HIGH) {
      Serial.println("Button Press Detected");
       if (value == 1){
       // We now connect to local WIFI
       Serial.println("Attempting to connect to Router");
       WiFi.begin(ssid, password);
       while (WiFi.status() != WL_CONNECTED) {
       digitalWrite(ledError,1);  // Turn the Error LED on to show there is a problem
       delay(250);
       digitalWrite(ledError,0);  // Turn off the LED so it will have a flash effect, or least keep it off when loop ends
       delay(250);
       Serial.println("Trying Again");
       }
       WiFiClient client;
       const int httpPort = 80;
       if (!client.connect(host, httpPort)) {
       return;
       }
       // We now create a URI for the request
       digitalWrite(led,1); // TURN ON LED Showing that we will send something to IFTTT
       Serial.println("Sending Command to IFTTT's Maker Channel");
       if (buttonState1 == HIGH) {
          url = "/trigger/MAKERNAME1/with/key/MYREALLONGMAKERPRIVATEKEY";   //our link to trigger the event with special key and event name
       }
       if (buttonState2 == HIGH) {
          url = "/trigger/MAKERNAME2/with/key/MYREALLONGMAKERPRIVATEKEY";   //our link to trigger the event with special key and event name
       }
       if (buttonState3 == HIGH) {
          url = "/trigger/MAKERNAME3/with/key/MYREALLONGMAKERPRIVATEKEY";   //our link to trigger the event with special key and event name
       }
       if (buttonState4 == HIGH) {
          url = "/trigger/MAKERNAME3/with/key/MYREALLONGMAKERPRIVATEKEY";   //our link to trigger the event with special key and event name
       }
       // This will send the request to the server
       Serial.println(url);
       client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
       value = 0;
       delay(1000);
       }
         digitalWrite(led,0);  // TURN OFF LED to Show that we are done with IFTTT
    }
    else{
    value = 1;
    delay(500);
    }
}


Thank you!
User avatar
By johntech2014
#28572 I am looking for information (not write the code for me) on interfacing the MC23017 with the ESP8266-01 so I can have a web page with 16 buttons that I can turn on and off the outputs on the MC23017.

I looked at Steve's sketch above, even though I don't understand all the networking stuff yet, but do see how he turns on and off the relay.

I also have several different sketches that I can run on the 8266-01 that controls the 23017 and I can make LED's turn on and off. One I can turn multiple LED's on at once and the other, one at a time. I know I have to add Wire.begin(0,2) to setup for the data and clock pins.

I keep looking at the following bit of code and think in there lies the answer to the interfacing I'm looking for:

Code: Select all 
  server.on("/", handleRoot);
 
  server.on("/lightsON", [](){
  server.send(200, "text/plain", "Okay -- Light is ON!");
  digitalWrite(RELAY_PIN, 1);
  });
 
  server.on("/lightsOFF", [](){
  server.send(200, "text/plain", "Okay -- Light is OFF!");
  digitalWrite(RELAY_PIN, 0);
  });


You would have to setup all of the pins for OUTPUT mode. Then what ever button you clicked on the screen that would have to be passed back to the sketch for the digitalWrite(buttonPressed, HIGH or LOW).

The one sketch for the 23017 would use 0 to 15 for the LED's but could only turn 1 at a time on where the second one can turn multiple LED's by using 0 to 255..

Any ideas or directions to pursue would be greatly appreciated.

Thank you

John Frankforther
User avatar
By johntech2014
#28685 Yes I did get the Adafruit sketch to work on the ESP8266-01. Now I want to go to the next step and control the LED's on/off via web page buttons. So I will design a simple web interface with 15 buttons then try to figure out how to transfer a button push to turn on associated LED on the MC23017. I have seen it done simply in Lua .