From a4cbbee0671ff20560c31a2f3945b6131496ab2b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 13 Jun 2018 20:41:24 +0200 Subject: wip --- README.md | 5 ++ assignments/Makefile | 6 +- assignments/client1/client1.ino | 71 --------------------- assignments/mqtt-with-button/mqtt-with-button.md | 57 +++++++++++++++++ assignments/mqtt-with-button/mqtt-with-button.pdf | Bin 0 -> 857368 bytes .../schematic/mqtt-with-button.fzz | Bin 0 -> 24221 bytes .../schematic/mqtt-with-button_bb.pdf | Bin 0 -> 449304 bytes .../schematic/mqtt-with-button_schem.pdf | Bin 0 -> 305478 bytes assignments/mqtt/mqtt.md | 41 ------------ assignments/mqtt/mqtt.pdf | Bin 104784 -> 0 bytes assignments/mqtt2/mqtt2.md | 10 --- assignments/mqtt2/mqtt2.pdf | Bin 51245 -> 0 bytes assignments/mqtt3/mqtt3.md | 12 ---- assignments/mqtt3/mqtt3.pdf | Bin 53509 -> 0 bytes assignments/network/network.md | 7 -- assignments/old/client1/client1.ino | 71 +++++++++++++++++++++ assignments/old/mqtt2/mqtt2.md | 10 +++ assignments/old/mqtt2/mqtt2.pdf | Bin 0 -> 51245 bytes assignments/old/mqtt3/mqtt3.md | 12 ++++ assignments/old/mqtt3/mqtt3.pdf | Bin 0 -> 53509 bytes assignments/old/network/network.md | 7 ++ .../old/read-temperature/read-temperature.md | 13 ++++ .../old/read-temperature/read-temperature.pdf | Bin 0 -> 794796 bytes .../schematic/read-temperature.fzz | Bin 0 -> 23036 bytes .../schematic/read-temperature_bb.pdf | Bin 0 -> 392490 bytes .../schematic/read-temperature_schem.pdf | Bin 0 -> 354997 bytes .../solution/read-temperature/read-temperature.ino | 24 +++++++ assignments/read-temperature/read-temperature.md | 13 ---- assignments/read-temperature/read-temperature.pdf | Bin 794796 -> 0 bytes .../schematic/read-temperature.fzz | Bin 23036 -> 0 bytes .../schematic/read-temperature_bb.pdf | Bin 392490 -> 0 bytes .../schematic/read-temperature_schem.pdf | Bin 354997 -> 0 bytes .../solution/read-temperature/read-temperature.ino | 24 ------- 33 files changed, 200 insertions(+), 183 deletions(-) delete mode 100644 assignments/client1/client1.ino create mode 100644 assignments/mqtt-with-button/mqtt-with-button.md create mode 100644 assignments/mqtt-with-button/mqtt-with-button.pdf create mode 100644 assignments/mqtt-with-button/schematic/mqtt-with-button.fzz create mode 100644 assignments/mqtt-with-button/schematic/mqtt-with-button_bb.pdf create mode 100644 assignments/mqtt-with-button/schematic/mqtt-with-button_schem.pdf delete mode 100644 assignments/mqtt/mqtt.md delete mode 100644 assignments/mqtt/mqtt.pdf delete mode 100644 assignments/mqtt2/mqtt2.md delete mode 100644 assignments/mqtt2/mqtt2.pdf delete mode 100644 assignments/mqtt3/mqtt3.md delete mode 100644 assignments/mqtt3/mqtt3.pdf delete mode 100644 assignments/network/network.md create mode 100644 assignments/old/client1/client1.ino create mode 100644 assignments/old/mqtt2/mqtt2.md create mode 100644 assignments/old/mqtt2/mqtt2.pdf create mode 100644 assignments/old/mqtt3/mqtt3.md create mode 100644 assignments/old/mqtt3/mqtt3.pdf create mode 100644 assignments/old/network/network.md create mode 100644 assignments/old/read-temperature/read-temperature.md create mode 100644 assignments/old/read-temperature/read-temperature.pdf create mode 100644 assignments/old/read-temperature/schematic/read-temperature.fzz create mode 100644 assignments/old/read-temperature/schematic/read-temperature_bb.pdf create mode 100644 assignments/old/read-temperature/schematic/read-temperature_schem.pdf create mode 100644 assignments/old/read-temperature/solution/read-temperature/read-temperature.ino delete mode 100644 assignments/read-temperature/read-temperature.md delete mode 100644 assignments/read-temperature/read-temperature.pdf delete mode 100644 assignments/read-temperature/schematic/read-temperature.fzz delete mode 100644 assignments/read-temperature/schematic/read-temperature_bb.pdf delete mode 100644 assignments/read-temperature/schematic/read-temperature_schem.pdf delete mode 100644 assignments/read-temperature/solution/read-temperature/read-temperature.ino diff --git a/README.md b/README.md index 65feb30..f56c480 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,11 @@ This is the materials for a 1 hour workshop on IoT. > There are some software that you should have installed, check with > the person at the Bitraf stand. +## Assignments + +* [Blink a LED](assignments/blink-a-led/blink-a-led.pdf) +* [MQTT with button](assignments/mqtt-with-button/mqtt-with-button.pdf) + # Credits * Some TikZ diagrams where taken from: diff --git a/assignments/Makefile b/assignments/Makefile index e1ec335..52ee5c5 100644 --- a/assignments/Makefile +++ b/assignments/Makefile @@ -1,10 +1,6 @@ AS = AS += blink-a-led -AS += mqtt -AS += mqtt2 -AS += mqtt3 -AS += read-a-button -AS += read-temperature +AS += mqtt-with-button PDFS=$(foreach A,$(AS),$(A)/$(A).pdf) diff --git a/assignments/client1/client1.ino b/assignments/client1/client1.ino deleted file mode 100644 index 39eabfe..0000000 --- a/assignments/client1/client1.ino +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include - -#include "config.h" - -WiFiUDP Udp; -unsigned int localUdpPort = 5006; -IPAddress host_ip(192, 168, 10, 151); -unsigned int host_port = 5005; - -const int out_buf_len_start = 100; -const int out_buf_len_increment = 100; -const int out_buf_len_max = 2200; - -char buf[out_buf_len_max]; -int out_buf_len; - -static const char alphabet[] = "abcdefghijklmnopqrstuvxyz"; - -unsigned long time_start, time_end; - -void setup() { - Serial.begin(115200); - - Serial.printf("Connecting to %s ", wifi_ssid); - WiFi.begin(wifi_ssid, wifi_password); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - Serial.println(" connected"); - - Udp.begin(localUdpPort); - time_start = millis(); - out_buf_len = out_buf_len_start; -} - -void loop() { - - if (millis() > (time_start + 1000)) { - for (int i = 0; i < out_buf_len; i++) { - buf[i] = alphabet[i % sizeof(alphabet) - 1]; - } - out_buf_len += out_buf_len_increment; - if (out_buf_len > out_buf_len_max) { - out_buf_len = out_buf_len_start; - } - Serial.printf("Sending %d bytes to %s:%d\n", out_buf_len, host_ip.toString().c_str(), host_port); - Udp.beginPacket(host_ip, host_port); - Udp.write(buf, out_buf_len); - time_start = millis(); - auto ok = Udp.endPacket(); - - if (!ok) { - Serial.printf("Could not send packet with size %d\n", out_buf_len); - } - } - - int len = Udp.parsePacket(); - if (len) { - time_end = millis(); - Serial.printf("RX %s:%d: size=%d\n", Udp.remoteIP().toString().c_str(), Udp.remotePort(), len); - int len = Udp.read(buf, sizeof(buf)); - Serial.printf("len=%d\n"); - if (len > 0) { - buf[len] = 0; - } - Serial.printf("RTT: %d ms. Payload:%s\n", int(time_end - time_start), buf); - } -} - diff --git a/assignments/mqtt-with-button/mqtt-with-button.md b/assignments/mqtt-with-button/mqtt-with-button.md new file mode 100644 index 0000000..6b8872b --- /dev/null +++ b/assignments/mqtt-with-button/mqtt-with-button.md @@ -0,0 +1,57 @@ +# Assignment: MQTT with button + +## Goals + +* Get aquainted with MQTT. +* Publish a message when a button is pressed. + +## Step 1 + +Wire up this schematic on the bread board: + +![](schematic/mqtt-with-button_schem.pdf) + +![](schematic/mqtt-with-button_bb.pdf) + +# Step 2 + +* Read button, print message on terminal + +# Step 3 + +* Connect to the Wi-Fi network + * Use `WiFi.localIP()` + +* Connect to MQTT broker + +# Step 4 + +* Publish a message on button press + +## Tips + +To generate a client id make something with `ESP.getChipId()`{.cpp} + +Creating a `String` from a number: + +* `String(123) => "123"`{.cpp} +* Hex formatted: `String(0x123abc, HEX) => "123abc"`{.cpp} + +Some APIs require "plain C strings" aka a `char *`{.cpp}. They can be converted with `String::c_str()`: + +~~~.c++ +char *cStr = myString.c_str(); +~~~ + +## Bonus + +**1:** Print the heap free size at regular intervals. + +**2:** Implement min, max and average temperature over configured interval. + +Suggested parameters: + +* Sample interval: 2 seconds +* Publish interval: 10 seconds + +**3:** Make sure the values are calculated even if we're reconnecting to the Wi-Fi or MQTT server. diff --git a/assignments/mqtt-with-button/mqtt-with-button.pdf b/assignments/mqtt-with-button/mqtt-with-button.pdf new file mode 100644 index 0000000..ffb779e Binary files /dev/null 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 new file mode 100644 index 0000000..57e5e5d Binary files /dev/null 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 new file mode 100644 index 0000000..aa0d283 Binary files /dev/null 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 new file mode 100644 index 0000000..3ce5de3 Binary files /dev/null and b/assignments/mqtt-with-button/schematic/mqtt-with-button_schem.pdf differ diff --git a/assignments/mqtt/mqtt.md b/assignments/mqtt/mqtt.md deleted file mode 100644 index f52eed3..0000000 --- a/assignments/mqtt/mqtt.md +++ /dev/null @@ -1,41 +0,0 @@ -# Assignment: MQTT - -## Goal - -Get aquainted with MQTT. - -## Steps - -1. Connect to the Wi-Fi network - * Use `WiFi.localIP()` - -1. Connect to MQTT broker -1. Publish temperature - -## Tips - -To generate a client id make something with `ESP.getChipId()`{.cpp} - -Creating a `String` from a number: - -* `String(123) => "123"`{.cpp} -* Hex formatted: `String(0x123abc, HEX) => "123abc"`{.cpp} - -Some APIs require "plain C strings" aka a `char *`{.cpp}. They can be converted with `String::c_str()`: - -~~~.c++ -char *cStr = myString.c_str(); -~~~ - -## Bonus - -**1:** Print the heap free size at regular intervals. - -**2:** Implement min, max and average temperature over configured interval. - -Suggested parameters: - -* Sample interval: 2 seconds -* Publish interval: 10 seconds - -**3:** Make sure the values are calculated even if we're reconnecting to the Wi-Fi or MQTT server. diff --git a/assignments/mqtt/mqtt.pdf b/assignments/mqtt/mqtt.pdf deleted file mode 100644 index 8d35bb7..0000000 Binary files a/assignments/mqtt/mqtt.pdf and /dev/null differ diff --git a/assignments/mqtt2/mqtt2.md b/assignments/mqtt2/mqtt2.md deleted file mode 100644 index cfc623a..0000000 --- a/assignments/mqtt2/mqtt2.md +++ /dev/null @@ -1,10 +0,0 @@ -# Assignment: MQTT - -## Goal - -Get to know will messages and retained messages. - -## Steps - -1. Implement last will to indicate online status -1. Retained message diff --git a/assignments/mqtt2/mqtt2.pdf b/assignments/mqtt2/mqtt2.pdf deleted file mode 100644 index fdd47f6..0000000 Binary files a/assignments/mqtt2/mqtt2.pdf and /dev/null differ diff --git a/assignments/mqtt3/mqtt3.md b/assignments/mqtt3/mqtt3.md deleted file mode 100644 index 2994e9a..0000000 --- a/assignments/mqtt3/mqtt3.md +++ /dev/null @@ -1,12 +0,0 @@ -# Assignment: MQTT - -## Goal - -Connect two devices with MQTT - -## Steps - -1. Publish an alert -1. Subscribe to alert topic, blink a led when you get a message -1. Implement subscription to reconfigure device - 1. Change temperature report interval diff --git a/assignments/mqtt3/mqtt3.pdf b/assignments/mqtt3/mqtt3.pdf deleted file mode 100644 index 179a73e..0000000 Binary files a/assignments/mqtt3/mqtt3.pdf and /dev/null differ diff --git a/assignments/network/network.md b/assignments/network/network.md deleted file mode 100644 index 8e49b24..0000000 --- a/assignments/network/network.md +++ /dev/null @@ -1,7 +0,0 @@ -## Assignment: Network play time - -* Measure round trip time/latency. Measure UDP, TCP. Measure when the - packet size is greater than the MTU - -* Notice variations in RTT - diff --git a/assignments/old/client1/client1.ino b/assignments/old/client1/client1.ino new file mode 100644 index 0000000..39eabfe --- /dev/null +++ b/assignments/old/client1/client1.ino @@ -0,0 +1,71 @@ +#include +#include + +#include "config.h" + +WiFiUDP Udp; +unsigned int localUdpPort = 5006; +IPAddress host_ip(192, 168, 10, 151); +unsigned int host_port = 5005; + +const int out_buf_len_start = 100; +const int out_buf_len_increment = 100; +const int out_buf_len_max = 2200; + +char buf[out_buf_len_max]; +int out_buf_len; + +static const char alphabet[] = "abcdefghijklmnopqrstuvxyz"; + +unsigned long time_start, time_end; + +void setup() { + Serial.begin(115200); + + Serial.printf("Connecting to %s ", wifi_ssid); + WiFi.begin(wifi_ssid, wifi_password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(" connected"); + + Udp.begin(localUdpPort); + time_start = millis(); + out_buf_len = out_buf_len_start; +} + +void loop() { + + if (millis() > (time_start + 1000)) { + for (int i = 0; i < out_buf_len; i++) { + buf[i] = alphabet[i % sizeof(alphabet) - 1]; + } + out_buf_len += out_buf_len_increment; + if (out_buf_len > out_buf_len_max) { + out_buf_len = out_buf_len_start; + } + Serial.printf("Sending %d bytes to %s:%d\n", out_buf_len, host_ip.toString().c_str(), host_port); + Udp.beginPacket(host_ip, host_port); + Udp.write(buf, out_buf_len); + time_start = millis(); + auto ok = Udp.endPacket(); + + if (!ok) { + Serial.printf("Could not send packet with size %d\n", out_buf_len); + } + } + + int len = Udp.parsePacket(); + if (len) { + time_end = millis(); + Serial.printf("RX %s:%d: size=%d\n", Udp.remoteIP().toString().c_str(), Udp.remotePort(), len); + int len = Udp.read(buf, sizeof(buf)); + Serial.printf("len=%d\n"); + if (len > 0) { + buf[len] = 0; + } + Serial.printf("RTT: %d ms. Payload:%s\n", int(time_end - time_start), buf); + } +} + diff --git a/assignments/old/mqtt2/mqtt2.md b/assignments/old/mqtt2/mqtt2.md new file mode 100644 index 0000000..cfc623a --- /dev/null +++ b/assignments/old/mqtt2/mqtt2.md @@ -0,0 +1,10 @@ +# Assignment: MQTT + +## Goal + +Get to know will messages and retained messages. + +## Steps + +1. Implement last will to indicate online status +1. Retained message diff --git a/assignments/old/mqtt2/mqtt2.pdf b/assignments/old/mqtt2/mqtt2.pdf new file mode 100644 index 0000000..fdd47f6 Binary files /dev/null and b/assignments/old/mqtt2/mqtt2.pdf differ diff --git a/assignments/old/mqtt3/mqtt3.md b/assignments/old/mqtt3/mqtt3.md new file mode 100644 index 0000000..2994e9a --- /dev/null +++ b/assignments/old/mqtt3/mqtt3.md @@ -0,0 +1,12 @@ +# Assignment: MQTT + +## Goal + +Connect two devices with MQTT + +## Steps + +1. Publish an alert +1. Subscribe to alert topic, blink a led when you get a message +1. Implement subscription to reconfigure device + 1. Change temperature report interval diff --git a/assignments/old/mqtt3/mqtt3.pdf b/assignments/old/mqtt3/mqtt3.pdf new file mode 100644 index 0000000..179a73e Binary files /dev/null and b/assignments/old/mqtt3/mqtt3.pdf differ diff --git a/assignments/old/network/network.md b/assignments/old/network/network.md new file mode 100644 index 0000000..8e49b24 --- /dev/null +++ b/assignments/old/network/network.md @@ -0,0 +1,7 @@ +## Assignment: Network play time + +* Measure round trip time/latency. Measure UDP, TCP. Measure when the + packet size is greater than the MTU + +* Notice variations in RTT + diff --git a/assignments/old/read-temperature/read-temperature.md b/assignments/old/read-temperature/read-temperature.md new file mode 100644 index 0000000..31a32a8 --- /dev/null +++ b/assignments/old/read-temperature/read-temperature.md @@ -0,0 +1,13 @@ +# Read the temperature and humidity sensor DHT22 + +## Step 1 + +Wire up this schematic on the bread board: + +![](schematic/read-temperature_schem.pdf) + +![](schematic/read-temperature_bb.pdf) + +## Step 2 + +* Change sampling frequency diff --git a/assignments/old/read-temperature/read-temperature.pdf b/assignments/old/read-temperature/read-temperature.pdf new file mode 100644 index 0000000..f1ec641 Binary files /dev/null and b/assignments/old/read-temperature/read-temperature.pdf differ diff --git a/assignments/old/read-temperature/schematic/read-temperature.fzz b/assignments/old/read-temperature/schematic/read-temperature.fzz new file mode 100644 index 0000000..52a9fa0 Binary files /dev/null and b/assignments/old/read-temperature/schematic/read-temperature.fzz differ diff --git a/assignments/old/read-temperature/schematic/read-temperature_bb.pdf b/assignments/old/read-temperature/schematic/read-temperature_bb.pdf new file mode 100644 index 0000000..79cc8eb Binary files /dev/null and b/assignments/old/read-temperature/schematic/read-temperature_bb.pdf differ diff --git a/assignments/old/read-temperature/schematic/read-temperature_schem.pdf b/assignments/old/read-temperature/schematic/read-temperature_schem.pdf new file mode 100644 index 0000000..d01ab4b Binary files /dev/null and b/assignments/old/read-temperature/schematic/read-temperature_schem.pdf differ diff --git a/assignments/old/read-temperature/solution/read-temperature/read-temperature.ino b/assignments/old/read-temperature/solution/read-temperature/read-temperature.ino new file mode 100644 index 0000000..b261dc5 --- /dev/null +++ b/assignments/old/read-temperature/solution/read-temperature/read-temperature.ino @@ -0,0 +1,24 @@ +#include +#include + +const auto DHTPIN = D1; +const auto DHTTYPE = DHT22; + +DHT_Unified dht(DHTPIN, DHTTYPE); + +void setup() { + Serial.begin(115200); + pinMode(D2, INPUT); + pinMode(D0, OUTPUT); +} + +void loop() { + if (digitalRead(D2) == HIGH) { + Serial.println("HIGH"); + } else { + Serial.println("LOW"); + } + digitalWrite(D0, digitalRead(D2)); + delay(100); +} + diff --git a/assignments/read-temperature/read-temperature.md b/assignments/read-temperature/read-temperature.md deleted file mode 100644 index 31a32a8..0000000 --- a/assignments/read-temperature/read-temperature.md +++ /dev/null @@ -1,13 +0,0 @@ -# Read the temperature and humidity sensor DHT22 - -## Step 1 - -Wire up this schematic on the bread board: - -![](schematic/read-temperature_schem.pdf) - -![](schematic/read-temperature_bb.pdf) - -## Step 2 - -* Change sampling frequency diff --git a/assignments/read-temperature/read-temperature.pdf b/assignments/read-temperature/read-temperature.pdf deleted file mode 100644 index f1ec641..0000000 Binary files a/assignments/read-temperature/read-temperature.pdf and /dev/null differ diff --git a/assignments/read-temperature/schematic/read-temperature.fzz b/assignments/read-temperature/schematic/read-temperature.fzz deleted file mode 100644 index 52a9fa0..0000000 Binary files a/assignments/read-temperature/schematic/read-temperature.fzz and /dev/null differ diff --git a/assignments/read-temperature/schematic/read-temperature_bb.pdf b/assignments/read-temperature/schematic/read-temperature_bb.pdf deleted file mode 100644 index 79cc8eb..0000000 Binary files a/assignments/read-temperature/schematic/read-temperature_bb.pdf and /dev/null differ diff --git a/assignments/read-temperature/schematic/read-temperature_schem.pdf b/assignments/read-temperature/schematic/read-temperature_schem.pdf deleted file mode 100644 index d01ab4b..0000000 Binary files a/assignments/read-temperature/schematic/read-temperature_schem.pdf and /dev/null differ diff --git a/assignments/read-temperature/solution/read-temperature/read-temperature.ino b/assignments/read-temperature/solution/read-temperature/read-temperature.ino deleted file mode 100644 index b261dc5..0000000 --- a/assignments/read-temperature/solution/read-temperature/read-temperature.ino +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -const auto DHTPIN = D1; -const auto DHTTYPE = DHT22; - -DHT_Unified dht(DHTPIN, DHTTYPE); - -void setup() { - Serial.begin(115200); - pinMode(D2, INPUT); - pinMode(D0, OUTPUT); -} - -void loop() { - if (digitalRead(D2) == HIGH) { - Serial.println("HIGH"); - } else { - Serial.println("LOW"); - } - digitalWrite(D0, digitalRead(D2)); - delay(100); -} - -- cgit v1.2.3