SlideShare a Scribd company logo
1 of 35
Download to read offline
ESP8266 Workshop 1
Andy Gelme (@geekscape)
http://geekscape.org

Wednesday 2015-03-11
Copyright (c) 2015 by Geekscape Pty. Ltd. License CC-BY-NC-4.0
Workshop overview
• ESP8266 introduction: hardware and software
• ESP-01 and ESP-12 set-up for development
• Flashing firmware using esptool
• NodeMCU and Lua introduction
• ESPlorer IDE
• Using a button and LED(s)
• Setting up as a Wi-Fi client
• Using UDP to send and receive messages
• Creating an MQTT client
ESP8266 Hardware
• 3.3V device, operating current ~ 215 mA
• CPU: Tensilca Xtensa LX3: 32-bit, 80 MHz
• ESP8266 SOC: Expressif
• RAM 32Kb, DRAM 80Kb, Flash 200Kb (for SDK)
• Wi-Fi 802.11 b/g/n 2.4 GHz radio (station or AP)
• Timers, deep sleep mode, JTAG debugging
• Peripherals …
• GPIO (upto 16), PWM (3), ADC (one)
• UART, I2C, SPI
ESP8266 Memory Map
• More complicated than your AVR ATMega / Arduino
• SRAM 32Kb
• NodeMCU heap memory available: print(node.heap())
• SPI Flash ROM layout: Since SDK 0.8 …
• 00000h 248k app.v6.flash.bin User application

3E000h 8k master_device_key.bin OTA device key

40000h 240k app.v6.irom0text.bin SDK libraries

7C000h 8k esp_init_data_default.bin Default configuration

7E000h 8k blank.bin Filled with FFh. May be WiFi configuration
• Source: https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map
ESP8266 Software stack
• Can build using FreeRTOS: http://www.freertos.org
• https://github.com/espressif/esp_iot_rtos_sdk
• ESP8266 SDK: hardware access and C library
• Application layer, either …
• AT commands (standard on ESP-01)
• IoT demo (standard on yellow ESP-12 dev board)
• NodeMCU Lua interpreter
• Your own application written in C
ESP-xx modules
• Variety of form factors, ESP-01 through ESP-12 …
• Antenna: None, Chip, PCB or U-FL connector
• Which pins broken out and layout (2 mm pitch)
• Power: 3.3V typical, 3.0V min, 3.6V max
• Chip enable pin (CH_PD): Hold high
• ESP-12 dev board does this for you
• GPIO0: Hold low on power-up to enter boot loader
• Flash firmware
• After power-up, can use GPIO0 as button input
ESP-xx modules
ESP-01
ESP-05
ESP-02
ESP-12
ESP-07 ESP-08 ESP-06
ESP-09ESP-03
ESP-10 ESP-11
ESP-04
ESP-01 set-up
• PCB antenna
• 8 pins including UART Rx/Tx, GPIO (2), Reset
• No power regulator, use only 3.0V to 3.6V
• Need a means of holding GPIO0 low for firmware flash
• Standard firmware: AT commands
• Cable or use JohnS ESPkit-01 …
• ESP-01 USB serial adaptor

1 RXD <—— TXD

2 VCC —— VCC 3.3V

3 GPIO0 —— Connect to ground during power-up to flash firmware

4 RESET —— VCC 3.3V

5 GPIO2

6 CH_PD —— VCC 3.3V

7 GND —— GND

8 TXD ——> RXD
ESP-12 dev board set-up
• PCB antenna
• 16 pins inc. UART Rx/Tx, GPIO (9), ADC, Reset
• RGB LED GPIOs: Red = 8, Green = 6, Blue = 7
• Uses 3x AA batteries with on-board 3.3v regulator
• Provides GPIO0 header for bootloader / firmware flash
• Standard firmware: IoT demo
• Cable: Note ESP-12 dev board labels are confusing !
• ESP-12 USB serial adaptor

RXD <—— RXD

GND —— GND

TXD ——> TXD
esptool overview
• Cross-platform Python, requires pySerial
• https://github.com/themadinventor/esptool
• Various functions …
• Flash firmware
• Create bootable image from binary blobs
• Read MAC address
• Read chip identification
• Read / write memory
• Hold GPIO0 low and power cycle ESP8266
esptool flash firmware
• Flash standard NodeMCU pre-built image …
• Download image …
• https://github.com/nodemcu/nodemcu-firmware
• esptool -p SERIAL_PORT write_flash 0x00000 nodemcu_20150212.bin
• Flash NodeMCU image with WS2812B support
• https://github.com/geekscape/esp8266_nodemcu_examples/firmware
• esptool -p SERIAL_PORT write_flash 0x00000
nodemcu_dev_0x00000.bin 0x10000 nodemcu_dev_0x10000.bin
NodeMCU firmware
• eLua interpreter including ESP8266 SDK API
• NodeMCU API documentation …
• https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en
• Serial terminal connection at 9,600 baud
• Command line prompt is “> “
• Enter valid Lua statements
• Starts with around 23Kb heap available
• Need to take care with your memory consumption
• Further reading: nodemcu.com/index_en.html
NodeMCU API
• node.restart(), heap(), compile()
• file.format(), list(), remove(), open(), read(), write()
• wifi.sta, wifi.ap sub-modules
• net module: TCP and UDP client and server
• mqtt module: Message Queue Telemetry Transport
• timer module, tmr.alarm(), tmr.stop(), tmr.time()
• gpio.mode(), read(), write(), trig()
• pwm, adc, i2c, spi, one-wire modules
• bit module: bit-wise logical operations
Lua introduction - I
• http://www.lua.org/start.html
• print(1 + 2); print(“hello”); print(node.heap())
• Variables: boolean, numbers, strings, tables
• Global by default, but can use “local” keyword
• Variable type declarations are not required
• colour = {}; colour[“red”] = 1; colour[“blue”] = 2
• Functions are first-class values
• function name(parameters)

