--- tags: mdef, fablab --- [TOC] # Interface: machine vs machine :::info Useful links for this class: - [ESP32 Feather](https://hackmd.io/OcD2aBtRTG2pRfJKVV8CBg#ESP32-Feather) - [Networking Class](https://hackmd.io/Bl2DDqo6SrOX7SQMwyMbLA?view#Networking-and-communications) - [MQTT-Feather](https://hackmd.io/CAj0Y8O3QGKjmH2b4r3Tag?view) ::: ## How hard can it be to turn on a LED? ![](https://cdn-shop.adafruit.com/1200x900/297-00.jpg) In this class, we will do only one thing: turn on a LED. However, we will do it in many different ways: - The hardcoded way - Asking for it - Making it do animations - Asking for it from the internet (I and II) - Making sensors do it remotely ## LEDs are fun! ![](https://i.imgur.com/b9KgTWl.png) ![](https://oscgonfer.gitlab.io/portfolio//assets/images/mask.jpeg) :::success **More info:** https://github.com/oscgonfer/iron-skulls-mask ::: ## Hands on! **Resistor: 200-300Ω** ![](https://i.imgur.com/N5mcWKH.png) ### The hardcoded way - Blink :::spoiler The code is here Oups! You should know this already! :smiley: ::: ### Asking for it - Serial :::spoiler The code is here ```cpp= #include "Arduino.h" #define LED_PIN 14 // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_PIN, OUTPUT); Serial.begin(9600); } void blink () { digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_PIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } // the loop function runs over and over again forever void loop() { if (Serial.available()) { String newMsg = Serial.readString(); newMsg.trim(); Serial.print("Got new message!: "); Serial.println(newMsg); // blink if we tell it to! if (newMsg.equals("blink")){ blink(); } } } ``` ::: :::info **Extra!** Can you make it do something else with a different word? ::: ### Cool animations :::warning **JLED Installation** ![](https://i.imgur.com/XZ29Vco.png) ::: :::spoiler The code is here ```cpp= #include "Arduino.h" #include "JLED.h" #define LED_PIN 14 // Jled object. // More information here: https://github.com/jandelgado/jled#usage auto led = JLed(LED_PIN); // the setup function runs once when you press reset or power the board void setup() { Serial.begin(9600); } // Basic blink void blink () { led.Blink(1000, 600).Repeat(3); } // Smooth breathing void breathe() { led.Breathe(1000).Repeat(3); } // the loop function runs over and over again forever void loop() { if (Serial.available()) { // Read the string and clean it up String newMsg = Serial.readString(); newMsg.trim(); // For debugging purposes, print it Serial.print("Got new message!: "); Serial.println(newMsg); // Blink if we tell it to! if (newMsg.equals("blink")){ blink(); // Or breathe! } else if (newMsg.equals("breathe")) { breathe(); } } // Do not remove this line! led.Update(); } ``` ::: :::info **Extra!** Can you make it do other animations with different words? Check here! https://github.com/jandelgado/jled#usage ::: ### Online LEDS! :::info Dashboard! http://13.38.136.18:1880/ui ::: ![](https://i.imgur.com/zSYdY2d.png) :::spoiler The code is here ```cpp= #include "jled.h" #define LED_PIN 14 // New code for MQTT! //------------------- #include <WiFi.h> #include <PubSubClient.h> const char* ssid = "-"; const char* password = "-"; WiFiClient wifiClient; const char* mqttBroker = "mqtt-staging.smartcitizen.me"; const char* mqttClientName = ""; //Change this const char* mqttClientUser = "fablabbcn102"; //dont change this const char* mqttClientPass = ""; const char* topicToSub = "lab"; //dont change this // const char* topicToPub = "lab"; PubSubClient mqttClient(wifiClient); //------------------- // Jled object. // More information here: https://github.com/jandelgado/jled#usage JLed led = JLed(LED_PIN); //------------------- // Add more animations here! // Basic blink void blink () { led.Blink(1000, 600).Repeat(3); } // Smooth breathing void breathe() { led.Breathe(1000).Repeat(3); } //------------------- void mqttConnect() { while (!mqttClient.connected()) { Serial.print("Attempting MQTT connection..."); if (mqttClient.connect(mqttClientName, mqttClientUser, mqttClientPass)) { Serial.println("connected"); mqttClient.publish("hello", mqttClientName); // Topic(s) subscription mqttClient.subscribe(topicToSub); } else { Serial.print("failed, rc="); Serial.print(mqttClient.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void callback(char* topic, byte* message, unsigned int length) { String newMsg; for (int i = 0; i < length; i++) { newMsg += (char)message[i]; } Serial.print("Message arrived on topic: "); Serial.print(topic); Serial.print(". Message: "); Serial.println(newMsg); if (String(topic) == topicToSub) { // For debugging purposes, print it Serial.print("Got new message!: "); Serial.println(newMsg); // Blink if we tell it to! if (newMsg.equals("blink")){ blink(); // Or breathe! } else if (newMsg.equals("breathe")) { breathe(); } } } // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. Serial.begin(9600); // Connect to wifi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // MQTT setup mqttClient.setServer(mqttBroker, 1883); mqttClient.setCallback(callback); } // the loop function runs over and over again forever void loop() { // Check if we are still connected to the MQTT broker if (!mqttClient.connected()) { mqttConnect(); } // Let PubSubClient library do his magic mqttClient.loop(); // Do not remove this line! led.Update(); } ``` ::: :::spoiler Inside Nodered * Function code: ``` msg.payload = `${msg.payload.reaction}/${msg.payload.intensity}` return msg; ``` * The join node: ![](https://i.imgur.com/DuSkjpE.png) ::: :::info **Extra!** Can you make it do other animations with different words? ::: ### Online leds, but cooler! ![](https://i.imgur.com/Yh02bBj.png) :::spoiler The code is here ```cpp= #include "Arduino.h" #include "jled.h" #define LED_PIN 14 // New code for MQTT! //------------------- #include <WiFi.h> #include <PubSubClient.h> const char* ssid = "-"; const char* password = "-"; WiFiClient wifiClient; const char* mqttBroker = "mqtt-staging.smartcitizen.me"; const char* mqttClientName = "team"; const char* mqttClientUser = "fablabbcn102"; const char* mqttClientPass = ""; const char* topicToSub = "lab"; // const char* topicToPub = "lab"; PubSubClient mqttClient(wifiClient); //------------------- // Jled object. // More information here: https://github.com/jandelgado/jled#usage JLed led = JLed(LED_PIN); //------------------- // Add more animations here! // Basic blink void blink (int time_on = 100) { led.Blink(time_on, time_on).Repeat(3); } // Smooth breathing void breathe(int time_on = 100) { led.Breathe(time_on).Repeat(3); } //------------------- void mqttConnect() { while (!mqttClient.connected()) { Serial.print("Attempting MQTT connection..."); if (mqttClient.connect(mqttClientName, mqttClientUser, mqttClientPass)) { Serial.println("connected"); mqttClient.publish("hello", mqttClientName); // Topic(s) subscription mqttClient.subscribe(topicToSub); } else { Serial.print("failed, rc="); Serial.print(mqttClient.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void callback(char* topic, byte* message, unsigned int length) { String newMsg; for (int i = 0; i < length; i++) { newMsg += (char)message[i]; } // Serial.print("Message arrived on topic: "); // Serial.print(topic); // Serial.print(". Message: "); // Serial.println(newMsg); if (String(topic) == topicToSub) { // For debugging purposes, print it // Serial.print("Got new message!: "); // Serial.println(newMsg); int pos = newMsg.indexOf("/"); String action; int param; if (pos > -1) { action = newMsg.substring(0, pos); param = newMsg.substring(pos+1).toInt(); } else { action = newMsg; param = 100; } // Serial.print("Action requested: "); // Serial.println(action); // Serial.print("Parameter: "); // Serial.println(param); // Blink if we tell it to! if (action.equals("blink")){ blink(param); // Or breathe! } else if (action.equals("breathe")) { breathe(param); } } } // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. Serial.begin(9600); // Connect to wifi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // MQTT setup mqttClient.setServer(mqttBroker, 1883); mqttClient.setCallback(callback); } // the loop function runs over and over again forever void loop() { // Check if we are still connected to the MQTT broker if (!mqttClient.connected()) { mqttConnect(); } // Let PubSubClient library do his magic mqttClient.loop(); // Do not remove this line! led.Update(); } ``` ::: ### Sensors and LEDs ![](https://i.imgur.com/UPl1Ik2.png) :::spoiler The code is here ```cpp= #include "Arduino.h" #include "JLED.h" #define LED_PIN 14 // New code for MQTT! //------------------- #include <WiFi.h> #include <PubSubClient.h> const char* ssid = "Iaac-Wifi"; const char* password = "EnterIaac22@"; WiFiClient wifiClient; const char* mqttBroker = "mqtt-staging.smartcitizen.me"; const char* mqttClientName = "team"; const char* mqttClientUser = "fablabbcn102"; const char* mqttClientPass = ""; const char* topicToSub = "lab/fun"; // const char* topicToPub = "lab"; PubSubClient mqttClient(wifiClient); //------------------- // Jled object. // More information here: https://github.com/jandelgado/jled#usage JLed led = JLed(LED_PIN); // Basic blink void blink (int time_on = 100) { led.Blink(time_on, time_on).Repeat(3); } // Smooth breathing void breathe(int time_on = 100) { led.Breathe(time_on).Repeat(3); } void mqttConnect() { while (!mqttClient.connected()) { Serial.print("Attempting MQTT connection..."); if (mqttClient.connect(mqttClientName, mqttClientUser, mqttClientPass)) { Serial.println("connected"); mqttClient.publish("hello", mqttClientName); // Topic(s) subscription mqttClient.subscribe(topicToSub); } else { Serial.print("failed, rc="); Serial.print(mqttClient.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void callback(char* topic, byte* message, unsigned int length) { String newMsg; for (int i = 0; i < length; i++) { newMsg += (char)message[i]; } Serial.print("Message arrived on topic: "); Serial.print(topic); Serial.print(". Message: "); Serial.println(newMsg); if (String(topic) == topicToSub) { // For debugging purposes, print it Serial.print("Got new message!: "); Serial.println(newMsg); int pos = newMsg.indexOf("/"); String action; int param; if (pos > -1) { action = newMsg.substring(0, pos); param = newMsg.substring(pos+1).toInt(); } else { action = newMsg; param = 100; } Serial.print("Action requested: "); Serial.println(action); Serial.print("Parameter: "); Serial.println(param); // Blink if we tell it to! if (action.equals("blink")){ blink(param); // Or breathe! } else if (action.equals("breathe")) { breathe(param); } } } // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. Serial.begin(9600); // Connect to wifi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // MQTT setup mqttClient.setServer(mqttBroker, 1883); mqttClient.setCallback(callback); } // the loop function runs over and over again forever void loop() { // Check if we are still connected to the MQTT broker if (!mqttClient.connected()) { mqttConnect(); } // Let PubSubClient library do his magic mqttClient.loop(); // Do not remove this line! led.Update(); // This is pseudocode. Uncomment and read a button to send your data! //------------------- // if (buttonPressed) { // mqttClient.publish(topicToPub, msg); // } //------------------- } ``` ::: :::spoiler Inside nodered * The function code ``` msg.payload = `${msg.payload.reaction}/${msg.payload["lab/test"]}` return msg; ``` :::