Difference between revisions of "ESP8266"

From Hackerspace ACKspace
Jump to: navigation, search
m (more typoKTHXBYE)
m (fixed TOC transclusion)
 
(19 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{Project
 
{{Project
|State=Active
+
|Featured=No
 +
|State=Completed
 
|Members=Prodigity, Da Syntax, xopr
 
|Members=Prodigity, Da Syntax, xopr
 
|Description=Playing around with cheap wifi modules
 
|Description=Playing around with cheap wifi modules
 +
|Picture=SpaceState.png
 
}}
 
}}
{{InfoBox
+
<noinclude>__TOC__</noinclude>
|Type=featured
 
|Title=NOTE
 
|Text=For the ESP Egg Salad programmer, see [[ESP^2]]
 
|Clear=both
 
|Background=#f7fff7
 
}}
 
  
 
== programming the ESP8266 ==
 
== programming the ESP8266 ==
 
=== hardware: using the [[ESP^2]] ===
 
 
:NOTE: the used serial connection is 5v (but has some resistors), which is officially not supported
 
:NOTE2: currently, there is only an 8-pin header for the ESP-01
 
* before connecting the ESP^2, plug in the ESP-01 (note the markings)
 
* flip the switch to program mode
 
* plug in the USB cable
 
* restore the switch
 
* upload using your favorite software method
 
  
 
=== hardware: using an FTDI cable ===
 
=== hardware: using an FTDI cable ===
Line 38: Line 24:
 
* release program
 
* release program
 
* upload using your favorite software method
 
* upload using your favorite software method
 +
 +
