ESP8266 first code help

Hello ,
I have start to work with the ESP8266
I have connected it
open serial monitor
use AT command to connect to my WIFI - and it works

now I understand I can write a simple code to it
so I have copt the code from here

https://www.geekstips.com/esp8266-arduino-tutorial-iot-code-example/
/*
 ESP8266 Arduino Blink by Simon Peter
 Blink the blue LED on the ESP-01 module
 This example code is in the public domain
 
 The blue LED on the ESP-01 module is connected to GPIO1 
 (which is also the TXD pin; so we cannot use Serial.print() at the same time)
 
 Note that this sketch uses LED_BUILTIN to find the pin with the internal LED
*/

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level
                                    // but actually the LED is on; this is because 
                                    // it is acive low on the ESP-01)
  delay(1000);                      // Wait for a second
  digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
}

but now I see garbage in the serial monitor ....
and the blue led is not blinking....
what to do ?
how can I retuen it to the default setting ?
and what went wrong?

my project will be connecting a temp sensor to GPIO and send me a UDP when the tmp is over 40
Thanks ,

and what went wrong?

We have no idea what you did or how you did it so how can we answer that. Have you installed the Arduino core?

Describe the steps that you took to compile and upload the blink sketch to the ESP.

If you want to go back to the AT firmware you will need to upload the AT firmware. If you upload anything else to the ESP the AT firmware gets overwritten.

Ok
this is what I have and did
I’m using Arduino IDE 1.8.2

