From f54375fa0958c3a79baf1444ee00161a7e6e0d6c Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 13 Jun 2018 23:46:46 +0200 Subject: wip --- README.md | 2 ++ assignments/mqtt-with-button/mqtt-with-button.md | 14 ++++++-- assignments/mqtt-with-button/mqtt-with-button.pdf | Bin 841020 -> 841066 bytes .../schematic/mqtt-with-button.fzz | Bin 24221 -> 24142 bytes .../schematic/mqtt-with-button_bb.pdf | Bin 449304 -> 449659 bytes .../schematic/mqtt-with-button_schem.pdf | Bin 305478 -> 308141 bytes slides/TODO.md | 16 +++++++++ slides/connected-arduino-slides.pdf | Bin 293025 -> 293791 bytes slides/connected-arduino-text.pdf | Bin 64793 -> 298798 bytes slides/connected-arduino.md | 37 +++++++++++++++++++-- slides/toc.md | 7 ++-- 11 files changed, 67 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3726b61..8ae53f3 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ This is the materials for a 1 hour workshop on IoT. * Install Arduino IDE * Install the "ESP8266 core" for Arduino, follow the guide on https://github.com/esp8266/Arduino#installing-with-boards-manager. +* Install PubSubClient library with Library manager from within the + Arduino IDE. **Assignments** diff --git a/assignments/mqtt-with-button/mqtt-with-button.md b/assignments/mqtt-with-button/mqtt-with-button.md index 77f0b22..c825615 100644 --- a/assignments/mqtt-with-button/mqtt-with-button.md +++ b/assignments/mqtt-with-button/mqtt-with-button.md @@ -16,13 +16,17 @@ Wire up this schematic on the bread board: # Step 2 -* Read button, print message on terminal +* Tip: change upload speed to max. + +* Read button in `loop()`. If the button's state changes, print a + message. + +*Note:* reading the button in a busy loop is not really a best +practice as it uses lots of energy. Instead use the `attachInterrupt`. # Step 3 * Connect to the Wi-Fi network - * Use `WiFi.localIP()` - * Connect to MQTT broker # Step 4 @@ -32,6 +36,10 @@ Wire up this schematic on the bread board: # Step 5 (Bonus) +Subscripe to a topic and do something with the led. + +* Subscribe to the topic you're publishing to. + * Subscribe to the topic `ndc/$device-id/led`. * Use the value to for example turn the LED on/off, or change the diff --git a/assignments/mqtt-with-button/mqtt-with-button.pdf b/assignments/mqtt-with-button/mqtt-with-button.pdf index 93079d7..8fcccf7 100644 Binary files a/assignments/mqtt-with-button/mqtt-with-button.pdf and b/assignments/mqtt-with-button/mqtt-with-button.pdf differ diff --git a/assignments/mqtt-with-button/schematic/mqtt-with-button.fzz b/assignments/mqtt-with-button/schematic/mqtt-with-button.fzz index 57e5e5d..1f56f4e 100644 Binary files a/assignments/mqtt-with-button/schematic/mqtt-with-button.fzz and b/assignments/mqtt-with-button/schematic/mqtt-with-button.fzz differ diff --git a/assignments/mqtt-with-button/schematic/mqtt-with-button_bb.pdf b/assignments/mqtt-with-button/schematic/mqtt-with-button_bb.pdf index aa0d283..8ba636a 100644 Binary files a/assignments/mqtt-with-button/schematic/mqtt-with-button_bb.pdf and b/assignments/mqtt-with-button/schematic/mqtt-with-button_bb.pdf differ diff --git a/assignments/mqtt-with-button/schematic/mqtt-with-button_schem.pdf b/assignments/mqtt-with-button/schematic/mqtt-with-button_schem.pdf index 3ce5de3..5d97abf 100644 Binary files a/assignments/mqtt-with-button/schematic/mqtt-with-button_schem.pdf and b/assignments/mqtt-with-button/schematic/mqtt-with-button_schem.pdf differ diff --git a/slides/TODO.md b/slides/TODO.md index 7d053b9..5b84672 100644 --- a/slides/TODO.md +++ b/slides/TODO.md @@ -4,4 +4,20 @@ Slide: *Device and application architecture with MQTT* Illustrates that everyone connects to the broker, but messages can be pushed. Contrast to HTTP. +Slide: *MQTT on Arduino* +* flytt callback til egen slide. +* legg til Serial.println("connecting"); og Serial.println("connected"); +* sett mqtt_server = "trygvis.io"; +ditto på neste slide + +* vis client_id + * Assignments: D0 is the pin to use. + + +MQTT +* fix switch orientation +* husk å gjøre preparations +* screen shot av library manager og installering av PubSubClient + +* vis hvordan man formaterer stringer., clientId og subscription patterns. diff --git a/slides/connected-arduino-slides.pdf b/slides/connected-arduino-slides.pdf index 83e8750..cc258eb 100644 Binary files a/slides/connected-arduino-slides.pdf and b/slides/connected-arduino-slides.pdf differ diff --git a/slides/connected-arduino-text.pdf b/slides/connected-arduino-text.pdf index e5088b9..6882209 100644 Binary files a/slides/connected-arduino-text.pdf and b/slides/connected-arduino-text.pdf differ diff --git a/slides/connected-arduino.md b/slides/connected-arduino.md index 79ba123..16f93d4 100644 --- a/slides/connected-arduino.md +++ b/slides/connected-arduino.md @@ -126,6 +126,27 @@ class { ESP.restart(); ~~~ +## ESP + +~~~c++ +// Top of file +#include + +// In setup() +WiFi.mode(WIFI_STA); +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()); +~~~ + ## ESP Arduino APIs ~~~c++ @@ -447,6 +468,8 @@ Broker PubSubClient is our MQTT client implementation. ~~~c++ +#include + WiFiClient wifiClient; PubSubClient mqtt(wifiClient); @@ -465,16 +488,24 @@ void setup() { ~~~c++ void loop() { - if (!mqtt.connected()) + if (!mqtt.connected()) { reconnect(); - else + } + else { mqtt.loop(); + } + // Do work } void reconnect() { - while (!mqtt.connect(client_id)); + do { + Serial.println("Connecting to MQTT"); + delay(1000); + } while (!mqtt.connect(client_id)); + Serial.println("Connected to MQTT server"); + // Subscribe to any topics you need mqtt.subscribe(topic_pattern); } ~~~ diff --git a/slides/toc.md b/slides/toc.md index 6e54e03..227f00a 100644 --- a/slides/toc.md +++ b/slides/toc.md @@ -8,6 +8,8 @@ * Arduino file structure * Generic Arduino APIs * ESP Arduino APIs + * ESP +#include * ESP Arduino APIs * What is IoT * What is IoT @@ -19,16 +21,15 @@ * Lecture: MQTT * MQTT * Device and application architecture with MQTT + * MQTT Topic * MQTT - Implementations * MQTT Cloud Connectors * MQTT - The protocol - * MQTT - The protocol - MQTT Packet - * MQTT Connect * MQTT - The protocol - MQTT Topic * MQTT - The protocol - Retained message * MQTT - The protocol - Will message - * MQTT Topic * MQTT on Arduino +#include * MQTT on Arduino * Assignment * MQTT topic architecture -- cgit v1.2.3