ESP8266 WIFI serial to emoncms link
For applications that only require basic posting of data from one emonTx to a remote server such as Emoncms.org an emonTx with this WiFi module provides a lower cost route than an emonBase or emonPi base-station installation.
#The core of EmonESP is also used for emonPixel and OpenEVSE ESP WiFi 2.0.
- EmonESP
- EmonESP User Guide
- ESP-12E module with 4M Flash
- Purchase a pre-loaded ESP8266
- To connect an ESP to emonTx see This User Guide Section
On first boot, ESP should broadcast a WiFI AP emonESP_XXX
. Connect to this AP and the captive portal should forward you to the log-in page. If this does not happen navigate to http://192.168.4.1
Note: You may need to disable mobile data if connecting via a Android 6 device
- Select your WiFi network from list of available networks
- Enter WiFi PSK key then click
Connect
- emonESP should now connect to local wifi network and return local IP address.
- Browse to local IP address by clicking the hyperlink (assuming your computer is on the same WiFi network) On future boots EmonESP will automatically connect to this network.
Note: on some networks it's possible to browse to the EmonESP using hostname http://emonesp or on windows http://emonesp.local
If re-connection fails (e.g. network cannot be found) the EmonESP will automatically revert back to WiFi AP mode after a short while to allow a new network to be re-configued if required. Re-connection to existing network will be attempted every 5min.
Holding the boot
button at startup (for about 10's) will force AP mode. This is useful when trying to connect the unit to a new WiFi network.
EmonESP can post data to emoncms.org or any other Emoncms server (e.g. emonPi) using Emoncms API.
In the Emoncms Server field, enter just the hostname or address without any path (e.g. emoncms.org), in the Emoncms Path field enter the path including the leading slash (e.g. /emoncms) or leave it empty if not required.
Data can be posted using HTTP or HTTPS. For HTTPS the Emoncms server must support HTTPS (emoncms.org does, emonPi does not). Due to the limited resources on the ESP the SSL SHA-1 fingerprint for the Emoncms server certificate must be manually entered and regularly updated.
Note: the emoncms.org fingerprint will change every 90 days when the SSL certificate is renewed.
To obtain the certificate fingerprint, you can use several methods, some examples:
- Chrome under Windows: click the secure icon next to the address bar and click on the certificate row to get the details, in the Details tab copy the hexadecimal digits from the box Thumbprint substituting spaces with colons and paying attention not to include any leading invisible character;
- Firefox under Linux: click the secure icon next to the address bar, Show connection details, More information, in the security tab click View Certificate and copy the SHA1 Fingerprint
- openssl under Linux: issue the following command substituting your host in place of www.example.com:
echo | openssl s_client -connect www.example.com:443 -servername www.example.com |& openssl x509 -fingerprint -sha1 -noout
EmonESP can post data to an MQTT server. Each data key:pair value will be published to a sub-topic of base topic.E.g data CT1:346
will results in 346
being published to <base-topic>/CT1
- Enter MQTT server host and base-topic
- (Optional) Enter server authentication details if required
- Click connect
- After a few seconds
Connected: No
should change toConnected: Yes
if connection is successful. Re-connection will be attempted every 10s.
Note: emon/xxxx
should be used as the base-topic if posting to emonPi MQTT server if you want the data to appear in emonPi Emoncms. See emonPi MQTT docs.
HTTP Authentication (highly recomended) can be enabled by saving admin config by default username and password
HTTP authentication is required for all HTTP requests including input API
Displays free system memory and firmware version
TBC
http://<IP-ADDRESS>/status
Example return in JSON:
{"mode":"STA","networks":[],"rssi":[],"ssid":"OpenEnergyMonitor","srssi":"-58","ipaddress":"10.0.1.93","emoncms_server":"emoncms.org","emoncms_node":"emonesp","emoncms_apikey":"xxxxxxxx","emoncms_connected":"0","packets_sent":"0","packets_success":"0","mqtt_server":"emonpi","mqtt_topic":"emonesp","mqtt_user":"emonpi","mqtt_pass":"xxxxxx","mqtt_connected":"0","free_heap":"25040"}
Data can be inputed to EmonESP via serial UART or HTTP API.
Data in serial:pairs string format can be inputed to EmonESP via serial UART (115200 baud) e.g:
ct1:3935,ct2:325,t1:12.5,t2:16.9,t3:11.2,t4:34.7
Data in string:pairs can be sent to EmonESP via HTTP API. This is useful to emulate the serial string data function while using the UART for code upload and debug. API example:
http://<IP-ADDRESS>/input?string=ct1:3935,ct2:325,t1:12.5,t2:16.9,t3:11.2,t4:34.7
http://<IP-ADDRESS>/saveemoncms?&server=emoncms.org&apikey=xxxxxxxxxxxxxxxxxx&node=emonesp&fingerprint=7D:82:15:BE:D7:BC:72:58:87:7D:8E:40:D4:80:BA:1A:9F:8B:8D:DA
SSL SHA-1 fingerprint is optional, HTTPS connection will be enabled if present
http://<IP-ADDRESS>/savemqtt?&server=emonpi&topic=emonesp&user=emonpi&pass=emonpimqtt2016
MQTT user and pass are optional, leave blank for connection to un-authenticated MQTT servers
EmonESP uses ESP8266 core, Arduino framework
Firmware can be compiled and uploaded either using (PlatfomIO,) Arduino IDE, or flashed with pre-compiles binaries found in Releases using esptool.py.
Note different flash sizes of ESP12 module (4Mb) verses Sonoff devices (1Mb).
Use our emonupload tool to download latest pre-compiled firmware release and upload to EmonESP: https://github.com/openenergymonitor/emonupload
Find and Install esptool, python required. Navigate to the Releases section of the github page and get the firmware.bin and spiffs.bin files. Use the command below to flash the ESP.
esptool.py --baud 460800 write_flash 0x0 ./firmware.bin 0x300000 ./spiffs.bin
or for sonoff devices:
esptool.py --baud 460800 write_flash 0x0 ./firmware.bin 0x7B000 ./spiffs.bin
The file paths in the line above are relative, the command is run from the directory where the files are contained, you might need to substitute them for absolute file paths. The 460800 baud can be too high for some programmers, 115200 is a failsafe option.
For more detailed ESP8266 Arduino core specific PlatfomIO notes see: https://github.com/esp8266/Arduino#using-platformio
The easiest way if running Linux is to install use the install script, this installs platformIO via python pip and installs pip if not present. See PlatformIO installation docs.
$ sudo python -c "$(curl -fsSL https://raw.githubusercontent.com/platformio/platformio/master/scripts/get-platformio.py)"
$ git clone https://github.com/openenergymonitor/EmonESP
$ cd EmonESP
$ pio run
The first time platformIO is run the espressif Arduino toolchain and all the required libs will be installed if required.
To compile EmonESP for sonoff s20 smartplugs, WIFI Relay and Heatpump Monitor specify the following environment:
$ pio run -e smartplug
$ pio run -e wifirelay
$ pio run -e hpmon
To compile for 4Mb esp12e modules use environment:
$ pio run -e esp12e
- Put ESP into bootloader mode for every upload.
- On Heatpump monitor use jumper to pull
GPIO0
low then reset then connect power (simulates reset) or pull RST pin low. - On other ESP boards (Adafruit HUZZAH) press and hold
GPIO0
button then press Reset, LED should light dimly to indicate bootloader mode.
- On Heatpump monitor use jumper to pull
$ pio run -t upload
To compile and upload EmonESP for sonoff s20 smartplugs, WIFI Relay and Heatpump Monitor specify the following environment:
$ pio run -e smartplug -t upload
$ pio run -e wifirelay -t upload
$ pio run -e hpmon -t upload
To compile for 4Mb esp12e modules use environment:
$ pio run -e esp12e -t upload
$ pio run -t uploadfs
To compile and upload the EmonESP file system for sonoff s20 smartplugs, WIFI Relay and Heatpump Monitor specify the following environment:
$ pio run -e smartplug -t uploadfs
$ pio run -e wifirelay -t uploadfs
$ pio run -e hpmon -t uploadfs
To compile for 4Mb esp12e modules use environment:
$ pio run -e esp12e -t uploadfs
See PlatfomrIO docs regarding SPIFFS uploading
When the firmware and spiffs are compiled both can be uploaded in a single command:
$ esptool.py write_flash 0x000000 .pioenvs/emonesp/firmware.bin 0x300000 .pioenvs/emonesp/spiffs.bin
or for 1Mb flash sonoff devices:
$ esptool.py write_flash 0x000000 .pioenvs/emonesp/firmware.bin 0x7B000 .pioenvs/emonesp/spiffs.bin
Note that OTA flashing works if using a 4Mb ESP module, it's not suitable for Sonoff devices with 1Mb flash, this is because the OTA flash requires free space on the device and emonESP hasn't been designed such that this is possible, yet.
$ pio run -e esp12e -t upload --upload-port <LOCAL-ESP-IP-ADDRESS>
$ pio run -e esp12e -t uploadfs --upload-port <LOCAL-ESP-IP-ADDRESS>
OTA uses port 8266. See PlatformIO ESP OTA docs:
The ESP subsystems have a lot of logging that can be enabled via setting various build options.
Using Platform IO the easiest way to configure these is via the PLATFORMIO_BUILD_FLAGS environment variable.
First select the serial port to output debug;
-DDEBUG_ESP_PORT=Serial
-DDEBUG_ESP_PORT=Serial1
Then add one or more of the debug options;
-DDEBUG_ESP_CORE
-DDEBUG_ESP_WIFI
-DDEBUG_ESP_HTTP_CLIENT
-DDEBUG_ESP_HTTP_SERVER
-DDEBUG_ESP_HTTP_UPDATE
-DDEBUG_ESP_UPDATER
-DDEBUG_ESP_OTA
-DDEBUG_ESP_SSL
-DDEBUG_TLS_MEM
For example from the Windows Power shell you may do something like;
$env:PLATFORMIO_BUILD_FLAGS="-DDEBUG_ESP_PORT=Serial1 -DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI"
pio run -t clean
pio run
pio run -t upload --upload-port 172.16.0.80
An overview of this process is:
- Install ESP boards using board manager.
- Install Arduino IDE plug-in for uploading spiffs files.
- Download the emonESP source files.
- Upload src.ino and Upload spiffs.
Install steps from: https://github.com/esp8266/Arduino
- Install Arduino IDE 1.6.8 or later from the Arduino website.
- Start Arduino and open ‘Preferences’ window from the menu bar.
- At the bottom of this window find the field “Additional Board Manager URLs” and enter ‘http://arduino.esp8266.com/stable/package_esp8266com_index.json`. You can add multiple URLs, separating them with commas.
- Open
Tools > Board > Board Manager
, scroll down and click on esp8266 platform, select the latest version then install. Now the Arduino IDE can communicate with ESP8266 based modules.
Install steps from: http://esp8266.github.io/Arduino/versions/2.0.0/doc/filesystem.html
- Download the plug-in (.zip) from (https://github.com/esp8266/arduino-esp8266fs-plugin/releases/).
- Open Arduino IDE ‘Preferences’ from the menu bar. Note ‘Sketchbook location:’ and navigate to there using your file explorer, then:
- Create folder ‘tools’ if it doesn’t exist, and within ’tools’ create folder ‘ESP8266FS’.
- The folder structure should be “…/Arduino/tools/ESP8266FS/
- Unpack the plug-in from the .zip here, in total it should look like “…/Arduino/tools/ESP8266FS/tool/esp8266fs.jar”.
- Restart Arduino IDE
$ git clone https://github.com/openenergymonitor/EmonESP
or click the green ‘Clone or download’ button at the top of this page.
- Open src/src.ino in the Arduino IDE from this cloned or downloaded repo.
- Select 'Tools > Board > Generic ESP8266 Module'.
- Select 'Tools > Flash Size: > 4M (1M SPIFFS)'
- Put ESP into bootloader mode/
- On Heatpump monitor use jumper to pull
GPIO0
low then reset then connect power (simulates reset) - On other ESP boards (Adafruit HUZZAH) press and hold
GPIO0
button then press Reset, LED should light dimly to indicate bootloader mode.
- On Heatpump monitor use jumper to pull
- Upload main sketch: Compile and Upload as normal using Arduino IDE [CTRL + u]
- Reset into bootloader mode again (optional if next step doesn't work consistently).
- Upload 'data' folder: Upload data folder (home.html web page etc) using
tools > ESP8266 Sketch Data Upload tool
. - If compiling fails because PubSubClient.h library (or any other libraries) cannot be found. Open the Library Manager again (Sketch > Include Library > Library Manager) and search for 'PubSubClient', install.
If you are experiencing ESP hanging in a reboot loop after upload it may be that the ESP flash has remnants of previous code (which may have the used the ESP memory in a different way). The ESP flash can be fully erased using esptool. With the unit in bootloder mode run:
$ esptool.py erase_flash
sudo
maybe be required
Output:
esptool.py v1.2-dev
Connecting...
Running Cesanta flasher stub...
Erasing flash (this may take a while)...
Erase took 8.0 seconds
- https://community.openenergymonitor.org/t/emonesp-firmware-development/1191/43
- https://community.openenergymonitor.org/tags/emonesp
GNU V3 General Public License