latinet:unicaes:workshops:communication-23
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
latinet:unicaes:workshops:communication-23 [2023/08/29 20:00] – jan.sonntag | latinet:unicaes:workshops:communication-23 [2024/03/23 19:02] (current) – ↷ Links adapted because of a move operation 47.128.112.114 | ||
---|---|---|---|
Line 8: | Line 8: | ||
===== 2. ESP8266 and WiFi ===== | ===== 2. ESP8266 and WiFi ===== | ||
+ | Next, we'll take a closer look at connecting the ESP8266 to WiFi. This step is crucial for enabling wireless communication, | ||
+ | |||
+ | <file c++ wifi-http-api-example.ino> | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // Replace with your WiFi credentials | ||
+ | const char* ssid = " | ||
+ | const char* password = " | ||
+ | |||
+ | // Replace with your API details | ||
+ | const char* apiHost = " | ||
+ | const char* apiEndpoint = "/ | ||
+ | |||
+ | // Create an instance of WiFiClientSecure | ||
+ | WiFiClientSecure client; | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(115200); | ||
+ | | ||
+ | // Connect to Wi-Fi | ||
+ | WiFi.begin(ssid, | ||
+ | | ||
+ | Serial.print(" | ||
+ | Serial.print(ssid); | ||
+ | | ||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(1000); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | | ||
+ | Serial.println(" | ||
+ | |||
+ | // Set the client to verify the server' | ||
+ | client.setInsecure(); | ||
+ | |||
+ | // Give the client a chance to perform the handshake | ||
+ | delay(1000); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | if (WiFi.status() == WL_CONNECTED) { | ||
+ | // Make an HTTPS GET request | ||
+ | client.connect(apiHost, | ||
+ | client.print(String(" | ||
+ | " | ||
+ | " | ||
+ | | ||
+ | // Wait for the response | ||
+ | while (client.connected()) { | ||
+ | String line = client.readStringUntil(' | ||
+ | if (line == " | ||
+ | Serial.println(" | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | // Print the response from the server | ||
+ | while (client.available()) { | ||
+ | String response = client.readStringUntil(' | ||
+ | Serial.println(response); | ||
+ | } | ||
+ | | ||
+ | client.stop(); | ||
+ | } | ||
+ | | ||
+ | // Wait for a while before making the next request | ||
+ | delay(5000); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | In this example code, you will first of all connect to a WiFi network and then try to access a predefined API. This API is the root of our public weather station API. You can find out more about the project right here: [[eolab: | ||
===== 3. Our first MQTT Publish ===== | ===== 3. Our first MQTT Publish ===== | ||
+ | Stepping ahead, you'll have the chance to implement your first MQTT publish from the ESP8266. While the following example provides a static demonstration, | ||
+ | |||
+ | PubSubClient Library: [[https:// | ||
+ | |||
+ | <file c++ mqtt-publish.ino> | ||
+ | /* | ||
+ | Basic ESP8266 MQTT example | ||
+ | This sketch demonstrates the capabilities of the pubsub library in combination | ||
+ | with the ESP8266 board/ | ||
+ | It connects to an MQTT server then: | ||
+ | - publishes "hello world" to the topic defined as outTopic every two seconds | ||
+ | It will reconnect to the server if the connection is lost using a blocking | ||
+ | | ||
+ | | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define MSG_BUFFER_SIZE (50) | ||
+ | |||
+ | // Update these with values suitable for your network and mqtt broker | ||
+ | |||
+ | const char* ssid = ""; | ||
+ | const char* password = ""; | ||
+ | const char* mqtt_server = " | ||
+ | |||
+ | const char* outTopic = " | ||
+ | const char* inTopic = " | ||
+ | |||
+ | // Set up of some needed variables | ||
+ | |||
+ | WiFiClient espClient; | ||
+ | PubSubClient client(espClient); | ||
+ | unsigned long lastMsg = 0; | ||
+ | char msg[MSG_BUFFER_SIZE]; | ||
+ | int value = 0; | ||
+ | |||
+ | void setup_wifi() { | ||
+ | |||
+ | delay(10); | ||
+ | // We start by connecting to a WiFi network | ||
+ | Serial.println(); | ||
+ | Serial.print(" | ||
+ | Serial.println(ssid); | ||
+ | |||
+ | WiFi.mode(WIFI_STA); | ||
+ | WiFi.begin(ssid, | ||
+ | |||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | |||
+ | randomSeed(micros()); | ||
+ | |||
+ | Serial.println("" | ||
+ | Serial.println(" | ||
+ | Serial.println(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | } | ||
+ | |||
+ | void reconnect() { | ||
+ | // Loop until we're reconnected | ||
+ | while (!client.connected()) { | ||
+ | Serial.print(" | ||
+ | // Create a random client ID | ||
+ | String clientId = " | ||
+ | clientId += String(random(0xffff), | ||
+ | // Attempt to connect | ||
+ | if (client.connect(clientId.c_str())) { | ||
+ | Serial.println(" | ||
+ | // Once connected, publish an announcement... | ||
+ | client.publish(outTopic, | ||
+ | } else { | ||
+ | Serial.print(" | ||
+ | Serial.print(client.state()); | ||
+ | Serial.println(" | ||
+ | // Wait 5 seconds before retrying | ||
+ | delay(5000); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(BUILTIN_LED, | ||
+ | Serial.begin(115200); | ||
+ | setup_wifi(); | ||
+ | client.setServer(mqtt_server, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | |||
+ | if (!client.connected()) { | ||
+ | reconnect(); | ||
+ | } | ||
+ | client.loop(); | ||
+ | |||
+ | unsigned long now = millis(); | ||
+ | if (now - lastMsg > 2000) { | ||
+ | lastMsg = now; | ||
+ | ++value; | ||
+ | snprintf (msg, MSG_BUFFER_SIZE, | ||
+ | Serial.print(" | ||
+ | Serial.println(msg); | ||
+ | client.publish(outTopic, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
===== 4. Subscribe ===== | ===== 4. Subscribe ===== | ||
+ | Now, let's move on to an essential aspect of our workshop. We'll guide you through the process of subscribing to an MQTT topic. This step is a practical gateway to interactive IoT applications. You'll learn how to exchange MQTT messages between devices, potentially triggering actions like LED reactions or serial outputs. While we'll provide initial guidance, feel free to explore further and experiment with this dynamic feature. | ||
+ | |||
+ | <file c++ mqtt-subscribe-example.ino> | ||
+ | /* | ||
+ | Basic ESP8266 MQTT example | ||
+ | This sketch demonstrates the capabilities of the pubsub library in combination | ||
+ | with the ESP8266 board/ | ||
+ | It connects to an MQTT server then: | ||
+ | - publishes "hello world" to the topic defined as outTopic every two seconds | ||
+ | - subscribes to the topic defined as inTopic, printing out any messages | ||
+ | it receives. NB - it assumes the received payloads are strings not binary | ||
+ | - If the first character of the topic defined as inTopic is an 1, switch ON the ESP Led, | ||
+ | else switch it off | ||
+ | It will reconnect to the server if the connection is lost using a blocking | ||
+ | | ||
+ | | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // Update these with values suitable for your network and mqtt broker | ||
+ | |||
+ | const char* ssid = ""; | ||
+ | const char* password = ""; | ||
+ | const char* mqtt_server = " | ||
+ | |||
+ | const char* outTopic = " | ||
+ | const char* inTopic = " | ||
+ | |||
+ | // Set up of some needed variables | ||
+ | |||
+ | WiFiClient espClient; | ||
+ | PubSubClient client(espClient); | ||
+ | unsigned long lastMsg = 0; | ||
+ | #define MSG_BUFFER_SIZE (50) | ||
+ | char msg[MSG_BUFFER_SIZE]; | ||
+ | int value = 0; | ||
+ | |||
+ | void setup_wifi() { | ||
+ | |||
+ | delay(10); | ||
+ | // We start by connecting to a WiFi network | ||
+ | Serial.println(); | ||
+ | Serial.print(" | ||
+ | Serial.println(ssid); | ||
+ | |||
+ | WiFi.mode(WIFI_STA); | ||
+ | WiFi.begin(ssid, | ||
+ | |||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | |||
+ | randomSeed(micros()); | ||
+ | |||
+ | Serial.println("" | ||
+ | Serial.println(" | ||
+ | Serial.println(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | } | ||
+ | |||
+ | void callback(char* topic, byte* payload, unsigned int length) { | ||
+ | Serial.print(" | ||
+ | Serial.print(topic); | ||
+ | Serial.print(" | ||
+ | for (int i = 0; i < length; i++) { | ||
+ | Serial.print((char)payload[i]); | ||
+ | } | ||
+ | Serial.println(); | ||
+ | |||
+ | // Switch on the LED if an 1 was received as first character | ||
+ | if ((char)payload[0] == ' | ||
+ | digitalWrite(BUILTIN_LED, | ||
+ | // but actually the LED is on; this is because | ||
+ | // it is active low on the ESP-01) | ||
+ | } else { | ||
+ | digitalWrite(BUILTIN_LED, | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | void reconnect() { | ||
+ | // Loop until we're reconnected | ||
+ | while (!client.connected()) { | ||
+ | Serial.print(" | ||
+ | // Create a random client ID | ||
+ | String clientId = " | ||
+ | clientId += String(random(0xffff), | ||
+ | // Attempt to connect | ||
+ | if (client.connect(clientId.c_str())) { | ||
+ | Serial.println(" | ||
+ | // Once connected, publish an announcement... | ||
+ | client.publish(outTopic, | ||
+ | // ... and resubscribe | ||
+ | client.subscribe(inTopic); | ||
+ | } else { | ||
+ | Serial.print(" | ||
+ | Serial.print(client.state()); | ||
+ | Serial.println(" | ||
+ | // Wait 5 seconds before retrying | ||
+ | delay(5000); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(BUILTIN_LED, | ||
+ | Serial.begin(115200); | ||
+ | setup_wifi(); | ||
+ | client.setServer(mqtt_server, | ||
+ | client.setCallback(callback); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | |||
+ | if (!client.connected()) { | ||
+ | reconnect(); | ||
+ | } | ||
+ | client.loop(); | ||
+ | |||
+ | unsigned long now = millis(); | ||
+ | if (now - lastMsg > 2000) { | ||
+ | lastMsg = now; | ||
+ | ++value; | ||
+ | snprintf (msg, MSG_BUFFER_SIZE, | ||
+ | Serial.print(" | ||
+ | Serial.println(msg); | ||
+ | client.publish(outTopic, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
===== Recording ===== | ===== Recording ===== |
latinet/unicaes/workshops/communication-23.txt · Last modified: 2024/03/23 19:02 by 47.128.112.114