ESP8266 page

From Eric

(Difference between revisions)
Jump to: navigation, search
m
m
Line 69: Line 69:
I have tried to use Esptool on Ubuntu without success (impossible to connect to the ESP). I guess this problem is related to my RS232 adapter, but I am not sure...
I have tried to use Esptool on Ubuntu without success (impossible to connect to the ESP). I guess this problem is related to my RS232 adapter, but I am not sure...
-
SO I ended up using Espressif flasher: it works... more or less. I have managed to upload the firmware at 115200 but I had to restart the process several times *** with power cycling in between *** before success.
+
So I finally switched to Espressif's flasher: it works... more or less. I have managed to upload the firmware at 115200 (and even faster) but I had to do the process several times *** with power cycling in between *** before success.
-
 
+
Line 79: Line 78:
The NodeMcu firmware can be found [here https://github.com/nodemcu/nodemcu-firmware].  
The NodeMcu firmware can be found [here https://github.com/nodemcu/nodemcu-firmware].  
 +
To download the firmware, do  
To download the firmware, do  
   wget https://github.com/nodemcu/nodemcu-firmware/raw/master/0.9.2/512k-flash/nodemcu_512k.bin
   wget https://github.com/nodemcu/nodemcu-firmware/raw/master/0.9.2/512k-flash/nodemcu_512k.bin
and then flash it to the module using
and then flash it to the module using
   python esptool.py --port /dev/tty.usbserial-ABC123456 write_flash 0x000000 nodemcu_512k.bin
   python esptool.py --port /dev/tty.usbserial-ABC123456 write_flash 0x000000 nodemcu_512k.bin
-
or using Espressif flasher (that's what I did).
+
or using Espressif's flasher (that's what I did).
As of april 2016, you can build your own customized NodeMCU firmware using [http://nodemcu-build.com/].  
As of april 2016, you can build your own customized NodeMCU firmware using [http://nodemcu-build.com/].  
Line 102: Line 102:
and to execute:
and to execute:
   nodemcu-uploader
   nodemcu-uploader
 +
 +
Another solution is to use [http://esp8266.ru/esplorer/|ESPlorer], a fancy IDE for NodeMCU. It allows to edit Lua scripts on your desktop, updload and execute them on the ESP with mouse clicks (for those who like ).
 +
 +
In practice, the Lua script is uploaded as a file to the ESP. Once the file is uploaded, the Lua "dofile" commands executes the script. If the file name is "init.lua", it is automatically executed at startup. 
=Using MQTT=
=Using MQTT=
 +
MQTT is a stripped down version of DDS. A typical configuration contains a broker, one or several clients which publish and subscribe to topics. There are several brokers available on the Internet, e.g., test.mosquitto.org.
The MQTT standard is available [http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html here]. Other information can be found on the dedicated web site [http://mqtt.org/ mqtt.org].
The MQTT standard is available [http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html here]. Other information can be found on the dedicated web site [http://mqtt.org/ mqtt.org].
Line 109: Line 114:
* [http://www.penninkhof.com/2015/05/linking-the-esp8266-a-raspberry-pi-through-mqtt/ Connecting a Raspberry and an ESP using MQTT]. Mosquitto is used as the MQTT server. On the ESP, the client is [https://github.com/tuanpmt/esp_mqtt esp_mqtt]. On an Arduino, you may use [https://projects.eclipse.org/projects/technology.paho/downloads Paho] (which also supports Windows, Linux, and FreeRtos].
* [http://www.penninkhof.com/2015/05/linking-the-esp8266-a-raspberry-pi-through-mqtt/ Connecting a Raspberry and an ESP using MQTT]. Mosquitto is used as the MQTT server. On the ESP, the client is [https://github.com/tuanpmt/esp_mqtt esp_mqtt]. On an Arduino, you may use [https://projects.eclipse.org/projects/technology.paho/downloads Paho] (which also supports Windows, Linux, and FreeRtos].
-
== Setting up the MQTT broker on the Raspberry Pi ==
+
== Setting up MQTT on the Raspberry Pi ==
 +
=== Using Mosquitto ===
Just type:
Just type:
   sudo apt-get install mosquitto mosquitto-clients python-mosquitto
   sudo apt-get install mosquitto mosquitto-clients python-mosquitto
Line 120: Line 126:
   mosquitto
   mosquitto
-
Then you can publish your own data:
+
Then you can publish your own data on the command line:
   mosquitto_pub -h localhost -t "eric" -m "10"
   mosquitto_pub -h localhost -t "eric" -m "10"
Line 131: Line 137:
When you publish a new value of topic "temp/random", it is displayed on a graphical gauge.
When you publish a new value of topic "temp/random", it is displayed on a graphical gauge.
-
== Setting up the MQTT client on windows ==
+
== Using Paho ==
-
I use [MQTT.fx].
+
Let's try to compile paho on the Raspberry:
 +
  git clone https://git.eclipse.org/r/paho/org.eclipse.paho.mqtt.embedded-c
 +
  cd org.eclipse.paho.mqtt.embedded-c/
 +
  make
-
== Compiling and installing the MQTT client on the ESP8266==
+
Well, it should work but it doesn't:
-
In a first attempt, I used the NodeMCU firmware which includes MQTT. I used [http://nodemcu-build.com/|this site] to build a version of the firmware with MQTT.
+
  mkdir -p build/output/samples
 +
  mkdir -p build/output/test
 +
  cc -o build/output/samples/pub0sub1 MQTTPacket/src/../samples/pub0sub1.c -lpaho-embed-mqtt3c -I MQTTPacket/src  -L build/output
 +
  /tmp/ccV9SzgN.o: In function `main':
 +
  pub0sub1.c:(.text+0x148): undefined reference to `transport_open'
 +
  pub0sub1.c:(.text+0x1cc): undefined reference to `transport_sendPacketBuffer'
 +
  pub0sub1.c:(.text+0x29c): undefined reference to `transport_sendPacketBuffer'
 +
  pub0sub1.c:(.text+0x414): undefined reference to `transport_sendPacketBuffer'
 +
  pub0sub1.c:(.text+0x458): undefined reference to `transport_sendPacketBuffer'
 +
  pub0sub1.c:(.text+0x474): undefined reference to `transport_close'
 +
  pub0sub1.c:(.text+0x4a8): undefined reference to `transport_getdata'
 +
  collect2: ld returned 1 exit status
 +
  Makefile:106: recipe for target 'build/output/samples/pub0sub1' failed
 +
  make: *** [build/output/samples/pub0sub1] Error 1
 +
 
 +
The makefile is erroneous and must be corrected as follows (see [https://bugs.eclipse.org/bugs/show_bug.cgi?id=472694 here]):
 +
  ${SYNC_SAMPLES}: ${blddir}/samples/%: ${srcdir}/../samples/%.c ${srcdir}/../samples/transport.o
 +
  ${CC} -o $@ $^ -l${MQTT_EMBED_LIB_C} ${FLAGS_EXE}
 +
 
 +
 
 +
And now it compiles smoothly. To run the samples, the LD_LIBRARY_PATH shall include "<wherever>/org.eclipse.paho.mqtt.embedded-c/build/output".
 +
 
 +
 
 +
=== With Python ===
-
== Compiling the MQTT client on the Raspberry Pi ==
+
I used paho-mqtt.
-
The simplest way uses [Paho] and Python:
+
To install:
   sudo pip install paho-mqtt
   sudo pip install paho-mqtt
Line 177: Line 209:
   client.loop_forever()
   client.loop_forever()
    
    
-
Let's try to compile paho on the Raspberry:
+
== Setting up the MQTT client on windows ==
-
  git clone https://git.eclipse.org/r/paho/org.eclipse.paho.mqtt.embedded-c
+
-
  cd org.eclipse.paho.mqtt.embedded-c/
+
-
  make
+
-
Well, it should work but it doesn't:
+
I use [http://mqttfx.jfx4ee.org/|MQTT.fx]. It is nice but heavy...
-
  mkdir -p build/output/samples
+
-
  mkdir -p build/output/test
+
-
  cc -o build/output/samples/pub0sub1 MQTTPacket/src/../samples/pub0sub1.c -lpaho-embed-mqtt3c -I MQTTPacket/src  -L build/output
+
-
  /tmp/ccV9SzgN.o: In function `main':
+
-
  pub0sub1.c:(.text+0x148): undefined reference to `transport_open'
+
-
  pub0sub1.c:(.text+0x1cc): undefined reference to `transport_sendPacketBuffer'
+
-
  pub0sub1.c:(.text+0x29c): undefined reference to `transport_sendPacketBuffer'
+
-
  pub0sub1.c:(.text+0x414): undefined reference to `transport_sendPacketBuffer'
+
-
  pub0sub1.c:(.text+0x458): undefined reference to `transport_sendPacketBuffer'
+
-
  pub0sub1.c:(.text+0x474): undefined reference to `transport_close'
+
-
  pub0sub1.c:(.text+0x4a8): undefined reference to `transport_getdata'
+
-
  collect2: ld returned 1 exit status
+
-
  Makefile:106: recipe for target 'build/output/samples/pub0sub1' failed
+
-
  make: *** [build/output/samples/pub0sub1] Error 1
+
-
The makefile is erroneous and must be corrected as follows (see [https://bugs.eclipse.org/bugs/show_bug.cgi?id=472694 here]):
 
-
  ${SYNC_SAMPLES}: ${blddir}/samples/%: ${srcdir}/../samples/%.c ${srcdir}/../samples/transport.o
 
-
  ${CC} -o $@ $^ -l${MQTT_EMBED_LIB_C} ${FLAGS_EXE}
 
-
And now it compiles smoothly. To run the samples, the LD_LIBRARY_PATH shall include "<wherever>/org.eclipse.paho.mqtt.embedded-c/build/output".
+
== Compiling and installing the MQTT client on the ESP8266==
 +
 
 +
=== Using NodeMCU ===
 +
In a first attempt, I used the NodeMCU firmware which comes with MQTT.
 +
Here is a typical script that publish and subcribes to the "/eric/test" topic. I use the mosquitto" test broker.
 +
 
 +
  wifi.sta.config("edvac","xxxxxxx")
 +
  wifi.sta.connect()
 +
 
 +
  print(wifi.sta.getip())
 +
 
 +
  m = mqtt.Client("my_id", 120)
 +
 
 +
  m:on("message", function(conn,topic,data)
 +
  print(topic .. ":")
 +
  if data ~= nil then
 +
  print(data)
 +
  end
 +
  end)
 +
 
 +
  m:connect("test.mosquitto.org",1883,0, function(conn)
 +
 
 +
  m:subscribe("/eric/#",0,function(conn)
 +
  print("Subscription OK")
 +
  end)
 +
 
 +
  m:publish("/eric/my_value", 1234,0,0,function(conn)
 +
  print("Topic published")
 +
  end)
 +
 
 +
  end)
 +
 
 +
The command '''dofile("script.lua")''' runs the script. If the connection succeeds, one should see the following display:
 +
  Subscription OK
 +
  Topic published
 +
  /eric/my_value
 +
  1234
 +
=== Using the bare ESP ===
 +
See [https://github.com/tuanpmt/esp_mqtt|esp_mqtt].
=Other sources of information=
=Other sources of information=

Revision as of 08:41, 1 May 2016

Contents

The chip

The ESP8266 is a SoC designed by Espressif.

Someone managed to decap the chip (well, a similar one) and show its internal layout (source here):

Esp8266 internals.jpg

The MCU is a Tensilica lx106 processor IP.

The board

I use the following board.

Esp8266 board.jpg


Connecting the ESP8266 board to a PC

The main things to know:

  • VCC is 3.3V (NOT 5V).
  • Input pins are NOT 5V tolerant.
  • The power supply shall deliver at least 500mA.
  • Pin CH_PD must be pulled-up to 3.3V
  • Pin RST must be pulled-up to 3.3V.

The ESP8266 board pin-out is the following:

Esp8266 pinout.jpg

WARNING: GPIO16 is actually RST on my board.

Using AT commands

Every command must end with a CR/LF sequence. On minicom, this can be obtained using CTRL-M followed by CTRL-J. The AT commands are fully described in Espressif manual "AT Instruction Set".


Reprogramming the ESP8266

The ESP8266 can be reprogrammed. To do so, you can either go the easy way using Arduino or the hard way installing a cross-compiler, a firmware uploading tool, etc.

Using the Arduino environment

(To be completed.)

Building and installing the cross-compiler and sdk

I have followed the indications given here. A lot of information is also provided by Espressif on their BBS). In particular, a lot of documents are available on this page.

On a fresh MINT installation, here are the commands I had to type to get a complete development environment:

 git clone --recursive https://github.com/pfalcon/esp-open-sdk.git
 cd esp-open-sdk/
 sudo apt-get install autoconf
 sudo apt-get install gperf
 sudo apt-get install texinfo
 sudo apt-get install libtool
 sudo apt-get install automake
 sudo apt-get install ncurses-dev
 sudo apt-get install g++
 sudo apt-get install expat
 make STANDALONE=y


A lot of very interesting/important information about programming the ESP8266 are given here.


Uploading the software to the chip

The chip can be placed in the flash programming mode by pulling down GPIO0 during reset (Note: on my board I need to keep GPIO0 low during all the flashing process).

A flash download tool is provided by Espressif here. Other tools:

I have tried to use Esptool on Ubuntu without success (impossible to connect to the ESP). I guess this problem is related to my RS232 adapter, but I am not sure... So I finally switched to Espressif's flasher: it works... more or less. I have managed to upload the firmware at 115200 (and even faster) but I had to do the process several times *** with power cycling in between *** before success.


Restoring the AT firmware

The AT firmware can be found [4] at Espressif.

Using NodeMcu

The NodeMcu firmware can be found [here https://github.com/nodemcu/nodemcu-firmware].

To download the firmware, do

 wget https://github.com/nodemcu/nodemcu-firmware/raw/master/0.9.2/512k-flash/nodemcu_512k.bin

and then flash it to the module using

 python esptool.py --port /dev/tty.usbserial-ABC123456 write_flash 0x000000 nodemcu_512k.bin

or using Espressif's flasher (that's what I did).

As of april 2016, you can build your own customized NodeMCU firmware using [5].

Using Lua to communicate through Wifi

See WiFi module.

 wifi.sta.config("myssid", "password");
 wifi.sta.connect();

Uploading LUA scripts to the node

Use [ https://github.com/4refr0nt/luatool|luatools], [6] or [7].

To install the latter (nodemcu-uploader), type

 pip install nodemcu-uploader

and to execute:

 nodemcu-uploader

Another solution is to use [8], a fancy IDE for NodeMCU. It allows to edit Lua scripts on your desktop, updload and execute them on the ESP with mouse clicks (for those who like ).

In practice, the Lua script is uploaded as a file to the ESP. Once the file is uploaded, the Lua "dofile" commands executes the script. If the file name is "init.lua", it is automatically executed at startup.

Using MQTT

MQTT is a stripped down version of DDS. A typical configuration contains a broker, one or several clients which publish and subscribe to topics. There are several brokers available on the Internet, e.g., test.mosquitto.org.

The MQTT standard is available here. Other information can be found on the dedicated web site mqtt.org.

Setting up MQTT on the Raspberry Pi

Using Mosquitto

Just type:

 sudo apt-get install mosquitto mosquitto-clients python-mosquitto

Then you can retrieve all topics from the mosquitto test broker:

 mosquitto_sub -h test.mosquitto.org -t "#" –v

To use you own broker, type

 mosquitto

Then you can publish your own data on the command line:

 mosquitto_pub -h localhost -t "eric" -m "10"

and see (in another terminal) that it has been correctly handled by the broker:

 mosquitto_sub -h localhost -t "#" -v

returns:

 eric 10

You can also use the following page: http://test.mosquitto.org/gauge/ When you publish a new value of topic "temp/random", it is displayed on a graphical gauge.

Using Paho

Let's try to compile paho on the Raspberry:

 git clone https://git.eclipse.org/r/paho/org.eclipse.paho.mqtt.embedded-c
 cd org.eclipse.paho.mqtt.embedded-c/
 make

Well, it should work but it doesn't:

 mkdir -p build/output/samples
 mkdir -p build/output/test
 cc -o build/output/samples/pub0sub1 MQTTPacket/src/../samples/pub0sub1.c -lpaho-embed-mqtt3c -I MQTTPacket/src  -L build/output
 /tmp/ccV9SzgN.o: In function `main':
 pub0sub1.c:(.text+0x148): undefined reference to `transport_open'
 pub0sub1.c:(.text+0x1cc): undefined reference to `transport_sendPacketBuffer'
 pub0sub1.c:(.text+0x29c): undefined reference to `transport_sendPacketBuffer'
 pub0sub1.c:(.text+0x414): undefined reference to `transport_sendPacketBuffer'
 pub0sub1.c:(.text+0x458): undefined reference to `transport_sendPacketBuffer'
 pub0sub1.c:(.text+0x474): undefined reference to `transport_close'
 pub0sub1.c:(.text+0x4a8): undefined reference to `transport_getdata'
 collect2: ld returned 1 exit status
 Makefile:106: recipe for target 'build/output/samples/pub0sub1' failed
 make: *** [build/output/samples/pub0sub1] Error 1

The makefile is erroneous and must be corrected as follows (see here):

 ${SYNC_SAMPLES}: ${blddir}/samples/%: ${srcdir}/../samples/%.c ${srcdir}/../samples/transport.o
 	${CC} -o $@ $^ -l${MQTT_EMBED_LIB_C} ${FLAGS_EXE}


And now it compiles smoothly. To run the samples, the LD_LIBRARY_PATH shall include "<wherever>/org.eclipse.paho.mqtt.embedded-c/build/output".


With Python

I used paho-mqtt. To install:

 sudo pip install paho-mqtt

The python code (taken from I-don't-remember-where) connect to IBM test server :

 host="messagesight.demos.ibm.com"
 port=1883
 
 def on_connect(pahoClient, obj, rc):
 # Once connected, publish message
 	print "Connected Code = %d"%(rc)
 	client.publish("eric/test", "Hello World", 0)
 
 
 def on_log(pahoClient, obj, level, string):
 	print string
 
 def on_publish(pahoClient, packet, mid):
 # Once published, disconnect
 	print "Published"
 	pahoClient.disconnect()
 
 def on_disconnect(pahoClient, obj, rc):
 	print "Disconnected"
 
 # Create a client instance
 client=paho.Client()
 
 # Register callbacks
 client.on_connect = on_connect
 client.on_log = on_log
 client.on_publish = on_publish
 client.on_disconnnect = on_disconnect
 
 #connect
 x = client.connect(host, port, 60)
 
 client.loop_forever()
 

Setting up the MQTT client on windows

I use [9]. It is nice but heavy...


Compiling and installing the MQTT client on the ESP8266

Using NodeMCU

In a first attempt, I used the NodeMCU firmware which comes with MQTT. Here is a typical script that publish and subcribes to the "/eric/test" topic. I use the mosquitto" test broker.

 wifi.sta.config("edvac","xxxxxxx")
 wifi.sta.connect()
 
 print(wifi.sta.getip())
 
 m = mqtt.Client("my_id", 120)
 
 m:on("message", function(conn,topic,data)
 	print(topic .. ":")
 	if data ~= nil then
 		print(data)
 	end
 end)
 
 m:connect("test.mosquitto.org",1883,0, function(conn)
 
 	m:subscribe("/eric/#",0,function(conn)
 		print("Subscription OK")
 	end)
 
 	m:publish("/eric/my_value", 1234,0,0,function(conn)
 		print("Topic published")
 	end)
 
 end)

The command dofile("script.lua") runs the script. If the connection succeeds, one should see the following display:

 Subscription OK
 Topic published
 /eric/my_value
 1234

Using the bare ESP

See [10].

Other sources of information

Personal tools