2. www.ba0sh1.com
2
Internet of Things (?)
• Let coffee pot twitter you a message is less
productive than just a beep!
• Same for turning on/off a light.
• Beware of the over hype!
• But why connect things onto internet?
Remote convenience
Did you forget to … ?
Monitoring
Diagnose / support
Why-not ?
Data driven actuators
Weather
4. www.ba0sh1.com
4
ESP8266 In-a-nutshell
• Developed by Espressif System
• LOW COST
• A 32-bit Microcontroller, using
Tensilica Xtensa core
• RF frontend
• Runs at 80MHz or 160MHz
• External Flash
• ~80kB DRAM
• ~35kB IRAM
• ADC, GPIO, SPI, I2S, DMA
• Part number is ESP8266EX
• 20 million chip sold. 5000 active
developers
11. www.ba0sh1.com
11
Know your Hardware
• WiFi
– Supports 802.11b/g/n, WPA/WPA2
– AP, STA, AP+STA modes
– With my ASUS router it connects at 65Mbps
– With WebSocket speed test I receive 5-6KB/s
– NetIO test best at 1040KB/s, reference from
http://www.esp8266.com/viewtopic.php?f=5&t=2
45 (doing nothing but receive TCP packets)
13. www.ba0sh1.com
13
Power Consumption @ WiFi idle
100ms
64mA
424mA
847ns
Average for 1 second: 993ms@64mA+7ms@424mA = 66.5mA
With 22uF tantalum bypass capacitor. A bigger capacitor may help reduce the
spike.
16. www.ba0sh1.com
16
Will you go battery power?
Standby Always on
Wake up once
every minute
Wake up once
every hour
2500mAh lithium
battery
7 years 37 hours 15.5 days 689 days
2xAA alkaline
3000mAh
8.4 years 44 hours 18.6 days 827 days
*Your actual millage may vary
17. www.ba0sh1.com
17
Keep your options open
• Particle Photon ($19)
• Lightblue Bean ($30)
• OpenWRT routers ($7-20)
Linux
USB
High performance
Not low power
Slow boot
20. www.ba0sh1.com
20
ESP8266 Arduino Library
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
WiFiClient client;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1rn" + "Host: " + host + "rn" +
"Connection: closernrn");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('r');
Serial.print(line);
}
21. www.ba0sh1.com
21
NodeMCU
• http://www.nodemcu.com/index_en.html
(mqtt.lua)
m = mqtt.Client(“client”, 120, “user”, “pass”)
m:connect(“192.168.0.99”, 1831, 0, function(conn)
print("Connected to MQTT:" .. BROKER .. ":" .. BRPORT .." as " .. CLIENTID)
m:publish("sensors/".. CLIENTID .. "/temperature",30,0,0, function(conn)
print ("temp published")
node.dsleep(10*1000000)
end)
end)
(init.lua)
tmr.alarm(1,500, 1, function()
if wifi.sta.getip()==nil
then
print(" Wait to IP address!")
else
print("New IP address is "..wifi.sta.getip())
dofile("mqtt.lua")
end
end)
22. www.ba0sh1.com
22
ESP8266 SDK and Toolchain
• I want to have more control / more speed / be
cool
• ESP8266 SDK available at bbs.espressif.com
• Espressif compiler in VM
• crosstool-NG on Win/Linux/Mac
github.com/pfalcon/esp-open-sdk
• Unofficial DevKit for ESP8266 (Windows) at
http://programs74.ru/udkew-en.html
23. www.ba0sh1.com
23
ESP8266 IoT SDK
• Low-level C SDK, partially open source
• Soft Timer
#define DELAY 100 /* milliseconds */
LOCAL os_timer_t blink_timer;
LOCAL void ICACHE_FLASH_ATTR blink_cb(void *arg)
{
GPIO_OUTPUT_SET(LED_GPIO, led_state);
led_state ^=1;
}
void user_init(void)
{
......
// Set up a timer to blink the LED
os_timer_disarm(&blink_timer);
os_timer_setfn(&blink_timer, (os_timer_func_t *)blink_cb, (void *)0);
os_timer_arm(&blink_timer, DELAY, 1);
}
26. www.ba0sh1.com
26
RTOS SDK
• Partially open source at
https://github.com/espressif/esp_iot_rtos_sdk
• Based on FreeRTOS / lwip
• BSD sockets (thread-safe)
• Newer, clearer code
• Foundation of esp8266_iot_platform
28. www.ba0sh1.com
28
Example
wifi_thread
Set WiFi parameters
while (not no ip and not timeout)
{ vTaskDelay; }
while (have ip)
{
xSemaphoreGive(wifi_alive);
vTaskDelay;
}
wifi_disconnect();
vTaskDelay;
mqtt_thread
xSemaphoreTake(wifi_alive);
mqtt_connect
while (not fail)
{
if have message publish;
mqtt_yield(timeout);
}
mqtt_subscribe