Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By dmpotter
#25135 Thank you both for this thread, it got me going on my ESP12E

With a few changes

This is the wiring I had to do
Image

Then I ran a lead from GPIO14 to a LED & Resistor to GND

In the code provided I replaced RELAY_PIN = 2 with RELAY_PIN = 14

I also had to do this each time I recompiled
Leave GPIO0 to GND
Pull the 3.3v from Power Supply and put it back in (I suppose this put the board back into flash mode)

Thank you again!


PS: Here is my ugly wiring on my desk.
Orange Wire is from GPIO0
Yellow Wire is from GPIO14

Image
User avatar
By Mario Mikočević
#25218 Heya,

here's my example -

Code: Select all/*
 *
 * Copyright (c) 2015. Mario Mikočević <mozgy>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

// Serial printing ON/OFF
#include "Arduino.h"
#define DEBUG false
#define Serial if(DEBUG)Serial
#define DEBUG_OUTPUT Serial

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

// if you use GPIO1 set Serial #define to false
#define LED_PIN   1 // ESP-01 blue LED, GPIO1
// #define LED_PIN  16 // NodeMCU blue LED
#define LED_ON    digitalWrite( LED_PIN, LOW )
#define LED_OFF   digitalWrite( LED_PIN, HIGH )

const char* myssid  = "ESPWiFi";
const char* mypass  = "esp8266pwd";

ESP8266WebServer server ( 80 );

char tmpstr[40];

extern "C" {
#include "user_interface.h"
uint32_t readvdd33(void);
}

void setup() {
  uint8_t i;

  Serial.begin(115200);
  delay(10);
  Serial.setDebugOutput(true);

  Serial.println();
  Serial.print(F("Heap: ")); Serial.println(system_get_free_heap_size());
  Serial.print(F("Boot Vers: ")); Serial.println(system_get_boot_version());
  Serial.print(F("CPU: ")); Serial.println(system_get_cpu_freq());
  Serial.print(F("SDK: ")); Serial.println(system_get_sdk_version());
  Serial.print(F("Chip ID: ")); Serial.println(system_get_chip_id());
  Serial.print(F("Flash ID: ")); Serial.println(spi_flash_get_id());
  // Serial.print(F("Flash Size: ")); Serial.println(???());
  Serial.print(F("Vcc: ")); Serial.println(readvdd33());
  Serial.println();

  delay( 5000 );
  pinMode( LED_PIN, OUTPUT );

  // let them know we're alive
  for ( i=0; i<10; i++ ) {
    LED_ON;
    delay(40);
    LED_OFF;
    delay(80);
  }
  LED_OFF;

  initWiFi();

  setupWebServer();

}

void loop() {
  server.handleClient();
  delay(1);
}

void initWiFi(void) {

  WiFi.softAP( myssid, mypass );
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

}

String HTML_Root = "<!doctype html> <html> <head> ESP8266 Web Server </head> <body>\
  <button onclick=\"window.location.href='/ledon'\">LED ON</button>\
  <button onclick=\"window.location.href='/ledoff'\">LED OFF</button>\
</body> </html>";

String HTML_LED_ON = "<!doctype html> <html> <head>\
<script>window.onload = function() { setInterval(function() {window.location.replace('/');}, 1500); };</script>\
</head> <body> <h1>LED is now ON!</h1> </body> </html>";

String HTML_LED_OFF = "<!doctype html> <html> <head>\
<script>window.onload = function() { setInterval(function() {window.location.replace('/');}, 1500); };</script>\
</head> <body> <h1>LED is now OFF!</h1> </body> </html>";

void handleLEDON() {
  Serial.println("LED ON");
  ElapsedStr( tmpstr );
  Serial.println( tmpstr );
  server.send ( 200, "text/html", HTML_LED_ON );
  LED_ON;
}

void handleLEDOFF() {
  Serial.println("LED OFF");
  ElapsedStr( tmpstr );
  Serial.println( tmpstr );
  server.send ( 200, "text/html", HTML_LED_OFF );
  LED_OFF;
}

void handleRoot() {
  server.send ( 200, "text/html", HTML_Root );
}

void setupWebServer(void) {

  server.on ( "/", handleRoot );

  server.on ( "/favicon.ico", []() {
    Serial.println("favicon.ico");
    ElapsedStr( tmpstr );
    Serial.println( tmpstr );
    server.send ( 200, "text/html", "" ); // better than 404
  } );

  server.on ( "/ledon", handleLEDON );

  server.on ( "/ledoff", handleLEDOFF );

  server.onNotFound ( []() {
    Serial.println("Page Not Found");
    server.send ( 404, "text/html", "Page not Found" );
  } );

  server.begin();

}

void ElapsedStr( char *str ) {

  unsigned long sec, minute, hour;

  sec = millis() / 1000;
  minute = ( sec % 3600 ) / 60;
  hour = sec / 3600;
  sprintf( str, "Elapsed " );
  if ( hour == 0 ) {
    sprintf( str, "%s   ", str );
  } else {
    sprintf( str, "%s%2d:", str, hour );
  }
  if ( minute >= 10 ) {
    sprintf( str, "%s%2d:", str, minute );
  } else {
    if ( hour != 0 ) {
      sprintf( str, "%s0%1d:", str, minute );
    } else {
      sprintf( str, "%s ", str );
      if ( minute == 0 ) {
        sprintf( str, "%s  ", str );
      } else {
        sprintf( str, "%s%1d:", str, minute );
      }
    }
  }
  if ( ( sec % 60 ) < 10 ) {
    sprintf( str, "%s0%1d", str, ( sec % 60 ) );
  } else {
    sprintf( str, "%s%2d", str, ( sec % 60 ) );
  }

}


you can choose GPIO1 for ESP blue LED or GPIO16 for NodeMCU dev boards.
Note to *remember* that on GPIO1 you don't have serial debug output this way.

--
Mozz
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!