How to program esp8266 to connect to wifi

Discussion in 'Embedded Systems and Microcontrollers' started by quique123, Jul 12, 2019.

  1. quique123

    Thread Starter Member

    May 15, 2015
    301
    3
    If I connect an Esp8266 to a battery and dht sensor, how would I program the esp8266 to be configurable by a user to connect to a wifi?

    Iow, when it is powered up by th user by plugging in a battery, how could he configure the module to connect to a local wifi?
     
  2. dendad

    Distinguished Member

    Feb 20, 2016
    2,829
    764
  3. quique123

    Thread Starter Member

    May 15, 2015
    301
    3
    OK I found that what I need is a Wifi manager library that preloads with the sketch.

    When I went to the ide library manager I found 4 options actually, WifiManager by tzupac and 3 others. Is there a more recommended one?
     
  4. quique123

    Thread Starter Member

    May 15, 2015
    301
    3
    ok i got the wifiManager library and ArduinoJSON library on my IDE. I tried uploading the sketch from Example #1 here:

    https://randomnerdtutorials.com/wif...-parameter-and-manage-your-ssid-and-password/

    but I get an error when uploading:
    Code (Text):
    1.  
    2. esptool.py v2.6
    3. 2.6
    4. esptool.py v2.6
    5. Serial port /dev/cu.wchusbserialfd120
    6. Connecting......
    7. Chip is ESP8266EX
    8. Features: WiFi
    9. MAC: 5c:cf:7f:a3:f9:99
    10. Uploading stub...
    11. Running stub...
    12. Stub running...
    13. Configuring flash size...
    14. Auto-detected Flash size: 1MB
    15. Flash params set to 0x0320
    16. Compressed 261248 bytes to 190878...
    17.  
    18. Writing at 0x00000000... (8 %)Traceback (most recent call last):
    19.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/upload.py", line 25, in <module>
    20.     esptool.main(fakeargs)
    21.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool/esptool.py", line 2698, in main
    22.     operation_func(esp, args)
    23.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool/esptool.py", line 2158, in write_flash
    24.     esp.flash_defl_block(block, seq, timeout=DEFAULT_TIMEOUT * ratio * 2)
    25.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool/esptool.py", line 104, in inner
    26.     return func(*args, **kwargs)
    27.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool/esptool.py", line 632, in flash_defl_block
    28.     self.ESP_FLASH_DEFL_DATA, struct.pack('<IIII', len(data), seq, 0, 0) + data, self.checksum(data), timeout=timeout)
    29.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool/esptool.py", line 354, in check_command
    30.     val, data = self.command(op, data, chk, timeout=timeout)
    31.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool/esptool.py", line 332, in command
    32.     p = self.read()
    33.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool/esptool.py", line 277, in read
    34.     return next(self._slip_reader)
    35.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool/esptool.py", line 1889, in slip_reader
    36.     raise FatalError('Invalid head of packet (0x%s)' % hexify(b))
    37. esptool.FatalError: Invalid head of packet (0x00)
    38. esptool.FatalError: Invalid head of packet (0x00)
    39.  
    the wiring is:
    ESP-Nano
    Tx------Tx
    Rx------Rx
    G-------G
    Vcc-----3.3V
    EN---10k---3.3V
    GPIO0---NC (only when plugging in the 3.3V pin but then disconnected before uploading)
    GPIO2---NC
    RST------NC

    Ive uploaded code to ESP8266 before but it was a while back. I have the black ESP01 module i believe its called.
     
  5. quique123

    Thread Starter Member

    May 15, 2015
    301
    3
    I just tried with an Arduino UNO which is what I originally used and I get pretty much the same error:

    Code (Text):
    1. esptool.py v2.6
    2. 2.6
    3. esptool.py v2.6
    4. Serial port /dev/cu.usbmodemFD121
    5. Connecting........_____....._____....._____....._____....._____....._____.....____Traceback (most recent call last):
    6.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/upload.py", line 25, in <module>
    7.     esptool.main(fakeargs)
    8.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool/esptool.py", line 2653, in main
    9.     esp.connect(args.before)
    10.   File "/Users/quique123/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/tools/esptool/esptool.py", line 468, in connect
    11.     raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))
    12. esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
    13. esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
     
  6. dendad

    Distinguished Member

    Feb 20, 2016
    2,829
    764
    Last edited: Jul 17, 2019
  7. quique123

    Thread Starter Member

    May 15, 2015
    301
    3
    No I'm using an Arduino UNO. I've done it before without an ftdi adapter.

    Although I bought one of these a while back, I might still have it but I know I never used it:

    USB to TTL Adapter, USB to Serial Converter for Development Projects - Featuring Genuine FTDI USB UART IC 'FT232RL' https://www.amazon.com/dp/B075N82CDL/ref=cm_sw_r_cp_api_i_7j5lDb8912PPQ
     
    Last edited: Jul 17, 2019
  8. dendad

    Distinguished Member

    Feb 20, 2016
    2,829
    764
  9. quique123

    Thread Starter Member

    May 15, 2015
    301
    3
    Yes it's this setup:

    [​IMG]

    And I get the error
     
  10. dendad

    Distinguished Member

    Feb 20, 2016
    2,829
    764
    You may have damaged the ESP8266 by running the serial 5V.
    Can you add the resistors and try another board?
     
  11. quique123

    Thread Starter Member

    May 15, 2015
    301
    3
    Ok i just tried the ftdi adapter and it uploaded fine. So:

    Rx-Tx
    Tx-Rx
    3v3-EN & Vcc
    GND-GPIO0 & GND

    So Im just weirded out why the previous setups didnt work. I just took this FTDI adapter out of its sealed bag so I know I hadnt used it before. And in my notes I dont show having used a voltage divider before and I have 2 specific notes that show my wiring:

    first with the nano, tx-rx and rx-tx and in the serial monitor i was able to write to it and get responses. That didnt work this time.

    second with the UNO, rx-rx and tx-tx and i was able to upload the sketch for sampling dht22 and posting to cloud server for emoncms and to this day that ESP is still running fine.

    I mean, my notes might be wrong or incomplete, but i dont know...ill check visual evidence such as pictures and videos to see if I did something different than my notes.
     
  12. dendad

    Distinguished Member

    Feb 20, 2016
    2,829
    764
    I have found some ESP8266 boards to be fussy. And a couple of mine no longer work. There are some forum comments about the lack of power supply stability bricking some too.
    But it just may be the phase of the moon or you not holding your mouth quite right ;)
     
    Sensacell likes this.
Loading...