From 3c04ab07f66f279b0a346b91c6b4a131acb1d156 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 14 Jun 2018 11:57:29 +0200 Subject: wip --- assignments/blink-a-led/blink-a-led.md | 26 ++++++++--- assignments/blink-a-led/blink-a-led.pdf | Bin 722602 -> 731445 bytes assignments/mqtt-with-button/mqtt-with-button.md | 50 ++++++++++++---------- assignments/mqtt-with-button/mqtt-with-button.pdf | Bin 841066 -> 857213 bytes 4 files changed, 47 insertions(+), 29 deletions(-) (limited to 'assignments') diff --git a/assignments/blink-a-led/blink-a-led.md b/assignments/blink-a-led/blink-a-led.md index a08bc4e..72bbb9c 100644 --- a/assignments/blink-a-led/blink-a-led.md +++ b/assignments/blink-a-led/blink-a-led.md @@ -6,10 +6,19 @@ Check that your local environment is working properly. ## Configure the Arduino IDE -Under *Tools* -> *Board* make sure that "NodeMCU 1.0 (ESP-12E Module)" -is available and selected. +1. Make sure the "ESP8266" core is installed. If it is not installed + follow the installation instructions: -Create a new sketch with *File* -> *New* and compile it with *ctrl-R*. + [https://github.com/esp8266/Arduino#installing-with-boards-manager](https://github.com/esp8266/Arduino#installing-with-boards-manager) + +1. Under *Tools* -> *Board* make sure that "NodeMCU 1.0 (ESP-12E + Module)" is available and selected. + +1. Select the correct serial port: *Tools -> Port*. + +1. Select the highest upload speed: *Tools -> Upload Speed*. + +1. Create a new sketch with *File* -> *New* and compile it with *ctrl-R*. ## Step 1 @@ -24,7 +33,7 @@ orientation is not important, but the LED's orientation is important. ## Step 2 -Implement `setup()` and `loop()`. In `setup()` configure the LED pin and blink the LED in `loop()`. +Implement `setup()` and `loop()`: In `setup()` configure the LED pin and blink the LED in `loop()`. Use these functions: @@ -32,9 +41,14 @@ Use these functions: Serial.begin(115200); Serial.println(string); +// Pin is D1/D2/D.. +// mode is INPUT or OUTPUT pinMode(pin, mode); -digitalWrite(pin, state); // HIGH or LOW -delay(); + +// HIGH or LOW +digitalWrite(pin, state); + +delay(ms); ~~~ ## Tips diff --git a/assignments/blink-a-led/blink-a-led.pdf b/assignments/blink-a-led/blink-a-led.pdf index da370c4..655f964 100644 Binary files a/assignments/blink-a-led/blink-a-led.pdf and b/assignments/blink-a-led/blink-a-led.pdf differ diff --git a/assignments/mqtt-with-button/mqtt-with-button.md b/assignments/mqtt-with-button/mqtt-with-button.md index c825615..30defc8 100644 --- a/assignments/mqtt-with-button/mqtt-with-button.md +++ b/assignments/mqtt-with-button/mqtt-with-button.md @@ -6,6 +6,11 @@ * Publish a message when a button is pressed. * Subscribe to a topic to control the LED +## Preparation + +* Install the *PubSubClient* library. Use the library manager under + *Sketch -> Include library -> Library Manager* to install it. + ## Step 1 Wire up this schematic on the bread board: @@ -16,10 +21,8 @@ Wire up this schematic on the bread board: # Step 2 -* Tip: change upload speed to max. - -* Read button in `loop()`. If the button's state changes, print a - message. +* Read button's value inside `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`. @@ -29,34 +32,35 @@ practice as it uses lots of energy. Instead use the `attachInterrupt`. * Connect to the Wi-Fi network * Connect to MQTT broker +See the slides for example code. + # Step 4 -* Publish a message on button press on the topic +* Create a subscription for `ndc/$device-id/#` with HiveMQ's web ui: + http://www.hivemq.com/demos/websocket-client/ +* Publish a message when the button is pressed on the topic `ndc/$device-id/button` # 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 - LED's blinking pattern. +*Feel free to be creative here.* -## Tips +The other useful half of MQTT is subscribing to topics and reacting to +messages. Here are some example things you can do: -To generate a client id make something with `ESP.getChipId()`{.cpp} +* Subscribe to the topic you're publishing to and change the state of + the LED when the button is pressed. This will give you a very + complicated and brittle way of toggling a LED. -Creating a `String` from a number: +* In `loop` blink the led. Subscribe to another topic (for example + `ndc/$device-id/frequency`), and use the value as a way to control + the blink rate. -* `String(123) => "123"`{.cpp} -* Hex formatted: `String(0x123abc, HEX) => "123abc"`{.cpp} +# Step 6 (Strech goal) -Some APIs require "plain C strings" aka a `char *`{.cpp}. They can be -converted with `String::c_str()`: +Use a last will message to indicate if the device is online or not. -~~~.c++ -char *cStr = myString.c_str(); -~~~ +* When connecting, include a last will message with topic + `ndc/$device-id/online` and payload `0`. +* After a successfull connection, publish a similar message with the + payload `1`. Observe what happens when you unplug the device. diff --git a/assignments/mqtt-with-button/mqtt-with-button.pdf b/assignments/mqtt-with-button/mqtt-with-button.pdf index 8fcccf7..ccb511e 100644 Binary files a/assignments/mqtt-with-button/mqtt-with-button.pdf and b/assignments/mqtt-with-button/mqtt-with-button.pdf differ -- cgit v1.2.3