return value - - Can return multiple values

end
Lua introduction - 2
• Control-flow …
• if condition then

print(true)

else

print(false)

end
• for variable = first, last, delta do

end
• while condition do

end
Lua introduction - 3
• NodeMCU after boot will try to run “init.lua”
• Compile using node.compile(“file.lua”)
• Byte-code written to “file.lc”
• Saves some memory
• Modules …
• moduleA = require(“module.lua”)
• local module = {}

function module.name()

end

return module
NodeMCU example
• You can’t just type … print(file.list()) !

• You need to type …
• for n,s in pairs(file.list()) do print(n.." size: "..s) end
• file.list() … returns a table
• pairs() … returns an iterator
• for n,s in … loops through each iterator pair
• n = file name, s = file size
ESPlorer IDE
• Simplify process of managing files and running
scripts on NodeMCU or MicroPython
• Also short-cuts for AT commands firmware
• http://esp8266.ru/esplorer
• Open source …
• https://github.com/4refr0nt/ESPlorer
• Start via command line … java -jar ESPlorer.jar
• Scans for available serial ports
• Provides Graphical User Interface for development
ESPlorer IDE
Button input
• Connect button between GPIO0 and Ground
• Example code …
• https://github.com/geekscape/esp8266_nodemcu_examples/blob/master/
examples/button.lua
• Load into ESPlorer IDE: File -> Open
• Save to ESP8266: [Save to ESP]
• Run script: [Do File]
• ESP-12 bonus: Try to use LDR and adc.read()
Button input - FAIL
• Our first attempt is likely to look like this …
PIN_BUTTON = 3 -- GPIO0

gpio.mode(PIN_BUTTON, gpio.INPUT, gpio.PULLUP)



while true do

if gpio.read(PIN_BUTTON) == 0 then

print("BUTTON PRESSED")

tmr.delay(20000) - - microseconds

end