Go to File → Preferences
Add this link to Additional Board Manager(http://arduino.esp8266.com/versions/2.4.1/package_esp8266com_index.json)
Go to Tools → Board Manager
Find ESP8266 board set and activate it(download it)
Select Generic ESP8266 board from Tools->Boards
Choose your programmer COM port

then I connected the GPIO0 to GND and powered up the device
upload the code
turn off the device
remove GPIO0 cable (now it’s floating)
turn on the device
and here we are :slight_smile: It doesn’t blink

maybe something in the “tools” in wrong?
this is the default seting I have

also I have check every rate on the serial monitor and I only see noraml data at 74800???
why is it?

this is what I see at startup-

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v614f7c32
~ld

I ahve change the power supply
and now it's working

I ahve also try to upload the AT firmware from here

and it didn't give me any error -

but after it I uplaoded an example called "WiFiScan" - and it works..
so maybe it did something good :slight_smile:

I have eanother question about the GPIO - I will ask in another post

Hello ,
I have uplaod to the ESP8266 the sample WiFIClientBasic code -
and see it’s working
send me a FTp string every 5 seconds (like it need to to )

my question is -
I want to define the GPIO2 as input , and connect it to my movment sensor that send “1” when there is movement

how do I do this ?
I know how to to the connection with the wire and write the code -
my problem is the definition of the GPIO2 …

Thanks ,

You have to read this:

A Beginner’s Guide to the ESP8266

Your problem was that you didn’t select the correct boot mode.

Pieter

david1234:
my question is -
I want to define the GPIO2 as input , and connect it to my movment sensor that send “1” when there is movement

how do I do this ?
I know how to to the connection with the wire and write the code -
my problem is the definition of the GPIO2 …

you do it like in any arduino, with pinMode and digitalRead for pin 2

great
it’s "working "

but I have a electric problem

I have connect the window sensor like this :
1 wire to GND
1 wire to GPIO2

and start to check it - it never work
I’m printing the analogRead value - and every time I get 1023
so I thought there is a problem with the wire from the GPIO
well - when I connect it to GND I see - 0 , and when I connect it to 3.3V I see 1023
I have check the sensor with a simple LED and 2.2Kohm - and it’s working(so no problem with the sensor)
what could be the problem ?

Juraj:
you do it like in any arduino, with pinMode and digitalRead for pin 2

Due to the way the esp8266 does the internal pin mappings vs the way it is done on the AVR and many other cores, there can be some differences when using Arduino pin numbers that can create issues for certain esp8266 boards.

The esp8266 core implements the pin mappings for the digital i/o routines in a much more efficient way than what was done in the AVR code that most other cores also adopted.
While the way the mappings are done in the esp8266 core this makes the code simpler, smaller, and MUCH faster, it does have a few issues on a few of the esp8266 boards.

The Arduino pin number for pinMode(), digitalRead(), and digitalWrite(), uses the GPIO bit number directly.
i.e. if you say digitalRead(2) you are reading GPIO2

Where things get messy is that some esp8266 boards provide mapping define symbols like Dn or more specifically D0, D1, D2, .... D14, D15 etc...
This was done to provide some compatibility with Arduino pin numbers used on AVR based boards.
The issue is that Dn is the same as n
in other words digitalRead(2) is not necessarily the same as digitalRead(D2) - usually it will not be the same.

Complicating matters is that only some of the boards in the esp8266 core use/provide these Dn symbols.
The boards that use this are:

  • WeMos D1 R2 & mini
  • WeMos D1 mini Pro
  • WeMos D1 mini Lite
  • WeMos D1 R1
  • NodeMCU 0.9 (ESP-12 Module)
  • NodeMCU 1.0 (ESP-12E Module)

Of these boards, most label the pins on the board using the Dn number not the n number.
i.e. if you look at the board you will often see D0, D1, D2 etc... on the board.
that means that in order to use a pin that is labeled "D2" you must use D2 in the digital i/o functions not 2
i.e. digitalWrite(D2) not digitalWrite(2)

Some boards like the LinkNode D1 which is an UNO form factor board have multiple labels on the pins.
They show the Dn numbers on the top and GPIOn on the bottom of the PCB.

The one board I would recommend that you do not ever buy is the Wemos D1 R2
That board has a totally messed pin mapping. It shifted all the pins over which makes a wreck of things.
So even though that board is UNO form factor and uses Dn pin symbols, all the symbols are shifted over.
The entire point of having the Dn symbols was so you could use the Dn symbol on an UNO form factor board
and get the n pin. i.e if you used D8 you get the physical pin on the header for pin 8 on an UNO board.
With the WeMos D1 R2 that isn't the case so it can get confusing.

The main thing to keep in mind is that depending on the board you have it may use Dn pin mappings, and if so,
pin n is not the same as pin Dn so you will generally need to use the Dn pin in order to match was is labeled on the PCB.

Also you must be aware that there are certain pins that you need to stay away from or at least be very careful when using as they have special meaning and powerup/reset time.
GPIO 2 happens to be one of them.
Here is a note I created about it:

 * GPIO2 is used to control the built in LED.
 * It is recommended to avoid using GPIO0, GPIO2, & GPIO15 for inputs (see below)
 * GPIO0, GPIO2, and GPIO15 are special inputs used at reset and power up.
 * They control the boot mode:
 *  GPIO15    GPIO0    GPIO2    MODE       Description
 *    L         L        H      UART    Download Code from Uart
 *    L         H        H      FLASH   Boot from SPI flash (normal)
 *    H         x        x      SDIO    Boot from SD card
 * Programing flash using uart0 requires a boot mode of "boot from uart0"
 * with boot_sel=b001, that is, it must make sure
 * GPIO15=0, GPIO0=0, and GPIO2=1 during reset 
 * Because of this, GPIO15 has an external pulldown.
 * This makes these GPIO pins unsuitable for simple grounding switches especially
 * when switches can be pressed at powerup to set modes.
 * GPIO15 and GPIO0 do appear to work correctly after boot and GPIO0 does
 * not appear to have a pull down on it.

You can read more about this here:
http://esp8266.github.io/Arduino/versions/2.0.0/doc/reference.html

--- bill

why analogRead?

@bperrybap, sorry, but do you try to confuse OP?

this is very strange when it say

it is recommended to avoid using GPIO0, GPIO2

because this is all I have in my the ESP8266......
Now I know I need to gnd the GPIO0 for programing
and put GPIO2 as HIGH

but this is all the GPIO I have - so what can I do ?
my GPIO2 is connected to the window\door sensor that sending "1""0"

another issue that I have is that it not sending me the notification to my phone after a while
but I can see the data is going to the server (using wireshark and simple serail.print)
so I know the code is working
can it be that this is a problem with the pushingBox server? -
I'm still waitng for answer for them - so I ask here also

an 1 last thing
how can I see what the server response ?
maybe there I can see the problem

this is the sending code :

void sendNotification(String message) {

  Serial.println("starting client");

  WiFiClient client;

  Serial.println("- connecting to pushing server: " + String(logServer));
  if (client.connect(logServer, 80)) {
    Serial.println("- succesfully connected");
    Serial.println(deviceId);
    String postStr = "devid=";
    postStr += String(deviceId);
    postStr += "&message_parameter=";
    postStr += String(message);
    postStr += "\r\n\r\n";

    Serial.println("- sending data...");

    client.print("POST /pushingbox HTTP/1.1\n");
    client.print("Host: api.pushingbox.com\n");
    client.print("Connection: close\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
    Serial.println(postStr);
  }
  client.stop();
  Serial.println("- stopping the client");
}

I have read the guide you show me
didn't find an answer to my problem....

maybe you can help me find the answer (or even guide me to it:-) )

I have GPIO0 connected to 3.3V
and GPIO2 to the sensor leg
I don't have any other GPIO pins

so waht else can I do?

Thank ,

Have you tried the schematic in chapter 2?

And did you see the table with GPIO restrictions in chapter 4?

again, why analogRead? it doesn't work on io pins of esp8266. use digitalRead

david1234:
this is very strange when it say

it is recommended to avoid using GPIO0, GPIO2

because this is all I have in my the ESP8266......

that comment about not using those GPIO pins is a comment in my code for one of my projects.
I also said, for inputs
It isn't that you can't use those GPIO pins, it is that you must be careful how you use them.
It can create issues if you use them for inputs as the external source can be driving the pin and affect the boot mode.
See the documents I linked to for full details.
In my case I was using a Wemos D1 mini and I had a button connected to GPIO2. Normally that would be ok as the button was a normally open switch so as long as it wasn't pushed while the processor was booting it would be ok.
However, in my case I needed the ability to hold the button down at powerup for a special purpose so I couldn't use GPIO2 for that.

Now I know I need to gnd the GPIO0 for programing
and put GPIO2 as HIGH

but this is all the GPIO I have - so what can I do ?
my GPIO2 is connected to the window\door sensor that sending "1""0"

What specific esp8266 module do you have?
You never said which board you have. There is some slight variation between modules.
But for certain pins you can't drive the pin until after the processor has booted.
After it has booted, it can work as a normal input pin.
You should read the esp8266 documentation and the documentation for your specific board for full details.

I think you should test things methodically and in small steps rather than all at once.
It will make debugging much simpler.
i.e. get the sensor input working using just serial output without using any WiFi.
Once that is working, add in the WiFi & web support.

--- bill

Juraj:
again, why analogRead? it doesn't work on io pins of esp8266. use digitalRead

The esp8266 does have 1 analog to digital channel so analogRead(0) works.
However, I do agree that if the sensor provides a digital output, that analogRead() is not necessary.

I think one big issue here is that we don't know which specific esp8266 module is being used.
There are many and they vary substantially on what pins are available and some of the external h/w wrapped around the pins on the module.

We are also not seeing actual full code of what is being used just small snippets.
This creates many unknowns and ends up creating some confusion since the answers being provided have to take into consideration issues on boards that the OP may not be using.

--- bill

yes ,
this is what I don't understadn
what is GPIO15?

and where is GPIO2 - the one I can use?

Thanks ,

The analogRead was just to see that the sensor is working
I have read the esp8266 documentation - and didn’t find an answer there
I’m using this ESP8266
https://www.ebay.com/itm/New-ESP8266-Serial-WIFI-Wireless-Transceiver-WLAN-Module-for-Arduino-8051-AVR/231816914527?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2057872.m2749.l2649

as you can see he have only 2 GPIO

also all the code is working - because I can see in wiresahrk on my netwrok that the module is sending the data to the server .
it’s just been “ignore”? I have send mail to them - but didn’t get any repaly

also If I create a server on my computer and send the data to my computer - I can see that everytime he send me the notification.
so maye I need to add “delay”? or wait until I’m getting an answer from the Notification server ?

this is the full code :

/************
  GeeksTips.com
  ESP8266 Arduino Tutorial - Push notification messages example
  Arduino code example
  www.geekstips.com

  - replace the dots with your Wi-fi credentials and
  - your deviceId from pushingBox account
*/
#include <ESP8266WiFi.h>

// PushingBox scenario DeviceId code and API
String deviceId = "v12451BA7TR33CAB";
const char* logServer = "api.pushingbox.com";

const char* ssid = "David";
const char* password = "1234556788";

const int GPIO2 = 2;//door sensor input
bool sending = false;



void setup() {
  Serial.begin(115200);
  delay(1);


  // Sending a notification to your mobile phone
  // function takes the message as a parameter

  Serial.println("- connecting to Home Router SID: " + String(ssid));

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println();
  Serial.println("- succesfully connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  delay(500);
  pinMode (GPIO2 , INPUT);
  delay(1500);
  sendNotification("Sensor is now OnLIne!!");
}



void loop()
{

  if (digitalRead(GPIO2) == LOW)
  {
    String  line = "Door1 is Close";
    if (sending == false)
    {
      Serial.println(line);
      sendNotification(line);
      sending = true;
    }


  }

  else
  {
    String line = "Door1 is Open";
    if (sending == true)
    {
      Serial.println(line);
      sendNotification(line);
      sending = false;

    }


  }
}


void sendNotification(String message) {

  Serial.println("starting client");

  WiFiClient client;

  Serial.println("- connecting to pushing server: " + String(logServer));
  if (client.connect(logServer, 80)) {
    Serial.println("- succesfully connected");
    Serial.println(deviceId);
    String postStr = "devid=";
    postStr += String(deviceId);
    postStr += "&message_parameter=";
    postStr += String(message);
    postStr += "\r\n\r\n";

    Serial.println("- sending data...");

    client.print("POST /pushingbox HTTP/1.1\n");
    client.print("Host: api.pushingbox.com\n");
    client.print("Connection: close\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
    Serial.println(postStr);
  }
  client.stop();
  Serial.println("- stopping the client");
}

GPIO15 is not broken out on the ESP-01. It exists internally, and it is already connected to ground.

david1234:
and where is GPIO2 - the one I can use?

It's right here:

Keep in mind that:

  • GPIO1 and GPIO2 will burst out serial data on boot, this could damage your sensor.
  • GPIO2 cannot be low at boot.
  • GPIO0 cannot be low at boot, except during programming.

If you don't want to live with these restrictions, you could get a Wemos D1 mini for around $3.50.

http requires \r\n as line ending