merge from origin

pull/73/head
susisstrolch 9 years ago
commit 65f22d5dcf
  1. 1
      .gitignore
  2. 27
      Makefile
  3. 243
      README.md
  4. 110
      avrflash
  5. 28
      esp-link/cgi.c
  6. 5
      esp-link/cgi.h
  7. 24
      esp-link/cgiflash.c
  8. 4
      esp-link/cgimqtt.c
  9. 1
      esp-link/cgimqtt.h
  10. 650
      esp-link/cgioptiboot.c
  11. 11
      esp-link/cgioptiboot.h
  12. 116
      esp-link/cgipins.c
  13. 196
      esp-link/cgiwifi.c
  14. 1
      esp-link/cgiwifi.h
  15. 26
      esp-link/config.c
  16. 4
      esp-link/config.h
  17. 66
      esp-link/log.c
  18. 7
      esp-link/log.h
  19. 67
      esp-link/main.c
  20. 44
      esp-link/stk500.h
  21. 90
      html/console.html
  22. 108
      html/console.js
  23. 149
      html/home.html
  24. 5
      html/log.html
  25. 197
      html/style.css
  26. 189
      html/ui.js
  27. 10
      html/wifi/wifi.html
  28. 9
      html/wifi/wifi.js
  29. 25
      httpd/httpd.c
  30. 1
      include/espmissingincludes.h
  31. 17
      serial/console.c
  32. 1
      serial/console.h
  33. 26
      serial/serbridge.c
  34. 3
      serial/serbridge.h
  35. 1
      serial/serled.c
  36. 24
      serial/uart.c
  37. 15
      serial/uart.h

1
.gitignore vendored

@ -17,3 +17,4 @@ espfs/mkespfsimage/mman-win32/libmman.a
.localhistory/ .localhistory/
tools/ tools/
local.conf local.conf
*.tgz

