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 [2024/09/10 20:26] – [2. ESP8266 and WiFi] harley.laralatinet:unicaes:workshops:communication-23 [2024/09/10 20:39] (current) harley.lara
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, allowing your devices to seamlessly interact with the digital world.fi+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. 
 + 
 +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> <file c++ wifi-http-api-example.ino>
Line 27: Line 29:
  
 // Create an instance of WiFiClientSecure // Create an instance of WiFiClientSecure
-WiFiClientSecure client;+WiFiClientSecure wifi_client;
  
 void setup() { void setup() {
Line 46: Line 48:
  
   // 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 55: Line 57:
   if (WiFi.status() == WL_CONNECTED) {   if (WiFi.status() == WL_CONNECTED) {
  
-    client.connect(API_HOST, HTTP_PORT);+    wifi_client.connect(API_HOST, HTTP_PORT);
     // Make an HTTPS GET request     // Make an HTTPS GET request
-    client.println("GET " + String(API_ENDPOINT) + " HTTP/1.1"); +    wifi_client.println("GET " + String(API_ENDPOINT) + " HTTP/1.1"); 
-    client.println("Host: " + String(API_HOST)); +    wifi_client.println("Host: " + String(API_HOST)); 
-    client.println("Connection: close"); +    wifi_client.println("Connection: close"); 
-    client.println();+    wifi_client.println();
  
     // Wait for the response     // Wait for the response
-    while (client.connected()) { +    while (wifi_client.connected()) { 
-      if (client.available()) {+      if (wifi_client.available()) {
         // read an incoming byte from the server and print it to serial monitor:         // read an incoming byte from the server and print it to serial monitor:
-        char c = client.read();+        char c = wifi_client.read();
         Serial.print(c);         Serial.print(c);
       }       }
     }     }
  
-    client.stop();+    wifi_client.stop();
   }   }
  
Line 78: Line 80:
 } }
 </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: [[eolab:weather_station:kamp-lintfort: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>
latinet/unicaes/workshops/communication-23.1725992767.txt.gz · Last modified: 2024/09/10 20:26 by harley.lara