end
Button input
• … but previous example, after a short while it fails :(
• More reliable to use timers, rather than delays
• Also, code is much more modular and re-usable
• Try … tmr.alarm(id, interval, repeat_flag, function)
• id: timer number 0 to 6
• interval: alarm time in milliseconds
• repeat_flag: 0 = once, 1 = repeat until tmr.stop()
• function: Lua function to invoke
• The following code is more reliable …
PIN_BUTTON = 3 -- GPIO0

TIME_ALARM = 25 -- 0.025 second, 40 Hz

gpio.mode(PIN_BUTTON, gpio.INPUT, gpio.PULLUP) 

button_state = 1

button_time = 0



function buttonHandler()

button_state_new = gpio.read(PIN_BUTTON)

if button_state == 1 and button_state_new == 0 then

button_time = tmr.time()

print("BUTTON PRESSED")

elseif button_state == 0 and button_state_new == 1 then

button_time_new = tmr.time()

if tmr.time() > (button_time + 2) then

tmr.stop(1)

print("BUTTON DISABLED")

end

end

button_state = button_state_new

end



tmr.alarm(1, TIME_ALARM, 1, buttonHandler)
Regular LED output
• Connect LED and current limiting resistor between
GPIO2 and Ground
• Example code …
• https://github.com/geekscape/esp8266_nodemcu_examples/blob/master/
examples/led.lua
• Load into ESPlorer IDE: File -> Open
• Save to ESP8266: [Save to ESP]
• Run script: [Do File]
• Try … init.lua: dofile('led.lua')
Regular LED output
LED_PIN = 4 -- GPIO2

TIME_ALARM = 500 -- 0.5 second

gpio.mode(LED_PIN, gpio.OUTPUT)

led_state = gpio.LOW

function ledHandler()

if led_state == gpio.LOW then

led_state = gpio.HIGH

else

led_state = gpio.LOW

end

gpio.write(LED_PIN, led_state)

end
tmr.alarm(1, TIME_ALARM, 1, ledHandler)
WS2812B LED output
• Original development by Markus Gritsch
• http://www.esp8266.com/viewtopic.php?f=21&t=1143
• Isn’t yet part of the NodeMCU master branch
• BRIGHT = 0.1; ON = BRIGHT * 255

LED_PIN = 4 -- GPIO2

PIXELS = 8

RED = string.char( 0, ON, 0)

GREEN = string.char(ON, 0, 0)

BLUE = string.char( 0, 0, ON)

ws2812.write(LED_PIN, RED:rep(PIXELS))

ws2812.write(LED_PIN, GREEN:rep(PIXELS))

ws2812.write(LED_PIN, BLUE:rep(PIXELS))
WS2812B LED example
• Connect WS2812B to GPIO2
• Example code …
• https://github.com/geekscape/esp8266_nodemcu_examples/blob/master/
examples/ws2812.lua
• Load into ESPlorer IDE: File -> Open
• Save to ESP8266: [Save to ESP]
• Run script: [Do File]
• Try … init.lua: dofile('ws2812.lua')
• Scan to get available Wi-Fi Access Points …
• wifi.sta.getap(function)
• Calls function when list of Access Points is ready
• Start Wi-Fi station (client) …
• wifi.setmode(wifi.STATION);
• wifi.sta.config(“ssid”, “passphrase”)
• wifi.sta.connect()
• Returns your IP address when ready …
• wifi.sta.getip()
Setting up Wi-Fi client
• wifi.sta.getap(wifi_start)
• module.SSID = {}

module.SSID["SSID1"] = "passphrase1"

module.SSID["SSID2"] = "passphrase2"
• local function wifi_start(aps)

for key,value in pairs(aps) do

if config.SSID and config.SSID[key] then

wifi.setmode(wifi.STATION);

wifi.sta.config(key, config.SSID[key])

wifi.sta.connect()

tmr.alarm(1, 2500, 1, wifi_wait_ip)

end

end

end
• local function wifi_wait_ip()

if wifi.sta.getip() then 

tmr.stop(1)

end

end
• Modular, flexible, timer-based application skeleton
• Example code …
• https://github.com/geekscape/esp8266_nodemcu_examples/tree/master/
skeleton
• Load into ESPlorer IDE: File -> Open
• Save to ESP8266: [Save to ESP]
• Reset ESP8266
• print(wifi.sta.getip())
Wi-Fi client example - 1
• init.lua
• Called on boot
• Minimal, can’t be compiled :(
• config.lua
• Application configuration, e.g SSID and passphrases
• setup.lua
• Sets up Wi-Fi depending upon your location
• application.lua
• Your code, change filename as appropriate
• Use node.compile() on everything except init.lua
Wi-Fi client example - 2
UDP message transmit
• socket = net.createConnection(net.UDP, 0)

socket:connect(PORT_NUMBER, HOST_STRING)

socket:send(MESSAGE)

socket:close()
• Example code …
• https://github.com/geekscape/esp8266_nodemcu_examples/blob/master/
applications/button_udp.lua
• Test on laptop with command line UDP server …
• nc -l -u IP_ADDRESS 4000
UDP message receive
• udp_server = net.createServer(net.UDP)

udp_server:on("receive", function(srv, data)

print("udp: " .. data)

end)

udp_server.listen(PORT)
• udp_server:close()
• Example code …
• https://github.com/geekscape/esp8266_nodemcu_examples/blob/master/
applications/ws2812_udp.lua
• Test on laptop with command line UDP client …
• nc -u IP_ADDRESS 4000
MQTT client
• MQTT server address “192.168.0.32”
• m:on("message", function(conn, topic, data) 

print(topic .. ":" ) 

if data ~= nil then print(data) end

end)



m:connect(“192.168.0.32", 1883, 0, function(conn)

print("connected")

end)

m:subscribe("/topic",0, function(conn)

print("subscribe success”)

end)

m:publish("/topic","hello",0,0, function(conn)

print("sent")

end)

m:close();
ESP8266 / NodeMCU resources
• ESP8266 general information
• https://nurdspace.nl/ESP8266
• https://nurdspace.nl/images/e/e0/ESP8266_Specifications_English.pdf
• NodeMCU API documentation
• https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en
• Lua programming language: http://www.lua.org
• ESPlorer IDE
• http://esp8266.ru/esplorer
• esptool (cross-platform) for flashing firmware
• https://github.com/themadinventor/esptool

More Related Content

What's hot

What's hot (20)

Arduino presentation
Arduino presentationArduino presentation
Arduino presentation
 
Basics of arduino uno
Basics of arduino unoBasics of arduino uno
Basics of arduino uno
 
Arduino presentation by_warishusain
Arduino presentation by_warishusainArduino presentation by_warishusain
Arduino presentation by_warishusain
 
What is Arduino ?
What is Arduino ?What is Arduino ?
What is Arduino ?
 
Rdl esp32 development board trainer kit
Rdl esp32 development board trainer kitRdl esp32 development board trainer kit
Rdl esp32 development board trainer kit
 
Esp8266 basics
Esp8266 basicsEsp8266 basics
Esp8266 basics
 
ESP32 WiFi & Bluetooth Module - Getting Started Guide
ESP32 WiFi & Bluetooth Module - Getting Started GuideESP32 WiFi & Bluetooth Module - Getting Started Guide
ESP32 WiFi & Bluetooth Module - Getting Started Guide
 
Arduino Workshop
Arduino WorkshopArduino Workshop
Arduino Workshop
 
Arduino IDE
Arduino IDE Arduino IDE
Arduino IDE
 
Arduino
ArduinoArduino
Arduino
 
ESP8266 and IOT
ESP8266 and IOTESP8266 and IOT
ESP8266 and IOT
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
 
1. Introduction to Embedded Systems & IoT
1. Introduction to Embedded Systems & IoT1. Introduction to Embedded Systems & IoT
1. Introduction to Embedded Systems & IoT
 
Esp32 cam arduino-123
Esp32 cam arduino-123Esp32 cam arduino-123
Esp32 cam arduino-123
 
Arduino
ArduinoArduino
Arduino
 
Introduction of Arduino Uno
Introduction of Arduino UnoIntroduction of Arduino Uno
Introduction of Arduino Uno
 
Introduction to stm32-part1
Introduction to stm32-part1Introduction to stm32-part1
Introduction to stm32-part1
 
Introduction to raspberry pi
Introduction to raspberry piIntroduction to raspberry pi
Introduction to raspberry pi
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
 

Viewers also liked

Viewers also liked (10)

Implementation of the ZigBee ZCL Reporting Configuration Features
Implementation of the ZigBee ZCL Reporting Configuration FeaturesImplementation of the ZigBee ZCL Reporting Configuration Features
Implementation of the ZigBee ZCL Reporting Configuration Features
 
電路學 - [第一章] 電路元件與基本定律
電路學 - [第一章] 電路元件與基本定律電路學 - [第一章] 電路元件與基本定律
電路學 - [第一章] 電路元件與基本定律
 
電路學 - [第二章] 電路分析方法
電路學 - [第二章] 電路分析方法電路學 - [第二章] 電路分析方法
電路學 - [第二章] 電路分析方法
 
射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬
射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬
射頻電子實驗手冊 - [實驗8] 低雜訊放大器模擬
 
射頻電子實驗手冊 - [實驗7] 射頻放大器模擬
射頻電子實驗手冊 - [實驗7] 射頻放大器模擬射頻電子實驗手冊 - [實驗7] 射頻放大器模擬
射頻電子實驗手冊 - [實驗7] 射頻放大器模擬
 
Agilent ADS 模擬手冊 [實習1] 基本操作與射頻放大器設計
Agilent ADS 模擬手冊 [實習1] 基本操作與射頻放大器設計Agilent ADS 模擬手冊 [實習1] 基本操作與射頻放大器設計
Agilent ADS 模擬手冊 [實習1] 基本操作與射頻放大器設計
 
射頻電子實驗手冊 [實驗6] 阻抗匹配模擬
射頻電子實驗手冊 [實驗6] 阻抗匹配模擬射頻電子實驗手冊 [實驗6] 阻抗匹配模擬
射頻電子實驗手冊 [實驗6] 阻抗匹配模擬
 
Agilent ADS 模擬手冊 [實習3] 壓控振盪器模擬
Agilent ADS 模擬手冊 [實習3] 壓控振盪器模擬Agilent ADS 模擬手冊 [實習3] 壓控振盪器模擬
Agilent ADS 模擬手冊 [實習3] 壓控振盪器模擬
 
Agilent ADS 模擬手冊 [實習2] 放大器設計
Agilent ADS 模擬手冊 [實習2]  放大器設計Agilent ADS 模擬手冊 [實習2]  放大器設計
Agilent ADS 模擬手冊 [實習2] 放大器設計
 
射頻電子實驗手冊 [實驗1 ~ 5] ADS入門, 傳輸線模擬, 直流模擬, 暫態模擬, 交流模擬
射頻電子實驗手冊 [實驗1 ~ 5] ADS入門, 傳輸線模擬, 直流模擬, 暫態模擬, 交流模擬射頻電子實驗手冊 [實驗1 ~ 5] ADS入門, 傳輸線模擬, 直流模擬, 暫態模擬, 交流模擬
射頻電子實驗手冊 [實驗1 ~ 5] ADS入門, 傳輸線模擬, 直流模擬, 暫態模擬, 交流模擬
 

Similar to NodeMCU ESP8266 workshop 1

Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013
Tom Paulus
 

Similar to NodeMCU ESP8266 workshop 1 (20)

Remote tanklevelmonitor
Remote tanklevelmonitorRemote tanklevelmonitor
Remote tanklevelmonitor
 
Getting started with Intel IoT Developer Kit
Getting started with Intel IoT Developer KitGetting started with Intel IoT Developer Kit
Getting started with Intel IoT Developer Kit
 
Adafruit Huzzah Esp8266 WiFi Board
Adafruit Huzzah Esp8266 WiFi BoardAdafruit Huzzah Esp8266 WiFi Board
Adafruit Huzzah Esp8266 WiFi Board
 
Qiscus bot esp8266
Qiscus bot esp8266Qiscus bot esp8266
Qiscus bot esp8266
 
Introduction to FreeRTOS
Introduction to FreeRTOSIntroduction to FreeRTOS
Introduction to FreeRTOS
 
IoT Hands-On-Lab, KINGS, 2019
IoT Hands-On-Lab, KINGS, 2019IoT Hands-On-Lab, KINGS, 2019
IoT Hands-On-Lab, KINGS, 2019
 
Remote temperature monitor (DHT11)
Remote temperature monitor (DHT11)Remote temperature monitor (DHT11)
Remote temperature monitor (DHT11)
 
[5]投影片 futurewad樹莓派研習會 141218
[5]投影片 futurewad樹莓派研習會 141218[5]投影片 futurewad樹莓派研習會 141218
[5]投影片 futurewad樹莓派研習會 141218
 
Esp8266 Workshop
Esp8266 WorkshopEsp8266 Workshop
Esp8266 Workshop
 
arduino.pdf
arduino.pdfarduino.pdf
arduino.pdf
 
Arduino by yogesh t s'
Arduino by yogesh t s'Arduino by yogesh t s'
Arduino by yogesh t s'
 
arduinoedit.pptx
arduinoedit.pptxarduinoedit.pptx
arduinoedit.pptx
 
Arduino delphi 2014_7_bonn
Arduino delphi 2014_7_bonnArduino delphi 2014_7_bonn
Arduino delphi 2014_7_bonn
 
Начало работы с Intel IoT Dev Kit
Начало работы с Intel IoT Dev KitНачало работы с Intel IoT Dev Kit
Начало работы с Intel IoT Dev Kit
 
Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013Getting Started With Raspberry Pi - UCSD 2013
Getting Started With Raspberry Pi - UCSD 2013
 
Athens IoT Meetup #3 - Introduction to ESP8266 (Pavlos Isaris)
Athens IoT Meetup #3 - Introduction to ESP8266 (Pavlos Isaris)Athens IoT Meetup #3 - Introduction to ESP8266 (Pavlos Isaris)
Athens IoT Meetup #3 - Introduction to ESP8266 (Pavlos Isaris)
 
JS Fest 2018. Володимир Шиманський. Запуск двіжка JS на мікроконтролері
JS Fest 2018. Володимир Шиманський. Запуск двіжка JS на мікроконтролеріJS Fest 2018. Володимир Шиманський. Запуск двіжка JS на мікроконтролері
JS Fest 2018. Володимир Шиманський. Запуск двіжка JS на мікроконтролері
 
Micropython on MicroControllers
Micropython on MicroControllersMicropython on MicroControllers
Micropython on MicroControllers
 
[OpenStack Day in Korea 2015] Track 1-6 - 갈라파고스의 이구아나, 인프라에 오픈소스를 올리다. 그래서 보이...
[OpenStack Day in Korea 2015] Track 1-6 - 갈라파고스의 이구아나, 인프라에 오픈소스를 올리다. 그래서 보이...[OpenStack Day in Korea 2015] Track 1-6 - 갈라파고스의 이구아나, 인프라에 오픈소스를 올리다. 그래서 보이...
[OpenStack Day in Korea 2015] Track 1-6 - 갈라파고스의 이구아나, 인프라에 오픈소스를 올리다. 그래서 보이...
 
Python-in-Embedded-systems.pptx
Python-in-Embedded-systems.pptxPython-in-Embedded-systems.pptx
Python-in-Embedded-systems.pptx
 

More from Andy Gelme

More from Andy Gelme (8)

LCA2018 Open Hardware MiniConference: LoliBot Software
LCA2018 Open Hardware MiniConference: LoliBot SoftwareLCA2018 Open Hardware MiniConference: LoliBot Software
LCA2018 Open Hardware MiniConference: LoliBot Software
 
Connected Community Hackerspace (Melbourne, Australia) 2014 03-11
Connected Community Hackerspace (Melbourne, Australia) 2014 03-11Connected Community Hackerspace (Melbourne, Australia) 2014 03-11
Connected Community Hackerspace (Melbourne, Australia) 2014 03-11
 
Internet Of Things: Hands on: YOW! night
Internet Of Things: Hands on: YOW! nightInternet Of Things: Hands on: YOW! night
Internet Of Things: Hands on: YOW! night
 
BarCamp Melbourne 2012: Internet of Things
BarCamp Melbourne 2012: Internet of ThingsBarCamp Melbourne 2012: Internet of Things
BarCamp Melbourne 2012: Internet of Things
 
Internet Of Things, Smart Energy Groups
Internet Of Things, Smart Energy GroupsInternet Of Things, Smart Energy Groups
Internet Of Things, Smart Energy Groups
 
2011 01-24 dragino
2011 01-24 dragino2011 01-24 dragino
2011 01-24 dragino
 
TinyMCE: WYSIWYG editor 2010-12-08
TinyMCE: WYSIWYG editor 2010-12-08TinyMCE: WYSIWYG editor 2010-12-08
TinyMCE: WYSIWYG editor 2010-12-08
 
Internet of Things
Internet of ThingsInternet of Things
Internet of Things
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

NodeMCU ESP8266 workshop 1

  • 1. ESP8266 Workshop 1 Andy Gelme (@geekscape) http://geekscape.org
 Wednesday 2015-03-11 Copyright (c) 2015 by Geekscape Pty. Ltd. License CC-BY-NC-4.0
  • 2. Workshop overview • ESP8266 introduction: hardware and software • ESP-01 and ESP-12 set-up for development • Flashing firmware using esptool • NodeMCU and Lua introduction • ESPlorer IDE • Using a button and LED(s) • Setting up as a Wi-Fi client • Using UDP to send and receive messages • Creating an MQTT client
  • 3. ESP8266 Hardware • 3.3V device, operating current ~ 215 mA • CPU: Tensilca Xtensa LX3: 32-bit, 80 MHz • ESP8266 SOC: Expressif • RAM 32Kb, DRAM 80Kb, Flash 200Kb (for SDK) • Wi-Fi 802.11 b/g/n 2.4 GHz radio (station or AP) • Timers, deep sleep mode, JTAG debugging • Peripherals … • GPIO (upto 16), PWM (3), ADC (one) • UART, I2C, SPI
  • 4. ESP8266 Memory Map • More complicated than your AVR ATMega / Arduino • SRAM 32Kb • NodeMCU heap memory available: print(node.heap()) • SPI Flash ROM layout: Since SDK 0.8 … • 00000h 248k app.v6.flash.bin User application
 3E000h 8k master_device_key.bin OTA device key
 40000h 240k app.v6.irom0text.bin SDK libraries
 7C000h 8k esp_init_data_default.bin Default configuration
 7E000h 8k blank.bin Filled with FFh. May be WiFi configuration • Source: https://github.com/esp8266/esp8266-wiki/wiki/Memory-Map
  • 5. ESP8266 Software stack • Can build using FreeRTOS: http://www.freertos.org • https://github.com/espressif/esp_iot_rtos_sdk • ESP8266 SDK: hardware access and C library • Application layer, either … • AT commands (standard on ESP-01) • IoT demo (standard on yellow ESP-12 dev board) • NodeMCU Lua interpreter • Your own application written in C
  • 6. ESP-xx modules • Variety of form factors, ESP-01 through ESP-12 … • Antenna: None, Chip, PCB or U-FL connector • Which pins broken out and layout (2 mm pitch) • Power: 3.3V typical, 3.0V min, 3.6V max • Chip enable pin (CH_PD): Hold high • ESP-12 dev board does this for you • GPIO0: Hold low on power-up to enter boot loader • Flash firmware • After power-up, can use GPIO0 as button input
  • 7. ESP-xx modules ESP-01 ESP-05 ESP-02 ESP-12 ESP-07 ESP-08 ESP-06 ESP-09ESP-03 ESP-10 ESP-11 ESP-04
  • 8. ESP-01 set-up • PCB antenna • 8 pins including UART Rx/Tx, GPIO (2), Reset • No power regulator, use only 3.0V to 3.6V • Need a means of holding GPIO0 low for firmware flash • Standard firmware: AT commands • Cable or use JohnS ESPkit-01 … • ESP-01 USB serial adaptor
 1 RXD <—— TXD
 2 VCC —— VCC 3.3V
 3 GPIO0 —— Connect to ground during power-up to flash firmware
 4 RESET —— VCC 3.3V
 5 GPIO2
 6 CH_PD —— VCC 3.3V
 7 GND —— GND
 8 TXD ——> RXD
  • 9. ESP-12 dev board set-up • PCB antenna • 16 pins inc. UART Rx/Tx, GPIO (9), ADC, Reset • RGB LED GPIOs: Red = 8, Green = 6, Blue = 7 • Uses 3x AA batteries with on-board 3.3v regulator • Provides GPIO0 header for bootloader / firmware flash • Standard firmware: IoT demo • Cable: Note ESP-12 dev board labels are confusing ! • ESP-12 USB serial adaptor
 RXD <—— RXD
 GND —— GND
 TXD ——> TXD
  • 10. esptool overview • Cross-platform Python, requires pySerial • https://github.com/themadinventor/esptool • Various functions … • Flash firmware • Create bootable image from binary blobs • Read MAC address • Read chip identification • Read / write memory • Hold GPIO0 low and power cycle ESP8266
  • 11. esptool flash firmware • Flash standard NodeMCU pre-built image … • Download image … • https://github.com/nodemcu/nodemcu-firmware • esptool -p SERIAL_PORT write_flash 0x00000 nodemcu_20150212.bin • Flash NodeMCU image with WS2812B support • https://github.com/geekscape/esp8266_nodemcu_examples/firmware • esptool -p SERIAL_PORT write_flash 0x00000 nodemcu_dev_0x00000.bin 0x10000 nodemcu_dev_0x10000.bin
  • 12. NodeMCU firmware • eLua interpreter including ESP8266 SDK API • NodeMCU API documentation … • https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en • Serial terminal connection at 9,600 baud • Command line prompt is “> “ • Enter valid Lua statements • Starts with around 23Kb heap available • Need to take care with your memory consumption • Further reading: nodemcu.com/index_en.html
  • 13. NodeMCU API • node.restart(), heap(), compile() • file.format(), list(), remove(), open(), read(), write() • wifi.sta, wifi.ap sub-modules • net module: TCP and UDP client and server • mqtt module: Message Queue Telemetry Transport • timer module, tmr.alarm(), tmr.stop(), tmr.time() • gpio.mode(), read(), write(), trig() • pwm, adc, i2c, spi, one-wire modules • bit module: bit-wise logical operations
  • 14. Lua introduction - I • http://www.lua.org/start.html • print(1 + 2); print(“hello”); print(node.heap()) • Variables: boolean, numbers, strings, tables • Global by default, but can use “local” keyword • Variable type declarations are not required • colour = {}; colour[“red”] = 1; colour[“blue”] = 2 • Functions are first-class values • function name(parameters)
 return value - - Can return multiple values
 end
  • 15. Lua introduction - 2 • Control-flow … • if condition then
 print(true)
 else
 print(false)
 end • for variable = first, last, delta do
 end • while condition do
 end
  • 16. Lua introduction - 3 • NodeMCU after boot will try to run “init.lua” • Compile using node.compile(“file.lua”) • Byte-code written to “file.lc” • Saves some memory • Modules … • moduleA = require(“module.lua”) • local module = {}
 function module.name()
 end
 return module
  • 17. NodeMCU example • You can’t just type … print(file.list()) !
 • You need to type … • for n,s in pairs(file.list()) do print(n.." size: "..s) end • file.list() … returns a table • pairs() … returns an iterator • for n,s in … loops through each iterator pair • n = file name, s = file size
  • 18. ESPlorer IDE • Simplify process of managing files and running scripts on NodeMCU or MicroPython • Also short-cuts for AT commands firmware • http://esp8266.ru/esplorer • Open source … • https://github.com/4refr0nt/ESPlorer • Start via command line … java -jar ESPlorer.jar • Scans for available serial ports • Provides Graphical User Interface for development
  • 20. Button input • Connect button between GPIO0 and Ground • Example code … • https://github.com/geekscape/esp8266_nodemcu_examples/blob/master/ examples/button.lua • Load into ESPlorer IDE: File -> Open • Save to ESP8266: [Save to ESP] • Run script: [Do File] • ESP-12 bonus: Try to use LDR and adc.read()
  • 21. Button input - FAIL • Our first attempt is likely to look like this … PIN_BUTTON = 3 -- GPIO0
 gpio.mode(PIN_BUTTON, gpio.INPUT, gpio.PULLUP)
 
 while true do
 if gpio.read(PIN_BUTTON) == 0 then
 print("BUTTON PRESSED")
 tmr.delay(20000) - - microseconds
 end
 end
  • 22. Button input • … but previous example, after a short while it fails :( • More reliable to use timers, rather than delays • Also, code is much more modular and re-usable • Try … tmr.alarm(id, interval, repeat_flag, function) • id: timer number 0 to 6 • interval: alarm time in milliseconds • repeat_flag: 0 = once, 1 = repeat until tmr.stop() • function: Lua function to invoke • The following code is more reliable …
  • 23. PIN_BUTTON = 3 -- GPIO0
 TIME_ALARM = 25 -- 0.025 second, 40 Hz
 gpio.mode(PIN_BUTTON, gpio.INPUT, gpio.PULLUP) 
 button_state = 1
 button_time = 0
 
 function buttonHandler()
 button_state_new = gpio.read(PIN_BUTTON)
 if button_state == 1 and button_state_new == 0 then
 button_time = tmr.time()
 print("BUTTON PRESSED")
 elseif button_state == 0 and button_state_new == 1 then
 button_time_new = tmr.time()
 if tmr.time() > (button_time + 2) then
 tmr.stop(1)
 print("BUTTON DISABLED")
 end
 end
 button_state = button_state_new
 end
 
 tmr.alarm(1, TIME_ALARM, 1, buttonHandler)
  • 24. Regular LED output • Connect LED and current limiting resistor between GPIO2 and Ground • Example code … • https://github.com/geekscape/esp8266_nodemcu_examples/blob/master/ examples/led.lua • Load into ESPlorer IDE: File -> Open • Save to ESP8266: [Save to ESP] • Run script: [Do File] • Try … init.lua: dofile('led.lua')
  • 25. Regular LED output LED_PIN = 4 -- GPIO2
 TIME_ALARM = 500 -- 0.5 second
 gpio.mode(LED_PIN, gpio.OUTPUT)
 led_state = gpio.LOW
 function ledHandler()
 if led_state == gpio.LOW then
 led_state = gpio.HIGH
 else
 led_state = gpio.LOW
 end
 gpio.write(LED_PIN, led_state)
 end tmr.alarm(1, TIME_ALARM, 1, ledHandler)
  • 26. WS2812B LED output • Original development by Markus Gritsch • http://www.esp8266.com/viewtopic.php?f=21&t=1143 • Isn’t yet part of the NodeMCU master branch • BRIGHT = 0.1; ON = BRIGHT * 255
 LED_PIN = 4 -- GPIO2
 PIXELS = 8
 RED = string.char( 0, ON, 0)
 GREEN = string.char(ON, 0, 0)
 BLUE = string.char( 0, 0, ON)
 ws2812.write(LED_PIN, RED:rep(PIXELS))
 ws2812.write(LED_PIN, GREEN:rep(PIXELS))
 ws2812.write(LED_PIN, BLUE:rep(PIXELS))
  • 27. WS2812B LED example • Connect WS2812B to GPIO2 • Example code … • https://github.com/geekscape/esp8266_nodemcu_examples/blob/master/ examples/ws2812.lua • Load into ESPlorer IDE: File -> Open • Save to ESP8266: [Save to ESP] • Run script: [Do File] • Try … init.lua: dofile('ws2812.lua')
  • 28. • Scan to get available Wi-Fi Access Points … • wifi.sta.getap(function) • Calls function when list of Access Points is ready • Start Wi-Fi station (client) … • wifi.setmode(wifi.STATION); • wifi.sta.config(“ssid”, “passphrase”) • wifi.sta.connect() • Returns your IP address when ready … • wifi.sta.getip() Setting up Wi-Fi client
  • 29. • wifi.sta.getap(wifi_start) • module.SSID = {}
 module.SSID["SSID1"] = "passphrase1"
 module.SSID["SSID2"] = "passphrase2" • local function wifi_start(aps)
 for key,value in pairs(aps) do
 if config.SSID and config.SSID[key] then
 wifi.setmode(wifi.STATION);
 wifi.sta.config(key, config.SSID[key])
 wifi.sta.connect()
 tmr.alarm(1, 2500, 1, wifi_wait_ip)
 end
 end
 end • local function wifi_wait_ip()
 if wifi.sta.getip() then 
 tmr.stop(1)
 end
 end
  • 30. • Modular, flexible, timer-based application skeleton • Example code … • https://github.com/geekscape/esp8266_nodemcu_examples/tree/master/ skeleton • Load into ESPlorer IDE: File -> Open • Save to ESP8266: [Save to ESP] • Reset ESP8266 • print(wifi.sta.getip()) Wi-Fi client example - 1
  • 31. • init.lua • Called on boot • Minimal, can’t be compiled :( • config.lua • Application configuration, e.g SSID and passphrases • setup.lua • Sets up Wi-Fi depending upon your location • application.lua • Your code, change filename as appropriate • Use node.compile() on everything except init.lua Wi-Fi client example - 2
  • 32. UDP message transmit • socket = net.createConnection(net.UDP, 0)
 socket:connect(PORT_NUMBER, HOST_STRING)
 socket:send(MESSAGE)
 socket:close() • Example code … • https://github.com/geekscape/esp8266_nodemcu_examples/blob/master/ applications/button_udp.lua • Test on laptop with command line UDP server … • nc -l -u IP_ADDRESS 4000
  • 33. UDP message receive • udp_server = net.createServer(net.UDP)
 udp_server:on("receive", function(srv, data)
 print("udp: " .. data)
 end)
 udp_server.listen(PORT) • udp_server:close() • Example code … • https://github.com/geekscape/esp8266_nodemcu_examples/blob/master/ applications/ws2812_udp.lua • Test on laptop with command line UDP client … • nc -u IP_ADDRESS 4000
  • 34. MQTT client • MQTT server address “192.168.0.32” • m:on("message", function(conn, topic, data) 
 print(topic .. ":" ) 
 if data ~= nil then print(data) end
 end)
 
 m:connect(“192.168.0.32", 1883, 0, function(conn)
 print("connected")
 end)
 m:subscribe("/topic",0, function(conn)
 print("subscribe success”)
 end)
 m:publish("/topic","hello",0,0, function(conn)
 print("sent")
 end)
 m:close();
  • 35. ESP8266 / NodeMCU resources • ESP8266 general information • https://nurdspace.nl/ESP8266 • https://nurdspace.nl/images/e/e0/ESP8266_Specifications_English.pdf • NodeMCU API documentation • https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en • Lua programming language: http://www.lua.org • ESPlorer IDE • http://esp8266.ru/esplorer • esptool (cross-platform) for flashing firmware • https://github.com/themadinventor/esptool