User Tools

Site Tools


latinet:unicaes:workshops:communication-23

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
latinet:unicaes:workshops:communication-23 [2023/10/22 22:19] – [3. Our first MQTT Publish] rolf.beckerlatinet: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, allowing your devices to seamlessly interact with the digital world. Next, we'll take a closer look at connecting the ESP8266 to WiFi. This step is crucial for enabling wireless communication, allowing your devices to seamlessly interact with the digital world.
  
-<file ino wifi-http-api-example.ino>+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:weather_station:kamp-lintfort:start|HSRW Weather Station at Campus Kamp-Lintfort]]. 
 + 
 +<file c++ wifi-http-api-example.ino>
 #include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
 #include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
 +
 +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 = "wifi-password";+const char* PASSWORD = "iotlab18";
  
 // Replace with your API details // Replace with your API details
-const char* apiHost = "weather.eolab.de"; +const char* API_HOST = "weather.eolab.de"; 
-const char* apiEndpoint = "/api";+const char* API_ENDPOINT = "/api"
 + 
 +const uint8_t PERIOD_MINUTES = 1;
  
 // Create an instance of WiFiClientSecure // Create an instance of WiFiClientSecure
-WiFiClientSecure client;+WiFiClientSecure wifi_client;
  
 void setup() { void setup() {
   Serial.begin(115200);   Serial.begin(115200);
-  +
   // Connect to Wi-Fi   // Connect to Wi-Fi
-  WiFi.begin(ssidpassword); +  WiFi.begin(SSIDPASSWORD); 
-   + 
-  Serial.print("Connecting to "); +  Serial.print("\n\nConnecting to "); 
-  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("\nConnected to WiFi");   Serial.println("\nConnected to WiFi");
  
   // Set the client to verify the server's certificate   // Set the client to verify the server's certificate
-  client.setInsecure();+  wifi_client.setInsecure();
  
   // 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, HTTP_PORT);
     // Make an HTTPS GET request     // Make an HTTPS GET request
-    client.connect(apiHost, 443); // Use port 443 for HTTPS +    wifi_client.println("GET " + String(API_ENDPOINT) + " HTTP/1.1"); 
-    client.print(String("GET "apiEndpoint + " HTTP/1.1\r\n+ +    wifi_client.println("Host: " + String(API_HOST)); 
-                 "Host: " + apiHost + "\r\n" + +    wifi_client.println("Connection: close"); 
-                 "Connection: close\r\n\r\n"); +    wifi_client.println(); 
-    +
     // Wait for the response     // Wait for the response
-    while (client.connected()) { +    while (wifi_client.connected()) { 
-      String line = client.readStringUntil('\n')+      if (wifi_client.available()) { 
-      if (line == "\r") { +        // read an incoming byte from the server and print it to serial monitor: 
-        Serial.println("Headers received"); +        char c = wifi_client.read(); 
-        break;+        Serial.print(c);
       }       }
     }     }
-     + 
-    // Print the response from the server +    wifi_client.stop();
-    while (client.available()) { +
-      String response = client.readStringUntil('\n'); +
-      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)
 } }
- 
 </file> </file>
  
-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:start|HSRW Weather Station at Campus Kamp-Lintfort]]+===== 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, we encourage you to take it a step further by incorporating one of the sensors available to you. Stepping ahead, you'll have the chance to implement your first MQTT publish from the ESP8266. While the following example provides a static demonstration, we encourage you to take it a step further by incorporating one of the sensors available to you.
  
-PubSubClient Library: [[https://github.com/knolleary/pubsubclient|PubSubClient by knolleary]]+To start, search and install the library "PubSubClient " by Nick O'Leary in Arduino IDE or download and install it manually from source [[https://github.com/knolleary/pubsubclient| Github PubSubClient by knolleary]].
  
 <file c++ mqtt-publish.ino> <file c++ mqtt-publish.ino>
Line 196: Line 196:
 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.
  
-<file ino mqtt-subscribe-example.ino>+<file c++ mqtt-subscribe-example.ino>
 /* /*
  Basic ESP8266 MQTT example  Basic ESP8266 MQTT example
latinet/unicaes/workshops/communication-23.1698005985.txt.gz · Last modified: 2023/10/22 22:19 by rolf.becker