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:u4 [2019/08/05 20:01] pczekalskien:iot-open:remotelab:sut:generalpurpose2:u4 [2020/09/21 09:00] (current) – external edit 127.0.0.1
Line 26: Line 26:
  
 === 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. Please refer to scenario B1, if you need a recall on how to handle LCD screen. 
 == 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 51: Line 51:
 Declare some identifiers, here MQTT messages' topic (one with wildcard, for four LCD lines) and MQTT client ID. Declare some identifiers, here MQTT messages' topic (one with wildcard, for four LCD lines) and MQTT client ID.
 <note important>Use unique names for topics and for the MQTT client, do some random, use your MAC as part of it. It is important because MQTT broker identifies the client using its name. Also note, the set of MQTT topics that you use to send information to your LCD should be unique, otherwise some other users may "inject" information to your display.</note> <note important>Use unique names for topics and for the MQTT client, do some random, use your MAC as part of it. It is important because MQTT broker identifies the client using its name. Also note, the set of MQTT topics that you use to send information to your LCD should be unique, otherwise some other users may "inject" information to your display.</note>
 +Use topics that their last character is a line number (0 till 3) to easily suit ''lcd.setCursor(x,y)'' requirements, i.e. as below:
 +<code c>
 +// MQTT messages
 +#define MQTTClientName "thisissomeuniqueclientname"
 +#define lcdTopicWithWildcard "/sut/mydevice/LCD/+"
 +</code>
 +Valid MQTT messages are:
 +  * ''/sut/mydevice/LCD/0'' - delivers payload for line 1 of the LCD screen,
 +  * ''/sut/mydevice/LCD/1'' - delivers payload for line 2 of the LCD screen, and so on.
  
-== Step n == +== Step 3 == 
-//Describe activities done in Step n.//+We will declare an MQTT handler callback function that is called whenever new MQTT message comes. Mind - only those messages that you've subscribed to are triggering this function, so you do not need to check the topic other than decoding its part to obtain, which LCD line number comes (this is the last character of the MQTT topic): 
 +<code c> 
 +void mqttCallback(char* topic, byte* payload, unsigned int length) { 
 +  char nLine = topic[strlen(topic)-1]; 
 +  n = atoi(&nLine); 
 +  if (n>=0 && n<=3) 
 +  { 
 +    lcd.setCursor(0,n); 
 +    lcd.print("                    "); //clear one line 
 +    for (int i = 0; i < length; i++) 
 +    { 
 +      lcd.setCursor(i, n); 
 +      lcd.write((char)payload[i]); 
 +    } 
 +  } 
 +
 +</code> 
 +Note - header of the function is interface declared by the mqtt PubSubClient library. ''topic'' is MQTT message topic, ''payload'' is a content and ''length'' is payload's length. As ''payload'' is binary, ''length'' is essential to know, how to handle ''payload'' contents. 
 + 
 +== Step 4 == 
 +By the regular variables related to your WiFi ESP network client and so on, here you need to configure an object additionally to handle communication with MQTT broker. As you may use many brokers in your code, you need to instantiate it yourself, there is no singleton class as in case of the network interface. The constructor accepts ''WiFiClient'' as a parameter, so here you need explicitly declare one, to gain access to it: 
 +<code c> 
 +// WiFi & MQTT 
 +WiFiClient espClient; 
 +PubSubClient client(espClient); 
 +</code> 
 +Configure your network client, remember to set ''ESP.mode(WIFI_SFA)''. Once you're done with starting, initialise MQTT broker client. Once you're done with connecting to the WiFi, configure your MQTT PubSubClient and give it a handler function to let it be called, whenever a new message comes (here ''mqttCallback'': 
 +<code c> 
 +client.setServer(mqtt_server, 1883); 
 +client.setCallback(mqttCallback); 
 +</code> 
 +You can call it i.e. in the ''setup()'' function, after a successful connection to the WiFi AP but before you execute step 4. 
 + 
 +== Step 5 == 
 +If your WiFi client is working, your MQTT client is configured it is time to connect to the MQTT brokerIt 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 where we also subscribe to the number of topics delivering LCD content in the payload: 
 +<code c> 
 +void reconnect() { 
 +  // Loop until we're reconnected 
 +  while (!client.connected()) { 
 +    if (client.connect(MQTTClientName, mqtt_user, mqtt_password)) { 
 +      client.subscribe(lcdTopicWithWildcard); 
 +    } else { 
 +      // Wait 5 seconds before retrying 
 +      delay(5000); 
 +    } 
 +  } 
 +  lcd.setCursor(0,1); 
 +  lcd.print("MQTT Connected!"); 
 +
 +</code> 
 +== Step 6 == 
 +Finally your ''loop()'' may look like this: 
 +<code c> 
 +void loop() 
 +
 +  if (!client.connected()) { 
 +    reconnect(); 
 +  } 
 +  client.loop(); 
 +
 +</code> 
 +The ''client.loop()'' is to handle incoming MQTT messages.
  
 === Result validation === === Result validation ===
-//Provide some result validation methods, for self assesment.//+Compile and run your code then wait till it connects to the network and MQTT broker. Once it is connected, use your favourite MQTT client to connect to the MQTT broker and issue a number of MQTT messages that their topics are: 
 +  * ''/sut/mydevice/LCD/0'' 
 +  * ''/sut/mydevice/LCD/1'' 
 +  * ''/sut/mydevice/LCD/2'' 
 +  * ''/sut/mydevice/LCD/3'' 
 +and the payload is a text you want to display on your LCD. Note, your display is 20 characters each line only so keep your payload within those limits.
  
-=== FAQ === 
-This section is to be extended as new questions appear. \\ 
-When using the printed version of this manual please refer to the latest online version of this document to obtain the valid and up-to-date list of the FAQ. 
-//Provide some FAQs in the following form:\\ 
-**Question?**: Answer. 
-// 
  
en/iot-open/remotelab/sut/generalpurpose2/u4.1565035263.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