Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By tlerdrden
#33130 Finally, I've had time to make some tests. This is my report:

I've used the Arduino IDE and an ESP12 module.

1. Tried with the @igrr sketch ( https://github.com/esp8266/Arduino/issues/268, Remember to change your SSID and password, and selet in the Tools Menu 2M Flash Size ). The sketch automatically returns (through serial), the module status, in my case>

Code: Select all.......ip:192.168.0.5,mask:255.255.255.0,gw:192.168.0.1
.
Connected to LAURA
IP address: 192.168.0.5
Sketch size: 343068
Free size: 704512


I have closed the Arduino IDE and opened again waiting to see the ESP IP in the ports menu, but I haven't had luck.

2. I have also tried using the following sketch http://www.wemos.cc/d1/Wireless_Upload. (Not necessary to select 2M Flash Size). When I close and open the Arduino Ide, I can see the ESP module IP in the ports menu, but when I try to upload a sketch, the module asks me a password. I have no idea about what password should I type....

Any comments or similar experiences?

Further information:

Arduino IDE version installed: 1.6.5

Additional Boards manager installed: http://arduino.esp8266.com/staging/pack ... index.json

Thanks,

A.
User avatar
By krzychb
#33181 The OTA implementation for esp8266 / Arduino by igrr is freaking awesome!

I used the following s/w and h/w:
    • Arduino IDE 1.6.5
    • Platform package: esp8266-1.6.5-947-g39819f0
    • Sktech: DNS_SD_Arduino_OTA.ino
    • ESP module: LoLin NodeMcu V3 with CH340G

It worked for me from the first attempt in the following simple seven steps:

1. Follow installation of Arduino IDE and ESP8266 board support described under https://github.com/esp8266/Arduino#installing-with-boards-manager

2. Upload python 2.7.10 (not 3.5.0, etc.), start installer, select “Add python.exe to Path” (not selected by default) and run installation.

3. Start Arduino IDE and load sketch DNS_SD_Arduino_OTA.ino available under File > Examples > ESP8266mDNS
esp8266-ota-1.png

4. Update ssid[] and pass[] in the sketch to match your router and flash this sketch over serial using the following settings:
esp8266-ota-2.png

5. After a dozen (or two dozens :D ) of seconds the esp8266-ota port will show up :
esp8266-ota-3.png

6. Then change setting as follows:
esp8266-ota-4.png

7. And flash again the same sketch over OTA :D :D
esp8266-ota-5.png


Good luck!

Loading of sketches using OTA is several times faster than using serial. This argument alone is enough for me to switch to OTA at least during firmware development.

@igrr - thank you for raising ESP8266 revolution by providing slick, versatile and super user friendly implementation of ESP8266 in Arduino IDE :D

Krzysztof
You do not have the required permissions to view the files attached to this post.
Last edited by krzychb on Sat Nov 07, 2015 8:23 am, edited 3 times in total.
User avatar
By danbicks
#33186 This sounds great if the initial sketch is a type of WIFI bootloader.

Does this mean that to flash normal sketch's one would require the ESP8266mDNS example code embedded within their own sketch?

If this is the case this would be a limitation.

Great work by Igrr in all instance's.

Regards

Dans
User avatar
By krzychb
#33222 To have OTA functionality in your own sketch you need to embed inside three pieces of code:

1. File includes and variable declarations - put them before setup()
Code: Select all#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
const char* host = "esp8266-ota";
const uint16_t aport = 8266;
WiFiServer TelnetServer(aport);
WiFiClient Telnet;
WiFiUDP OTA;

2. Initialization of OTA services. Run this code after checking if conditions to perform OTA are satisfied (see below). Quick and dirty approach is to put this code just inside the setup(), after WiFi connection is established.
Code: Select all    MDNS.begin(host);
    MDNS.addService("arduino", "tcp", aport);
    OTA.begin(aport);
    TelnetServer.begin();
    TelnetServer.setNoDelay(true);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

3. Waiting for first OTA packet and subsequent flashing. Run this code if you are ready to flash (see below). The quick and dirty solution is exposing “if (OTA.parsePacket()) {…}” directly inside the loop().
Code: Select all  if (OTA.parsePacket()) {
    IPAddress remote = OTA.remoteIP();
    int cmd  = OTA.parseInt();
    int port = OTA.parseInt();
    int size   = OTA.parseInt();

    Serial.print("Update Start: ip:");
    Serial.print(remote);
    Serial.printf(", port:%d, size:%d\n", port, size);
    uint32_t startTime = millis();

    WiFiUDP::stopAll();

    if(!Update.begin(size)){
      Serial.println("Update Begin Error");
      return;
    }

    WiFiClient client;
    if (client.connect(remote, port)) {

      uint32_t written;
      while(!Update.isFinished()){
        written = Update.write(client);
        if(written > 0) client.print(written, DEC);
      }
      Serial.setDebugOutput(false);

      if(Update.end()){
        client.println("OK");
        Serial.printf("Update Success: %u\nRebooting...\n", millis() - startTime);
        ESP.restart();
      } else {
        Update.printError(client);
        Update.printError(Serial);
      }
    } else {
      Serial.printf("Connect Failed: %u\n", millis() - startTime);
    }
  }
  //IDE Monitor (connected to Serial)
  if (TelnetServer.hasClient()){
    if (!Telnet || !Telnet.connected()){
      if(Telnet) Telnet.stop();
      Telnet = TelnetServer.available();
    } else {
      WiFiClient toKill = TelnetServer.available();
      toKill.stop();
    }
  }
  if (Telnet && Telnet.connected() && Telnet.available()){
    while(Telnet.available())
      Serial.write(Telnet.read());
  }
  if(Serial.available()){
    size_t len = Serial.available();
    uint8_t * sbuf = (uint8_t *)malloc(len);
    Serial.readBytes(sbuf, len);
    if (Telnet && Telnet.connected()){
      Telnet.write((uint8_t *)sbuf, len);
      yield();
    }
    free(sbuf);
  }

Under “checking if conditions to perform OTA are satisfied” or “if you are ready to flash” I would put the following:
1. Check if ESP8266 may not damage or adversely affect equipment connected to it or itself.
2. Verification of source of OTA packets is legitimate / trusted.
This is to make sure that the module may be safely flashed with minimum risk of damaging something or being violently hacked.

Happy and safe OTA flashing :D

Krzysztof