@ -117,12 +117,13 @@ YUI_COMPRESSOR ?= yuicompressor-2.4.8.jar
HTML_PATH = $(abspath ./html)/ HTML_PATH = $(abspath ./html)/
WIFI_PATH = $(HTML_PATH)wifi/ WIFI_PATH = $(HTML_PATH)wifi/
ESP_FLASH_MAX ?= 503808 # max bin file
ifeq ("$(FLASH_SIZE)","512KB") ifeq ("$(FLASH_SIZE)","512KB")
# Winbond 25Q40 512KB flash, typ for esp-01 thru esp-11 # Winbond 25Q40 512KB flash, typ for esp-01 thru esp-11
ESP_SPI_SIZE ?= 0 # 0->512KB (256KB+256KB) ESP_SPI_SIZE ?= 0 # 0->512KB (256KB+256KB)
ESP_FLASH_MODE ?= 0 # 0->QIO ESP_FLASH_MODE ?= 0 # 0->QIO
ESP_FLASH_FREQ_DIV ?= 0 # 0->40Mhz ESP_FLASH_FREQ_DIV ?= 0 # 0->40Mhz
ESP_FLASH_MAX ?= 241664 # max bin file for 512KB flash: 236KB
ET_FS ?= 4m # 4Mbit flash size in esptool flash command ET_FS ?= 4m # 4Mbit flash size in esptool flash command
ET_FF ?= 40m # 40Mhz flash speed in esptool flash command ET_FF ?= 40m # 40Mhz flash speed in esptool flash command
ET_BLANK ?= 0x7E000 # where to flash blank.bin to erase wireless settings ET_BLANK ?= 0x7E000 # where to flash blank.bin to erase wireless settings
@ -132,7 +133,6 @@ else ifeq ("$(FLASH_SIZE)","1MB")
ESP_SPI_SIZE ?= 2 # 2->1MB (512KB+512KB) ESP_SPI_SIZE ?= 2 # 2->1MB (512KB+512KB)
ESP_FLASH_MODE ?= 0 # 0->QIO ESP_FLASH_MODE ?= 0 # 0->QIO
ESP_FLASH_FREQ_DIV ?= 15 # 15->80MHz ESP_FLASH_FREQ_DIV ?= 15 # 15->80MHz
ESP_FLASH_MAX ?= 503808 # max bin file for 1MB flash: 492KB
ET_FS ?= 8m # 8Mbit flash size in esptool flash command ET_FS ?= 8m # 8Mbit flash size in esptool flash command
ET_FF ?= 80m # 80Mhz flash speed in esptool flash command ET_FF ?= 80m # 80Mhz flash speed in esptool flash command
ET_BLANK ?= 0xFE000 # where to flash blank.bin to erase wireless settings ET_BLANK ?= 0xFE000 # where to flash blank.bin to erase wireless settings
@ -145,8 +145,6 @@ else ifeq ("$(FLASH_SIZE)","2MB")
ESP_SPI_SIZE ?= 4 # 6->4MB (1MB+1MB) or 4->4MB (512KB+512KB) ESP_SPI_SIZE ?= 4 # 6->4MB (1MB+1MB) or 4->4MB (512KB+512KB)
ESP_FLASH_MODE ?= 0 # 0->QIO, 2->DIO ESP_FLASH_MODE ?= 0 # 0->QIO, 2->DIO
ESP_FLASH_FREQ_DIV ?= 15 # 15->80Mhz ESP_FLASH_FREQ_DIV ?= 15 # 15->80Mhz
ESP_FLASH_MAX ?= 503808 # max bin file for 512KB flash partition: 492KB
#ESP_FLASH_MAX ?= 1028096 # max bin file for 1MB flash partition: 1004KB
ET_FS ?= 16m # 16Mbit flash size in esptool flash command ET_FS ?= 16m # 16Mbit flash size in esptool flash command
ET_FF ?= 80m # 80Mhz flash speed in esptool flash command ET_FF ?= 80m # 80Mhz flash speed in esptool flash command
ET_BLANK ?= 0x1FE000 # where to flash blank.bin to erase wireless settings ET_BLANK ?= 0x1FE000 # where to flash blank.bin to erase wireless settings
@ -159,8 +157,6 @@ else
ESP_SPI_SIZE ?= 4 # 6->4MB (1MB+1MB) or 4->4MB (512KB+512KB) ESP_SPI_SIZE ?= 4 # 6->4MB (1MB+1MB) or 4->4MB (512KB+512KB)
ESP_FLASH_MODE ?= 0 # 0->QIO, 2->DIO ESP_FLASH_MODE ?= 0 # 0->QIO, 2->DIO
ESP_FLASH_FREQ_DIV ?= 15 # 15->80Mhz ESP_FLASH_FREQ_DIV ?= 15 # 15->80Mhz
ESP_FLASH_MAX ?= 503808 # max bin file for 512KB flash partition: 492KB
#ESP_FLASH_MAX ?= 1028096 # max bin file for 1MB flash partition: 1004KB
ET_FS ?= 32m # 32Mbit flash size in esptool flash command ET_FS ?= 32m # 32Mbit flash size in esptool flash command
ET_FF ?= 80m # 80Mhz flash speed in esptool flash command ET_FF ?= 80m # 80Mhz flash speed in esptool flash command
ET_BLANK ?= 0x3FE000 # where to flash blank.bin to erase wireless settings ET_BLANK ?= 0x3FE000 # where to flash blank.bin to erase wireless settings
@ -174,7 +170,7 @@ endif
# on the release tag, make release, upload esp-link.tgz into the release files # on the release tag, make release, upload esp-link.tgz into the release files
#VERSION ?= "esp-link custom version" #VERSION ?= "esp-link custom version"
DATE := $(shell date '+%F %T') DATE := $(shell date '+%F %T')
BRANCH := $(shell if git diff --quiet HEAD; then git describe --tags; \ BRANCH ?= $(shell if git diff --quiet HEAD; then git describe --tags; \
else git symbolic-ref --short HEAD; fi) else git symbolic-ref --short HEAD; fi)
SHA := $(shell if git diff --quiet HEAD; then git rev-parse --short HEAD | cut -d"/" -f 3; \ SHA := $(shell if git diff --quiet HEAD; then git rev-parse --short HEAD | cut -d"/" -f 3; \
else echo "development"; fi) else echo "development"; fi)
@ -394,7 +390,7 @@ ifeq ("$(COMPRESS_W_HTMLCOMPRESSOR)","yes")
$(WIFI_PATH)*.html $(WIFI_PATH)*.html
$(Q) echo "Compression assets with yui-compressor. This may take a while..." $(Q) echo "Compression assets with yui-compressor. This may take a while..."
$(Q) for file in `find html_compressed -type f -name "*.js"`; do \ $(Q) for file in `find html_compressed -type f -name "*.js"`; do \
java -jar tools/$(YUI_COMPRESSOR) $$file -o $$file; \ java -jar tools/$(YUI_COMPRESSOR) $$file --line-break 0 -o $$file; \
done done
$(Q) for file in `find html_compressed -type f -name "*.css"`; do \ $(Q) for file in `find html_compressed -type f -name "*.css"`; do \
java -jar tools/$(YUI_COMPRESSOR) $$file -o $$file; \ java -jar tools/$(YUI_COMPRESSOR) $$file -o $$file; \
@ -416,18 +412,6 @@ endif
# edit the loader script to add the espfs section to the end of irom with a 4 byte alignment. # edit the loader script to add the espfs section to the end of irom with a 4 byte alignment.
# we also adjust the sizes of the segments 'cause we need more irom0 # we also adjust the sizes of the segments 'cause we need more irom0
# in the end the only thing that matters wrt size is that the whole shebang fits into the
# 236KB available (in a 512KB flash)
ifeq ("$(FLASH_SIZE)","512KB")
build/eagle.esphttpd1.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.512.app1.ld
$(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \
-e '/^ irom0_0_seg/ s/2B000/38000/' \
$(SDK_LDDIR)/eagle.app.v6.new.512.app1.ld >$@
build/eagle.esphttpd2.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.512.app2.ld
$(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \
-e '/^ irom0_0_seg/ s/2B000/38000/' \
$(SDK_LDDIR)/eagle.app.v6.new.512.app2.ld >$@
else
build/eagle.esphttpd1.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.1024.app1.ld build/eagle.esphttpd1.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.1024.app1.ld
$(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \ $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \
-e '/^ irom0_0_seg/ s/6B000/7C000/' \ -e '/^ irom0_0_seg/ s/6B000/7C000/' \
@ -436,7 +420,6 @@ build/eagle.esphttpd2.v6.ld: $(SDK_LDDIR)/eagle.app.v6.new.1024.app2.ld
$(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \ $(Q) sed -e '/\.irom\.text/{' -e 'a . = ALIGN (4);' -e 'a *(.espfs)' -e '}' \
-e '/^ irom0_0_seg/ s/6B000/7C000/' \ -e '/^ irom0_0_seg/ s/6B000/7C000/' \
$(SDK_LDDIR)/eagle.app.v6.new.1024.app2.ld >$@ $(SDK_LDDIR)/eagle.app.v6.new.1024.app2.ld >$@
endif
espfs/mkespfsimage/mkespfsimage: espfs/mkespfsimage/ espfs/mkespfsimage/mkespfsimage: espfs/mkespfsimage/
$(Q) $(MAKE) -C espfs/mkespfsimage GZIP_COMPRESSION="$(GZIP_COMPRESSION)" $(Q) $(MAKE) -C espfs/mkespfsimage GZIP_COMPRESSION="$(GZIP_COMPRESSION)"
@ -446,7 +429,7 @@ release: all
$(Q) egrep -a 'esp-link [a-z0-9.]+ - 201' $(FW_BASE)/user1.bin | cut -b 1-80 $(Q) egrep -a 'esp-link [a-z0-9.]+ - 201' $(FW_BASE)/user1.bin | cut -b 1-80
$(Q) egrep -a 'esp-link [a-z0-9.]+ - 201' $(FW_BASE)/user2.bin | cut -b 1-80 $(Q) egrep -a 'esp-link [a-z0-9.]+ - 201' $(FW_BASE)/user2.bin | cut -b 1-80
$(Q) cp $(FW_BASE)/user1.bin $(FW_BASE)/user2.bin $(SDK_BASE)/bin/blank.bin \ $(Q) cp $(FW_BASE)/user1.bin $(FW_BASE)/user2.bin $(SDK_BASE)/bin/blank.bin \
"$(SDK_BASE)/bin/boot_v1.4(b1).bin" wiflash release/esp-link-$(BRANCH) "$(SDK_BASE)/bin/boot_v1.4(b1).bin" wiflash avrflash release/esp-link-$(BRANCH)
$(Q) tar zcf esp-link-$(BRANCH).tgz -C release esp-link-$(BRANCH) $(Q) tar zcf esp-link-$(BRANCH).tgz -C release esp-link-$(BRANCH)
$(Q) echo "Release file: esp-link-$(BRANCH).tgz" $(Q) echo "Release file: esp-link-$(BRANCH).tgz"
$(Q) rm -rf release $(Q) rm -rf release

@ -4,8 +4,9 @@ ESP-LINK
This firmware connects an attached micro-controller to the internet using a ESP8266 Wifi module. This firmware connects an attached micro-controller to the internet using a ESP8266 Wifi module.
It implements a number of features: It implements a number of features:
- transparent bridge between Wifi and serial, useful for debugging or inputting into a uC - transparent bridge between Wifi and serial, useful for debugging or inputting into a uC
- flash-programming attached Arduino/AVR microcontrollers as well as LPC800-series and other - flash-programming attached Arduino/AVR microcontrollers, esp8266 modules, as well as
ARM microcontrollers via Wifi LPC800-series and other ARM microcontrollers via Wifi
- built-in stk500v1 programmer for AVR uC's with optiboot: program using HTTP upload of hex file
- outbound TCP (and thus HTTP) connections from the attached micro-controller to the internet - outbound TCP (and thus HTTP) connections from the attached micro-controller to the internet
- outbound REST HTTP requests from the attached micro-controller to the internet, protocol - outbound REST HTTP requests from the attached micro-controller to the internet, protocol
based on espduino and compatible with [tuanpmt/espduino](https://github.com/tuanpmt/espduino) based on espduino and compatible with [tuanpmt/espduino](https://github.com/tuanpmt/espduino)
@ -17,11 +18,16 @@ Many thanks to https://github.com/brunnels for contributions around the espduino
###[Releases](https://github.com/jeelabs/esp-link/releases) ###[Releases](https://github.com/jeelabs/esp-link/releases)
- [V2.0.beta2](https://github.com/jeelabs/esp-link/releases/tag/v2.0.beta2) has REST support but - [V2.1.beta5](https://github.com/jeelabs/esp-link/releases/tag/v2.1.beta5) has the new built-in
requires a 1MByte or 4MByte ESP8266 flash, e.g. esp-12 or wroom-02 stk500v1 programmer and works on all modules (esp-01 through esp-12). This is still beta-ware!
- [V1.0.1](https://github.com/jeelabs/esp-link/releases/tag/v1.0.1) is _stable_ - [V2.0.rc1](https://github.com/jeelabs/esp-link/releases/tag/v2.0.rc1) has REST support but
requires a 1MByte or 4MByte ESP8266 flash, e.g. esp-12 or wroom-02. Despite being labeled
as release candidate this is a pretty stable release.
- [V1.0.4](https://github.com/jeelabs/esp-link/releases/tag/v1.0.4) is _stable_
and has the web server, transparent bridge, flash-programming support, but lacks and has the web server, transparent bridge, flash-programming support, but lacks
the REST and upcoming MQTT support. V1 works with 512KB flash, e.g. esp-1, esp-3, ... the REST and upcoming MQTT support. V1 works with 512KB flash, e.g. esp-01, esp-03, ...
Unless you've been using V1 and want to stay on it, the V1 series is really obsolete and
I recommend trying the latest V2 at this point.
For quick support and questions: For quick support and questions:
[![Chat at https://gitter.im/jeelabs/esp-link](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jeelabs/esp-link?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Chat at https://gitter.im/jeelabs/esp-link](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/jeelabs/esp-link?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
@ -45,7 +51,7 @@ registers a set of callbacks with esp-link that control sensors/actuators. This
commands in esp-link can receive MQTT messages, make simple callbacks into the uC to get sensor commands in esp-link can receive MQTT messages, make simple callbacks into the uC to get sensor
values or change actuators, and then respond back with MQTT. The way this is architected is that values or change actuators, and then respond back with MQTT. The way this is architected is that
the attached uC registers callbacks at start-up such that the code in the esp doesn't need to the attached uC registers callbacks at start-up such that the code in the esp doesn't need to
know which exact sensors/actuators the attached uC has, it learns thta through the initial know which exact sensors/actuators the attached uC has, it learns that through the initial
callback registration. callback registration.
Eye Candy Eye Candy
@ -60,47 +66,65 @@ attached microcontroller, and the pin assignments card:
Hardware info Hardware info
------------- -------------
This firmware is designed for esp8266 modules which have most ESP I/O pins available and This firmware is designed for any esp8266 module.
at least 1MB flash. (The V1 firmware supports modules with 512KB flash). The recommended connections for an esp-01 module are:
The default connections are: - URXD: connect to TX of microcontroller
- UTXD: connect to RX of microcontroller
- GPIO0: connect to RESET of microcontroller
- GPIO2: optionally connect green LED to 3.3V (indicates wifi status)
The recommended connections for an esp-12 module are:
- URXD: connect to TX of microcontroller - URXD: connect to TX of microcontroller
- UTXD: connect to RX of microcontroller - UTXD: connect to RX of microcontroller
- GPIO12: connect to RESET of microcontroller - GPIO12: connect to RESET of microcontroller
- GPIO13: connect to ISP of LPC/ARM microcontroller (not used with Arduino/AVR) - GPIO13: connect to ISP of LPC/ARM microcontroller or to GPIO0 of esp8266 being programmed
(not used with Arduino/AVR)
- GPIO0: optionally connect green "conn" LED to 3.3V (indicates wifi status) - GPIO0: optionally connect green "conn" LED to 3.3V (indicates wifi status)
- GPIO2: optionally connect yellow "ser" LED to 3.3V (indicates serial activity) - GPIO2: optionally connect yellow "ser" LED to 3.3V (indicates serial activity)
If you are using an FTDI connector, GPIO12 goes to DTR and GPIO13 goes to CTS. If your application has problems with the boot message that is output at ~74600 baud by the ROM
at boot time you can connect an esp-12 module as follows and choose the "swap_uart" pin assignment
in the esp-link web interface:
- GPIO13: connect to TX of microcontroller
- GPIO15: connect to RX of microcontroller
- GPIO1/UTXD: connect to RESET of microcontroller
- GPIO3/URXD: connect to ISP of LPC/ARM microcontroller or to GPIO0 of esp8266 being programmed
(not used with Arduino/AVR)
- GPIO0: optionally connect green "conn" LED to 3.3V (indicates wifi status)
- GPIO2: optionally connect yellow "ser" LED to 3.3V (indicates serial activity)
If you are using an esp-12 module, you can avoid the initial boot message from the esp8266 If you are using an FTDI connector, GPIO12 goes to DTR and GPIO13 goes to CTS (or vice-versa, I've
bootloader by using the swap-pins option. This swaps the esp8266 TX/RX to gpio15/gpio13 respectively. seen both used, sigh).
The GPIO pin assignments can be changed dynamically in the web UI and are saved in flash. The GPIO pin assignments can be changed dynamically in the web UI and are saved in flash.
Initial flashing Initial flashing
---------------- ----------------
(This is not necessary if you receive one of the jn-esp or esp-bridge modules from the author!) If you want to simply flash a pre-built firmware binary, you can download the latest
If you want to simply flash the provided firmware binary, you can download the latest
[release](https://github.com/jeelabs/esp-link/releases) and use your favorite [release](https://github.com/jeelabs/esp-link/releases) and use your favorite
ESP8266 flashing tool to flash the bootloader, the firmware, and blank settings. ESP8266 flashing tool to flash the bootloader, the firmware, and blank settings.
Detailed instructions are provided in the release notes. Detailed instructions are provided in the release notes.
Note that the firmware assumes a 512KB flash chip, which most of the esp-01 thru esp-11 _Important_: the firmware adapts automatically to the size of the flash chip using information
modules appear to have. A larger flash chip should work but has not been tested. stored in the boot sector (address 0). This is the standard way that the esp8266 SDK detects
the flash size. What this means is that you need to set this properly when you flash the bootloader.
If you use esptool.py you can do it using the -ff and -fs options.
Wifi configuration overview Wifi configuration overview
------------------ ------------------
For proper operation the end state the esp-link needs to arrive at is to have it For proper operation the end state that esp-link needs to arrive at is to have it
join your pre-existing wifi network as a pure station. join your pre-existing wifi network as a pure station.
However, in order to get there the esp-link will start out as an access point and you'll have However, in order to get there esp-link will start out as an access point and you'll have
to join its network to configure it. The short version is: to join its network to configure it. The short version is:
1. the esp-link creates a wifi access point with an SSID of the form `ESP_012ABC` 1. esp-link creates a wifi access point with an SSID of the form `ESP_012ABC` (some modules
2. you join your laptop or phone to the esp-link's network as a station and you configure use a different SSID form, such as `ai-thinker-012ABC`)
the esp-link wifi with your network info by pointing your browser at http://192.168.4.1/ 2. you join your laptop or phone to esp-link's network as a station and you configure
3. the esp-link starts to connect to your network while continuing to also be an access point esp-link wifi with your network info by pointing your browser at http://192.168.4.1/
("AP+STA"), the esp-link may show up with a `esp-link.local` hostname 3. you set a hostname for esp-link on the "home" page, or leave the default ("esp-link")
4. esp-link starts to connect to your network while continuing to also be an access point
("AP+STA"), the esp-link may show up with a `${hostname}.local` hostname
(depends on your DHCP/DNS config) (depends on your DHCP/DNS config)
4. the esp-link succeeds in connecting and shuts down its own access point after 15 seconds, 4. esp-link succeeds in connecting and shuts down its own access point after 15 seconds,
you reconnect your laptop/phone to your normal network and access esp-link via its hostname you reconnect your laptop/phone to your normal network and access esp-link via its hostname
or IP address or IP address
@ -113,7 +137,7 @@ status as follows:
- Very short flash once every two seconds: not connected to a network and running as AP-only - Very short flash once every two seconds: not connected to a network and running as AP-only
- Even on/off at 1HZ: connected to the configured network but no IP address (waiting on DHCP) - Even on/off at 1HZ: connected to the configured network but no IP address (waiting on DHCP)
- Steady on with very short off every 3 seconds: connected to the configured network with an - Steady on with very short off every 3 seconds: connected to the configured network with an
IP address (esp-link shuts down its AP after 15 seconds) IP address (esp-link shuts down its AP after 60 seconds)
The yellow "ser" LED will blink briefly every time serial data is sent or received by the esp-link. The yellow "ser" LED will blink briefly every time serial data is sent or received by the esp-link.
@ -152,11 +176,36 @@ used to configure the ssid/password info. The problem is that the initial STA+AP
channel 1 and you configure it to connect to an AP on channel 6. This requires the ESP8266's AP channel 1 and you configure it to connect to an AP on channel 6. This requires the ESP8266's AP
to also switch to channel 6 disconnecting you in the meantime. to also switch to channel 6 disconnecting you in the meantime.
Hostname, description, DHCP, mDNS
---------------------------------
You can set a hostname on the "home" page, this should be just the hostname and not a domain
name, i.e., something like "test-module-1" and not "test-module-1.mydomain.com".
This has a number of effects:
- you will see the first 12 chars of the hostname in the menu bar (top left of the page) so
if you have multiple modules you can distinguish them visually
- esp-link will use the hostname in its DHCP request, which allows you to identify the module's
MAC and IP addresses in your DHCP server (typ. your wifi router). In addition, some DHCP
servers will inject these names into the local DNS cache so you can use URLs like
`hostname.local`.
- someday, esp-link will inject the hostname into mDNS (multicast DNS, bonjour, etc...) so
URLs of the form `hostname.local` work for everyone (as of v2.1.beta5 mDNS is disabled due
to reliability issues with it)
You can also enter a description of up to 128 characters on the home page (bottom right). This
allows you to leave a memo for yourself, such as "installed in basement to control the heating
system". This descritpion is not used anywhere else.
Troubleshooting Troubleshooting
--------------- ---------------
- verify that you have sufficient power, borderline power can cause the esp module to seemingly - verify that you have sufficient power, borderline power can cause the esp module to seemingly
function until it tries to transmit and the power rail collapses function until it tries to transmit and the power rail collapses
- check the "conn" LED to see which mode esp-link is in (see LED info above) - if you just cannot flash your esp8266 module (some people call it the zombie mode) make sure you
have gpio0 and gpio15 pulled to gnd with a 1K resistor, gpio2 tied to 3.3V with 1K resistor, and
RX/TX connected without anything in series. If you need to level shift the signal going into the
esp8266's RX use a 1K resistor. Use 115200 baud in the flasher.
(For a permanent set-up I would use higher resistor values but
when nothing seems to work these are the ones I try.)
- if the flashing succeeded, check the "conn" LED to see which mode esp-link is in (see LED info above)
- reset or power-cycle the esp-link to force it to become an access-point if it can't - reset or power-cycle the esp-link to force it to become an access-point if it can't
connect to your network within 15-20 seconds connect to your network within 15-20 seconds
- if the LED says that esp-link is on your network but you can't get to it, make sure your - if the LED says that esp-link is on your network but you can't get to it, make sure your
@ -188,7 +237,15 @@ A few notes from others (I can't fully verify these):
- Make sure the paths at the beginning of the makefile are correct - Make sure the paths at the beginning of the makefile are correct
- Make sure `esp-open-sdk/xtensa-lx106-elf/bin` is in the PATH set in the Makefile - Make sure `esp-open-sdk/xtensa-lx106-elf/bin` is in the PATH set in the Makefile
Flashing the firmware It is possible to build esp-link on Windows, but it requires a gaggle of software to be installed:
- Install the unofficial sdk, mingw, SourceTree (gui git client), python 2.7, git cli, Java
- Use SourceTree to checkout under C:\espressif or wherever you installed the unofficial sdk,
(see this thread for the unofficial sdk http://www.esp8266.com/viewtopic.php?t=820)
- Create a symbolic link under c:/espressif for the git bin directory under program files and
the java bin directory under program files.
- ...
Updating the firmware over-the-air
--------------------- ---------------------
This firmware supports over-the-air (OTA) flashing, so you do not have to deal with serial This firmware supports over-the-air (OTA) flashing, so you do not have to deal with serial
flashing again after the initial one! The recommended way to flash is to use `make wiflash` flashing again after the initial one! The recommended way to flash is to use `make wiflash`
@ -198,10 +255,11 @@ If you are downloading firmware binaries use `./wiflash`.
You can easily do that using something like `ESP_HOSTNAME=192.168.1.5 make wiflash`. You can easily do that using something like `ESP_HOSTNAME=192.168.1.5 make wiflash`.
The flashing, restart, and re-associating with your wireless network takes about 15 seconds The flashing, restart, and re-associating with your wireless network takes about 15 seconds
and is fully automatic. The 512KB flash are divided into two 236KB partitions allowing for new and is fully automatic. The first 1MB of flash are divided into two 512KB partitions allowing for new
code to be uploaded into one partition while running from the other. This is the official code to be uploaded into one partition while running from the other. This is the official
OTA upgrade method supported by the SDK, except that the firmware is POSTed to the module OTA upgrade method supported by the SDK, except that the firmware is POSTed to the module
using curl as opposed to having the module download it from a cloud server. using curl as opposed to having the module download it from a cloud server. On a module with
512KB flash there is only space for one partition and thus no way to do an OTA update.
If you are downloading the binary versions of the firmware (links forthcoming) you need to have If you are downloading the binary versions of the firmware (links forthcoming) you need to have
both `user1.bin` and `user2.bin` handy and run `wiflash.sh <esp-hostname> user1.bin user2.bin`. both `user1.bin` and `user2.bin` handy and run `wiflash.sh <esp-hostname> user1.bin user2.bin`.
@ -220,12 +278,80 @@ Serial bridge and connections to Arduino, AVR, ARM, LPC microcontrollers
In order to connect through the esp-link to a microcontroller use port 23. For example, In order to connect through the esp-link to a microcontroller use port 23. For example,
on linux you can use `nc esp-hostname 23` or `telnet esp-hostname 23`. on linux you can use `nc esp-hostname 23` or `telnet esp-hostname 23`.
You can reprogram an Arduino / AVR microcontroller by pointing avrdude at port 23. Instead of Note that multiple connections to port 23 and 2323 can be made simultaneously. Esp-link will
specifying a serial port of the form /dev/ttyUSB0 use `net:esp-link:23` with avrdude's -P option intermix characters received on all these connections onto the serial TX and it will
(where `esp-link` is either the hostname of your esp-link or its IP address). broadcast incoming characters from the serial RX to all connections. Use with caution!
The esp-link detects that avrdude starts its connection with a flash synchronization sequence
### Flashing an attached AVR/Arduino
There are three options for reprogramming an attached AVR/Arduino microcontroller:
- Use avrdude and point it at port 23 of esp-link. Esp-link automatically detects the programming
sequence and issues a reset to the AVR.
- Use avrdude and point it at port 2323 of esp-link. This is the same as port 23 except that the
autodectection is not used and the reset happens because port 2323 is used
- Use curl or a similar tool to HTTP POST the firmware to esp-link. This uses the built-in
programmer, which only works for AVRs/Arduinos with the optiboot bootloader (which is std).
To reprogram an Arduino / AVR microcontroller by pointing avrdude at port 23 or 2323 you
specify a serial port of the form `net:esp-link:23` in avrdude's -P option, where
`esp-link` is either the hostname of your esp-link or its IP address).
This is instead of specifying a serial port of the form /dev/ttyUSB0.
Esp-link detects that avrdude starts its connection with a flash synchronization sequence
and sends a reset to the AVR microcontroller so it can switch into flash programming mode. and sends a reset to the AVR microcontroller so it can switch into flash programming mode.
To reprogram using the HTTP POST method you need to first issue a POST to put optiboot into
programming mode: POST to `http://esp-link/pgm/sync`, this starts the process. Then check that
synchronization with optiboot has been achieved by issuing a GET to the same URL
(`http://esp-link/pgm/sync`). Repeat until you have sync (takes <500ms normally). Finally
issue a POST request to `http://esp-link/pgm/upload` with your hex file as POST data (raw,
not url-encoded or multipart-mime. Please look into the avrflash script for the curl command-line
details or use that script directly (`./avrflash esp-link.local my_sketch.hex`).
_Important_: after the initial sync request that resets the AVR you have 10 seconds to get to the
upload post or esp-link will time-out. So if you're manually entering curl commands have them
prepared so you can copy&paste!
Beware of the baud rate, which you can set on the uC Console page. Sometimes you may be using
115200 baud in sketches but the bootloader may use 57600 baud. When you use port 23 or 2323 you
need to set the baud rate correctly. If you use the built-in programmer (HTTP POST method) then
esp-link will try the configured baud rate and also 9600, 57600, and 115200 baud, so it should
work even if you have the wrong baud rate configured...
When to use which method? If port 23 works then go with that. If you have trouble getting sync
or it craps out in the middle too often then try the built-in programmer with the HTTP POST.
If your AVR doesn't use optiboot then use port 2323 since esp-link may not recognize the programming
sequence and not issue a reset if you use port 23.
If you are having trouble with the built-in programmer and see something like this:
```
# ./avrflash 192.168.3.104 blink.hex
Error checking sync: FAILED to SYNC: abandoned after timeout, got:
:\xF/\x00\xCj\xCz\xCJ\xCZ\xC\xAÜ\xC\xAä\xC\xAÜ\xC\xAä\xC\xBì\xC\xBô\xC\xBì\xC\xBô\xC\xAÜ\xC\xAä\xC
```
the most likely cause is a baud rate mismatch and/or a bad connection from the esp8266 to the
AVRs reset line.
The baud rate used by esp-link is set on the uC Console web page and, as mentioned above, it will
automatically try 9600, 57600, and 115200 as well.
The above garbage characters are most likely due to optiboot timing out and starting the sketch
and then the sketch sending data at a different baud rate than configured into esp-link.
Note that sketches don't necessarily use the same baud rate as optiboot, so you may have the
correct baud rate configured but reset isn't functioning, or reset may be functioning but the
baud rate may be incorrect.
The output of a successful flash using the built-in programmer looks like this:
```
Success. 3098 bytes at 57600 baud in 0.8s, 3674B/s 63% efficient
```
This says that the sketch comprises 3098 bytes of flash, was written in 0.8 seconds
(excludes the initial sync time) at 57600 baud,
and the 3098 bytes were flashed at a rate of 3674 bytes per second.
The efficiency measure is the ratio of the actual rate to the serial baud rate,
thus 3674/5760 = 0.63 (there are 10 baud per character).
The efficiency is not 100% because there is protocol overhead (such as sync, record type, and
length characters)
and there is dead time waiting for an ack or preparing the next record to be sent.
### Flashing an attached ARM processor
You can reprogram NXP's LPC800-series and many other ARM processors as well by pointing your You can reprogram NXP's LPC800-series and many other ARM processors as well by pointing your
programmer similarly at the esp-link's port 23. For example, if you are using programmer similarly at the esp-link's port 23. For example, if you are using
https://github.com/jeelabs/embello/tree/master/tools/uploader a command line like https://github.com/jeelabs/embello/tree/master/tools/uploader a command line like
@ -235,9 +361,25 @@ make esp-link issue the appropriate "ISP" and reset sequence to the microcontrol
flash programming. If you use a different ARM programming tool it will work as well as long as flash programming. If you use a different ARM programming tool it will work as well as long as
it starts the connection with the `?\r\n` synchronization sequence. it starts the connection with the `?\r\n` synchronization sequence.
Note that multiple connections to port 23 can be made simultaneously. The esp-link will ### Flashing an attached esp8266
intermix characters received on all these connections onto the serial TX and it will
broadcast incoming characters from the serial RX to all connections. Use with caution! (This is not well tested, more details forthcoming...)
Yes, you can use esp-link running on one esp8266 module to flash another esp8266 module!
The basic idea is to use some method to direct the esp8266 flash program to port 2323 of
esp-link. Using port 2323 with the appropriate wiring will cause the esp8266's reset and
gpio0 pins to be toggled such that the chip enters the flash programming mode.
One option for connecting the programmer with esp-link is to use my version of esptool.py
at http://github.com/tve/esptool, which supports specifying a URL instead of a port. Thus
instead of specifying something like `--port /dev/ttyUSB0` or `--port COM1` you specify
`--port socket://esp-link.local:2323`. Important: the baud rate specified on the esptool.py
command-line is irrelevant as the baud rate used by esp-link will be the one set in the
uC console page. Fortunately the esp8266 bootloader does auto-baud detection. (Setting the
baud rate to 115200 is recommended.)
Another option is to use a serial-to-tcp port forwarding driver and point that to port 2323
of esp-link. On windows users have reported success with
[HW Virtual Serial Port](http://www.hw-group.com/products/hw_vsp/hw_vsp2_en.html)
Debug log Debug log
--------- ---------
@ -245,35 +387,32 @@ The esp-link web UI can display the esp-link debug log (os_printf statements in
is handy but sometimes not sufficient. Esp-link also prints the debug info to the UART where is handy but sometimes not sufficient. Esp-link also prints the debug info to the UART where
it is sometimes more convenient and sometimes less... For this reason three UART debug log it is sometimes more convenient and sometimes less... For this reason three UART debug log
modes are supported that can be set in the web UI (and the mode is saved in flash): modes are supported that can be set in the web UI (and the mode is saved in flash):
- auto: the UART log starts enabled at boot and disables itself when esp-link associates with - auto: the UART log starts enabled at boot using uart0 and disables itself when esp-link
an AP. It re-enables itself if the association is lost. associates with an AP. It re-enables itself if the association is lost.
- off: the UART log is always off - off: the UART log is always off
- on: the UART log is always on - on0: the UART log is always on using uart0
- on1: the UART log is always on using uart1 (gpio2 pin)
Note that even if the UART log is always off the bootloader prints to uart0 whenever the Note that even if the UART log is always off the ROM prints to uart0 whenever the
esp8266 comes out of reset. This cannot be disabled. esp8266 comes out of reset. This cannot be disabled.
Outbound TCP connections Outbound HTTP REST requests and MQTT client
------------------------ -------------------------------------------
The attached micro-controller can open outbound TCP connections using a simple
[serial protocol](https://gist.github.com/tve/a46c44bf1f6b42bc572e).
More info and sample code forthcoming...
Outbound HTTP REST requests
---------------------------
The V2 versions of esp-link support the espduino SLIP protocol that supports simple outbound The V2 versions of esp-link support the espduino SLIP protocol that supports simple outbound
HTTP REST requests. The SLIP protocol consists of commands with binary arguments sent from the HTTP REST requests as well as an MQTT client. The SLIP protocol consists of commands with
binary arguments sent from the
attached microcontroller to the esp8266, which then performs the command and responds back. attached microcontroller to the esp8266, which then performs the command and responds back.
The responses back use a callback address in the attached microcontroller code, i.e., the The responses back use a callback address in the attached microcontroller code, i.e., the
command sent by the uC contains a callback address and the response from the esp8266 starts command sent by the uC contains a callback address and the response from the esp8266 starts
with that callback address. This enables asynchronous communication where esp-link can notify the with that callback address. This enables asynchronous communication where esp-link can notify the
uC when requests complete or when other actions happen, such as wifi connectivity status changes. uC when requests complete or when other actions happen, such as wifi connectivity status changes.
Support for MQTT is forthcoming.
You can find a demo sketch in a fork of the espduino library at You can find a demo sketch in a fork of the espduino library at
https://github.com/tve/espduino in the https://github.com/tve/espduino in the
[examples/demo folder](https://github.com/tve/espduino/tree/master/espduino/examples/demo). [examples/demo folder](https://github.com/tve/espduino/tree/master/espduino/examples/demo).
More docs forthcoming...
Contact Contact
------- -------
If you find problems with esp-link, please create a github issue. If you have a question, please If you find problems with esp-link, please create a github issue. If you have a question, please

@ -0,0 +1,110 @@
#! /bin/bash
#
# Flash an AVR with optiboot using the esp-link built-in programmer
# Basically we first reset the AVR and get in sync, and then send the hex file
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# Thorsten von Eicken wrote this file. As long as you retain
# this notice you can do whatever you want with this stuff. If we meet some day,
# and you think this stuff is worth it, you can buy me a beer in return.
# ----------------------------------------------------------------------------
show_help() {
cat <<EOT
Usage: ${0##*/} [-options...] hostname sketch.hex
Flash the AVR running optiboot attached to esp-link with the sketch.
-v Be verbose
-h show this help
Example: ${0##*/} -v esp-link mysketch.hex
${0##*/} 192.168.4.1 mysketch.hex
EOT
}
if ! which curl >/dev/null; then
echo "ERROR: Cannot find curl: it is required for this script." >&2
exit 1
fi
start=`date +%s`
# ===== Parse arguments
verbose=
while getopts "hvx:" opt; do
case "$opt" in
h) show_help; exit 0 ;;
v) verbose=1 ;;
x) foo="$OPTARG" ;;
'?') show_help >&2; exit 1 ;;
esac
done
# Shift off the options and optional --.
shift "$((OPTIND-1))"
# Get the fixed arguments
if [[ $# != 2 ]]; then
show_help >&2
exit 1
fi
hostname=$1
hex=$2
re='[-A-Za-z0-9.]+'
if [[ ! "$hostname" =~ $re ]]; then
echo "ERROR: hostname ${hostname} is not a valid hostname or ip address" >&2
exit 1
fi
if [[ ! -r "$hex" ]]; then
echo "ERROR: cannot read hex file ($hex)" >&2
exit 1
fi
# ===== Get AVR in sync
[[ -n "$verbose" ]] && echo "Resetting AVR with http://$hostname/pgm/sync" >&2
v=; [[ -n "$verbose" ]] && v=-v
sync=`curl -m 10 $v -s -w '%{http_code}' -XPOST "http://$hostname/pgm/sync"`
if [[ $? != 0 || "$sync" != 204 ]]; then
echo "Error resetting AVR" >&2
exit 1
fi
while true; do
sync=`curl -m 10 $v -s "http://$hostname/pgm/sync"`
if [[ $? != 0 ]]; then
echo "Error checking sync" >&2
exit 1
fi
case "$sync" in
SYNC*)
echo "AVR in $sync" >&2
break;;
"NOT READY"*)
[[ -n "$verbose" ]] && echo " Waiting for sync..." >&2
;;
*)
echo "Error checking sync: $sync" >&2
exit 1
;;
esac
sleep 0.1
done
# ===== Send HEX file
[[ -n "$verbose" ]] && echo "Sending HEX file for programming" >&2
sync=`curl -m 10 $v -s -g -d "@$hex" "http://$hostname/pgm/upload"`
echo $sync
if [[ $? != 0 || ! "$sync" =~ ^Success ]]; then
echo "Error programming AVR" >&2
exit 1
fi
sec=$(( `date +%s` - $start ))
echo "Success, took $sec seconds" >&2
exit 0

@ -16,8 +16,10 @@ Some random cgi routines.
#include <esp8266.h> #include <esp8266.h>
#include "cgi.h" #include "cgi.h"
#include "config.h"
void noCacheHeaders(HttpdConnData *connData, int code) { void ICACHE_FLASH_ATTR
noCacheHeaders(HttpdConnData *connData, int code) {
httpdStartResponse(connData, code); httpdStartResponse(connData, code);
httpdHeader(connData, "Cache-Control", "no-cache, no-store, must-revalidate"); httpdHeader(connData, "Cache-Control", "no-cache, no-store, must-revalidate");
httpdHeader(connData, "Pragma", "no-cache"); httpdHeader(connData, "Pragma", "no-cache");
@ -57,6 +59,23 @@ getStringArg(HttpdConnData *connData, char *name, char *config, int max_len) {
return 1; return 1;
} }
// look for the HTTP arg 'name' and store it at 'config' as an 8-bit integer
// returns -1 on error, 0 if not found, 1 if found and OK
int8_t ICACHE_FLASH_ATTR
getInt8Arg(HttpdConnData *connData, char *name, int8_t *config) {
char buff[16];
int len = httpdFindArg(connData->getArgs, name, buff, sizeof(buff));
if (len < 0) return 0; // not found, skip
int m = atoi(buff);
if (len >= 6 || m < -128 || m > 255) {
os_sprintf(buff, "Value for %s out of range", name);
errorResponse(connData, 400, buff);
return -1;
}
*config = m;
return 1;
}
int8_t ICACHE_FLASH_ATTR int8_t ICACHE_FLASH_ATTR
getBoolArg(HttpdConnData *connData, char *name, bool*config) { getBoolArg(HttpdConnData *connData, char *name, bool*config) {
char buff[64]; char buff[64];
@ -148,6 +167,10 @@ int ICACHE_FLASH_ATTR cgiMenu(HttpdConnData *connData) {
httpdHeader(connData, "Cache-Control", "max-age=3600, must-revalidate"); httpdHeader(connData, "Cache-Control", "max-age=3600, must-revalidate");
httpdHeader(connData, "Content-Type", "application/json"); httpdHeader(connData, "Content-Type", "application/json");
httpdEndHeaders(connData); httpdEndHeaders(connData);
// limit hostname to 12 chars
char name[13];
os_strncpy(name, flashConfig.hostname, 12);
name[12] = 0;
// construct json response // construct json response
os_sprintf(buff, os_sprintf(buff,
"{\"menu\": [\"Home\", \"/home.html\", " "{\"menu\": [\"Home\", \"/home.html\", "
@ -157,7 +180,8 @@ int ICACHE_FLASH_ATTR cgiMenu(HttpdConnData *connData) {
"\"REST/MQTT\", \"/mqtt.html\"," "\"REST/MQTT\", \"/mqtt.html\","
#endif #endif
"\"Debug log\", \"/log.html\" ],\n" "\"Debug log\", \"/log.html\" ],\n"
" \"version\": \"%s\" }", esp_link_version); " \"version\": \"%s\","
"\"name\":\"%s\"}", esp_link_version, name);
httpdSend(connData, buff, -1); httpdSend(connData, buff, -1);
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }

@ -4,6 +4,7 @@
#include <esp8266.h> #include <esp8266.h>
#include "httpd.h" #include "httpd.h"
void noCacheHeaders(HttpdConnData *connData, int code);
void jsonHeader(HttpdConnData *connData, int code); void jsonHeader(HttpdConnData *connData, int code);
void errorResponse(HttpdConnData *connData, int code, char *message); void errorResponse(HttpdConnData *connData, int code, char *message);
@ -11,6 +12,10 @@ void errorResponse(HttpdConnData *connData, int code, char *message);
// 'max_len' (incl terminating zero), returns -1 on error, 0 if not found, 1 if found // 'max_len' (incl terminating zero), returns -1 on error, 0 if not found, 1 if found
int8_t getStringArg(HttpdConnData *connData, char *name, char *config, int max_len); int8_t getStringArg(HttpdConnData *connData, char *name, char *config, int max_len);
// Get the HTTP query-string param 'name' and store it as a int8 value at 'config',
// supports signed and unsigned, returns -1 on error, 0 if not found, 1 if found
int8_t getInt8Arg(HttpdConnData *connData, char *name, int8_t *config);
// Get the HTTP query-string param 'name' and store it boolean value at 'config', // Get the HTTP query-string param 'name' and store it boolean value at 'config',
// supports 1/true and 0/false, returns -1 on error, 0 if not found, 1 if found // supports 1/true and 0/false, returns -1 on error, 0 if not found, 1 if found
int8_t getBoolArg(HttpdConnData *connData, char *name, bool*config); int8_t getBoolArg(HttpdConnData *connData, char *name, bool*config);

@ -16,6 +16,7 @@ Some flash handling cgi routines. Used for reading the existing flash and updati
#include <esp8266.h> #include <esp8266.h>
#include <osapi.h> #include <osapi.h>
#include "cgi.h"
#include "cgiflash.h" #include "cgiflash.h"
#include "espfs.h" #include "espfs.h"
@ -33,10 +34,23 @@ static char* ICACHE_FLASH_ATTR check_header(void *buf) {
return NULL; return NULL;
} }
// check whether the flash map/size we have allows for OTA upgrade
static bool canOTA(void) {
enum flash_size_map map = system_get_flash_size_map();
return map >= FLASH_SIZE_8M_MAP_512_512;
}
static char *flash_too_small = "Flash too small for OTA update";
//===== Cgi to query which firmware needs to be uploaded next //===== Cgi to query which firmware needs to be uploaded next
int ICACHE_FLASH_ATTR cgiGetFirmwareNext(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiGetFirmwareNext(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
if (!canOTA()) {
errorResponse(connData, 400, flash_too_small);
return HTTPD_CGI_DONE;
}
uint8 id = system_upgrade_userbin_check(); uint8 id = system_upgrade_userbin_check();
httpdStartResponse(connData, 200); httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", "text/plain"); httpdHeader(connData, "Content-Type", "text/plain");
@ -55,6 +69,11 @@ int ICACHE_FLASH_ATTR cgiGetFirmwareNext(HttpdConnData *connData) {
int ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
if (!canOTA()) {
errorResponse(connData, 400, flash_too_small);
return HTTPD_CGI_DONE;
}
int offset = connData->post->received - connData->post->buffLen; int offset = connData->post->received - connData->post->buffLen;
if (offset == 0) { if (offset == 0) {
connData->cgiPrivData = NULL; connData->cgiPrivData = NULL;
@ -131,6 +150,11 @@ static ETSTimer flash_reboot_timer;
int ICACHE_FLASH_ATTR cgiRebootFirmware(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiRebootFirmware(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
if (!canOTA()) {
errorResponse(connData, 400, flash_too_small);
return HTTPD_CGI_DONE;
}
// sanity-check that the 'next' partition actually contains something that looks like // sanity-check that the 'next' partition actually contains something that looks like
// valid firmware // valid firmware
uint8 id = system_upgrade_userbin_check(); uint8 id = system_upgrade_userbin_check();

@ -11,6 +11,10 @@ static char *mqtt_states[] = {
"disconnected", "reconnecting", "connecting", "connected", "disconnected", "reconnecting", "connecting", "connected",
}; };
char *mqttState(void) {
return mqtt_states[mqttClient.connState];
}
// Cgi to return MQTT settings // Cgi to return MQTT settings
int ICACHE_FLASH_ATTR cgiMqttGet(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiMqttGet(HttpdConnData *connData) {
char buff[1024]; char buff[1024];

@ -4,6 +4,7 @@
#include "httpd.h" #include "httpd.h"
int cgiMqtt(HttpdConnData *connData); int cgiMqtt(HttpdConnData *connData);
char *mqttState(void);
#endif // CGIMQTT_H #endif // CGIMQTT_H
#endif // MQTT #endif // MQTT

@ -0,0 +1,650 @@
// Copyright (c) 2015 by Thorsten von Eicken, see LICENSE.txt in the esp-link repo
#include <esp8266.h>
#include <osapi.h>
#include "cgi.h"
#include "cgioptiboot.h"
#include "config.h"
#include "uart.h"
#include "stk500.h"
#include "serbridge.h"
#include "serled.h"
#define SYNC_TIMEOUT 3600 // to achieve sync on initial baud rate, in milliseconds
#define SYNC_INTERVAL 25 // interval at which we try to sync
#define BAUD_INTERVAL 400 // interval after which we change baud rate
#define PGM_TIMEOUT 20000 // timeout when sync is achieved, in milliseconds
#define PGM_INTERVAL 200 // send sync at this interval in ms when in programming mode
#define OPTIBOOT_DBG
#undef DBG
#ifdef OPTIBOOT_DBG
#define DBG(format, ...) os_printf(format, ## __VA_ARGS__)
#else
#define DBG(format, ...) do { } while(0)
#endif
#define DBG_GPIO5 1 // define to 1 to use GPIO5 to trigger scope
//===== global state
static ETSTimer optibootTimer;
static enum { // overall programming states
stateSync = 0, // trying to get sync
stateGetSig, // reading device signature
stateGetVersLo, // reading optiboot version, low bits
stateGetVersHi, // reading optiboot version, high bits
stateProg, // programming...
} progState;
static short syncCnt; // counter & timeout for sync attempts
static short baudCnt; // counter for sync attempts at different baud rates
static short ackWait; // counter of expected ACKs
static uint16_t optibootVers;
static uint32_t baudRate; // baud rate at which we're programming
#define RESP_SZ 64
static char responseBuf[RESP_SZ]; // buffer to accumulate responses from optiboot
static short responseLen = 0; // amount accumulated so far
#define ERR_MAX 128
static char errMessage[ERR_MAX]; // error message
#define MAX_PAGE_SZ 512 // max flash page size supported
#define MAX_SAVED 512 // max chars in saved buffer
// structure used to remember request details from one callback to the next
// allocated dynamically so we don't burn so much static RAM
static struct optibootData {
char *saved; // buffer for saved incomplete hex records
char *pageBuf; // buffer for received data to be sent to AVR
uint16_t pageLen; // number of bytes in pageBuf
uint16_t pgmSz; // size of flash page to be programmed at a time
uint16_t pgmDone; // number of bytes programmed
uint32_t address; // address to write next page to
uint32_t startTime; // time of program POST request
HttpdConnData *conn; // request doing the programming, so we can cancel it
bool eof; // got EOF record
} *optibootData;
// forward function references
static void optibootTimerCB(void *);
static void optibootUartRecv(char *buffer, short length);
static bool processRecord(char *buf, short len);
static bool programPage(void);
static void armTimer(void);
static void initBaud(void);
static void ICACHE_FLASH_ATTR optibootInit() {
progState = stateSync;
syncCnt = 0;
baudCnt = 0;
uart0_baud(flashConfig.baud_rate);
ackWait = 0;
errMessage[0] = 0;
responseLen = 0;
programmingCB = NULL;
if (optibootData != NULL) {
if (optibootData->conn != NULL)
optibootData->conn->cgiPrivData = (void *)-1; // signal that request has been aborted
if (optibootData->pageBuf) os_free(optibootData->pageBuf);
if (optibootData->saved) os_free(optibootData->saved);
os_free(optibootData);
optibootData = NULL;
}
os_timer_disarm(&optibootTimer);
DBG("OB init\n");
}
// append one string to another but visually escape non-printing characters in the second
// string using \x00 hex notation, max is the max chars in the concatenated string.
void ICACHE_FLASH_ATTR appendPretty(char *buf, int max, char *raw, int rawLen) {
int off = strlen(buf);
max -= off + 1; // for null termination
for (int i=0; i<max && i<rawLen; i++) {
unsigned char c = raw[i++];
if (c >= ' ' && c <= '~') {
buf[off++] = c;
} else if (c == '\n') {
buf[off++] = '\\';
buf[off++] = 'n';
} else if (c == '\r') {
buf[off++] = '\\';
buf[off++] = 'r';
} else {
buf[off++] = '\\';
buf[off++] = 'x';
buf[off++] = '0'+(unsigned char)((c>>4)+((c>>4)>9?7:0));
buf[off++] = '0'+(unsigned char)((c&0xf)+((c&0xf)>9?7:0));
}
}
buf[off] = 0;
}
//===== Cgi to reset AVR and get Optiboot into sync
int ICACHE_FLASH_ATTR cgiOptibootSync(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
// check that we know the reset pin, else error out with that
if (flashConfig.reset_pin < 0) {
errorResponse(connData, 400, "No reset pin defined");
} else if (connData->requestType == HTTPD_METHOD_POST) {
// issue reset
optibootInit();
baudRate = flashConfig.baud_rate;
programmingCB = optibootUartRecv;
initBaud();
serbridgeReset();
#if DBG_GPIO5
makeGpio(5);
gpio_output_set(0, (1<<5), (1<<5), 0); // output 0
#endif
// start sync timer
os_timer_disarm(&optibootTimer);
os_timer_setfn(&optibootTimer, optibootTimerCB, NULL);
os_timer_arm(&optibootTimer, 50, 0); // fire in 50ms and don't recur
// respond with optimistic OK
noCacheHeaders(connData, 204);
httpdEndHeaders(connData);
httpdSend(connData, "", 0);
} else if (connData->requestType == HTTPD_METHOD_GET) {
noCacheHeaders(connData, 200);
httpdEndHeaders(connData);
if (!errMessage[0] && progState >= stateProg) {
char buf[64];
DBG("OB got sync\n");
os_sprintf(buf, "SYNC at %ld baud: Optiboot %d.%d",
baudRate, optibootVers>>8, optibootVers&0xff);
httpdSend(connData, buf, -1);
} else if (errMessage[0] && progState == stateSync) {
DBG("OB cannot sync\n");
char buf[512];
os_sprintf(buf, "FAILED to SYNC: %s, got: %d chars\r\n", errMessage, responseLen);
appendPretty(buf, 512, responseBuf, responseLen);
httpdSend(connData, buf, -1);
} else {
httpdSend(connData, errMessage[0] ? errMessage : "NOT READY", -1);
}
} else {
errorResponse(connData, 404, "Only GET and POST supported");
}
return HTTPD_CGI_DONE;
}
// verify that N chars are hex characters
static bool ICACHE_FLASH_ATTR checkHex(char *buf, short len) {
while (len--) {
char c = *buf++;
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
continue;
DBG("OB non-hex\n");
os_sprintf(errMessage, "Non hex char in POST record: '%c'/0x%02x", c, c);
return false;
}
return true;
}
// get hex value of some hex characters
static uint32_t ICACHE_FLASH_ATTR getHexValue(char *buf, short len) {
uint32_t v = 0;
while (len--) {
v = (v<<4) | (uint32_t)(*buf & 0xf);
if (*buf > '9') v += 9;
buf++;
}
return v;
}
//===== Cgi to write firmware to Optiboot, requires prior sync call
int ICACHE_FLASH_ATTR cgiOptibootData(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
DBG("OB pgm: state=%d PrivData=%p postLen=%d\n", progState, connData->cgiPrivData, connData->post->len);
// check that we have sync
if (errMessage[0] || progState < stateProg) {
DBG("OB not in sync, state=%d, err=%s\n", progState, errMessage);
errorResponse(connData, 400, errMessage[0] ? errMessage : "Optiboot not in sync");
return HTTPD_CGI_DONE;
}
// check that we don't have two concurrent programming requests going on
if (connData->cgiPrivData == (void *)-1) {
DBG("OB aborted\n");
errorResponse(connData, 400, "Request got aborted by a concurrent sync request");
return HTTPD_CGI_DONE;
}
// allocate data structure to track programming
if (!optibootData) {
optibootData = os_zalloc(sizeof(struct optibootData));
char *saved = os_zalloc(MAX_SAVED+1); // need space for string terminator
char *pageBuf = os_zalloc(MAX_PAGE_SZ+MAX_SAVED/2);
if (!optibootData || !pageBuf || !saved) {
errorResponse(connData, 400, "Out of memory");
return HTTPD_CGI_DONE;
}
optibootData->pageBuf = pageBuf;
optibootData->saved = saved;
optibootData->startTime = system_get_time();
optibootData->pgmSz = 128; // hard coded for 328p for now, should be query string param
DBG("OB data alloc\n");
}
// iterate through the data received and program the AVR one block at a time
HttpdPostData *post = connData->post;
char *saved = optibootData->saved;
while (post->buffLen > 0) {
// first fill-up the saved buffer
short saveLen = strlen(saved);
if (saveLen < MAX_SAVED) {
short cpy = MAX_SAVED-saveLen;
if (cpy > post->buffLen) cpy = post->buffLen;
os_memcpy(saved+saveLen, post->buff, cpy);
saveLen += cpy;
saved[saveLen] = 0; // string terminator
os_memmove(post->buff, post->buff+cpy, post->buffLen-cpy);
post->buffLen -= cpy;
//DBG("OB cp %d buff->saved\n", cpy);
}
// process HEX records
while (saveLen >= 11) { // 11 is minimal record length
// skip any CR/LF
short skip = 0;
while (skip < saveLen && (saved[skip] == '\n' || saved[skip] == '\r'))
skip++;
if (skip > 0) {
// shift out cr/lf (keep terminating \0)
os_memmove(saved, saved+skip, saveLen+1-skip);
saveLen -= skip;
if (saveLen < 11) break;
DBG("OB skip %d cr/lf\n", skip);
}
// inspect whether we have a proper record start
if (saved[0] != ':') {
DBG("OB found non-: start\n");
os_sprintf(errMessage, "Expected start of record in POST data, got %s", saved);
errorResponse(connData, 400, errMessage);
optibootInit();
return HTTPD_CGI_DONE;
}
if (!checkHex(saved+1, 2)) {
errorResponse(connData, 400, errMessage);
optibootInit();
return HTTPD_CGI_DONE;
}
uint8_t recLen = getHexValue(saved+1, 2);
//DBG("OB record %d\n", recLen);
// process record
if (saveLen >= 11+recLen*2) {
if (!processRecord(saved, 11+recLen*2)) {
DBG("OB process err %s\n", errMessage);
errorResponse(connData, 400, errMessage);
optibootInit();
return HTTPD_CGI_DONE;
}
short shift = 11+recLen*2;
os_memmove(saved, saved+shift, saveLen+1-shift);
saveLen -= shift;
//DBG("OB %d byte record\n", shift);
} else {
break;
}
}
}
short code;
if (post->received < post->len) {
//DBG("OB pgm need more\n");
return HTTPD_CGI_MORE;
}
if (optibootData->eof) {
// tell optiboot to reboot into the sketch
uart0_write_char(STK_LEAVE_PROGMODE);
uart0_write_char(CRC_EOP);
code = 200;
// calculate some stats
float dt = ((system_get_time() - optibootData->startTime)/1000)/1000.0; // in seconds
uint16_t pgmDone = optibootData->pgmDone;
optibootInit();
os_sprintf(errMessage, "Success. %d bytes at %ld baud in %d.%ds, %dB/s %d%% efficient",
pgmDone, baudRate, (int)dt, (int)(dt*10)%10, (int)(pgmDone/dt),
(int)(100.0*(10.0*pgmDone/baudRate)/dt));
} else {
code = 400;
optibootInit();
os_strcpy(errMessage, "Improperly terminated POST data");
}
DBG("OB pgm done: %d -- %s\n", code, errMessage);
noCacheHeaders(connData, code);
httpdEndHeaders(connData);
httpdSend(connData, errMessage, -1);
errMessage[0] = 0;
return HTTPD_CGI_DONE;
}
// verify checksum
static bool ICACHE_FLASH_ATTR verifyChecksum(char *buf, short len) {
uint8_t sum = 0;
while (len >= 2) {
sum += (uint8_t)getHexValue(buf, 2);
buf += 2;
len -= 2;
}
return sum == 0;
}
// Process a hex record -- assumes that the records starts with ':' & hex length
static bool ICACHE_FLASH_ATTR processRecord(char *buf, short len) {
buf++; len--; // skip leading ':'
// check we have all hex chars
if (!checkHex(buf, len)) return false;
// verify checksum
if (!verifyChecksum(buf, len)) {
buf[len] = 0;
os_sprintf(errMessage, "Invalid checksum for record %s", buf);
return false;
}
// dispatch based on record type
uint8_t type = getHexValue(buf+6, 2);
switch (type) {
case 0x00: { // data
//DBG("OB REC data %ld pglen=%d\n", getHexValue(buf, 2), optibootData->pageLen);
uint32_t addr = getHexValue(buf+2, 4);
// check whether we need to program previous record(s)
if (optibootData->pageLen > 0 &&
addr != ((optibootData->address+optibootData->pageLen)&0xffff)) {
//DBG("OB addr chg\n");
if (!programPage()) return false;
}
// set address, unless we're adding to the end (programPage may have changed pageLen)
if (optibootData->pageLen == 0) {
optibootData->address = (optibootData->address & 0xffff0000) | addr;
//DBG("OB set-addr 0x%lx\n", optibootData->address);
}
// append record
uint16_t recLen = getHexValue(buf, 2);
for (uint16_t i=0; i<recLen; i++)
optibootData->pageBuf[optibootData->pageLen++] = getHexValue(buf+8+2*i, 2);
// program page, if we have a full page
if (optibootData->pageLen >= optibootData->pgmSz) {
//DBG("OB full\n");
if (!programPage()) return false;
}
break; }
case 0x01: // EOF
DBG("OB EOF\n");
// program any remaining partial page
if (optibootData->pageLen > 0)
if (!programPage()) return false;
optibootData->eof = true;
break;
case 0x04: // address
DBG("OB address 0x%lx\n", getHexValue(buf+8, 4) << 16);
// program any remaining partial page
if (optibootData->pageLen > 0)
if (!programPage()) return false;
optibootData->address = getHexValue(buf+8, 4) << 16;
break;
case 0x05: // start address
// ignore, there's no way to tell optiboot that...
break;
default:
DBG("OB bad record type\n");
os_sprintf(errMessage, "Invalid/unknown record type: 0x%02x", type);
return false;
}
return true;
}
// Poll UART for ACKs, max 50ms
static bool pollAck() {
char recv[16];
uint16_t need = ackWait*2;
uint16_t got = uart0_rx_poll(recv, need, 50000);
#ifdef DBG_GPIO5
gpio_output_set(0, (1<<5), (1<<5), 0); // output 0
#endif
if (got < need) {
os_strcpy(errMessage, "Timeout waiting for flash page to be programmed");
return false;
}
ackWait = 0;
if (recv[0] == STK_INSYNC && recv[1] == STK_OK)
return true;
os_sprintf(errMessage, "Did not get ACK after programming cmd: %x02x %x02x", recv[0], recv[1]);
return false;
}
// Program a flash page
static bool ICACHE_FLASH_ATTR programPage(void) {
if (optibootData->pageLen == 0) return true;
armTimer(); // keep the timerCB out of the picture
if (ackWait > 7) {
os_sprintf(errMessage, "Lost sync while programming\n");
return false;
}
uint16_t pgmLen = optibootData->pageLen;
if (pgmLen > optibootData->pgmSz) pgmLen = optibootData->pgmSz;
DBG("OB pgm %d@0x%lx ackWait=%d\n", pgmLen, optibootData->address, ackWait);
// send address to optiboot (little endian format)
#ifdef DBG_GPIO5
gpio_output_set((1<<5), 0, (1<<5), 0); // output 1
#endif
ackWait++;
uart0_write_char(STK_LOAD_ADDRESS);
uint16_t addr = optibootData->address >> 1; // word address
uart0_write_char(addr & 0xff);
uart0_write_char(addr >> 8);
uart0_write_char(CRC_EOP);
armTimer();
if (!pollAck()) {
DBG("OB pgm failed in load address\n");
return false;
}
armTimer();
// send page length (big-endian format, go figure...)
#ifdef DBG_GPIO5
gpio_output_set((1<<5), 0, (1<<5), 0); // output 1
#endif
ackWait++;
uart0_write_char(STK_PROG_PAGE);
uart0_write_char(pgmLen>>8);
uart0_write_char(pgmLen&0xff);
uart0_write_char('F'); // we're writing flash
// send page content
for (short i=0; i<pgmLen; i++)
uart0_write_char(optibootData->pageBuf[i]);
uart0_write_char(CRC_EOP);
armTimer();
bool ok = pollAck();
armTimer();
if (!ok) {
DBG("OB pgm failed in prog page\n");
return false;
}
// shift data out of buffer
os_memmove(optibootData->pageBuf, optibootData->pageBuf+pgmLen, optibootData->pageLen-pgmLen);
optibootData->pageLen -= pgmLen;
optibootData->address += pgmLen;
optibootData->pgmDone += pgmLen;
//DBG("OB pgm OK\n");
return true;
}
//===== Rebooting and getting sync
static void ICACHE_FLASH_ATTR armTimer() {
os_timer_disarm(&optibootTimer);
// time-out every 50ms, except when programming to allow for 9600baud (133ms for 128 bytes)
os_timer_arm(&optibootTimer, progState==stateProg ? PGM_INTERVAL : SYNC_INTERVAL, 0);
}
static int baudRates[] = { 0, 9600, 57600, 115200 };
static void ICACHE_FLASH_ATTR setBaud() {
baudRate = baudRates[(syncCnt / (BAUD_INTERVAL/SYNC_INTERVAL)) % 4];
uart0_baud(baudRate);
//DBG("OB changing to %d baud\n", b);
}
static void ICACHE_FLASH_ATTR initBaud() {
baudRates[0] = flashConfig.baud_rate;
setBaud();
}
static void ICACHE_FLASH_ATTR optibootTimerCB(void *arg) {
// see whether we've issued so many sync in a row that it's time to give up
syncCnt++;
switch (progState) {
case stateSync: // we're trying to get sync, all we do here is send a sync request
if (syncCnt >= SYNC_TIMEOUT/SYNC_INTERVAL) {
// we're doomed, give up
DBG("OB sync abandoned after timeout, state=%d syncCnt=%d\n", progState, syncCnt);
optibootInit();
strcpy(errMessage, "sync abandoned after timeout");
return;
}
if (syncCnt % (BAUD_INTERVAL/SYNC_INTERVAL) == 0) {
// time to switch baud rate and issue a reset
setBaud();
serbridgeReset();
// no point sending chars if we just switched
} else {
uart0_write_char(STK_GET_SYNC);
uart0_write_char(CRC_EOP);
}
break;
case stateProg: // we're programming and we timed-out of inaction
uart0_write_char(STK_GET_SYNC);
uart0_write_char(CRC_EOP);
ackWait++; // we now expect an ACK
break;
default: // we're trying to get some info from optiboot and it should have responded!
optibootInit(); // abort
os_sprintf(errMessage, "No response in state %d\n", progState);
DBG("OB %s\n", errMessage);
return; // do not re-arm timer
}
// we need to come back...
armTimer();
}
// skip in-sync responses
static short ICACHE_FLASH_ATTR skipInSync(char *buf, short length) {
while (length > 1 && buf[0] == STK_INSYNC && buf[1] == STK_OK) {
// not the most efficient, oh well...
os_memcpy(buf, buf+2, length-2);
length -= 2;
}
return length;
}
// receive response from optiboot, we only store the last response
static void ICACHE_FLASH_ATTR optibootUartRecv(char *buf, short length) {
// append what we got to what we have accumulated
if (responseLen < RESP_SZ-1) {
char *rb = responseBuf+responseLen;
for (short i=0; i<length && (rb-responseBuf)<(RESP_SZ-1); i++)
if (buf[i] != 0) *rb++ = buf[i]; // don't copy NULL characters, TODO: fix it
responseLen = rb-responseBuf;
responseBuf[responseLen] = 0; // string terminator
}
// dispatch based the current state
switch (progState) {
case stateSync: // we're trying to get a sync response
// look for STK_INSYNC+STK_OK at end of buffer
if (responseLen > 0 && responseBuf[responseLen-1] == STK_INSYNC) {
// missing STK_OK after STK_INSYNC, shift stuff out and try again
responseBuf[0] = STK_INSYNC;
responseLen = 1;
} else if (responseLen > 1 && responseBuf[responseLen-2] == STK_INSYNC &&
responseBuf[responseLen-1] == STK_OK) {
// got sync response, send signature request
progState++;
os_memcpy(responseBuf, responseBuf+2, responseLen-2);
responseLen -= 2;
uart0_write_char(STK_READ_SIGN);
uart0_write_char(CRC_EOP);
armTimer(); // reset timer
} else {
// nothing useful, keep at most half the buffer for error message purposes
if (responseLen > RESP_SZ/2) {
os_memcpy(responseBuf, responseBuf+responseLen-RESP_SZ/2, RESP_SZ/2);
responseLen = RESP_SZ/2;
responseBuf[responseLen] = 0; // string terminator
}
}
break;
case stateGetSig: // expecting signature
responseLen = skipInSync(responseBuf, responseLen);
if (responseLen >= 5 && responseBuf[0] == STK_INSYNC && responseBuf[4] == STK_OK) {
if (responseBuf[1] == 0x1e && responseBuf[2] == 0x95 && responseBuf[3] == 0x0f) {
// right on... ask for optiboot version
progState++;
uart0_write_char(STK_GET_PARAMETER);
uart0_write_char(0x82);
uart0_write_char(CRC_EOP);
armTimer(); // reset timer
} else {
optibootInit(); // abort
os_sprintf(errMessage, "Bad programmer signature: 0x%02x 0x%02x 0x%02x\n",
responseBuf[1], responseBuf[2], responseBuf[3]);
}
os_memcpy(responseBuf, responseBuf+5, responseLen-5);
responseLen -= 5;
}
break;
case stateGetVersLo: // expecting version
if (responseLen >= 3 && responseBuf[0] == STK_INSYNC && responseBuf[2] == STK_OK) {
optibootVers = responseBuf[1];
progState++;
os_memcpy(responseBuf, responseBuf+3, responseLen-3);
responseLen -= 3;
uart0_write_char(STK_GET_PARAMETER);
uart0_write_char(0x81);
uart0_write_char(CRC_EOP);
armTimer(); // reset timer
}
break;
case stateGetVersHi: // expecting version
if (responseLen >= 3 && responseBuf[0] == STK_INSYNC && responseBuf[2] == STK_OK) {
optibootVers |= responseBuf[1]<<8;
progState++;
os_memcpy(responseBuf, responseBuf+3, responseLen-3);
responseLen -= 3;
armTimer(); // reset timer
ackWait = 0;
}
break;
case stateProg: // count down expected sync responses
//DBG("UART recv %d\n", length);
while (responseLen >= 2 && responseBuf[0] == STK_INSYNC && responseBuf[1] == STK_OK) {
if (ackWait > 0) ackWait--;
os_memmove(responseBuf, responseBuf+2, responseLen-2);
responseLen -= 2;
}
armTimer(); // reset timer
default:
break;
}
}

@ -0,0 +1,11 @@
// Copyright (c) 2015 by Thorsten von Eicken, see LICENSE.txt in the esp-link repo
#ifndef OPTIBOOT_H
#define OPTIBOOT_H
#include <httpd.h>
int ICACHE_FLASH_ATTR cgiOptibootSync(HttpdConnData *connData);
int ICACHE_FLASH_ATTR cgiOptibootData(HttpdConnData *connData);
#endif

@ -7,6 +7,7 @@
#include "status.h" #include "status.h"
#include "serbridge.h" #include "serbridge.h"
#if 0
static char *map_names[] = { static char *map_names[] = {
"esp-bridge", "jn-esp-v2", "esp-01(AVR)", "esp-01(ARM)", "esp-br-rev", "wifi-link-12", "esp-bridge", "jn-esp-v2", "esp-01(AVR)", "esp-01(ARM)", "esp-br-rev", "wifi-link-12",
}; };
@ -17,50 +18,23 @@ static int8_t map_asn[][5] = {
{ 0, -1, 2, -1, 0 }, // esp-01(AVR) { 0, -1, 2, -1, 0 }, // esp-01(AVR)
{ 0, 2, -1, -1, 0 }, // esp-01(ARM) { 0, 2, -1, -1, 0 }, // esp-01(ARM)
{ 13, 12, 14, 0, 0 }, // esp-br-rev -- for test purposes { 13, 12, 14, 0, 0 }, // esp-br-rev -- for test purposes
{ 3, 1, 0, 2, 1 }, // esp-link-12 { 1, 3, 0, 2, 1 }, // esp-link-12
}; };
static const int num_map_names = sizeof(map_names)/sizeof(char*); static const int num_map_names = sizeof(map_names)/sizeof(char*);
static const int num_map_func = sizeof(map_func)/sizeof(char*); static const int num_map_func = sizeof(map_func)/sizeof(char*);
#endif
// Cgi to return choice of pin assignments // Cgi to return choice of pin assignments
int ICACHE_FLASH_ATTR cgiPinsGet(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiPinsGet(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted
char buff[2048]; char buff[1024];
int len; int len;
// figure out current mapping len = os_sprintf(buff,
int curr = 0; "{ \"reset\":%d, \"isp\":%d, \"conn\":%d, \"ser\":%d, \"swap\":%d, \"rxpup\":%d }",
for (int i=0; i<num_map_names; i++) { flashConfig.reset_pin, flashConfig.isp_pin, flashConfig.conn_led_pin,
int8_t *map = map_asn[i]; flashConfig.ser_led_pin, !!flashConfig.swap_uart, 1);
if (map[0] == flashConfig.reset_pin && map[1] == flashConfig.isp_pin &&
map[2] == flashConfig.conn_led_pin && map[3] == flashConfig.ser_led_pin &&
map[4] == flashConfig.swap_uart) {
curr = i;
}
}
// print mapping
len = os_sprintf(buff, "{ \"curr\":\"%s\", \"map\": [ ", map_names[curr]);
for (int i=0; i<num_map_names; i++) {
if (i != 0) buff[len++] = ',';
len += os_sprintf(buff+len, "\n{ \"value\":%d, \"name\":\"%s\"", i, map_names[i]);
for (int f=0; f<num_map_func; f++) {
len += os_sprintf(buff+len, ", \"%s\":%d", map_func[f], map_asn[i][f]);
}
len += os_sprintf(buff+len, ", \"descr\":\"");
for (int f=0; f<num_map_func; f++) {
int8_t p = map_asn[i][f];
if (f == 4)
len += os_sprintf(buff+len, " %s:%s", map_func[f], p?"yes":"no");
else if (p >= 0)
len += os_sprintf(buff+len, " %s:gpio%d", map_func[f], p);
else
len += os_sprintf(buff+len, " %s:n/a", map_func[f]);
}
len += os_sprintf(buff+len, "\" }");
}
len += os_sprintf(buff+len, "\n] }");
jsonHeader(connData, 200); jsonHeader(connData, 200);
httpdSend(connData, buff, len); httpdSend(connData, buff, len);
@ -73,41 +47,75 @@ int ICACHE_FLASH_ATTR cgiPinsSet(HttpdConnData *connData) {
return HTTPD_CGI_DONE; // Connection aborted return HTTPD_CGI_DONE; // Connection aborted
} }
char buff[128]; int8_t ok = 0;
int len = httpdFindArg(connData->getArgs, "map", buff, sizeof(buff)); int8_t reset, isp, conn, ser;
if (len <= 0) { bool swap, rxpup;
jsonHeader(connData, 400); ok |= getInt8Arg(connData, "reset", &reset);
return HTTPD_CGI_DONE; ok |= getInt8Arg(connData, "isp", &isp);
} ok |= getInt8Arg(connData, "conn", &conn);
ok |= getInt8Arg(connData, "ser", &ser);
ok |= getBoolArg(connData, "swap", &swap);
ok |= getBoolArg(connData, "rxpup", &rxpup);
if (ok < 0) return HTTPD_CGI_DONE;
int m = atoi(buff); char *coll;
if (m < 0 || m >= num_map_names) { if (ok > 0) {
jsonHeader(connData, 400); // check whether two pins collide
return HTTPD_CGI_DONE; uint16_t pins = 0;
if (reset >= 0) pins = 1 << reset;
if (isp >= 0) {
if (pins & (1<<isp)) { coll = "ISP/Flash"; goto collision; }
pins |= 1 << isp;
} }
#ifdef CGIPINS_DBG if (conn >= 0) {
os_printf("Switching pin map to %s (%d)\n", map_names[m], m); if (pins & (1<<conn)) { coll = "Conn LED"; goto collision; }
#endif pins |= 1 << conn;
int8_t *map = map_asn[m]; }
flashConfig.reset_pin = map[0]; if (ser >= 0) {
flashConfig.isp_pin = map[1]; if (pins & (1<<ser)) { coll = "Serial LED"; goto collision; }
flashConfig.conn_led_pin = map[2]; pins |= 1 << ser;
flashConfig.ser_led_pin = map[3]; }
flashConfig.swap_uart = map[4]; if (swap) {
if (pins & (1<<15)) { coll = "Uart TX"; goto collision; }
if (pins & (1<<13)) { coll = "Uart RX"; goto collision; }
} else {
if (pins & (1<<1)) { coll = "Uart TX"; goto collision; }
if (pins & (1<<3)) { coll = "Uart RX"; goto collision; }
}
// we're good, set flashconfig
flashConfig.reset_pin = reset;
flashConfig.isp_pin = isp;
flashConfig.conn_led_pin = conn;
flashConfig.ser_led_pin = ser;
flashConfig.swap_uart = swap;
flashConfig.rx_pullup = rxpup;
os_printf("Pins changed: reset=%d isp=%d conn=%d ser=%d swap=%d rx-pup=%d\n",
reset, isp, conn, ser, swap, rxpup);
// apply the changes
serbridgeInitPins(); serbridgeInitPins();
serledInit(); serledInit();
statusInit(); statusInit();
// save to flash
if (configSave()) { if (configSave()) {
httpdStartResponse(connData, 200); httpdStartResponse(connData, 204);
httpdEndHeaders(connData); httpdEndHeaders(connData);
} else { } else {
httpdStartResponse(connData, 500); httpdStartResponse(connData, 500);
httpdEndHeaders(connData); httpdEndHeaders(connData);
httpdSend(connData, "Failed to save config", -1); httpdSend(connData, "Failed to save config", -1);
} }
}
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
collision: {
char buff[128];
os_sprintf(buff, "Pin assignment for %s collides with another assignment", coll);
errorResponse(connData, 400, buff);
return HTTPD_CGI_DONE;
}
} }
int ICACHE_FLASH_ATTR cgiPins(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiPins(HttpdConnData *connData) {

@ -21,8 +21,13 @@ Cgi/template routines for the /wifi url.
#include "config.h" #include "config.h"
#include "log.h" #include "log.h"
//#define SLEEP_MODE LIGHT_SLEEP_T #ifdef CGIWIFI_DBG
#define SLEEP_MODE MODEM_SLEEP_T #define DBG(format, ...) os_printf(format, ## __VA_ARGS__)
#else
#define DBG(format, ...) do { } while(0)
#endif
static void wifiStartMDNS(struct ip_addr);
// ===== wifi status change callbacks // ===== wifi status change callbacks
static WifiStateChangeCb wifi_state_change_cb[4]; static WifiStateChangeCb wifi_state_change_cb[4];
@ -55,48 +60,37 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) {
case EVENT_STAMODE_CONNECTED: case EVENT_STAMODE_CONNECTED:
wifiState = wifiIsConnected; wifiState = wifiIsConnected;
wifiReason = 0; wifiReason = 0;
#ifdef CGIWIFI_DBG DBG("Wifi connected to ssid %s, ch %d\n", evt->event_info.connected.ssid,
os_printf("Wifi connected to ssid %s, ch %d\n", evt->event_info.connected.ssid,
evt->event_info.connected.channel); evt->event_info.connected.channel);
#endif
statusWifiUpdate(wifiState); statusWifiUpdate(wifiState);
break; break;
case EVENT_STAMODE_DISCONNECTED: case EVENT_STAMODE_DISCONNECTED:
wifiState = wifiIsDisconnected; wifiState = wifiIsDisconnected;
wifiReason = evt->event_info.disconnected.reason; wifiReason = evt->event_info.disconnected.reason;
#ifdef CGIWIFI_DBG DBG("Wifi disconnected from ssid %s, reason %s (%d)\n",
os_printf("Wifi disconnected from ssid %s, reason %s (%d)\n",
evt->event_info.disconnected.ssid, wifiGetReason(), evt->event_info.disconnected.reason); evt->event_info.disconnected.ssid, wifiGetReason(), evt->event_info.disconnected.reason);
#endif
statusWifiUpdate(wifiState); statusWifiUpdate(wifiState);
break; break;
case EVENT_STAMODE_AUTHMODE_CHANGE: case EVENT_STAMODE_AUTHMODE_CHANGE:
#ifdef CGIWIFI_DBG DBG("Wifi auth mode: %d -> %d\n",
os_printf("Wifi auth mode: %d -> %d\n",
evt->event_info.auth_change.old_mode, evt->event_info.auth_change.new_mode); evt->event_info.auth_change.old_mode, evt->event_info.auth_change.new_mode);
#endif
break; break;
case EVENT_STAMODE_GOT_IP: case EVENT_STAMODE_GOT_IP:
wifiState = wifiGotIP; wifiState = wifiGotIP;
wifiReason = 0; wifiReason = 0;
#ifdef CGIWIFI_DBG DBG("Wifi got ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR "\n",
os_printf("Wifi got ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR "\n",
IP2STR(&evt->event_info.got_ip.ip), IP2STR(&evt->event_info.got_ip.mask), IP2STR(&evt->event_info.got_ip.ip), IP2STR(&evt->event_info.got_ip.mask),
IP2STR(&evt->event_info.got_ip.gw)); IP2STR(&evt->event_info.got_ip.gw));
#endif
statusWifiUpdate(wifiState); statusWifiUpdate(wifiState);
wifiStartMDNS(evt->event_info.got_ip.ip);
break; break;
case EVENT_SOFTAPMODE_STACONNECTED: case EVENT_SOFTAPMODE_STACONNECTED:
#ifdef CGIWIFI_DBG DBG("Wifi AP: station " MACSTR " joined, AID = %d\n",
os_printf("Wifi AP: station " MACSTR " joined, AID = %d\n",
MAC2STR(evt->event_info.sta_connected.mac), evt->event_info.sta_connected.aid); MAC2STR(evt->event_info.sta_connected.mac), evt->event_info.sta_connected.aid);
#endif
break; break;
case EVENT_SOFTAPMODE_STADISCONNECTED: case EVENT_SOFTAPMODE_STADISCONNECTED:
#ifdef CGIWIFI_DBG DBG("Wifi AP: station " MACSTR " left, AID = %d\n",
os_printf("Wifi AP: station " MACSTR " left, AID = %d\n",
MAC2STR(evt->event_info.sta_disconnected.mac), evt->event_info.sta_disconnected.aid); MAC2STR(evt->event_info.sta_disconnected.mac), evt->event_info.sta_disconnected.aid);
#endif
break; break;
default: default:
break; break;
@ -116,9 +110,24 @@ wifiAddStateChangeCb(WifiStateChangeCb cb) {
return; return;
} }
} }
#ifdef CGIWIFI_DBG DBG("WIFI: max state change cb count exceeded\n");
os_printf("WIFI: max state change cb count exceeded\n"); }
#endif
static bool mdns_started = false;
static struct mdns_info mdns_info;
// cannot allocate the info struct on the stack, it crashes!
static ICACHE_FLASH_ATTR
void wifiStartMDNS(struct ip_addr ip) {
if (!mdns_started) {
os_memset(&mdns_info, 0, sizeof(struct mdns_info));
mdns_info.host_name = flashConfig.hostname;
mdns_info.server_name = "http", // service name
mdns_info.server_port = 80, // service port
mdns_info.ipAddr = ip.addr,
espconn_mdns_init(&mdns_info);
mdns_started = true;
}
} }
// ===== wifi scanning // ===== wifi scanning
@ -147,9 +156,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) {
struct bss_info *bss_link = (struct bss_info *)arg; struct bss_info *bss_link = (struct bss_info *)arg;
if (status!=OK) { if (status!=OK) {
#ifdef CGIWIFI_DBG DBG("wifiScanDoneCb status=%d\n", status);
os_printf("wifiScanDoneCb status=%d\n", status);
#endif
cgiWifiAps.scanInProgress=0; cgiWifiAps.scanInProgress=0;
return; return;
} }
@ -169,9 +176,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) {
//Allocate memory for access point data //Allocate memory for access point data
cgiWifiAps.apData=(ApData **)os_malloc(sizeof(ApData *)*n); cgiWifiAps.apData=(ApData **)os_malloc(sizeof(ApData *)*n);
cgiWifiAps.noAps=n; cgiWifiAps.noAps=n;
#ifdef CGIWIFI_DBG DBG("Scan done: found %d APs\n", n);
os_printf("Scan done: found %d APs\n", n);
#endif
//Copy access point data to the static struct //Copy access point data to the static struct
n=0; n=0;
@ -180,9 +185,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) {
if (n>=cgiWifiAps.noAps) { if (n>=cgiWifiAps.noAps) {
//This means the bss_link changed under our nose. Shouldn't happen! //This means the bss_link changed under our nose. Shouldn't happen!
//Break because otherwise we will write in unallocated memory. //Break because otherwise we will write in unallocated memory.
#ifdef CGIWIFI_DBG DBG("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps);
os_printf("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps);
#endif
break; break;
} }
//Save the ap data. //Save the ap data.
@ -190,9 +193,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) {
cgiWifiAps.apData[n]->rssi=bss_link->rssi; cgiWifiAps.apData[n]->rssi=bss_link->rssi;
cgiWifiAps.apData[n]->enc=bss_link->authmode; cgiWifiAps.apData[n]->enc=bss_link->authmode;
strncpy(cgiWifiAps.apData[n]->ssid, (char*)bss_link->ssid, 32); strncpy(cgiWifiAps.apData[n]->ssid, (char*)bss_link->ssid, 32);
#ifdef CGIWIFI_DBG DBG("bss%d: %s (%d)\n", n+1, (char*)bss_link->ssid, bss_link->rssi);
os_printf("bss%d: %s (%d)\n", n+1, (char*)bss_link->ssid, bss_link->rssi);
#endif
bss_link = bss_link->next.stqe_next; bss_link = bss_link->next.stqe_next;
n++; n++;
@ -203,9 +204,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) {
static ETSTimer scanTimer; static ETSTimer scanTimer;
static void ICACHE_FLASH_ATTR scanStartCb(void *arg) { static void ICACHE_FLASH_ATTR scanStartCb(void *arg) {
#ifdef CGIWIFI_DBG DBG("Starting a scan\n");
os_printf("Starting a scan\n");
#endif
wifi_station_scan(NULL, wifiScanDoneCb); wifi_station_scan(NULL, wifiScanDoneCb);
} }
@ -216,15 +215,41 @@ static int ICACHE_FLASH_ATTR cgiWiFiStartScan(HttpdConnData *connData) {
cgiWifiAps.scanInProgress = 1; cgiWifiAps.scanInProgress = 1;
os_timer_disarm(&scanTimer); os_timer_disarm(&scanTimer);
os_timer_setfn(&scanTimer, scanStartCb, NULL); os_timer_setfn(&scanTimer, scanStartCb, NULL);
os_timer_arm(&scanTimer, 1000, 0); os_timer_arm(&scanTimer, 200, 0);
} }
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
static int ICACHE_FLASH_ATTR cgiWiFiGetScan(HttpdConnData *connData) { static int ICACHE_FLASH_ATTR cgiWiFiGetScan(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
char buff[2048]; char buff[1460];
int len; const int chunk = 1460/64; // ssid is up to 32 chars
int len = 0;
os_printf("GET scan: cgiData=%d noAps=%d\n", (int)connData->cgiData, cgiWifiAps.noAps);
// handle continuation call, connData->cgiData-1 is the position in the scan results where we
// we need to continue sending from (using -1 'cause 0 means it's the first call)
if (connData->cgiData) {
int next = (int)connData->cgiData-1;
int pos = next;
while (pos < cgiWifiAps.noAps && pos < next+chunk) {
len += os_sprintf(buff+len, "{\"essid\": \"%s\", \"rssi\": %d, \"enc\": \"%d\"}%c\n",
cgiWifiAps.apData[pos]->ssid, cgiWifiAps.apData[pos]->rssi, cgiWifiAps.apData[pos]->enc,
(pos+1 == cgiWifiAps.noAps) ? ' ' : ',');
pos++;
}
// done or more?
if (pos == cgiWifiAps.noAps) {
len += os_sprintf(buff+len, "]}}\n");
httpdSend(connData, buff, len);
return HTTPD_CGI_DONE;
} else {
connData->cgiData = (void*)(pos+1);
httpdSend(connData, buff, len);
return HTTPD_CGI_MORE;
}
}
jsonHeader(connData, 200); jsonHeader(connData, 200);
@ -236,15 +261,9 @@ static int ICACHE_FLASH_ATTR cgiWiFiGetScan(HttpdConnData *connData) {
} }
len = os_sprintf(buff, "{\"result\": {\"inProgress\": \"0\", \"APs\": [\n"); len = os_sprintf(buff, "{\"result\": {\"inProgress\": \"0\", \"APs\": [\n");
for (int pos=0; pos<cgiWifiAps.noAps; pos++) { connData->cgiData = (void *)1; // start with first result next time we're called
len += os_sprintf(buff+len, "{\"essid\": \"%s\", \"rssi\": %d, \"enc\": \"%d\"}%s\n",
cgiWifiAps.apData[pos]->ssid, cgiWifiAps.apData[pos]->rssi,
cgiWifiAps.apData[pos]->enc, (pos==cgiWifiAps.noAps-1)?"":",");
}
len += os_sprintf(buff+len, "]}}\n");
//os_printf("Sending %d bytes: %s\n", len, buff);
httpdSend(connData, buff, len); httpdSend(connData, buff, len);
return HTTPD_CGI_DONE; return HTTPD_CGI_MORE;
} }
int ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiWiFiScan(HttpdConnData *connData) {
@ -270,35 +289,26 @@ static ETSTimer resetTimer;
static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) { static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) {
int x = wifi_station_get_connect_status(); int x = wifi_station_get_connect_status();
int m = wifi_get_opmode() & 0x3; int m = wifi_get_opmode() & 0x3;
#ifdef CGIWIFI_DBG DBG("Wifi check: mode=%s status=%d\n", wifiMode[m], x);
os_printf("Wifi check: mode=%s status=%d\n", wifiMode[m], x);
#endif
if (x == STATION_GOT_IP) { if (x == STATION_GOT_IP) {
if (m != 1) { if (m != 1) {
#ifdef CHANGE_TO_STA #ifdef CHANGE_TO_STA
// We're happily connected, go to STA mode // We're happily connected, go to STA mode
#ifdef CGIWIFI_DBG DBG("Wifi got IP. Going into STA mode..\n");
os_printf("Wifi got IP. Going into STA mode..\n");
#endif
wifi_set_opmode(1); wifi_set_opmode(1);
wifi_set_sleep_type(SLEEP_MODE); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); // check one more time after switching to STA-only
os_timer_arm(&resetTimer, RESET_TIMEOUT, 0);
#endif #endif
} }
log_uart(false); log_uart(false);
// no more resetTimer at this point, gotta use physical reset to recover if in trouble // no more resetTimer at this point, gotta use physical reset to recover if in trouble
} else { } else {
if (m != 3) { if (m != 3) {
#ifdef CGIWIFI_DBG DBG("Wifi connect failed. Going into STA+AP mode..\n");
os_printf("Wifi connect failed. Going into STA+AP mode..\n");
#endif
wifi_set_opmode(3); wifi_set_opmode(3);
} }
log_uart(true); log_uart(true);
#ifdef CGIWIFI_DBG DBG("Enabling/continuing uart log\n");
os_printf("Enabling/continuing uart log\n");
#endif
os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0);
} }
} }
@ -310,17 +320,17 @@ static ETSTimer reassTimer;
// Callback actually doing reassociation // Callback actually doing reassociation
static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) { static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) {
#ifdef CGIWIFI_DBG DBG("Wifi changing association\n");
os_printf("Wifi changing association\n");
#endif
wifi_station_disconnect(); wifi_station_disconnect();
stconf.bssid_set = 0; stconf.bssid_set = 0;
wifi_station_set_config(&stconf); wifi_station_set_config(&stconf);
wifi_station_connect(); wifi_station_connect();
// Schedule check // Schedule check, we give some extra time (4x) 'cause the reassociation can cause the AP
// to have to change channel, and then the client needs to follow before it can see the
// IP address
os_timer_disarm(&resetTimer); os_timer_disarm(&resetTimer);
os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_setfn(&resetTimer, resetTimerCb, NULL);
os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); os_timer_arm(&resetTimer, 4*RESET_TIMEOUT, 0);
} }
// This cgi uses the routines above to connect to a specific access point with the // This cgi uses the routines above to connect to a specific access point with the
@ -338,14 +348,12 @@ int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) {
//Set to 0 if you want to disable the actual reconnecting bit //Set to 0 if you want to disable the actual reconnecting bit
os_strncpy((char*)stconf.ssid, essid, 32); os_strncpy((char*)stconf.ssid, essid, 32);
os_strncpy((char*)stconf.password, passwd, 64); os_strncpy((char*)stconf.password, passwd, 64);
#ifdef CGIWIFI_DBG DBG("Wifi try to connect to AP %s pw %s\n", essid, passwd);
os_printf("Wifi try to connect to AP %s pw %s\n", essid, passwd);
#endif
//Schedule disconnect/connect //Schedule disconnect/connect
os_timer_disarm(&reassTimer); os_timer_disarm(&reassTimer);
os_timer_setfn(&reassTimer, reassTimerCb, NULL); os_timer_setfn(&reassTimer, reassTimerCb, NULL);
os_timer_arm(&reassTimer, 1000, 0); os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it
jsonHeader(connData, 200); jsonHeader(connData, 200);
} else { } else {
jsonHeader(connData, 400); jsonHeader(connData, 400);
@ -394,16 +402,14 @@ static void ICACHE_FLASH_ATTR debugIP() {
#endif #endif
// configure Wifi, specifically DHCP vs static IP address based on flash config // configure Wifi, specifically DHCP vs static IP address based on flash config
static void ICACHE_FLASH_ATTR configWifiIP() { void ICACHE_FLASH_ATTR configWifiIP() {
if (flashConfig.staticip == 0) { if (flashConfig.staticip == 0) {
// let's DHCP! // let's DHCP!
wifi_station_set_hostname(flashConfig.hostname); wifi_station_set_hostname(flashConfig.hostname);
if (wifi_station_dhcpc_status() == DHCP_STARTED) if (wifi_station_dhcpc_status() == DHCP_STARTED)
wifi_station_dhcpc_stop(); wifi_station_dhcpc_stop();
wifi_station_dhcpc_start(); wifi_station_dhcpc_start();
#ifdef CGIWIFI_DBG DBG("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname);
os_printf("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname);
#endif
} else { } else {
// no DHCP, we got static network config! // no DHCP, we got static network config!
wifi_station_dhcpc_stop(); wifi_station_dhcpc_stop();
@ -412,9 +418,7 @@ static void ICACHE_FLASH_ATTR configWifiIP() {
ipi.netmask.addr = flashConfig.netmask; ipi.netmask.addr = flashConfig.netmask;
ipi.gw.addr = flashConfig.gateway; ipi.gw.addr = flashConfig.gateway;
wifi_set_ip_info(0, &ipi); wifi_set_ip_info(0, &ipi);
#ifdef CGIWIFI_DBG DBG("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr));
os_printf("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr));
#endif
} }
#ifdef DEBUGIP #ifdef DEBUGIP
debugIP(); debugIP();
@ -424,7 +428,6 @@ static void ICACHE_FLASH_ATTR configWifiIP() {
// Change special settings // Change special settings
int ICACHE_FLASH_ATTR cgiWiFiSpecial(HttpdConnData *connData) { int ICACHE_FLASH_ATTR cgiWiFiSpecial(HttpdConnData *connData) {
char dhcp[8]; char dhcp[8];
char hostname[32];
char staticip[20]; char staticip[20];
char netmask[20]; char netmask[20];
char gateway[20]; char gateway[20];
@ -433,12 +436,11 @@ int ICACHE_FLASH_ATTR cgiWiFiSpecial(HttpdConnData *connData) {
// get args and their string lengths // get args and their string lengths
int dl = httpdFindArg(connData->getArgs, "dhcp", dhcp, sizeof(dhcp)); int dl = httpdFindArg(connData->getArgs, "dhcp", dhcp, sizeof(dhcp));
int hl = httpdFindArg(connData->getArgs, "hostname", hostname, sizeof(hostname));
int sl = httpdFindArg(connData->getArgs, "staticip", staticip, sizeof(staticip)); int sl = httpdFindArg(connData->getArgs, "staticip", staticip, sizeof(staticip));
int nl = httpdFindArg(connData->getArgs, "netmask", netmask, sizeof(netmask)); int nl = httpdFindArg(connData->getArgs, "netmask", netmask, sizeof(netmask));
int gl = httpdFindArg(connData->getArgs, "gateway", gateway, sizeof(gateway)); int gl = httpdFindArg(connData->getArgs, "gateway", gateway, sizeof(gateway));
if (!(dl > 0 && hl >= 0 && sl >= 0 && nl >= 0 && gl >= 0)) { if (!(dl > 0 && sl >= 0 && nl >= 0 && gl >= 0)) {
jsonHeader(connData, 400); jsonHeader(connData, 400);
httpdSend(connData, "Request is missing fields", -1); httpdSend(connData, "Request is missing fields", -1);
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
@ -466,18 +468,16 @@ int ICACHE_FLASH_ATTR cgiWiFiSpecial(HttpdConnData *connData) {
os_sprintf(url, "{\"url\": \"http://%d.%d.%d.%d\"}", IP2STR(&ipi.ip)); os_sprintf(url, "{\"url\": \"http://%d.%d.%d.%d\"}", IP2STR(&ipi.ip));
} else { } else {
// no static IP, set hostname // dynamic IP
if (hl == 0) os_strcpy(hostname, "esp-link");
flashConfig.staticip = 0; flashConfig.staticip = 0;
os_strcpy(flashConfig.hostname, hostname); os_sprintf(url, "{\"url\": \"http://%s\"}", flashConfig.hostname);
os_sprintf(url, "{\"url\": \"http://%s\"}", hostname);
} }
configSave(); // ignore error... configSave(); // ignore error...
// schedule change-over // schedule change-over
os_timer_disarm(&reassTimer); os_timer_disarm(&reassTimer);
os_timer_setfn(&reassTimer, configWifiIP, NULL); os_timer_setfn(&reassTimer, configWifiIP, NULL);
os_timer_arm(&reassTimer, 1000, 0); os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it
// return redirect info // return redirect info
jsonHeader(connData, 200); jsonHeader(connData, 200);
httpdSend(connData, url, -1); httpdSend(connData, url, -1);
@ -494,13 +494,10 @@ int ICACHE_FLASH_ATTR cgiWiFiSetMode(HttpdConnData *connData) {
len=httpdFindArg(connData->getArgs, "mode", buff, sizeof(buff)); len=httpdFindArg(connData->getArgs, "mode", buff, sizeof(buff));
if (len!=0) { if (len!=0) {
int m = atoi(buff); int m = atoi(buff);
#ifdef CGIWIFI_DBG DBG("Wifi switching to mode %d\n", m);
os_printf("Wifi switching to mode %d\n", m);
#endif
wifi_set_opmode(m&3); wifi_set_opmode(m&3);
if (m == 1) { if (m == 1) {
wifi_set_sleep_type(SLEEP_MODE); // STA-only mode, reset into STA+AP after a timeout if we don't get an IP address
// STA-only mode, reset into STA+AP after a timeout
os_timer_disarm(&resetTimer); os_timer_disarm(&resetTimer);
os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_setfn(&resetTimer, resetTimerCb, NULL);
os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0);
@ -599,9 +596,7 @@ int ICACHE_FLASH_ATTR cgiWiFiConnStatus(HttpdConnData *connData) {
#endif #endif
len += os_sprintf(buff+len, "\"x\":0}\n"); len += os_sprintf(buff+len, "\"x\":0}\n");
#ifdef CGIWIFI_DBG //DBG(" -> %s\n", buff);
//os_printf(" -> %s\n", buff);
#endif
httpdSend(connData, buff, len); httpdSend(connData, buff, len);
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
@ -624,13 +619,16 @@ int ICACHE_FLASH_ATTR cgiWifiInfo(HttpdConnData *connData) {
// Init the wireless, which consists of setting a timer if we expect to connect to an AP // Init the wireless, which consists of setting a timer if we expect to connect to an AP
// so we can revert to STA+AP mode if we can't connect. // so we can revert to STA+AP mode if we can't connect.
void ICACHE_FLASH_ATTR wifiInit() { void ICACHE_FLASH_ATTR wifiInit() {
wifi_set_phy_mode(2); // wifi_set_phy_mode(2); // limit to 802.11b/g 'cause n is flaky
#ifdef CGIWIFI_DBG
int x = wifi_get_opmode() & 0x3; int x = wifi_get_opmode() & 0x3;
os_printf("Wifi init, mode=%s\n", wifiMode[x]); DBG("Wifi init, mode=%s\n", wifiMode[x]);
#endif
configWifiIP(); configWifiIP();
// The default sleep mode should be modem_sleep, but we set it here explicitly for good
// measure. We can't use light_sleep because that powers off everthing and we would loose
// all connections.
wifi_set_sleep_type(MODEM_SLEEP_T);
wifi_set_event_handler_cb(wifiHandleEventCb); wifi_set_event_handler_cb(wifiHandleEventCb);
// check on the wifi in a few seconds to see whether we need to switch mode // check on the wifi in a few seconds to see whether we need to switch mode
os_timer_disarm(&resetTimer); os_timer_disarm(&resetTimer);

@ -13,6 +13,7 @@ int cgiWiFiConnect(HttpdConnData *connData);
int cgiWiFiSetMode(HttpdConnData *connData); int cgiWiFiSetMode(HttpdConnData *connData);
int cgiWiFiConnStatus(HttpdConnData *connData); int cgiWiFiConnStatus(HttpdConnData *connData);
int cgiWiFiSpecial(HttpdConnData *connData); int cgiWiFiSpecial(HttpdConnData *connData);
void configWifiIP();
void wifiInit(void); void wifiInit(void);
void wifiAddStateChangeCb(WifiStateChangeCb cb); void wifiAddStateChangeCb(WifiStateChangeCb cb);

@ -22,6 +22,8 @@ FlashConfig flashDefault = {
2, 1, // mqtt_timeout, mqtt_clean_session 2, 1, // mqtt_timeout, mqtt_clean_session
1883, 60, // mqtt port, mqtt_keepalive 1883, 60, // mqtt port, mqtt_keepalive
"\0", "\0", "\0", "\0", "\0", // mqtt host, client_id, user, password, status-topic "\0", "\0", "\0", "\0", "\0", // mqtt host, client_id, user, password, status-topic
"\0", // system description
1, // rx_pullup
}; };
typedef union { typedef union {
@ -35,9 +37,15 @@ typedef union {
// size of the setting sector // size of the setting sector
#define FLASH_SECT (4096) #define FLASH_SECT (4096)
// address where to flash the settings: there are 16KB of reserved space at the end of the first // address where to flash the settings: if we have >512KB flash then there are 16KB of reserved
// flash partition, we use the upper 8KB (2 sectors) // space at the end of the first flash partition, we use the upper 8KB (2 sectors). If we only
#define FLASH_ADDR (FLASH_SECT + FIRMWARE_SIZE + 2*FLASH_SECT) // have 512KB then that space is used by the SDK and we use the 8KB just before that.
static uint32_t ICACHE_FLASH_ATTR flashAddr(void) {
enum flash_size_map map = system_get_flash_size_map();
return map >= FLASH_SIZE_8M_MAP_512_512
? FLASH_SECT + FIRMWARE_SIZE + 2*FLASH_SECT // bootloader + firmware + 8KB free
: FLASH_SECT + FIRMWARE_SIZE - 2*FLASH_SECT;// bootloader + firmware - 8KB (risky...)
}
static int flash_pri; // primary flash sector (0 or 1, or -1 for error) static int flash_pri; // primary flash sector (0 or 1, or -1 for error)
@ -56,7 +64,7 @@ bool ICACHE_FLASH_ATTR configSave(void) {
os_memcpy(&ff, &flashConfig, sizeof(FlashConfig)); os_memcpy(&ff, &flashConfig, sizeof(FlashConfig));
uint32_t seq = ff.fc.seq+1; uint32_t seq = ff.fc.seq+1;
// erase secondary // erase secondary
uint32_t addr = FLASH_ADDR + (1-flash_pri)*FLASH_SECT; uint32_t addr = flashAddr() + (1-flash_pri)*FLASH_SECT;
if (spi_flash_erase_sector(addr>>12) != SPI_FLASH_RESULT_OK) if (spi_flash_erase_sector(addr>>12) != SPI_FLASH_RESULT_OK)
goto fail; // no harm done, give up goto fail; // no harm done, give up
// calculate CRC // calculate CRC
@ -76,7 +84,7 @@ bool ICACHE_FLASH_ATTR configSave(void) {
if (spi_flash_write(addr, (void *)&ff, sizeof(uint32_t)) != SPI_FLASH_RESULT_OK) if (spi_flash_write(addr, (void *)&ff, sizeof(uint32_t)) != SPI_FLASH_RESULT_OK)
goto fail; // most likely failed, but no harm if successful goto fail; // most likely failed, but no harm if successful
// now that we have safely written the new version, erase old primary // now that we have safely written the new version, erase old primary
addr = FLASH_ADDR + flash_pri*FLASH_SECT; addr = flashAddr() + flash_pri*FLASH_SECT;
flash_pri = 1-flash_pri; flash_pri = 1-flash_pri;
if (spi_flash_erase_sector(addr>>12) != SPI_FLASH_RESULT_OK) if (spi_flash_erase_sector(addr>>12) != SPI_FLASH_RESULT_OK)
return true; // no back-up but we're OK return true; // no back-up but we're OK
@ -95,8 +103,8 @@ fail:
} }
void ICACHE_FLASH_ATTR configWipe(void) { void ICACHE_FLASH_ATTR configWipe(void) {
spi_flash_erase_sector(FLASH_ADDR>>12); spi_flash_erase_sector(flashAddr()>>12);
spi_flash_erase_sector((FLASH_ADDR+FLASH_SECT)>>12); spi_flash_erase_sector((flashAddr()+FLASH_SECT)>>12);
} }
static int ICACHE_FLASH_ATTR selectPrimary(FlashFull *fc0, FlashFull *fc1); static int ICACHE_FLASH_ATTR selectPrimary(FlashFull *fc0, FlashFull *fc1);
@ -104,9 +112,9 @@ static int ICACHE_FLASH_ATTR selectPrimary(FlashFull *fc0, FlashFull *fc1);
bool ICACHE_FLASH_ATTR configRestore(void) { bool ICACHE_FLASH_ATTR configRestore(void) {
FlashFull ff0, ff1; FlashFull ff0, ff1;
// read both flash sectors // read both flash sectors
if (spi_flash_read(FLASH_ADDR, (void *)&ff0, sizeof(ff0)) != SPI_FLASH_RESULT_OK) if (spi_flash_read(flashAddr(), (void *)&ff0, sizeof(ff0)) != SPI_FLASH_RESULT_OK)
os_memset(&ff0, 0, sizeof(ff0)); // clear in case of error os_memset(&ff0, 0, sizeof(ff0)); // clear in case of error
if (spi_flash_read(FLASH_ADDR+FLASH_SECT, (void *)&ff1, sizeof(ff1)) != SPI_FLASH_RESULT_OK) if (spi_flash_read(flashAddr()+FLASH_SECT, (void *)&ff1, sizeof(ff1)) != SPI_FLASH_RESULT_OK)
os_memset(&ff1, 0, sizeof(ff1)); // clear in case of error os_memset(&ff1, 0, sizeof(ff1)); // clear in case of error
// figure out which one is good // figure out which one is good
flash_pri = selectPrimary(&ff0, &ff1); flash_pri = selectPrimary(&ff0, &ff1);

@ -13,7 +13,7 @@ typedef struct {
char hostname[32]; // if using DHCP char hostname[32]; // if using DHCP
uint32_t staticip, netmask, gateway; // using DHCP if staticip==0 uint32_t staticip, netmask, gateway; // using DHCP if staticip==0
uint8_t log_mode; // UART log debug mode uint8_t log_mode; // UART log debug mode
uint8_t swap_uart; // swap uart0 to gpio 13&15 int8_t swap_uart; // swap uart0 to gpio 13&15
uint8_t tcp_enable, rssi_enable; // TCP client settings uint8_t tcp_enable, rssi_enable; // TCP client settings
char api_key[48]; // RSSI submission API key (Grovestreams for now) char api_key[48]; // RSSI submission API key (Grovestreams for now)
uint8_t slip_enable, mqtt_enable, // SLIP protocol, MQTT client uint8_t slip_enable, mqtt_enable, // SLIP protocol, MQTT client
@ -23,6 +23,8 @@ typedef struct {
uint16_t mqtt_port, mqtt_keepalive; // MQTT Host port, MQTT Keepalive timer uint16_t mqtt_port, mqtt_keepalive; // MQTT Host port, MQTT Keepalive timer
char mqtt_host[32], mqtt_clientid[48], mqtt_username[32], mqtt_password[32]; char mqtt_host[32], mqtt_clientid[48], mqtt_username[32], mqtt_password[32];
char mqtt_status_topic[32]; char mqtt_status_topic[32];
char sys_descr[129]; // system description
int8_t rx_pullup; // internal pull-up on RX pin
} FlashConfig; } FlashConfig;
extern FlashConfig flashConfig; extern FlashConfig flashConfig;

@ -6,6 +6,12 @@
#include "config.h" #include "config.h"
#include "log.h" #include "log.h"
#ifdef LOG_DBG
#define DBG(format, ...) os_printf(format, ## __VA_ARGS__)
#else
#define DBG(format, ...) do { } while(0)
#endif
// Web log for the esp8266 to replace outputting to uart1. // Web log for the esp8266 to replace outputting to uart1.
// The web log has a 1KB circular in-memory buffer which os_printf prints into and // The web log has a 1KB circular in-memory buffer which os_printf prints into and
// the HTTP handler simply displays the buffer content on a web page. // the HTTP handler simply displays the buffer content on a web page.
@ -18,28 +24,31 @@ static int log_pos;
static bool log_no_uart; // start out printing to uart static bool log_no_uart; // start out printing to uart
static bool log_newline; // at start of a new line static bool log_newline; // at start of a new line
// write to the uart designated for logging
static void uart_write_char(char c) {
if (flashConfig.log_mode == LOG_MODE_ON1)
uart1_write_char(c);
else
uart0_write_char(c);
}
// called from wifi reset timer to turn UART on when we loose wifi and back off // called from wifi reset timer to turn UART on when we loose wifi and back off
// when we connect to wifi AP. Here this is gated by the flash setting // when we connect to wifi AP. Here this is gated by the flash setting
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
log_uart(bool enable) { log_uart(bool enable) {
if (!enable && !log_no_uart && flashConfig.log_mode != LOG_MODE_ON) { if (!enable && !log_no_uart && flashConfig.log_mode < LOG_MODE_ON0) {
// we're asked to turn uart off, and uart is on, and the flash setting isn't always-on // we're asked to turn uart off, and uart is on, and the flash setting isn't always-on
#if 1 DBG("Turning OFF uart log\n");
#ifdef LOG_DBG
os_printf("Turning OFF uart log\n");
#endif
os_delay_us(4*1000L); // time for uart to flush os_delay_us(4*1000L); // time for uart to flush
log_no_uart = !enable; log_no_uart = !enable;
#endif
} else if (enable && log_no_uart && flashConfig.log_mode != LOG_MODE_OFF) { } else if (enable && log_no_uart && flashConfig.log_mode != LOG_MODE_OFF) {
// we're asked to turn uart on, and uart is off, and the flash setting isn't always-off // we're asked to turn uart on, and uart is off, and the flash setting isn't always-off
log_no_uart = !enable; log_no_uart = !enable;
#ifdef LOG_DBG DBG("Turning ON uart log\n");
os_printf("Turning ON uart log\n");
#endif
} }
} }
// write a character into the log buffer
static void ICACHE_FLASH_ATTR static void ICACHE_FLASH_ATTR
log_write(char c) { log_write(char c) {
log_buf[log_wr] = c; log_buf[log_wr] = c;
@ -50,34 +59,24 @@ log_write(char c) {
} }
} }
#if 0 // write a character to the log buffer and the uart, and handle newlines specially
static char ICACHE_FLASH_ATTR
log_read(void) {
char c = 0;
if (log_rd != log_wr) {
c = log_buf[log_rd];
log_rd = (log_rd+1) % BUF_MAX;
}
return c;
}
#endif
static void ICACHE_FLASH_ATTR static void ICACHE_FLASH_ATTR
log_write_char(char c) { log_write_char(char c) {
// Uart output unless disabled // log timestamp
if (!log_no_uart) {
if (log_newline) { if (log_newline) {
char buff[16]; char buff[16];
int l = os_sprintf(buff, "%6d> ", (system_get_time()/1000)%1000000); int l = os_sprintf(buff, "%6d> ", (system_get_time()/1000)%1000000);
for (int i=0; i<l; i++) if (!log_no_uart)
uart0_write_char(buff[i]); for (int i=0; i<l; i++) uart_write_char(buff[i]);
if (1) // set to 0 to remove timestamps from log buffer to save some space
for (int i=0; i<l; i++) log_write(buff[i]);
log_newline = false; log_newline = false;
} }
uart0_write_char(c); if (c == '\n') log_newline = true;
if (c == '\n') { // Uart output unless disabled
log_newline = true; if (!log_no_uart) {
uart0_write_char('\r'); if (c == '\n') uart_write_char('\r');
} uart_write_char(c);
} }
// Store in log buffer // Store in log buffer
if (c == '\n') log_write('\r'); if (c == '\n') log_write('\r');
@ -129,7 +128,7 @@ ajaxLog(HttpdConnData *connData) {
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
static char *dbg_mode[] = { "auto", "off", "on" }; static char *dbg_mode[] = { "auto", "off", "on0", "on1" };
int ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR
ajaxLogDbg(HttpdConnData *connData) { ajaxLogDbg(HttpdConnData *connData) {
@ -141,10 +140,11 @@ ajaxLogDbg(HttpdConnData *connData) {
int8_t mode = -1; int8_t mode = -1;
if (os_strcmp(buff, "auto") == 0) mode = LOG_MODE_AUTO; if (os_strcmp(buff, "auto") == 0) mode = LOG_MODE_AUTO;
if (os_strcmp(buff, "off") == 0) mode = LOG_MODE_OFF; if (os_strcmp(buff, "off") == 0) mode = LOG_MODE_OFF;
if (os_strcmp(buff, "on") == 0) mode = LOG_MODE_ON; if (os_strcmp(buff, "on0") == 0) mode = LOG_MODE_ON0;
if (os_strcmp(buff, "on1") == 0) mode = LOG_MODE_ON1;
if (mode >= 0) { if (mode >= 0) {
flashConfig.log_mode = mode; flashConfig.log_mode = mode;
if (mode != LOG_MODE_AUTO) log_uart(mode == LOG_MODE_ON); if (mode != LOG_MODE_AUTO) log_uart(mode >= LOG_MODE_ON0);
status = configSave() ? 200 : 400; status = configSave() ? 200 : 400;
} }
} else if (connData->requestType == HTTPD_METHOD_GET) { } else if (connData->requestType == HTTPD_METHOD_GET) {

@ -3,9 +3,10 @@
#include "httpd.h" #include "httpd.h"
#define LOG_MODE_AUTO 0 #define LOG_MODE_AUTO 0 // start by logging to uart0, turn aff after we get an IP
#define LOG_MODE_OFF 1 #define LOG_MODE_OFF 1 // always off
#define LOG_MODE_ON 2 #define LOG_MODE_ON0 2 // always log to uart0
#define LOG_MODE_ON1 3 // always log to uart1
void logInit(void); void logInit(void);
void log_uart(bool enable); void log_uart(bool enable);

@ -19,6 +19,7 @@
#include "cgitcp.h" #include "cgitcp.h"
#include "cgimqtt.h" #include "cgimqtt.h"
#include "cgiflash.h" #include "cgiflash.h"
#include "cgioptiboot.h"
#include "auth.h" #include "auth.h"
#include "espfs.h" #include "espfs.h"
#include "uart.h" #include "uart.h"
@ -30,6 +31,9 @@
#include "log.h" #include "log.h"
#include <gpio.h> #include <gpio.h>
static int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData);
static int ICACHE_FLASH_ATTR cgiSystemSet(HttpdConnData *connData);
/* /*
This is the main url->function dispatching data struct. This is the main url->function dispatching data struct.
In short, it's a struct with various URLs plus their handlers. The handlers can In short, it's a struct with various URLs plus their handlers. The handlers can
@ -46,11 +50,14 @@ HttpdBuiltInUrl builtInUrls[] = {
{ "/flash/next", cgiGetFirmwareNext, NULL }, { "/flash/next", cgiGetFirmwareNext, NULL },
{ "/flash/upload", cgiUploadFirmware, NULL }, { "/flash/upload", cgiUploadFirmware, NULL },
{ "/flash/reboot", cgiRebootFirmware, NULL }, { "/flash/reboot", cgiRebootFirmware, NULL },
{ "/pgm/sync", cgiOptibootSync, NULL },
{ "/pgm/upload", cgiOptibootData, NULL },
{ "/log/text", ajaxLog, NULL }, { "/log/text", ajaxLog, NULL },
{ "/log/dbg", ajaxLogDbg, NULL }, { "/log/dbg", ajaxLogDbg, NULL },
{ "/console/reset", ajaxConsoleReset, NULL }, { "/console/reset", ajaxConsoleReset, NULL },
{ "/console/baud", ajaxConsoleBaud, NULL }, { "/console/baud", ajaxConsoleBaud, NULL },
{ "/console/text", ajaxConsole, NULL }, { "/console/text", ajaxConsole, NULL },
{ "/console/send", ajaxConsoleSend, NULL },
//Enable the line below to protect the WiFi configuration with an username/password combo. //Enable the line below to protect the WiFi configuration with an username/password combo.
// {"/wifi/*", authBasic, myPassFn}, // {"/wifi/*", authBasic, myPassFn},
{ "/wifi", cgiRedirect, "/wifi/wifi.html" }, { "/wifi", cgiRedirect, "/wifi/wifi.html" },
@ -61,8 +68,9 @@ HttpdBuiltInUrl builtInUrls[] = {
{ "/wifi/connstatus", cgiWiFiConnStatus, NULL }, { "/wifi/connstatus", cgiWiFiConnStatus, NULL },
{ "/wifi/setmode", cgiWiFiSetMode, NULL }, { "/wifi/setmode", cgiWiFiSetMode, NULL },
{ "/wifi/special", cgiWiFiSpecial, NULL }, { "/wifi/special", cgiWiFiSpecial, NULL },
{ "/system/info", cgiSystemInfo, NULL },
{ "/system/update", cgiSystemSet, NULL },
{ "/pins", cgiPins, NULL }, { "/pins", cgiPins, NULL },
{ "/tcpclient", cgiTcp, NULL },
#ifdef MQTT #ifdef MQTT
{ "/mqtt", cgiMqtt, NULL }, { "/mqtt", cgiMqtt, NULL },
#endif #endif
@ -94,6 +102,61 @@ static char *flash_maps[] = {
"2MB:1024/1024", "4MB:1024/1024" "2MB:1024/1024", "4MB:1024/1024"
}; };
// Cgi to return various System information
static int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData) {
char buff[1024];
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
uint8 part_id = system_upgrade_userbin_check();
uint32_t fid = spi_flash_get_id();
struct rst_info *rst_info = system_get_rst_info();
os_sprintf(buff, "{\"name\": \"%s\", \"reset cause\": \"%d=%s\", "
"\"size\": \"%s\"," "\"id\": \"0x%02lX 0x%04lX\"," "\"partition\": \"%s\","
"\"slip\": \"%s\"," "\"mqtt\": \"%s/%s\"," "\"baud\": \"%ld\","
"\"description\": \"%s\"" "}",
flashConfig.hostname, rst_info->reason, rst_codes[rst_info->reason],
flash_maps[system_get_flash_size_map()], fid & 0xff, (fid&0xff00)|((fid>>16)&0xff),
part_id ? "user2.bin" : "user1.bin",
flashConfig.slip_enable ? "enabled" : "disabled",
flashConfig.mqtt_enable ? "enabled" : "disabled",
mqttState(), flashConfig.baud_rate, flashConfig.sys_descr
);
jsonHeader(connData, 200);
httpdSend(connData, buff, -1);
return HTTPD_CGI_DONE;
}
static ETSTimer reassTimer;
// Cgi to update system info (name/description)
static int ICACHE_FLASH_ATTR cgiSystemSet(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
int8_t n = getStringArg(connData, "name", flashConfig.hostname, sizeof(flashConfig.hostname));
int8_t d = getStringArg(connData, "description", flashConfig.sys_descr, sizeof(flashConfig.sys_descr));
if (n < 0 || d < 0) return HTTPD_CGI_DONE; // getStringArg has produced an error response
if (n > 0) {
// schedule hostname change-over
os_timer_disarm(&reassTimer);
os_timer_setfn(&reassTimer, configWifiIP, NULL);
os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it
}
if (configSave()) {
httpdStartResponse(connData, 204);
httpdEndHeaders(connData);
} else {
httpdStartResponse(connData, 500);
httpdEndHeaders(connData);
httpdSend(connData, "Failed to save config", -1);
}
return HTTPD_CGI_DONE;
}
extern void app_init(void); extern void app_init(void);
extern void mqtt_client_init(void); extern void mqtt_client_init(void);
@ -109,7 +172,7 @@ void user_init(void) {
bool restoreOk = configRestore(); bool restoreOk = configRestore();
// init gpio pin registers // init gpio pin registers
gpio_init(); gpio_init();
gpio_output_set(0, 0, 0, (1<<15)); // some people tie it GND, gotta ensure it's disabled gpio_output_set(0, 0, 0, (1<<15)); // some people tie it to GND, gotta ensure it's disabled
// init UART // init UART
uart_init(flashConfig.baud_rate, 115200); uart_init(flashConfig.baud_rate, 115200);
logInit(); // must come after init of uart logInit(); // must come after init of uart

@ -0,0 +1,44 @@
/* STK500 constants list, from AVRDUDE
*
* Trivial set of constants derived from Atmel App Note AVR061
* Not copyrighted. Released to the public domain.
*/
#define STK_OK 0x10
#define STK_FAILED 0x11 // Not used
#define STK_UNKNOWN 0x12 // Not used
#define STK_NODEVICE 0x13 // Not used
#define STK_INSYNC 0x14 // ' '
#define STK_NOSYNC 0x15 // Not used
#define ADC_CHANNEL_ERROR 0x16 // Not used
#define ADC_MEASURE_OK 0x17 // Not used
#define PWM_CHANNEL_ERROR 0x18 // Not used
#define PWM_ADJUST_OK 0x19 // Not used
#define CRC_EOP 0x20 // 'SPACE'
#define STK_GET_SYNC 0x30 // '0'
#define STK_GET_SIGN_ON 0x31 // '1'
#define STK_SET_PARAMETER 0x40 // '@'
#define STK_GET_PARAMETER 0x41 // 'A'
#define STK_SET_DEVICE 0x42 // 'B'
#define STK_SET_DEVICE_EXT 0x45 // 'E'
#define STK_ENTER_PROGMODE 0x50 // 'P'
#define STK_LEAVE_PROGMODE 0x51 // 'Q'
#define STK_CHIP_ERASE 0x52 // 'R'
#define STK_CHECK_AUTOINC 0x53 // 'S'
#define STK_LOAD_ADDRESS 0x55 // 'U'
#define STK_UNIVERSAL 0x56 // 'V'
#define STK_PROG_FLASH 0x60 // '`'
#define STK_PROG_DATA 0x61 // 'a'
#define STK_PROG_FUSE 0x62 // 'b'
#define STK_PROG_LOCK 0x63 // 'c'
#define STK_PROG_PAGE 0x64 // 'd'
#define STK_PROG_FUSE_EXT 0x65 // 'e'
#define STK_READ_FLASH 0x70 // 'p'
#define STK_READ_DATA 0x71 // 'q'
#define STK_READ_FUSE 0x72 // 'r'
#define STK_READ_LOCK 0x73 // 's'
#define STK_READ_PAGE 0x74 // 't'
#define STK_READ_SIGN 0x75 // 'u'
#define STK_READ_OSCCAL 0x76 // 'v'
#define STK_READ_FUSE_EXT 0x77 // 'w'
#define STK_READ_OSCCAL_EXT 0x78 // 'x'

@ -1,18 +1,70 @@
<div id="main"> <div id="main" class="flex-fill flex-vbox" style="max-height:100%">
<div class="header"> <div class="header">
<h1>Microcontroller Console</h1> <h1>Microcontroller Console</h1>
</div> </div>
<div class="content"> <div class="content flex-fill flex-vbox">
<p>The Microcontroller console shows the last 1024 characters
received from UART0, to which a microcontroller is typically attached.
The UART is configured for 8 bits, no parity, 1 stop bit (8N1).</p>
<p> <p>
<a id="reset-button" class="pure-button button-primary" href="#">Reset µC</a> <a id="reset-button" class="pure-button button-primary" href="#">Reset µC</a>
&nbsp;Baud: &nbsp;&nbsp;Baud:
<span id="baud-btns"></span> <select id="baud-sel" class="pure-button" href="#">
<option value="460800">460800</option>
<option value="250000">250000</option>
<option value="230400">230400</option>
<option value="115200">115200</option>
<option value="57600">57600</option>
<option value="38400">38400</option>
<option value="19200">19200</option>
<option value="9600">9600</option>
</select>
<!--
&nbsp;&nbsp;Pgm baud:
<select id="baud-pgm" class="pure-button">
<option value="same">same</option>
<option value="460800">460800</option>
<option value="250000">250000</option>
<option value="230400">230400</option>
<option value="115200">115200</option>
<option value="57600">57600</option>
<option value="38400">38400</option>
<option value="19200">19200</option>
<option value="9600">9600</option>
</select> -->
&nbsp;&nbsp;Fmt: 8N1
</p> </p>
<pre class="console" id="console"></pre> <div class="pure-g">
<div class="pure-u-1-4"><legend><b>Console</b></legend></div>
<div class="pure-u-3-4"></div>
</div>
<pre class="console flex-fill" id="console">--- No Content ---</pre>
<div>
<div class="pure-g">
<div class="pure-u-1-4"><legend><b>Console entry</b></legend></div>
<div class="pure-u-2-4">
<legend>(ENTER to submit, ESC to clear)</legend>
</div>
<div class="pure-u-1-4">
<legend>Add:
<input type="checkbox" id="input-add-cr" checked class="inline"><label>CR(\r)</label>
<input type="checkbox" id="input-add-lf" checked class="inline"><label>LF(\n)</label>
</legend>
</div>
</div>
<div class="pure-g">
<div class="pure-u-1-1">
<span style="float:right; width:10px;"></span>
<input type="text" class="console-in" id="input-text" value="">
</div>
</div>
<div class="pure-g">
<div class="pure-u-1-4"><legend><b>History buffer</b></legend></div>
<div class="pure-u-2-4"><legend>(UP/DOWN arrows to select)</legend></div>
<div class="pure-u-1-4"></div>
</div>
<div class="pure-g">
<div class="pure-u-1-1"><select class="console-in" id="send-history" size="5"></select></div>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -20,8 +72,6 @@
<script type="text/javascript">console_url = "/console/text"</script> <script type="text/javascript">console_url = "/console/text"</script>
<script src="console.js"></script> <script src="console.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var rates = [9600, 57600, 115200, 250000];
onLoad(function() { onLoad(function() {
fetchText(100, true); fetchText(100, true);
@ -35,12 +85,26 @@
); );
}); });
rates.forEach(function(r) { baudButton(r); });
ajaxJson('GET', "/console/baud", ajaxJson('GET', "/console/baud",
function(data) { showRate(data.rate); }, function(data) { $("#baud-sel").value = data.rate; },
function(s, st) { showNotification(st); } function(s, st) { showNotification(st); }
); );
bnd($("#baud-sel"), "change", function(ev) {
ev.preventDefault();
var baud = $("#baud-sel").value;
ajaxSpin('POST', "/console/baud?rate="+baud,
function(resp) { showNotification("" + baud + " baud set"); },
function(s, st) { showWarning("Error setting baud rate: " + st); }
);
});
consoleSendInit();
addClass($('html')[0], "height100");
addClass($('body')[0], "height100");
addClass($('#layout'), "height100");
addClass($('#layout'), "flex-vbox");
}); });
</script> </script>
</body></html> </body></html>

@ -1,3 +1,5 @@
//===== Fetching console text
function fetchText(delay, repeat) { function fetchText(delay, repeat) {
var el = $("#console"); var el = $("#console");
if (el.textEnd == undefined) { if (el.textEnd == undefined) {
@ -20,12 +22,21 @@ function updateText(resp) {
var delay = 3000; var delay = 3000;
if (resp != null && resp.len > 0) { if (resp != null && resp.len > 0) {
console.log("updateText got", resp.len, "chars at", resp.start); console.log("updateText got", resp.len, "chars at", resp.start);
var isScrolledToBottom = el.scrollHeight - el.clientHeight <= el.scrollTop + 1;
//console.log("isScrolledToBottom="+isScrolledToBottom, "scrollHeight="+el.scrollHeight,
// "clientHeight="+el.clientHeight, "scrollTop="+el.scrollTop,
// "" + (el.scrollHeight - el.clientHeight) + "<=" + (el.scrollTop + 1));
// append the text
if (resp.start > el.textEnd) { if (resp.start > el.textEnd) {
el.innerHTML = el.innerHTML.concat("\r\n<missing lines\r\n"); el.innerHTML = el.innerHTML.concat("\r\n<missing lines\r\n");
} }
el.innerHTML = el.innerHTML.concat(resp.text); el.innerHTML = el.innerHTML.concat(resp.text);
el.textEnd = resp.start + resp.len; el.textEnd = resp.start + resp.len;
delay = 500; delay = 500;
// scroll to bottom
if(isScrolledToBottom) el.scrollTop = el.scrollHeight - el.clientHeight;
} }
return delay; return delay;
} }
@ -34,28 +45,93 @@ function retryLoad(repeat) {
fetchText(1000, repeat); fetchText(1000, repeat);
} }
//===== Console page //===== Text entry
function showRate(rate) { function consoleSendInit() {
rates.forEach(function(r) { var sendHistory = $("#send-history");
var el = $("#"+r+"-button"); var inputText = $("#input-text");
el.className = el.className.replace(" button-selected", ""); var inputAddCr = $("#input-add-cr");
}); var inputAddLf = $("#input-add-lf");
var el = $("#"+rate+"-button"); function findHistory(text) {
if (el != null) el.className += " button-selected"; for (var i = 0; i < sendHistory.children.length; i++) {
} if (text == sendHistory.children[i].value) {
return i;
}
}
return null;
}
function baudButton(baud) { function loadHistory(idx) {
$("#baud-btns").appendChild(m( sendHistory.value = sendHistory.children[idx].value;
' <a id="'+baud+'-button" href="#" class="pure-button">'+baud+'</a>')); inputText.value = sendHistory.children[idx].value;
}
$("#"+baud+"-button").addEventListener("click", function(e) { function navHistory(rel) {
var idx = findHistory(sendHistory.value) + rel;
if (idx < 0) {
idx = sendHistory.children.length - 1;
}
if (idx >= sendHistory.children.length) {
idx = 0;
}
loadHistory(idx);
}
sendHistory.addEventListener("change", function(e) {
inputText.value = sendHistory.value;
});
function pushHistory(text) {
var idx = findHistory(text);
if (idx !== null) {
loadHistory(idx);
return false;
}
var newOption = m('<option>'+
(text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;'))
+'</option>');
newOption.value = text;
sendHistory.appendChild(newOption);
sendHistory.value = text;
for (; sendHistory.children.length > 15; ) {
sendHistory.removeChild(sendHistory.children[0]);
}
return true;
}
inputText.addEventListener("keydown", function(e) {
switch (e.keyCode) {
case 38: /* the up arrow key pressed */
e.preventDefault();
navHistory(-1);
break;
case 40: /* the down arrow key pressed */
e.preventDefault();
navHistory(+1);
break;
case 27: /* the escape key pressed */
e.preventDefault(); e.preventDefault();
ajaxSpin('POST', "/console/baud?rate="+baud, inputText.value = "";
function(resp) { showNotification("" + baud + " baud set"); showRate(baud); }, sendHistory.value = "";
function(s, st) { showWarning("Error setting baud rate: " + st); } break;
case 13: /* the enter key pressed */
e.preventDefault();
var text = inputText.value;
if (inputAddCr.checked) text += '\r';
if (inputAddLf.checked) text += '\n';
pushHistory(inputText.value);
inputText.value = "";
ajaxSpin('POST', "/console/send?text=" + encodeURIComponent(text),
function(resp) { showNotification("Text sent"); },
function(s, st) { showWarning("Error sending text"); }
); );
break;
}
}); });
} }

@ -7,46 +7,133 @@
<div class="content"> <div class="content">
<div class="pure-g"> <div class="pure-g">
<div class="pure-u-1"><div class="card"> <!-- LEFT COLUMN -->
<p>The JeeLabs esp-link firmware bridges the ESP8266 serial port to Wifi and can <div class="pure-u-1 pure-u-md-1-2">
<div class="card">
<h1>System overview</h1>
<div id="wifi-spinner" class="spinner spinner-small"></div>
<table id="wifi-table" class="pure-table pure-table-horizontal" hidden><tbody>
<tr><td class="popup-target">Hostname</td><td>
<div class="click-to-edit system-name">
<span class="edit-off"></span>
<input class="edit-on" maxlength=31 hidden></input>
<div class="popup">Click to edit!<br>Hostname displayed in menu bar
and used by DHCP and mDNS</div>
</div>
</td></tr>
<tr><td>Network SSID</td><td id="wifi-ssid"></td></tr>
<tr><td>Wifi status</td><td id="wifi-status"></td></tr>
<tr><td>Wifi address</td><td id="wifi-ip"></td></tr>
<tr><td>SLIP status</td><td class="system-slip"></td></tr>
<tr><td>MQTT status</td><td class="system-mqtt"></td></tr>
<tr><td>Serial baud</td><td class="system-baud"></td></tr>
</tbody></table>
</div>
<div class="card">
<h1>Info</h1>
<p style="margin-bottom:0;">The JeeLabs esp-link firmware bridges the ESP8266
serial port to Wifi and can
program microcontrollers over the serial port, in particular Arduinos, AVRs, and program microcontrollers over the serial port, in particular Arduinos, AVRs, and
NXP's LPC800 and other ARM processors.</p> NXP's LPC800 and other ARM processors. Typical avrdude command line to
<p style="margin-bottom:0;">Program an Arduino/AVR using avrdude using a command program an Arduino:</p>
line similar to:</p> <div class="tt" style="font-size:100%;">
<div class="tt">/home/arduino-1.0.5/hardware/tools/avrdude \<br> /home/arduino/hardware/tools/avrdude&nbsp;\<br>
&nbsp;&nbsp;-DV -patmega328p -Pnet:esp-link.local:23 -carduino -b115200 -U \<br> &nbsp;&nbsp;-DV -patmega328p \<br>
&nbsp;&nbsp;-C /home/arduino-1.0.5/hardware/tools/avrdude.conf flash:w:my_sketch.hex:i &nbsp;&nbsp;-Pnet:esp-link.local:23 \<br>
&nbsp;&nbsp;-carduino -b115200 -U -C \<br>
&nbsp;&nbsp;/home/arduino/hardware/tools/avrdude.conf&nbsp;\<br>
&nbsp;&nbsp;flash:w:my_sketch.hex:i
</div> </div>
<p>where <tt>-Pnet:esp-link.local:23</tt> tells avrdude to connect to port 23 of esp-link. <p>where <tt>-Pnet:esp-link.local:23</tt> tells avrdude to connect to port 23 of esp-link.
You can substitute the IP address of your esp-link for esp-link.local if necessary.</p> You can substitute the IP address of your esp-link for esp-link.local if necessary.
<p>Please refer to Please refer to
<a href="https://github.com/jeelabs/esp-link/blob/master/README.md">the online README</a> <a href="https://github.com/jeelabs/esp-link/blob/master/README.md">the online README</a>
for up-to-date help and to the forthcoming for up-to-date help.</p>
<a href="http://jeelabs.org">JeeLabs blog</a> for an intro to the codebase.</p>
</div></div>
</div> </div>
<div class="pure-g"> </div>
<!-- RIGHT COLUMN -->
<div class="pure-u-1 pure-u-md-1-2"> <div class="pure-u-1 pure-u-md-1-2">
<div class="card"> <div class="card">
<h1>Wifi summary</h1> <h1>Pin assignment</h1>
<div id="wifi-spinner" class="spinner spinner-small"></div> <div id="pin-spinner" class="spinner spinner-small"></div>
<table id="wifi-table" class="pure-table pure-table-horizontal" hidden><tbody> <div id="pin-table" hidden>
<form action="#" id="pinform" class="pure-form pure-form-aligned form-narrow">
<div class="pure-control-group">
<label for="pin-preset">Presets</label><select id="pin-preset" class="pure-button">
<option value="" selected disabled></option>
</select></label>
</div>
<hr>
<div class="pure-control-group">
<label for="pin-reset">Reset</label>
<select id="pin-reset"></select>
<div class="popup">Connect to uC reset pin for programming and reset-uC function</div>
</div>
<div class="pure-control-group">
<label for="pin-isp">ISP/Flash</label>
<select id="pin-isp"></select>
<div class="popup">Second signal to program uC.
AVR:not used, esp8266:gpio2, ARM:ISP</div>
</div>
<div class="pure-control-group">
<label for="pin-conn">Conn LED</label>
<select id="pin-conn"></select>
<div class="popup">LED to show wifi connectivity</div>
</div>
<div class="pure-control-group">
<label for="pin-ser">Serial LED</label>
<select id="pin-ser"></select>
<div class="popup">LED to show serial activity</div>
</div>
<div class="pure-control-group">
<label for="pin-swap">UART pins</label>
<select id="pin-swap" class="pure-button">
<option value="0">normal</option>
<option value="1">swapped</option>
</select>
<div class="popup">Swap UART0 pins to avoid ROM boot message.<br>Normal is
TX on gpio1/TX0 and RX on gpio3/RX0, swapped is TX on gpio15 and RX on gpio13.
</div>
</div>
<div class="pure-control-group">
<label for="pin-rxpup" class="pure-checkbox">RX pull-up</label>
<input id="pin-rxpup" type="checkbox">
<div class="popup">Enable internal 40K pull-up on RX</div>
</div>
<button id="set-pins" type="submit" class="pure-button button-primary">Change!</button>
</form>
</div>
</div>
<div class="card">
<h1>System details</h1>
<div id="system-spinner" class="spinner spinner-small"></div>
<table id="system-table" class="pure-table pure-table-horizontal" hidden><tbody>
<tr><td>WiFi mode</td><td id="wifi-mode"></td></tr> <tr><td>WiFi mode</td><td id="wifi-mode"></td></tr>
<tr><td>Configured network</td><td id="wifi-ssid"></td></tr>
<tr><td>Wifi channel</td><td id="wifi-chan"></td></tr> <tr><td>Wifi channel</td><td id="wifi-chan"></td></tr>
<tr><td>Wifi status</td><td id="wifi-status"></td></tr> <tr><td>Flash chip ID</td><td>
<tr><td>Wifi address</td><td id="wifi-ip"></td></tr> <div>
<tr><td>Configured hostname</td><td id="wifi-hostname"></td></tr> <span class="system-id"></span>
</tbody> </table> <div class="popup pop-left">Common IDs: 4016=4MB, 4014=1MB, 4013=512KB</div>
</div>
</td></tr>
<tr><td>Flash size</td><td>
<div>
<span class="system-size"></span>
<div class="popup pop-left">Size configured into bootloader, must match chip size</div>
</div>
</td></tr>
<tr><td>Current partition</td><td class="system-partition"></td></tr>
<tr><td colspan=2 class="popup-target">Description:<br>
<div class="click-to-edit system-description">
<div class="edit-off"></div>
<textarea class="edit-on" rows=3 maxlength=127 hidden> </textarea>
<div class="popup">Click to edit!<br>A short description or memo for this esp-link
module, 128 chars max</div>
</div>
</td></tr>
</tbody></table>
</div> </div>
</div> </div>
<div class="pure-u-1 pure-u-md-1-2"><div class="card">
<h1>Pin assignment</h1>
<legend>Select one of the following signal/pin assignments to match your hardware</legend>
<fieldset class='radios' id='pin-mux'>
<div class="spinner spinner-small"></div>
</fieldset>
</div></div>
</div> </div>
<div class="pure-g"> <div class="pure-g">
</div> </div>
@ -56,8 +143,12 @@
<script type="text/javascript"> <script type="text/javascript">
onLoad(function() { onLoad(function() {
makeAjaxInput("system", "description");
makeAjaxInput("system", "name");
fetchPins(); fetchPins();
getWifiInfo(); getWifiInfo();
getSystemInfo();
bnd($("#pinform"), "submit", setPins);
}); });
</script> </script>
</body></html> </body></html>

@ -14,7 +14,8 @@
UART debug log: UART debug log:
<a id="dbg-auto" class="dbg-btn pure-button" href="#">auto</a> <a id="dbg-auto" class="dbg-btn pure-button" href="#">auto</a>
<a id="dbg-off" class="dbg-btn pure-button" href="#">off</a> <a id="dbg-off" class="dbg-btn pure-button" href="#">off</a>
<a id="dbg-on" class="dbg-btn pure-button" href="#">on</a> <a id="dbg-on0" class="dbg-btn pure-button" href="#">on uart0</a>
<a id="dbg-on1" class="dbg-btn pure-button" href="#">on uart1</a>
</p> </p>
</div> </div>
<pre id="console" class="console" style="margin-top: 0px;"></pre> <pre id="console" class="console" style="margin-top: 0px;"></pre>
@ -33,7 +34,7 @@
fetchText(100, false); fetchText(100, false);
}); });
["auto", "off", "on"].forEach(function(mode) { ["auto", "off", "on0", "on1"].forEach(function(mode) {
bnd($('#dbg-'+mode), "click", function(el) { bnd($('#dbg-'+mode), "click", function(el) {
ajaxJsonSpin('POST', "/log/dbg?mode="+mode, ajaxJsonSpin('POST', "/log/dbg?mode="+mode,
function(data) { showNotification("UART mode " + data.mode); showDbgMode(data.mode); }, function(data) { showNotification("UART mode " + data.mode); showDbgMode(data.mode); },

@ -3,7 +3,7 @@ html, button, input, select, textarea, .pure-g [class *= "pure-u"] {
font-family: sans-serif; font-family: sans-serif;
} }
input[type="text"], input[type="password"] { input[type="text"], input[type="password"], textarea {
width: 100%; width: 100%;
} }
@ -26,12 +26,89 @@ a:hover {
background-color: #eee; background-color: #eee;
padding: 1em; padding: 1em;
margin: 0.5em; margin: 0.5em;
-moz-border-radius: 0.5em;
-webkit-border-radius: 0.5em;
border-radius: 0.5em; border-radius: 0.5em;
border: 0px solid #000000; border: 0px solid #000000;
} }
/* click-to-edit fields */
.click-to-edit {
position: relative;
}
.edit-off {
cursor: pointer;
}
.click-to-edit input, .click-to-edit textarea {
color: black;
background-color: #eee;
width: 100%;
}
.click-to-edit span, .click-to-edit div {
/*background-color: #e0e0e0;*/
/*color: #1c0099;*/
padding: 0.3em;
width: 100%;
color: #444; /* rgba not supported (IE 8) */
color: rgba(0, 0, 0, 0.80); /* rgba supported */
border: 1px solid #999; /*IE 6/7/8*/
border: none rgba(0, 0, 0, 0); /*IE9 + everything else*/
background-color: #E6E6E6;
text-decoration: none;
border-radius: 2px;
}
.click-to-edit span:hover, .click-to-edit div:hover,
.click-to-edit span:focus, .click-to-edit div:focus {
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#00000000', endColorstr='#1a000000',GradientType=0);
background-image: -webkit-gradient(linear, 0 0, 0 100%, from(transparent), color-stop(40%, rgba(0,0,0, 0.05)), to(rgba(0,0,0, 0.10)));
background-image: -webkit-linear-gradient(transparent, rgba(0,0,0, 0.05) 40%, rgba(0,0,0, 0.10));
background-image: -moz-linear-gradient(top, rgba(0,0,0, 0.05) 0%, rgba(0,0,0, 0.10));
background-image: -o-linear-gradient(transparent, rgba(0,0,0, 0.05) 40%, rgba(0,0,0, 0.10));
background-image: linear-gradient(transparent, rgba(0,0,0, 0.05) 40%, rgba(0,0,0, 0.10));
}
.click-to-edit span:focus, .click-to-edit div:focus {
outline: 0;
}
.click-to-edit span:active, .click-to-edit div:active {
box-shadow: 0 0 0 1px rgba(0,0,0, 0.15) inset, 0 0 6px rgba(0,0,0, 0.20) inset;
border-color: #000\9;
}
/* Firefox: Get rid of the inner focus border */
.click-to-edit span::-moz-focus-inner,
.click-to-edit div::-moz-focus-inner {
padding: 0;
border: 0;
}
/* pop-ups */
.popup-hidden {
display: none;
}
.popup, div.popup {
position: absolute;
/*top: 100%;*/
bottom: 100%;
background-color: #fff0b3;
border-radius: 5px;
border: 0px solid #000;
color: #333;
font-size: 80%;
line-height: 110%;
z-index: 100;
padding: 5px;
min-width: 20em;
}
.popup:not(.pop-left) {
left: 20px;
}
.popup.pop-left {
right: 20px;
}
.popup-target:hover .popup {
display: block;
}
.popup-target {
position: relative;
}
/* wifi AP selection form */ /* wifi AP selection form */
#aps label div { #aps label div {
display: inline-block; display: inline-block;
@ -59,6 +136,16 @@ fieldset fields {
padding: 0.5em 0.5em; padding: 0.5em 0.5em;
} }
/* Narrow left-aligned Forms */
.pure-form-aligned.form-narrow .pure-control-group label {
text-align: left;
width: 6em;
}
.pure-form-aligned.form-narrow input[type=checkbox],
.pure-form-aligned.form-narrow input[type=radio] {
float: none;
}
/* make images size-up */ /* make images size-up */
.xx-pure-img-responsive { .xx-pure-img-responsive {
max-width: 100%; max-width: 100%;
@ -68,9 +155,6 @@ fieldset fields {
/* Add transition to containers so they can push in and out */ /* Add transition to containers so they can push in and out */
#layout, #menu, .menu-link { #layout, #menu, .menu-link {
-webkit-transition: all 0.2s ease-out; -webkit-transition: all 0.2s ease-out;
-moz-transition: all 0.2s ease-out;
-ms-transition: all 0.2s ease-out;
-o-transition: all 0.2s ease-out;
transition: all 0.2s ease-out; transition: all 0.2s ease-out;
} }
@ -103,8 +187,11 @@ div.tt {
margin: 0 auto; margin: 0 auto;
padding: 0 2em; padding: 0 2em;
max-width: 800px; max-width: 800px;
margin-bottom: 50px; margin-bottom: 20px;
line-height: 1.6em; line-height: 1.6em;
width: 100%;
box-sizing: border-box;
overflow: auto;
} }
.header { .header {
@ -146,22 +233,91 @@ form button {
.button-selected { .button-selected {
background-color: #fc6; background-color: #fc6;
} }
select.pure-button {
padding: 0.465em 1em;
color: #009; /* same as a:link */
}
.button-small {
font-size: 75%;
background: #ccc;
}
input.inline {
float:none;
margin-right: 0px;
margin-left: 0.5em;
}
/* Text console */ /* Text console */
pre.console { pre.console {
background-color: #663300; background-color: #663300;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px; border-radius: 5px;
border: 0px solid #000000; border: 0px solid #000000;
color: #66ff66; color: #66ff66;
padding: 5px; padding: 5px;
overflow: scroll;
margin: 0px;
} }
pre.console a { pre.console a {
color: #66ff66; color: #66ff66;
} }
.console-in {
background-color: #fff0b3;
border-radius: 5px;
border: 0px solid #630;
color: #0c0;
padding: 5px;
width: 100%;
height: 100%;
box-sizing: border-box;
}
.console-in option:checked {
background-image: -webkit-linear-gradient(#fc0, #fc0);
background-image: linear-gradient(#fc0, #fc0);
}
/* console flex */
.flex-hbox, .flex-vbox {
display: -webkit-box;
display: flex;
display: -ms-flexbox;
display: -webkit-flex;
}
.flex-hbox {
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
ms-flex-direction: row;
webkit-flex-direction: row;
}
.flex-vbox {
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
ms-flex-direction: column;
webkit-flex-direction: column;
/*width: 100%; */
}
.flex-fill {
-webkit-box-flex: 1;
-webkit-flex: 1 1 auto;
-ms-flex: 1 1 auto;
flex: 1 1 auto;
ms-flex: 1 1 auto;
webkit-flex: 1 1 auto;
}
.height100 {
height: 100%;
}
/* log page */ /* log page */
.dbg-btn, #refresh-button { .dbg-btn, #refresh-button {
vertical-align: baseline; vertical-align: baseline;
@ -184,7 +340,7 @@ pre.console a {
bottom: 0; bottom: 0;
z-index: 1000; z-index: 1000;
background: #191818; background: #191818;
overflow-y: auto; overflow: visible;
-webkit-overflow-scrolling: touch; -webkit-overflow-scrolling: touch;
} }
@ -339,8 +495,6 @@ pre.console a {
height: 50px; height: 50px;
width: 50px; width: 50px;
-webkit-animation: rotation 1s infinite linear; -webkit-animation: rotation 1s infinite linear;
-moz-animation: rotation 1s infinite linear;
-o-animation: rotation 1s infinite linear;
animation: rotation 1s infinite linear; animation: rotation 1s infinite linear;
border-left: 10px solid rgba(204, 51, 0, 0.15); border-left: 10px solid rgba(204, 51, 0, 0.15);
border-right: 10px solid rgba(204, 51, 0, 0.15); border-right: 10px solid rgba(204, 51, 0, 0.15);
@ -363,27 +517,14 @@ pre.console a {
-webkit-transform: rotate(359deg); -webkit-transform: rotate(359deg);
} }
} }
@-moz-keyframes rotation {
from {
-moz-transform: rotate(0deg);
}
to {
-moz-transform: rotate(359deg);
}
}
@-o-keyframes rotation {
from {
-o-transform: rotate(0deg);
}
to {
-o-transform: rotate(359deg);
}
}
@keyframes rotation { @keyframes rotation {
from { from {
-webkit-transform: rotate(0deg);
transform: rotate(0deg); transform: rotate(0deg);
} }
to { to {
-webkit-transform: rotate(359deg);
transform: rotate(359deg); transform: rotate(359deg);
} }
} }

@ -110,6 +110,9 @@ var j = function(
catch(e){} // ignore when it fails. catch(e){} // ignore when it fails.
} }
// dom element iterator: domForEach($(".some-class"), function(el) { ... });
function domForEach(els, fun) { return Array.prototype.forEach.call(els, fun); }
// createElement short-hand // createElement short-hand
e = function(a) { return document.createElement(a); } e = function(a) { return document.createElement(a); }
@ -210,6 +213,11 @@ function ajaxJsonSpin(method, url, ok_cb, err_cb) {
//===== main menu, header spinner and notification boxes //===== main menu, header spinner and notification boxes
function hidePopup(el) {
addClass(el, "popup-hidden");
addClass(el.parentNode, "popup-target");
}
onLoad(function() { onLoad(function() {
var l = $("#layout"); var l = $("#layout");
var o = l.childNodes[0]; var o = l.childNodes[0];
@ -226,6 +234,7 @@ onLoad(function() {
<div class="pure-menu">\ <div class="pure-menu">\
<a class="pure-menu-heading" href="https://github.com/jeelabs/esp-link">\ <a class="pure-menu-heading" href="https://github.com/jeelabs/esp-link">\
<img src="/favicon.ico" height="32">&nbsp;esp-link</a>\ <img src="/favicon.ico" height="32">&nbsp;esp-link</a>\
<div class="pure-menu-heading system-name" style="padding: 0px 0.6em"></div>\
<ul id="menu-list" class="pure-menu-list"></ul>\ <ul id="menu-list" class="pure-menu-list"></ul>\
</div>\ </div>\
</div>\ </div>\
@ -243,6 +252,11 @@ onLoad(function() {
toggleClass(ml, active); toggleClass(ml, active);
}); });
// hide pop-ups
domForEach($(".popup"), function(el) {
hidePopup(el);
});
// populate menu via ajax call // populate menu via ajax call
var getMenu = function() { var getMenu = function() {
ajaxJson("GET", "/menu", function(data) { ajaxJson("GET", "/menu", function(data) {
@ -256,8 +270,10 @@ onLoad(function() {
} }
$("#menu-list").innerHTML = html; $("#menu-list").innerHTML = html;
v = $("#version"); var v = $("#version");
if (v != null) { v.innerHTML = data.version; } if (v != null) { v.innerHTML = data.version; }
setEditToClick("system-name", data["name"]);
}, function() { setTimeout(getMenu, 1000); }); }, function() { setTimeout(getMenu, 1000); });
}; };
getMenu(); getMenu();
@ -285,12 +301,79 @@ function getWifiInfo() {
function(s, st) { window.setTimeout(getWifiInfo, 1000); }); function(s, st) { window.setTimeout(getWifiInfo, 1000); });
} }
//===== System info
function setEditToClick(klass, value) {
domForEach($("."+klass), function(div) {
if (div.children.length > 0) {
domForEach(div.children, function(el) {
if (el.nodeName === "INPUT") el.value = value;
else if (el.nodeName !== "DIV") el.innerHTML = value;
});
} else {
div.innerHTML = value;
}
});
}
function showSystemInfo(data) {
Object.keys(data).forEach(function(v) {
setEditToClick("system-"+v, data[v]);
});
$("#system-spinner").setAttribute("hidden", "");
$("#system-table").removeAttribute("hidden");
currAp = data.ssid;
}
function getSystemInfo() {
ajaxJson('GET', "/system/info", showSystemInfo,
function(s, st) { window.setTimeout(getSystemInfo, 1000); });
}
function makeAjaxInput(klass, field) {
domForEach($("."+klass+"-"+field), function(div) {
var eon = $(".edit-on", div);
var eoff = $(".edit-off", div)[0];
var url = "/"+klass+"/update?"+field;
if (eoff === undefined || eon == undefined) return;
var enableEditToClick = function() {
eoff.setAttribute('hidden','');
domForEach(eon, function(el){ el.removeAttribute('hidden'); });
eon[0].select();
return false;
}
var submitEditToClick = function(v) {
console.log("Submit POST "+url+"="+v);
ajaxSpin("POST", url+"="+v, function() {
domForEach(eon, function(el){ el.setAttribute('hidden',''); });
eoff.removeAttribute('hidden');
setEditToClick(klass+"-"+field, v)
showNotification(field + " changed to " + v);
}, function() {
showWarning(field + " change failed");
});
return false;
}
bnd(eoff, "click", function(){return enableEditToClick();});
bnd(eon[0], "blur", function(){return submitEditToClick(eon[0].value);});
bnd(eon[0], "keyup", function(ev){
if ((ev||window.event).keyCode==13) return submitEditToClick(eon[0].value);
});
});
}
//===== Notifications //===== Notifications
function showWarning(text) { function showWarning(text) {
var el = $("#warning"); var el = $("#warning");
el.innerHTML = text; el.innerHTML = text;
el.removeAttribute('hidden'); el.removeAttribute('hidden');
window.scrollTo(0, 0);
} }
function hideWarning() { function hideWarning() {
el = $("#warning").setAttribute('hidden', ''); el = $("#warning").setAttribute('hidden', '');
@ -309,36 +392,71 @@ function showNotification(text) {
//===== GPIO Pin mux card //===== GPIO Pin mux card
var currPin; var pinPresets = {
// pin={reset:12, isp:13, LED_conn:0, LED_ser:2} // array: reset, isp, conn, ser, swap, rxpup
function createInputForPin(pin) { "esp-01": [ 0, -1, 2, -1, 0, 1 ],
var input = document.createElement("input"); "esp-12": [ 12, 14, 0, 2, 0, 1 ],
input.type = "radio"; "esp-12 swap": [ 1, 3, 0, 2, 1, 1 ],
input.name = "pins"; "esp-bridge": [ 12, 13, 0, 14, 0, 0 ],
input.data = pin.name; "wifi-link-12": [ 1, 3, 0, 2, 1, 0 ],
input.className = "pin-input"; };
input.value= pin.value;
input.id = "opt-" + pin.value; function createPresets(sel) {
if (currPin == pin.name) input.checked = "1"; for (var p in pinPresets) {
var opt = m('<option value="' + p + '">' + p + '</option>');
var descr = m('<label for="opt-'+pin.value+'"><b>'+pin.name+":</b>"+pin.descr+"</label>"); sel.appendChild(opt);
var div = document.createElement("div"); }
div.appendChild(input);
div.appendChild(descr); function applyPreset(v) {
return div; var pp = pinPresets[v];
if (pp === undefined) return pp;
console.log("apply preset:", v, pp);
function setPP(k, v) { $("#pin-"+k).value = v; };
setPP("reset", pp[0]);
setPP("isp", pp[1]);
setPP("conn", pp[2]);
setPP("ser", pp[3]);
setPP("swap", pp[4]);
$("#pin-rxpup").checked = !!pp[5];
sel.value = 0;
};
bnd(sel, "change", function(ev) {
ev.preventDefault();
applyPreset(sel.value);
});
} }
function displayPins(resp) { function displayPins(resp) {
var po = $("#pin-mux"); function createSelectForPin(name, v) {
po.innerHTML = ""; var sel = $("#pin-"+name);
currPin = resp.curr; addClass(sel, "pure-button");
resp.map.forEach(function(v) { sel.innerHTML = "";
po.appendChild(createInputForPin(v)); [-1,0,1,2,3,4,5,12,13,14,15].forEach(function(i) {
var opt = document.createElement("option");
opt.value = i;
if (i >= 0) opt.innerHTML = "gpio"+i;
else opt.innerHTML = "disabled";
if (i===1) opt.innerHTML += "/TX0";
if (i===2) opt.innerHTML += "/TX1";
if (i===3) opt.innerHTML += "/RX0";
if (i==v) opt.selected = true;
sel.appendChild(opt);
}); });
var i, inputs = $(".pin-input"); var pup = $(".popup", sel.parentNode);
for (i=0; i<inputs.length; i++) { if (pup !== undefined) hidePopup(pup[0]);
inputs[i].onclick = function() { setPins(this.value, this.data) };
}; };
createSelectForPin("reset", resp["reset"]);
createSelectForPin("isp", resp["isp"]);
createSelectForPin("conn", resp["conn"]);
createSelectForPin("ser", resp["ser"]);
$("#pin-swap").value = resp["swap"];
$("#pin-rxpup").checked = !!resp["rxpup"];
createPresets($("#pin-preset"));
$("#pin-spinner").setAttribute("hidden", "");
$("#pin-table").removeAttribute("hidden");
} }
function fetchPins() { function fetchPins() {
@ -347,11 +465,20 @@ function fetchPins() {
}); });
} }
function setPins(v, name) { function setPins(ev) {
ajaxSpin("POST", "/pins?map="+v, function() { ev.preventDefault();
showNotification("Pin assignment changed to " + name); var url = "/pins";
}, function() { var sep = "?";
showNotification("Pin assignment change failed"); ["reset", "isp", "conn", "ser", "swap"].forEach(function(p) {
url += sep + p + "=" + $("#pin-"+p).value;
sep = "&";
});
url += "&rxpup=" + ($("#pin-rxpup").selected ? "1" : "0");
console.log("set pins: " + url);
ajaxSpin("POST", url, function() {
showNotification("Pin assignment changed");
}, function(status, errMsg) {
showWarning(errMsg);
window.setTimeout(fetchPins, 100); window.setTimeout(fetchPins, 100);
}); });
} }

@ -28,6 +28,11 @@
enter the password, and hit the connect button...</legend> enter the password, and hit the connect button...</legend>
<label>Network SSID</label> <label>Network SSID</label>
<div id="aps">Scanning... <div class="spinner spinner-small"></div></div> <div id="aps">Scanning... <div class="spinner spinner-small"></div></div>
<label for="opt-hiddenssid">
<input type="radio" name="essid" value="_hidden_ssid_" id="opt-hiddenssid">
<input type="text" id="hidden-ssid" value=""
style="width:auto; display:inline-block; margin-left: 0.7em">
</label>
<label>WiFi password, if applicable:</label> <label>WiFi password, if applicable:</label>
<input id="wifi-passwd" type="password" name="passwd" placeholder="password"> <input id="wifi-passwd" type="password" name="passwd" placeholder="password">
<button id="connect-button" type="submit" class="pure-button button-primary">Connect!</button> <button id="connect-button" type="submit" class="pure-button button-primary">Connect!</button>
@ -47,10 +52,7 @@
<input type="radio" name="dhcp" value="off" id="dhcp-roff"/> <input type="radio" name="dhcp" value="off" id="dhcp-roff"/>
Static IP</label> Static IP</label>
</div> </div>
<div id="dhcp-on" class="pure-form-stacked"> <div id="dhcp-on" class="pure-form-stacked"></div>
<label>Hostname when requesting DHCP lease</label>
<input id="wifi-hostname" type="text" name="hostname"/>
</div>
<div id="dhcp-off" class="pure-form-stacked"> <div id="dhcp-off" class="pure-form-stacked">
<label>Static IP address</label> <label>Static IP address</label>
<input id="wifi-staticip" type="text" name="staticip"/> <input id="wifi-staticip" type="text" name="staticip"/>

@ -41,7 +41,11 @@ function createInputForAp(ap) {
function getSelectedEssid() { function getSelectedEssid() {
var e = document.forms.wifiform.elements; var e = document.forms.wifiform.elements;
for (var i=0; i<e.length; i++) { for (var i=0; i<e.length; i++) {
if (e[i].type == "radio" && e[i].checked) return e[i].value; if (e[i].type == "radio" && e[i].checked) {
var v = e[i].value;
if (v == "_hidden_ssid_") v = $("#hidden-ssid").value;
return v;
}
} }
return currAp; return currAp;
} }
@ -56,7 +60,7 @@ function scanResult() {
scanReqCnt += 1; scanReqCnt += 1;
ajaxJson('GET', "scan", function(data) { ajaxJson('GET', "scan", function(data) {
currAp = getSelectedEssid(); currAp = getSelectedEssid();
if (data.result.inProgress == "0" && data.result.APs.length > 1) { if (data.result.inProgress == "0" && data.result.APs.length > 0) {
$("#aps").innerHTML = ""; $("#aps").innerHTML = "";
var n = 0; var n = 0;
for (var i=0; i<data.result.APs.length; i++) { for (var i=0; i<data.result.APs.length; i++) {
@ -171,7 +175,6 @@ function changeSpecial(e) {
e.preventDefault(); e.preventDefault();
var url = "special"; var url = "special";
url += "?dhcp=" + document.querySelector('input[name="dhcp"]:checked').value; url += "?dhcp=" + document.querySelector('input[name="dhcp"]:checked').value;
url += "&hostname=" + encodeURIComponent($("#wifi-hostname").value);
url += "&staticip=" + encodeURIComponent($("#wifi-staticip").value); url += "&staticip=" + encodeURIComponent($("#wifi-staticip").value);
url += "&netmask=" + encodeURIComponent($("#wifi-netmask").value); url += "&netmask=" + encodeURIComponent($("#wifi-netmask").value);
url += "&gateway=" + encodeURIComponent($("#wifi-gateway").value); url += "&gateway=" + encodeURIComponent($("#wifi-gateway").value);

@ -17,6 +17,8 @@ Esp8266 http server - core routines
#include <esp8266.h> #include <esp8266.h>
#include "httpd.h" #include "httpd.h"
//#define HTTPD_DBG
//Max length of request head //Max length of request head
#define MAX_HEAD_LEN 1024 #define MAX_HEAD_LEN 1024
@ -111,9 +113,15 @@ static void ICACHE_FLASH_ATTR httpdRetireConn(HttpdConnData *conn) {
uint32 dt = conn->startTime; uint32 dt = conn->startTime;
if (dt > 0) dt = (system_get_time() - dt) / 1000; if (dt > 0) dt = (system_get_time() - dt) / 1000;
if (conn->conn && conn->url) if (conn->conn && conn->url)
#if 0
os_printf("HTTP %s %s from %s -> %d in %ums, heap=%ld\n", os_printf("HTTP %s %s from %s -> %d in %ums, heap=%ld\n",
conn->requestType == HTTPD_METHOD_GET ? "GET" : "POST", conn->url, conn->priv->from, conn->requestType == HTTPD_METHOD_GET ? "GET" : "POST", conn->url, conn->priv->from,
conn->priv->code, dt, (unsigned long)system_get_free_heap_size()); conn->priv->code, dt, (unsigned long)system_get_free_heap_size());
#else
os_printf("HTTP %s %s: %d, %ums, h=%ld\n",
conn->requestType == HTTPD_METHOD_GET ? "GET" : "POST", conn->url,
conn->priv->code, dt, (unsigned long)system_get_free_heap_size());
#endif
#endif #endif
conn->conn = NULL; // don't try to send anything, the SDK crashes... conn->conn = NULL; // don't try to send anything, the SDK crashes...
@ -285,7 +293,8 @@ static void ICACHE_FLASH_ATTR xmitSendBuff(HttpdConnData *conn) {
sint8 status = espconn_sent(conn->conn, (uint8_t*)conn->priv->sendBuff, conn->priv->sendBuffLen); sint8 status = espconn_sent(conn->conn, (uint8_t*)conn->priv->sendBuff, conn->priv->sendBuffLen);
if (status != 0) { if (status != 0) {
#ifdef HTTPD_DBG #ifdef HTTPD_DBG
os_printf("%sERROR! espconn_sent returned %d\n", connStr, status); os_printf("%sERROR! espconn_sent returned %d, trying to send %d to %s\n",
connStr, status, conn->priv->sendBuffLen, conn->url);
#endif #endif
} }
conn->priv->sendBuffLen = 0; conn->priv->sendBuffLen = 0;
@ -341,6 +350,7 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) {
//See if we can find a CGI that's happy to handle the request. //See if we can find a CGI that's happy to handle the request.
while (1) { while (1) {
//Look up URL in the built-in URL table. //Look up URL in the built-in URL table.
if (conn->cgi == NULL) {
while (builtInUrls[i].url != NULL) { while (builtInUrls[i].url != NULL) {
int match = 0; int match = 0;
//See if there's a literal match //See if there's a literal match
@ -365,9 +375,11 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) {
#endif #endif
httpdSend(conn, httpNotFoundHeader, -1); httpdSend(conn, httpNotFoundHeader, -1);
xmitSendBuff(conn); xmitSendBuff(conn);
conn->cgi = NULL; //mark for destruction conn->cgi = NULL; //mark for destruction.
if (conn->post) conn->post->len = 0; // skip any remaining receives
return; return;
} }
}
//Okay, we have a CGI function that matches the URL. See if it wants to handle the //Okay, we have a CGI function that matches the URL. See if it wants to handle the
//particular URL we're supposed to handle. //particular URL we're supposed to handle.
@ -380,12 +392,17 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) {
else if (r == HTTPD_CGI_DONE) { else if (r == HTTPD_CGI_DONE) {
//Yep, it's happy to do so and already is done sending data. //Yep, it's happy to do so and already is done sending data.
xmitSendBuff(conn); xmitSendBuff(conn);
conn->cgi = NULL; //mark conn for destruction conn->cgi = NULL; //mark for destruction.
if (conn->post) conn->post->len = 0; // skip any remaining receives
return; return;
} }
else if (r == HTTPD_CGI_NOTFOUND || r == HTTPD_CGI_AUTHENTICATED) { else {
if (!(r == HTTPD_CGI_NOTFOUND || r == HTTPD_CGI_AUTHENTICATED)) {
os_printf("%shandler for %s returned invalid result %d\n", connStr, conn->url, r);
}
//URL doesn't want to handle the request: either the data isn't found or there's no //URL doesn't want to handle the request: either the data isn't found or there's no
//need to generate a login screen. //need to generate a login screen.
conn->cgi = NULL; // force lookup again
i++; //look at next url the next iteration of the loop. i++; //look at next url the next iteration of the loop.
} }
} }

@ -20,6 +20,7 @@ void ets_isr_unmask(unsigned intr);
int ets_memcmp(const void *s1, const void *s2, size_t n); int ets_memcmp(const void *s1, const void *s2, size_t n);
void *ets_memcpy(void *dest, const void *src, size_t n); void *ets_memcpy(void *dest, const void *src, size_t n);
void *ets_memmove(void *dest, const void *src, size_t n);
void *ets_memset(void *s, int c, size_t n); void *ets_memset(void *s, int c, size_t n);
int ets_sprintf(char *str, const char *format, ...) __attribute__ ((format (printf, 2, 3))); int ets_sprintf(char *str, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
int ets_str2macaddr(void *, void *); int ets_str2macaddr(void *, void *);

@ -81,6 +81,23 @@ ajaxConsoleBaud(HttpdConnData *connData) {
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
int ICACHE_FLASH_ATTR
ajaxConsoleSend(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
char buff[2048];
int len, status = 400;
// figure out where to start in buffer based on URI param
len = httpdFindArg(connData->getArgs, "text", buff, sizeof(buff));
if (len > 0) {
uart0_tx_buffer(buff, len);
status = 200;
}
jsonHeader(connData, status);
return HTTPD_CGI_DONE;
}
int ICACHE_FLASH_ATTR int ICACHE_FLASH_ATTR
ajaxConsole(HttpdConnData *connData) { ajaxConsole(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.

@ -8,6 +8,7 @@ void ICACHE_FLASH_ATTR console_write_char(char c);
int ajaxConsole(HttpdConnData *connData); int ajaxConsole(HttpdConnData *connData);
int ajaxConsoleReset(HttpdConnData *connData); int ajaxConsoleReset(HttpdConnData *connData);
int ajaxConsoleBaud(HttpdConnData *connData); int ajaxConsoleBaud(HttpdConnData *connData);
int ajaxConsoleSend(HttpdConnData *connData);
int tplConsole(HttpdConnData *connData, char *token, void **arg); int tplConsole(HttpdConnData *connData, char *token, void **arg);
#endif #endif

@ -20,6 +20,8 @@ static int8_t mcu_reset_pin, mcu_isp_pin;
extern uint8_t slip_disabled; // disable slip to allow flashing of attached MCU extern uint8_t slip_disabled; // disable slip to allow flashing of attached MCU
void (*programmingCB)(char *buffer, short length) = NULL;
// Connection pool // Connection pool
serbridgeConnData connData[MAX_CONN]; serbridgeConnData connData[MAX_CONN];
@ -97,7 +99,7 @@ telnetUnwrap(uint8_t *inBuf, int len, uint8_t state)
os_delay_us(100L); os_delay_us(100L);
} }
#ifdef SERBR_DBG #ifdef SERBR_DBG
else os_printf("MCU reset: no pin\n"); else { os_printf("MCU reset: no pin\n"); }
#endif #endif
break; break;
case DTR_OFF: case DTR_OFF:
@ -115,7 +117,7 @@ telnetUnwrap(uint8_t *inBuf, int len, uint8_t state)
os_delay_us(100L); os_delay_us(100L);
} }
#ifdef SERBR_DBG #ifdef SERBR_DBG
else os_printf("MCU isp: no pin\n"); else { os_printf("MCU isp: no pin\n"); }
#endif #endif
slip_disabled++; slip_disabled++;
break; break;
@ -143,11 +145,11 @@ serbridgeReset()
os_printf("MCU reset gpio%d\n", mcu_reset_pin); os_printf("MCU reset gpio%d\n", mcu_reset_pin);
#endif #endif
GPIO_OUTPUT_SET(mcu_reset_pin, 0); GPIO_OUTPUT_SET(mcu_reset_pin, 0);
os_delay_us(100L); os_delay_us(2000L); // esp8266 needs at least 1ms reset pulse, it seems...
GPIO_OUTPUT_SET(mcu_reset_pin, 1); GPIO_OUTPUT_SET(mcu_reset_pin, 1);
} }
#ifdef SERBR_DBG #ifdef SERBR_DBG
else os_printf("MCU reset: no pin\n"); else { os_printf("MCU reset: no pin\n"); }
#endif #endif
} }
@ -209,7 +211,7 @@ serbridgeRecvCb(void *arg, char *data, unsigned short len)
if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 0); if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 0);
os_delay_us(100L); os_delay_us(100L);
if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 0); if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 0);
os_delay_us(100L); os_delay_us(2000L);
if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 1); if (mcu_reset_pin >= 0) GPIO_OUTPUT_SET(mcu_reset_pin, 1);
//os_delay_us(100L); //os_delay_us(100L);
//if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 1); //if (mcu_isp_pin >= 0) GPIO_OUTPUT_SET(mcu_isp_pin, 1);
@ -318,7 +320,7 @@ static void ICACHE_FLASH_ATTR
serbridgeSentCb(void *arg) serbridgeSentCb(void *arg)
{ {
serbridgeConnData *conn = ((struct espconn*)arg)->reverse; serbridgeConnData *conn = ((struct espconn*)arg)->reverse;
os_printf("Sent CB %p\n", conn); //os_printf("Sent CB %p\n", conn);
if (conn == NULL) return; if (conn == NULL) return;
//os_printf("%d ST\n", system_get_time()); //os_printf("%d ST\n", system_get_time());
if (conn->sentbuffer != NULL) os_free(conn->sentbuffer); if (conn->sentbuffer != NULL) os_free(conn->sentbuffer);
@ -346,7 +348,9 @@ console_process(char *buf, short len)
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
serbridgeUartCb(char *buf, short length) serbridgeUartCb(char *buf, short length)
{ {
if (!flashConfig.slip_enable || slip_disabled > 0) { if (programmingCB) {
programmingCB(buf, length);
} else if (!flashConfig.slip_enable || slip_disabled > 0) {
//os_printf("SLIP: disabled got %d\n", length); //os_printf("SLIP: disabled got %d\n", length);
console_process(buf, length); console_process(buf, length);
} else { } else {
@ -398,7 +402,7 @@ serbridgeConnectCb(void *arg)
int i; int i;
for (i=0; i<MAX_CONN; i++) if (connData[i].conn==NULL) break; for (i=0; i<MAX_CONN; i++) if (connData[i].conn==NULL) break;
#ifdef SERBR_DBG #ifdef SERBR_DBG
os_printf("Accept port 23, conn=%p, pool slot %d\n", conn, i); os_printf("Accept port %d, conn=%p, pool slot %d\n", conn->proto.tcp->local_port, conn, i);
#endif #endif
if (i==MAX_CONN) { if (i==MAX_CONN) {
@ -442,11 +446,15 @@ serbridgeInitPins()
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 4); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 4);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 4); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 4);
PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTCK_U); PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTCK_U);
PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDO_U); if (flashConfig.rx_pullup) PIN_PULLUP_EN(PERIPHS_IO_MUX_MTDO_U);
else PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDO_U);
system_uart_swap(); system_uart_swap();
} else { } else {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, 0); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, 0);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, 0); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, 0);
PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0TXD_U);
if (flashConfig.rx_pullup) PIN_PULLUP_EN(PERIPHS_IO_MUX_U0RXD_U);
else PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0RXD_U);
system_uart_de_swap(); system_uart_de_swap();
} }

@ -36,4 +36,7 @@ void ICACHE_FLASH_ATTR serbridgeInitPins(void);
void ICACHE_FLASH_ATTR serbridgeUartCb(char *buf, short len); void ICACHE_FLASH_ATTR serbridgeUartCb(char *buf, short len);
void ICACHE_FLASH_ATTR serbridgeReset(); void ICACHE_FLASH_ATTR serbridgeReset();
// callback when receiving UART chars when in programming mode
extern void (*programmingCB)(char *buffer, short length);
#endif /* __SER_BRIDGE_H__ */ #endif /* __SER_BRIDGE_H__ */

@ -29,6 +29,7 @@ void ICACHE_FLASH_ATTR serledFlash(int duration) {
} }
void ICACHE_FLASH_ATTR serledInit(void) { void ICACHE_FLASH_ATTR serledInit(void) {
return;
int8_t pin = flashConfig.ser_led_pin; int8_t pin = flashConfig.ser_led_pin;
if (pin >= 0) { if (pin >= 0) {
makeGpio(pin); makeGpio(pin);

@ -52,17 +52,14 @@ uart_config(uint8 uart_no)
{ {
if (uart_no == UART1) { if (uart_no == UART1) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK); PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
//PIN_PULLDWN_DIS(PERIPHS_IO_MUX_GPIO2_U);
PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO2_U); PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO2_U);
} else { } else {
/* rcv_buff size is 0x100 */ /* rcv_buff size is 0x100 */
ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, &(UartDev.rcv_buff)); ETS_UART_INTR_ATTACH(uart0_rx_intr_handler, &(UartDev.rcv_buff));
PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0TXD_U);
//PIN_PULLDWN_DIS(PERIPHS_IO_MUX_U0TXD_U);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD);
PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0RXD_U);
//PIN_PULLDWN_DIS(PERIPHS_IO_MUX_U0RXD_U);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, 0); // FUNC_U0RXD==0 PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, 0); // FUNC_U0RXD==0
//PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0TXD_U); now done in serbridgeInitPins
//PIN_PULLUP_DIS (PERIPHS_IO_MUX_U0RXD_U);
} }
uart_div_modify(uart_no, UART_CLK_FREQ / UartDev.baut_rate); uart_div_modify(uart_no, UART_CLK_FREQ / UartDev.baut_rate);
@ -242,6 +239,23 @@ uart_recvTask(os_event_t *events)
ETS_UART_INTR_ENABLE(); ETS_UART_INTR_ENABLE();
} }
// Turn UART interrupts off and poll for nchars or until timeout hits
uint16_t ICACHE_FLASH_ATTR
uart0_rx_poll(char *buff, uint16_t nchars, uint32_t timeout_us) {
ETS_UART_INTR_DISABLE();
uint16_t got = 0;
uint32_t start = system_get_time(); // time in us
while (system_get_time()-start < timeout_us) {
while (READ_PERI_REG(UART_STATUS(UART0)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
buff[got++] = READ_PERI_REG(UART_FIFO(UART0)) & 0xFF;
if (got == nchars) goto done;
}
}
done:
ETS_UART_INTR_ENABLE();
return got;
}
void ICACHE_FLASH_ATTR void ICACHE_FLASH_ATTR
uart0_baud(int rate) { uart0_baud(int rate) {
os_printf("UART %d baud\n", rate); os_printf("UART %d baud\n", rate);

@ -8,19 +8,24 @@ typedef void (*UartRecv_cb)(char *buf, short len);
// Initialize UARTs to the provided baud rates (115200 recommended). This also makes the os_printf // Initialize UARTs to the provided baud rates (115200 recommended). This also makes the os_printf
// calls use uart1 for output (for debugging purposes) // calls use uart1 for output (for debugging purposes)
void ICACHE_FLASH_ATTR uart_init(UartBautRate uart0_br, UartBautRate uart1_br); void uart_init(UartBautRate uart0_br, UartBautRate uart1_br);
// Transmit a buffer of characters on UART0 // Transmit a buffer of characters on UART0
void ICACHE_FLASH_ATTR uart0_tx_buffer(char *buf, uint16 len); void uart0_tx_buffer(char *buf, uint16 len);
void ICACHE_FLASH_ATTR uart0_write_char(char c); void uart0_write_char(char c);
STATUS uart_tx_one_char(uint8 uart, uint8 c); STATUS uart_tx_one_char(uint8 uart, uint8 c);
void uart1_write_char(char c);
// Add a receive callback function, this is called on the uart receive task each time a chunk // Add a receive callback function, this is called on the uart receive task each time a chunk
// of bytes are received. A small number of callbacks can be added and they are all called // of bytes are received. A small number of callbacks can be added and they are all called
// with all new characters. // with all new characters.
void ICACHE_FLASH_ATTR uart_add_recv_cb(UartRecv_cb cb); void uart_add_recv_cb(UartRecv_cb cb);
// Turn UART interrupts off and poll for nchars or until timeout hits
uint16_t uart0_rx_poll(char *buff, uint16_t nchars, uint32_t timeout_us);
void ICACHE_FLASH_ATTR uart0_baud(int rate); void uart0_baud(int rate);
#endif /* __UART_H__ */ #endif /* __UART_H__ */

Loading…
Cancel
Save