* Computer1up found that the CH340 Serial to USB modules should work. [https://nl.aliexpress.com/item/CH340-module-instead-of-PL2303-CH340G-RS232-to-TTL-module-upgrade-USB-to-serial-port-in/32630048726.html?spm=a2g0s.9042311.0.0.eMlp89 CH340 modules on Aliexpess] We should have some of these modules in the hACKspace, in the corner where the ESP8266 modules are found.
 +
* Xopr originally thought that these don't supply enough power to program an ESP-12S (or E?), but computer1up has successfully programmed one of these ESP's using the CH340.
 +
* Also, computer1up has only had luck with programming his ESP-12S when the RESET pin is connected while programming, instead of disconnecting it after boot.
  
 
=== software: using Arduino 1.6.4 ===
 
=== software: using Arduino 1.6.4 ===
Line 44: Line 34:
 
* Install Arduino 1.6.4 from the [http://www.arduino.cc/en/main/software Arduino website].
 
* Install Arduino 1.6.4 from the [http://www.arduino.cc/en/main/software Arduino website].
 
* Start Arduino and open Perferences window.
 
* Start Arduino and open Perferences window.
* Enter ''http://arduino.esp8266.com/package_esp8266com_index.json'' into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
+
* Enter ''http://arduino.esp8266.com/stable/package_esp8266com_index.json'' into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
 
* Open Boards Manager from Tools > Board menu and install ESP8266 platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).
 
* Open Boards Manager from Tools > Board menu and install ESP8266 platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).
 
* For the ESP^2, the device name (under Linux) is /dev/ttyACM3
 
* For the ESP^2, the device name (under Linux) is /dev/ttyACM3
Line 53: Line 43:
 
* You can now upload your sketch
 
* You can now upload your sketch
  
NOTE: currently, programming the ESP-12E will result in corrupted firmware with all kinds of strange behaviour.  This is caused by the esptool that comes with the Arduino ESP8266 package.
+
NOTE: currently, programming the ESP-12E will result in corrupted firmware with all kinds of strange behaviour.  This is caused by the esptool that comes with the Arduino ESP8266 package. (See [https://github.com/esp8266/Arduino/issues/95#issuecomment-96399149 this comment] on april 28)
  
 
There are two ways to circumvent this:
 
There are two ways to circumvent this:
Line 60: Line 50:
 
#* note the location of the .bin file and use the [[ESP8266#software: using esptool.py to upload binaries|esptool]] method
 
#* note the location of the .bin file and use the [[ESP8266#software: using esptool.py to upload binaries|esptool]] method
 
# add a better working esptool.py in Arduino (which is what [[User:Xopr|xopr]] did) by changing some files within the ESP8266 package
 
# add a better working esptool.py in Arduino (which is what [[User:Xopr|xopr]] did) by changing some files within the ESP8266 package
#* download '''[https://raw.githubusercontent.com/themadinventor/esptool/master/esptool.py esptool.py]''' and put it in '''~/.arduino15/packages/esp8266/hardware/esp8266/1.6.5-939-g033463a/tools/'''
+
#* download '''[https://raw.githubusercontent.com/themadinventor/esptool/master/esptool.py esptool.py]''' and put it in '''~/.arduino15/packages/esp8266/hardware/esp8266/1.6.&lt;BUILD_NUMBER&gt;/tools/'''
#* edit '''~/.arduino15/packages/esp8266/hardware/esp8266/1.6.5-939-g033463a/boards.txt''' and after
+
#* edit '''~/.arduino15/packages/esp8266/hardware/esp8266/1.6.&lt;BUILD_NUMBER&gt;/boards.txt''' and after
 
#* ''generic.menu.UploadTool.espota.upload.tool=espota''
 
#* ''generic.menu.UploadTool.espota.upload.tool=espota''
 
#* add
 
#* add
 
#:'''generic.menu.UploadTool.esptoolpy=Serial (python)'''
 
#:'''generic.menu.UploadTool.esptoolpy=Serial (python)'''
 
#:'''generic.menu.UploadTool.esptoolpy.upload.tool=esptoolpy'''
 
#:'''generic.menu.UploadTool.esptoolpy.upload.tool=esptoolpy'''
#* edit '''~/.arduino15/packages/esp8266/hardware/esp8266/1.6.5-939-g033463a/platform.txt''' and at the bottom, add
+
#* edit '''~/.arduino15/packages/esp8266/hardware/esp8266/1.6.&lt;BUILD_NUMBER&gt;/platform.txt''' and at the bottom, add
 
#:'''tools.esptoolpy.cmd=python'''
 
#:'''tools.esptoolpy.cmd=python'''
 
#:'''tools.esptoolpy.cmd.windows=python.exe'''
 
#:'''tools.esptoolpy.cmd.windows=python.exe'''
Line 73: Line 63:
 
#:'''tools.esptoolpy.upload.params.verbose='''
 
#:'''tools.esptoolpy.upload.params.verbose='''
 
#:'''tools.esptoolpy.upload.params.quiet='''
 
#:'''tools.esptoolpy.upload.params.quiet='''
#:'''tools.esptoolpy.upload.pattern="{cmd}" "{path}/esptool.py" --baud {upload.speed} --port "{serial.port}" write_flash 0x00000 "{build.path}/{build.project_name}'''
+
#:'''tools.esptoolpy.upload.pattern="{cmd}" "{path}/esptool.py" --baud {upload.speed} --port "{serial.port}" write_flash 0x00000 "{build.path}/{build.project_name}"'''
 
#*Now, in Arduino, you have an extra menu option under 'Tools', 'Upload using', 'Serial (python)'
 
#*Now, in Arduino, you have an extra menu option under 'Tools', 'Upload using', 'Serial (python)'
 +
 +
=== software: using Arduino IDE 1.8.2 ===
 +
* Install the Arduino IDE from the Arduino website. Note: there's also a web editor, but this HAS NOT been tested if it works with the ESP boards yet.
 +
* Enter ''http://arduino.esp8266.com/stable/package_esp8266com_index.json'' into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
 +
* Open Boards Manager from Tools > Board menu and install ESP8266 platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).
 +
* Select your ESP8266 board from the board selector. The default settings are usually fine.
 +
* Note that this method is not tested with the [[ESP^2]]. This did work with an Arduino UNO R3 with its ATMega-chip removed and with the CH340 modules.
 +
* It seems that the strange behaviour of the esptool in the previous version of Arduino (or the board libraries) has been fixed.
  
 
=== software: using esptool.py to upload binaries ===
 
=== software: using esptool.py to upload binaries ===
Line 84: Line 82:
 
**:sudo ./esptool.py --port /dev/ttyUSB0  write_flash 0x00000 ../nodemcu_float_0.9.6-dev_20150704.bin  
 
**:sudo ./esptool.py --port /dev/ttyUSB0  write_flash 0x00000 ../nodemcu_float_0.9.6-dev_20150704.bin  
 
** Windows
 
** Windows
**:GOOD LUCK, edit me if you succeeded
+
**:esptool.py --port COM1  write_flash 0x00000 C:\example\nodemcu_float_0.9.6-dev_20150704.bin
 
** Mac
 
** Mac
 
**:GOOD LUCK, edit me if you succeeded
 
**:GOOD LUCK, edit me if you succeeded
Line 101: Line 99:
 
conn:send("GET /test.lua HTTP/1.1\r\nHost: www.example.com\r\n"        .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
 
conn:send("GET /test.lua HTTP/1.1\r\nHost: www.example.com\r\n"        .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
 
end
 
end
</pre>  
+
</pre>
 +
 
 +
[[User:Prodigity|Prodigity]]:
 +
I've modified [[User:Da Syntax|Da Syntax]]'s code so that it will now recognize <nowiki><code></nowiki> tags.
 +
There are still some restrictions though:
 +
* It does not recognize closing tags and therefore your code should be completely at the bottom of a http response
 +
* The code tag will only work if it's behind a newline and should end with a newline. (both \n and \r\n are fine)
 +
 
 +
<pre>
 +
function netload(ip, port, url) -- url is the remainder after the domain name
 +
    conn=net.createConnection(net.TCP, 0)
 +
    conn:on("receive", function(conn, payload)
 +
        file.open("download.dat", "w")
 +
        file.write(payload)
 +
        file.close()
 +
        file.open("download.dat", "r") -- Opens file twice, could be done nicer but does the job.
 +
        line = file.readline()
 +
        while line ~= nil do
 +
            if line == "<code>\n" then
 +
                break
 +
            elseif line == "<code>\r\n" then
 +
                break
 +
            end
 +
            line = file.readline() -- Move file pointer
 +
        end
 +
        data = file.read() -- Read remaining file contents after code tag
 +
        file.close()
 +
        if data == nil then
 +
            return 1
 +
        else
 +
            file.open("download.lua", "w")
 +
            file.write(data)
 +
            file.close()
 +
        end
 +
    end)
 +
    conn:connect(port,ip)
 +
    conn:send("GET " .. url .. " HTTP/1.1\r\nHost: " .. ip .."\r\n".."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
 +
end
 +
</pre>
 +
 
 
==== Future plans ====
 
==== Future plans ====
  
 
*Send mac address of the wifi module in get so the web server can return device specific firmwares  
 
*Send mac address of the wifi module in get so the web server can return device specific firmwares  
*Put the script between specific tags so there won't be any problems with headers that are added by the web server + gives the possibility to add meta data (e.g. version of script or a signature)  
+
*<s>Put the script between specific tags so there won't be any problems with headers that are added by the web server</s> + gives the possibility to add meta data (e.g. version of script or a signature)  
 
*Create a nice php/mysql webapp to easily manage the scripts per module
 
*Create a nice php/mysql webapp to easily manage the scripts per module
 
  
 
== sample projects ==
 
== sample projects ==
Line 116: Line 152:
  
 
This arduino sketch provides a webserver-like interface and provides a json file (somewhat compatible with [[SpaceAPI]])
 
This arduino sketch provides a webserver-like interface and provides a json file (somewhat compatible with [[SpaceAPI]])
There is a version running in the space in a 'hot' zone, and accessible on [http://192.168.1.132 http://192.168.1.132]
+
This implementation has been replaced with the [[SpaceAPI#the_firmware|new firmware]] which pushes its data to a MySQL server.
  
You can find the code [[ESP8266-DS18S20|here]]
+
You can find the code of this old project [[ESP8266-DS18S20|here]]
  
 
=== SpaceAPI (Arduino) ===
 
=== SpaceAPI (Arduino) ===
Line 132: Line 168:
  
 
=== NeoPixels (WS2812 LEDs) ===
 
=== NeoPixels (WS2812 LEDs) ===
 +
See: [[NeoPixel]]
  
[https://github.com/adafruit/Adafruit_NeoPixel Adafruit's NeoPixel] library has support for ESP8266 since the end of June ('15), so it's easy to add your (strand of) WS2812 LED(s)
+
Location: [[Location::hACKspace]] (ESP-12E for sale)
The only downside is, that the 5V logic doesn't accept 3.3v signals by default; there are two ways to get around this:
+
[[Category:ESP8266]][[Category:Network]]
# under-volt your LEDs (tested with about 3.75v, and the input logic accepts 3.3v
 
# add a level converter.
 
: You will often see a mosfet ([http://electronics.stackexchange.com/questions/20362/how-is-this-circuit-for-level-converting-5v3-3v-working BSS138] or [http://www.hobbytronics.co.uk/mosfet-voltage-level-converter 2N7000], which work bidirectional) or TTL logic (like the 74LS245 octal bus transceiver, or just an AND or OR gate), but since we only need a 5v output connected to a ''high impedance'' input, we can [http://electronics.stackexchange.com/questions/82104/single-transistor-level-up-shifter use an NPN transistor] for this. (reference: [http://www.talkingelectronics.com/projects/TheTransistorAmplifier/TheTransistorAmplifier-P2.html#Level], [http://iot-playground.com/2-uncategorised/17-esp8266-wifi-module-and-5v-arduino-connection] and [http://electronics.stackexchange.com/questions/82104/single-transistor-level-up-shifter])
 
: Connect the:
 
:* collector of a bc547 via a 4k7 resistor to 5v
 
:* same collector to the DI of the WS2812B LED (ASCII LED: top VCC, bottom DO) ''':[ #]:''' (top DI, bottom, GND) (top view, the # is where the chip is)
 
:* base of the transistor via a 10k resistor to 3.3v
 
:* emitter to your 3v3 output
 

Latest revision as of 16:33, 22 April 2023

Project: ESP8266
Featured: No
State Completed
Members Prodigity, Da Syntax, xopr
GitHub No GitHub project defined. Add your project here.
Description Playing around with cheap wifi modules
Picture
SpaceState.png

programming the ESP8266

hardware: using an FTDI cable

NOTE: some people state that you can connect GPIO0 to DTR and REST to RTS, but the current settings in Arduino don't support this properly (might be a configuration thing) This would enable automatic program mode for the ESP

Make sure your FTDI cable can do 3.3V!

  • connect CH_PD and VCC to 3.3V (use an external power supply as most FTDI cables are not capable of supplying the required amouunt of current
  • connect GPIO0 (program) via a momentary switch to GND
  • connect REST (reset), if available via a momentary switch to GND (or do a power cycle if you want to start the bootloader mode
  • and GPIO15 (if available) via a 10k resistor all to GND
  • press both reset (or cut power) and program
  • release reset (or restore power)
  • release program
  • upload using your favorite software method
  • Computer1up found that the CH340 Serial to USB modules should work. CH340 modules on Aliexpess We should have some of these modules in the hACKspace, in the corner where the ESP8266 modules are found.
  • Xopr originally thought that these don't supply enough power to program an ESP-12S (or E?), but computer1up has successfully programmed one of these ESP's using the CH340.
  • Also, computer1up has only had luck with programming his ESP-12S when the RESET pin is connected while programming, instead of disconnecting it after boot.

software: using Arduino 1.6.4

Taken from https://github.com/esp8266/arduino

  • Install Arduino 1.6.4 from the Arduino website.
  • Start Arduino and open Perferences window.
  • Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
  • Open Boards Manager from Tools > Board menu and install ESP8266 platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).
  • For the ESP^2, the device name (under Linux) is /dev/ttyACM3
  • Change the programmer from AVRISP mkII to ArduinoISP
  • poweroff ESP (either unplug the USB or pull the device from the socket
  • set the switch to prog
  • poweron the ESP
  • You can now upload your sketch

NOTE: currently, programming the ESP-12E will result in corrupted firmware with all kinds of strange behaviour. This is caused by the esptool that comes with the Arduino ESP8266 package. (See this comment on april 28)

There are two ways to circumvent this:

  1. Manual upload: In Arduino, click file, preferences and check verbose [ ]upload
    • upload without putting the ESP in bootloader mode so you don't waste any (corrupt) write cycles
    • note the location of the .bin file and use the esptool method
  2. add a better working esptool.py in Arduino (which is what xopr did) by changing some files within the ESP8266 package
    • download esptool.py and put it in ~/.arduino15/packages/esp8266/hardware/esp8266/1.6.<BUILD_NUMBER>/tools/
    • edit ~/.arduino15/packages/esp8266/hardware/esp8266/1.6.<BUILD_NUMBER>/boards.txt and after
    • generic.menu.UploadTool.espota.upload.tool=espota
    • add
    generic.menu.UploadTool.esptoolpy=Serial (python)
    generic.menu.UploadTool.esptoolpy.upload.tool=esptoolpy
    • edit ~/.arduino15/packages/esp8266/hardware/esp8266/1.6.<BUILD_NUMBER>/platform.txt and at the bottom, add
    tools.esptoolpy.cmd=python
    tools.esptoolpy.cmd.windows=python.exe
    tools.esptoolpy.path={runtime.platform.path}/tools
    tools.esptoolpy.upload.protocol=esptoolpy
    tools.esptoolpy.upload.params.verbose=
    tools.esptoolpy.upload.params.quiet=
    tools.esptoolpy.upload.pattern="{cmd}" "{path}/esptool.py" --baud {upload.speed} --port "{serial.port}" write_flash 0x00000 "{build.path}/{build.project_name}"
    • Now, in Arduino, you have an extra menu option under 'Tools', 'Upload using', 'Serial (python)'

software: using Arduino IDE 1.8.2

  • Install the Arduino IDE from the Arduino website. Note: there's also a web editor, but this HAS NOT been tested if it works with the ESP boards yet.
  • Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
  • Open Boards Manager from Tools > Board menu and install ESP8266 platform (and don't forget to select your ESP8266 board from Tools > Board menu after installation).
  • Select your ESP8266 board from the board selector. The default settings are usually fine.
  • Note that this method is not tested with the ESP^2. This did work with an Arduino UNO R3 with its ATMega-chip removed and with the CH340 modules.
  • It seems that the strange behaviour of the esptool in the previous version of Arduino (or the board libraries) has been fixed.

software: using esptool.py to upload binaries

  • Make sure you have a binary (for example, download NodeMCU here or do a search for "site:http://bbs.nodemcu.com nodemcu firmware"
  • Download esptool.py (and make sure you have python on your system
  • Upload using something that says:
    • Linux
      sudo ./esptool.py --port /dev/ttyUSB0 write_flash 0x00000 ../nodemcu_float_0.9.6-dev_20150704.bin
    • Windows
      esptool.py --port COM1 write_flash 0x00000 C:\example\nodemcu_float_0.9.6-dev_20150704.bin
    • Mac
      GOOD LUCK, edit me if you succeeded

loading scripts from a webserver

I (Da Syntax) got tired from typing the scripts line for line into the lua console of the nodeMCU firmware. I wrote a little function to load the scripts from a webserver running on my laptop. This way I can just save the script on my laptop and load it to my ESP8266 calling 2 functions ( netload() and dofile("netloaded.lua") )

function netload()
	conn=net.createConnection(net.TCP, 0)
	conn:on("receive", function(conn, payload)
		print(payload)
		file.open("netloaded.lua", "w")
		file.write(payload)
		file.close()
	end)
	conn:connect(8080,"192.168.1.193")
	conn:send("GET /test.lua HTTP/1.1\r\nHost: www.example.com\r\n"        .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
end

Prodigity: I've modified Da Syntax's code so that it will now recognize <code> tags. There are still some restrictions though:

  • It does not recognize closing tags and therefore your code should be completely at the bottom of a http response
  • The code tag will only work if it's behind a newline and should end with a newline. (both \n and \r\n are fine)
function netload(ip, port, url) -- url is the remainder after the domain name
    conn=net.createConnection(net.TCP, 0)
    conn:on("receive", function(conn, payload)
        file.open("download.dat", "w")
        file.write(payload)
        file.close()
        file.open("download.dat", "r") -- Opens file twice, could be done nicer but does the job.
        line = file.readline()
        while line ~= nil do
            if line == "<code>\n" then
                break
            elseif line == "<code>\r\n" then
                break
            end
            line = file.readline() -- Move file pointer
        end
        data = file.read() -- Read remaining file contents after code tag
        file.close()
        if data == nil then
            return 1
        else
            file.open("download.lua", "w")
            file.write(data)
            file.close()
        end
    end)
    conn:connect(port,ip)
    conn:send("GET " .. url .. " HTTP/1.1\r\nHost: " .. ip .."\r\n".."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
end

Future plans

  • Send mac address of the wifi module in get so the web server can return device specific firmwares
  • Put the script between specific tags so there won't be any problems with headers that are added by the web server + gives the possibility to add meta data (e.g. version of script or a signature)
  • Create a nice php/mysql webapp to easily manage the scripts per module

sample projects

Here are some sample projects

Webserver serving JSON (Arduino)

This arduino sketch provides a webserver-like interface and provides a json file (somewhat compatible with SpaceAPI) This implementation has been replaced with the new firmware which pushes its data to a MySQL server.

You can find the code of this old project here

SpaceAPI (Arduino)

The new SpaceAPI is a combination of some PHP, MySQL, and some ESP modules updating the space state and sensor data. The ESP sketch sketch will have several features like space state switch, indicator, Dallas temperature and other sensors, which can be enabled compile-time.

SpaceState (Arduino)

This Arduino sketch is a web client and will update the SpaceState

You can find the code here

NeoPixels (WS2812 LEDs)

See: NeoPixel

Location: hACKspace (ESP-12E for sale)