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/30 20:35] – jan.sonntag | latinet:unicaes:workshops:communication-23 [2024/09/10 20:39] (current) – harley.lara | ||
|---|---|---|---|
| Line 10: | Line 10: | ||
| Next, we'll take a closer look at connecting the ESP8266 to WiFi. This step is crucial for enabling wireless communication, | Next, we'll take a closer look at connecting the ESP8266 to WiFi. This step is crucial for enabling wireless communication, | ||
| - | < | + | 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: |
| + | |||
| + | < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| + | |||
| + | const uint16_t HTTP_PORT = 443; // Use 443 for HTTPS and 80 for HTTP | ||
| // Replace with your WiFi credentials | // Replace with your WiFi credentials | ||
| - | const char* ssid = "wifi-ssid"; | + | const char* SSID = "iotlab"; |
| - | const char* password | + | const char* PASSWORD |
| // Replace with your API details | // Replace with your API details | ||
| - | const char* apiHost | + | const char* API_HOST |
| - | const char* apiEndpoint | + | const char* API_ENDPOINT |
| + | |||
| + | const uint8_t PERIOD_MINUTES = 1; | ||
| // Create an instance of WiFiClientSecure | // Create an instance of WiFiClientSecure | ||
| - | WiFiClientSecure | + | WiFiClientSecure |
| void setup() { | void setup() { | ||
| Serial.begin(115200); | Serial.begin(115200); | ||
| - | | + | |
| // Connect to Wi-Fi | // Connect to Wi-Fi | ||
| - | WiFi.begin(ssid, password); | + | WiFi.begin(SSID, PASSWORD); |
| - | + | ||
| - | Serial.print(" | + | Serial.print(" |
| - | Serial.print(ssid); | + | Serial.print(SSID); |
| - | + | ||
| while (WiFi.status() != WL_CONNECTED) { | while (WiFi.status() != WL_CONNECTED) { | ||
| delay(1000); | delay(1000); | ||
| Serial.print(" | Serial.print(" | ||
| } | } | ||
| - | | + | |
| Serial.println(" | Serial.println(" | ||
| // Set the client to verify the server' | // Set the client to verify the server' | ||
| - | | + | |
| // Give the client a chance to perform the handshake | // Give the client a chance to perform the handshake | ||
| Line 50: | Line 56: | ||
| void loop() { | void loop() { | ||
| if (WiFi.status() == WL_CONNECTED) { | if (WiFi.status() == WL_CONNECTED) { | ||
| + | |||
| + | wifi_client.connect(API_HOST, | ||
| // Make an HTTPS GET request | // Make an HTTPS GET request | ||
| - | | + | |
| - | client.print(String("GET ") + apiEndpoint | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| // Wait for the response | // Wait for the response | ||
| - | while (client.connected()) { | + | while (wifi_client.connected()) { |
| - | | + | |
| - | if (line == " | + | |
| - | | + | char c = wifi_client.read(); |
| - | | + | |
| } | } | ||
| } | } | ||
| - | | + | |
| - | | + | |
| - | while (client.available()) { | + | |
| - | String response = client.readStringUntil(' | + | |
| - | Serial.println(response); | + | |
| - | } | + | |
| - | + | ||
| - | client.stop(); | + | |
| } | } | ||
| - | | + | |
| // Wait for a while before making the next request | // Wait for a while before making the next request | ||
| - | delay(5000); | + | delay(PERIOD_MINUTES * 60 * 1000); // convert from min to sec (60) and from sec to ms (1000) |
| } | } | ||
| - | |||
| </ | </ | ||
| - | |||
| - | 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: [[weather_station: | ||
| ===== 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, | Stepping ahead, you'll have the chance to implement your first MQTT publish from the ESP8266. While the following example provides a static demonstration, | ||
| + | |||
| + | To start, search and install the library " | ||
| + | |||
| + | <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. | 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. | ||
| - | < | + | < |
| /* | /* | ||
| Basic ESP8266 MQTT example | Basic ESP8266 MQTT example | ||
| Line 109: | Line 217: | ||
| // Update these with values suitable for your network and mqtt broker | // Update these with values suitable for your network and mqtt broker | ||
| - | const char* ssid = "iotlab"; | + | const char* ssid = ""; |
| - | const char* password = "iotlab18"; | + | const char* password = ""; |
| const char* mqtt_server = " | const char* mqtt_server = " | ||
latinet/unicaes/workshops/communication-23.1693420518.txt.gz · Last modified: 2023/08/30 20:35 by jan.sonntag