Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By juanmol
#15178 Hi, I've the next code:
Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "myssid";
const char* password = "mypass";

WiFiServer server(80);
int val=0;
int buttonState = 0;
int lastButtonState = 0;

void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(2, OUTPUT);
  pinMode(0, INPUT);
  digitalWrite(2, 0);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
}

void loop() {
  buttonState = digitalRead(0);

  if (buttonState == HIGH && lastButtonState == LOW) {
    delay(100);
    if (val == 0) {
      digitalWrite(2, HIGH);
      val = 1;
    }
    else {
      digitalWrite(2, LOW);
      val = 0;
    }
  }
  lastButtonState = buttonState;

  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }

  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();

  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  digitalWrite(2, val);

  client.flush();

  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  client.print(s);
  delay(1);
  Serial.println("Client disconnected");
}


I've a led connected to a pull down resistor to the GPIO 2, and a button connected to the GPIO 0 and a pull down resistor to ground, something like that:
Image
but to a ESP-01 obviusly.

When the system is up, i can turn on/off the led by url:
http://192.168.1.2/gpio/1
http://192.168.1.2/gpio/0
or by the button.

The problem i've is that i need to unplug both resistor, power the board and then plug the resistors. If i try to boot the board with the resistors connected, doesn't boot (no ping and no button working). What i need to do to boot with the resistors pluged?
User avatar
By lethe
#15181 GPIO0/2 are used to select boot mode. They both need to be high at startup for normal operation.
To achieve this, switch the VCC/GND wires on the button, so GPIO0 will read low when the button is pressed (and not blow up when accidentally configured as output/low), flip the LED and connect its resistor to VCC (it will light when GPIO2 is low).
User avatar
By juanmol
#15264 Many thanks for the answer, i've try it and somethig is not working. My first try was:
Image
with the first code. Then I read your answer and try:
Image
I think you mean that. With this code:
Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "myssid";
const char* password = "mypass";

WiFiServer server(80);
int val=0;
int buttonState = 0;
int lastButtonState = 0;

void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(2, OUTPUT);
  pinMode(0, INPUT);
  digitalWrite(2, 0);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
}

void loop() {
  buttonState = digitalRead(0);

  if (buttonState == LOW && lastButtonState == HIGH) {
    delay(100);
    if (val == 0) {
      digitalWrite(2, LOW);
      val = 1;
    }
    else {
      digitalWrite(2, HIGH);
      val = 0;
    }
  }
  lastButtonState = buttonState;

  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }

  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();

  if (req.indexOf("/gpio/0") != -1)
    val = 1;
  else if (req.indexOf("/gpio/1") != -1)
    val = 0;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  digitalWrite(2, val);

  client.flush();

  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  client.print(s);
  delay(1);
  Serial.println("Client disconnected");
}

this is correct? might works?
User avatar
By AcmeUK
#15281 Help yourself by using 74880 baudrate. Then the 'rubbish' at boot time becomes messages from the boot loader. These tell you what mode you have booted in. I suspect that you are booting in program mode.

If you are booting in normal mode you should see something like:-
ets Jan 8 2013,rst cause:1, boot mode:(3,7)

load 0x40100000, len 27564, room 16
tail 12
chksum 0x44
ho 0 tail 12 room 4
load 0x3ffe8000, len 2548, room 12
tail 8
chksum 0x6c
load 0x3ffe8a00, len 1340, room 0
tail 12
chksum 0xcf
csum 0xcf


If you are booting in program mode you wil see:-
ets Jan 8 2013,rst cause:1, boot mode:(1,7)


ALSO:- Looking at your second circuit you are showing Vcc on the Reset pin and nothing on the CH_PD pin. CH_PD needs Vcc for the thing to work, see this http://benlo.com/esp8266/esp8266-reflash-firmware.jpg