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
en:iot-open:remotelab:sut:generalpurpose2:u3 [2019/08/05 19:32] pczekalskien:iot-open:remotelab:sut:generalpurpose2:u3 [2021/02/27 10:00] (current) – external edit 127.0.0.1
Line 21: Line 21:
  
 === Start === === Start ===
-Define some identifiers to separate and update AP's SSID and passphrase easily. To format lines for the LCD, we suggest using a char buffer of 20 characters (one full line) and some 2-3 integers for iterators. Remember to declare the LCD control class in your code. You do not need to instantiate WiFi communication class - as you have only one interface here, it is singleton class you can refer directly using WiFi. Note - in this scenario, you connect only once. If your connection breaks, you will have no information about it here. To handle such situation, there are multiple solutions: you can check in the loop() if the connection is OK and in case it is down, you can call your re-connecting function, or you can use one of the asynchronous handlers, triggered automatically via the WiFi manager. To use the latter approach, refer to the ESP8266 WiFi implementation for Arduino documentation. +Define some identifiers to separate and update AP's SSID and passphrase easily. To format lines for the LCD, we suggest using a char buffer of 20 characters (one full line) and some 2-3 integers for iterators. Remember to declare the LCD control class in your code. You do not need to instantiate the WiFi communication class - as you have only one interface here, it is singleton class you can refer to directly using WiFi. Note - in this scenario, you connect only once. If your connection breaks, you will have no information about it here. To handle such situation, there are multiple solutions: you can check in the loop() if the connection is OK and in case it is down, you can call your re-connecting function, or you can use one of the asynchronous handlers, triggered automatically via the WiFi manager. To use the latter approach, refer to the ESP8266 WiFi implementation for Arduino documentation. 
  
 === Steps === === Steps ===
-Following steps do not present full code - you need to supply missing parts on your own! We do not present here how to connect to the WiFi AP. If you're in doubt, rever to the U1 scenario. We also do not present in details on how to organise and print DHT22 sensor data on the LCD screen. Please refer to the scenario B2 if you need a recall.+Following steps do not present full code - you need to supply missing parts on your own! We do not present here how to connect to the WiFi AP. If you're in doubt, rever to the U1 scenario. We also do not present in details how to organise and print DHT11 sensor data on the LCD screen. Please refer to scenario B2 if you need a recall.
 == Step 1 == == Step 1 ==
 Include all necessary libraries. We use ''PubSubClient'' library to contact MQTT broker. The minimum set here is: Include all necessary libraries. We use ''PubSubClient'' library to contact MQTT broker. The minimum set here is:
 <code c> <code c>
 #include <Arduino.h> #include <Arduino.h>
-#include <ittiot.h> 
 #include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
 #include <PubSubClient.h> #include <PubSubClient.h>
Line 35: Line 34:
 #include <DHT.h> #include <DHT.h>
 ... ...
 +</code>
 +Declare some identifiers to let you easier handle necessary modifications and keep code clear:
 +<code c>
 #define wifi_ssid "internal.IOT" #define wifi_ssid "internal.IOT"
 #define wifi_password "IoTlab32768" #define wifi_password "IoTlab32768"
Line 47: Line 49:
 <code c> <code c>
 #define MQTTClientName ...<your client name>... #define MQTTClientName ...<your client name>...
-#define tempTopic  ...<some topic for temperature>... // give it some unique topic i.e. including your name+#define tempTopic  ...<some topic for temperature>... // give it some unique topic 
 +                                                      // i.e. including your name
 #define humTopic   ...<come topic for humidity>...    // as above #define humTopic   ...<come topic for humidity>...    // as above
  
 //MQTT last will //MQTT last will
-#define lastWillTopic ...<some topic for exposing state and last will>...   // give it some unique topic i.e. including your name+#define lastWillTopic ...<some topic for exposing state and last will>...    
 +                                                      // give it some unique topic  
 +                                                      // i.e. including your name
 #define lastWillMessage "off" #define lastWillMessage "off"
 #define mqttWelcomeMessage "on" #define mqttWelcomeMessage "on"
Line 70: Line 75:
  
 == Step 4 == == Step 4 ==
-If your WiFi client is working, your MQTT client is configurd it is time to connect to the MQTT broker. It is a good idea to have this procedure separated to call as needed if your connection with MQTT broker goes down for any reason. Here we encapsulate it in the ''reconnect()'' function:+If your WiFi client is working, your MQTT client is configured it is time to connect to the MQTT broker. It is a good idea to have this procedure separated to call as needed if your connection with MQTT broker goes down for any reason. Here we encapsulate it in the ''reconnect()'' function:
 <code c> <code c>
 void reconnect() { void reconnect() {
   // Loop until we're reconnected   // Loop until we're reconnected
   while (!client.connected()) {   while (!client.connected()) {
-    if (client.connect(MQTTClientName, mqtt_user, mqtt_password, lastWillTopic, 0, true, lastWillMessage)) {+    if (client.connect(MQTTClientName, mqtt_user, mqtt_password,  
 +                       lastWillTopic, 0, true, lastWillMessage))  
 +    {
       client.publish(lastWillTopic, mqttWelcomeMessage, true);       client.publish(lastWillTopic, mqttWelcomeMessage, true);
-    } else {+    } else  
 +    {
       // Wait 5 seconds before retrying       // Wait 5 seconds before retrying
       delay(5000);       delay(5000);
Line 108: Line 116:
     if (!(isnan(temp)||isnan(hum)))     if (!(isnan(temp)||isnan(hum)))
     {     {
-      client.publish(tempTopic, String(temp).c_str(), false);  // Do not retain messages+      client.publish(tempTopic, String(temp).c_str(), false);  // Do not retain 
 +                                                               // messages
       client.publish(humTopic, String(hum).c_str(), false);       client.publish(humTopic, String(hum).c_str(), false);
     }     }
en/iot-open/remotelab/sut/generalpurpose2/u3.1565033571.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0