Differences

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

Link to this comparison view

Next revision
Previous revision
en:iot-open:practical:hardware:sut:stm32:iot_4 [2024/04/27 09:08] – created ktokarzen:iot-open:practical:hardware:sut:stm32:iot_4 [2024/04/27 09:41] (current) ktokarz
Line 3: Line 3:
  
 ===== Prerequisites ===== ===== Prerequisites =====
-To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first:+To implement this scenario, it is necessary to get familiar with the LED controlling scenario: 
 +  * [[en:iot-open:practical:hardware:sut:stm32:emb9A_1]], 
 + 
 +and at least one of the following scenarios first:
   * [[en:iot-open:practical:hardware:sut:stm32:emb5_1|]],   * [[en:iot-open:practical:hardware:sut:stm32:emb5_1|]],
   * [[en:iot-open:practical:hardware:sut:stm32:emb6_1|]],   * [[en:iot-open:practical:hardware:sut:stm32:emb6_1|]],
Line 87: Line 90:
 In our case, the command can look like this: In our case, the command can look like this:
 <code> <code>
-AT+MQTTUSERCFG=0,1,\"STM32#0001\",\"vrel\",\"vrel2018\",0,0,\"\"+AT+MQTTUSERCFG=0,1,\"STM32#0001\",\"username\",\"password\",0,0,\"\"
 </code> </code>
  
Line 102: Line 105:
 The command can look like this: The command can look like this:
 <code> <code>
-AT+MQTTCONN=0,\"192.168.1.90\",1883,1+AT+MQTTCONN=0,\"192.168.1.100\",1883,1
 </code> </code>
  
Line 114: Line 117:
   * "topic" - MQTT topic   * "topic" - MQTT topic
   * qos - mode of the quality of service, 0, 1 or 2, default 0.   * qos - mode of the quality of service, 0, 1 or 2, default 0.
 +The command can look like this in the example below:
 +<code>
 +AT+MQTTSUB=0,\"topic\",0
 +</code>
  
 After a successful subscription, the ESP32-C3 module will send messages in the following form: After a successful subscription, the ESP32-C3 module will send messages in the following form:
Line 128: Line 135:
  
 === Step 4 === === Step 4 ===
-Implement handling of the MQTT messages. Here we present how to control LED with messages sent with the topic "topic". If the message payload is 0 LED will be off, if it is 1 LED will be on. You can use it as the template for more advanced control of any device in the laboratory equipment.// +Implement handling of the MQTT messages. Here we present how to control LED with messages sent with the topic "topic". If the message payload is 0 LED will be off, if it is 1 LED will be on. You can use it as the template for more advanced control of any device in the laboratory equipment.\\ 
-Declare variables to store the strings for comparison. +Declare variables to store the strings for comparison and define LED pin
-<code>+<code c> 
 +#define LED_GREEN D3
 String compend0, compend1; String compend0, compend1;
 +String response;
 </code> </code>
 +
 Prepare string variables and set the mode of the LED pin. Prepare string variables and set the mode of the LED pin.
-<code>+<code c>
 compend0 = "+MQTTSUBRECV:0,\"topic\",1,0"; compend0 = "+MQTTSUBRECV:0,\"topic\",1,0";
 compend1 = "+MQTTSUBRECV:0,\"topic\",1,1"; compend1 = "+MQTTSUBRECV:0,\"topic\",1,1";
-pinMode(LED_BUILTIN,OUTPUT);+pinMode(LED_GREEN,OUTPUT);
 </code> </code>
  
 +In the "loop();" implement periodic reads of the hardware serial port, and handle incoming messages.
 +<code c>
 +response = WiFiSerial.readStringUntil(0x0A);
 +if (response.startsWith(compend0))
 +  {
 +    digitalWrite(LED_GREEN,0);
 +  };
 +if (response.startsWith(compend1))
 +  {
 +    digitalWrite(LED_GREEN,1);
 +  };
 +</code>
  
 ==== Result validation ==== ==== Result validation ====
-You should be able to connect to the WiFi and MQTT broker (verified by the status present on the selected display) and then publish message (once or periodically). Depending on whether you're fully remote or able to access our networks with an additional device, you need to implement a subscriber (as present in the scenario [[[en:iot-open:practical:hardware:sut:stm32:iot_4|]]) or use MQTT Explorer (or any other application capable of connecting to our MQTT Broker) to observe messages that you publish.+You should be able to connect to the WiFi and MQTT broker (verified by the status present on the selected display) and then subscribe to the topic. After successful subscription, you should receive messages published on the topic of your choice. Depending on whether you're fully remote or able to access our networks with an additional device, you need to implement a publisher (as present in the scenario [[[en:iot-open:practical:hardware:sut:stm32:iot_3|]]) or use MQTT Explorer (or any other application capable of connecting to our MQTT Broker) to publish messages.
 <note info> <note info>
 Because LCD can't properly display some non-visible characters the presented code sometimes shows additional, non-letter characters. It is out of the scope of this scenario to filter these characters out. We leave the task of making visual improvements to your invention. Because LCD can't properly display some non-visible characters the presented code sometimes shows additional, non-letter characters. It is out of the scope of this scenario to filter these characters out. We leave the task of making visual improvements to your invention.
Line 148: Line 170:
  
 ===== FAQ ===== ===== FAQ =====
-**Can I change the IP address?**: Normally IP addresses are assigned by the server known as DHCPIn some situations, you can use static IP assigned manually in the stationIt is not advised, however, because you may accidentally generate an overlapping address that will collide with another device in the same network.+**My MQTT client disconnects randomly**: The most common reason is you're using a non-unique MQTT client name. Please change it to some other (even random generated) and give it another try.\\ 
 +**Can I subscribe to more than one topic?**: Yes, you can. For every incoming message you would receive the information on the topic of this specific message, so you would be able to recognise them.\\ 
 +**How do I send messages to which I am subscribed?**: Use a software client, such as [[http://mqtt-explorer.com/| MQTT Explorer]]if you're able to access the "internal IoT" network (you're in the range of the network)If you're remote, the only way is to book another device and implement a client publishing a message with the appropriate topic, as presented in the scenario [[[en:iot-open:practical:hardware:sut:stm32:iot_3|]]. Our MQTT broker is also visible in the campus network on the wired interfaces so that you can access it, e.g. via EduVPN or from the laboratory computers. Refer to the supervisor for IP and credentials.\\ 
 +**Do I need to authorise to publish and subscribe?**: Yes, you do. The supervisor provides the user and password on demand, also presented in the Node's technical documentation.
  
 <WRAP noprint> <WRAP noprint>
en/iot-open/practical/hardware/sut/stm32/iot_4.1714208925.txt.gz · Last modified: 2024/04/27 09:08 by ktokarz
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