This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:remotelab:ume:smartme:m2 [2019/10/26 13:40] – pczekalski | en:iot-open:remotelab:ume:smartme:m2 [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ==== IM2: MQTT to subscribe to a topic to receive messages ==== | ||
| + | MQTT is one of the most commonly used protocols in IoT projects. It stands for Message Queuing Telemetry Transport. In addition, it is designed as a lightweight messaging protocol that provides publish/ | ||
| + | |||
| + | === Target group === | ||
| + | This hands-on lab guide is intended for master students. Other target groups may benefit from it only if they follow it after having dealt with all the exercises proposed which belong to lower levels of difficulty. | ||
| + | |||
| + | === Prerequisites === | ||
| + | == Liquid Crystal == | ||
| + | For this library, you may refer to the [[en: | ||
| + | |||
| + | == DHT == | ||
| + | For this library, you may refer to the [[en: | ||
| + | |||
| + | |||
| + | == Understand what Arduino Ethernet Shield is and how it works == | ||
| + | The Arduino Ethernet Shield V1 allows an Arduino board to connect to the internet. It is based on the Wiznet W5100 [1] ethernet chip (datasheet [2]). The Wiznet W5100 provides a network (IP) stack capable of both TCP and UDP-based communication. It supports up to four simultaneous socket connections. Use the Ethernet library [3] to write sketches that connect to the Internet using the shield. The ethernet shield connects to an Arduino board using long wire-wrap headers which extend through the shield. This keeps the pin layout intact and allows another shield to be stacked on top. | ||
| + | |||
| + | [1] http:// | ||
| + | |||
| + | [2] http:// | ||
| + | |||
| + | [3] https:// | ||
| + | |||
| + | == Understand what MQTT is and how it works. == | ||
| + | MQTT (Message Queue Telemetry Transport) is a standard (SO/IEC PRF 20922) machine-to-machine (M2M) communication protocol for the Internet of Things smart objects and devices. MQTT is a simple messaging protocol, designed for constrained devices with low-bandwidth. So, it’s the perfect solution for Internet of Things applications. | ||
| + | This publish/ | ||
| + | So a client must connect to a broker in order to: | ||
| + | publish messages specifying a topic so that other clients that have subscribed to that topic will be able to receive those messages; | ||
| + | receive messages subscribing to a specific topic. | ||
| + | |||
| + | |||
| + | This exercise uses shiftr.io [4]. | ||
| + | Shiftr.io is a web-based visualization for messages going through an MQTT broker. It is possible to use broker.shiftr.io directly as a public broker. | ||
| + | |||
| + | [4] https:// | ||
| + | |||
| + | |||
| + | === Scenario === | ||
| + | This node, which acts as a subscriber, shows the value of temperature received by the other node that was initialized with the exercise M1. | ||
| + | |||
| + | === Result === | ||
| + | It will be possible to display the value of the temperature, | ||
| + | Start | ||
| + | This exercise is to be considered as closely related to the M1 exercise. This means that if you want to reproduce a complete experiment, you have to book at least two nodes of the lab, and then you have to run both IM1 and IM2 sketches, one per node. | ||
| + | |||
| + | === Steps === | ||
| + | |||
| + | == Step 1 == | ||
| + | |||
| + | Include the following libraries: LCD driver, MQTT, and Ethernet. | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | </ | ||
| + | |||
| + | == Step 2 == | ||
| + | Instantiate the software controller component for the LCD display. Then assign to the ethernet interface a MAC and IP address, the latter valid for the internal laboratory network (192.168.0.0/ | ||
| + | |||
| + | <code c> | ||
| + | // initialize the library with the numbers of the interface pins | ||
| + | LiquidCrystal lcd(7, 6, 5, 4, 13, 2); | ||
| + | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF}; | ||
| + | byte ip[] = {192, 168, 0, 99}; | ||
| + | EthernetClient net; | ||
| + | MQTTClient client; | ||
| + | |||
| + | void connect() { | ||
| + | Serial.print(" | ||
| + | while (!client.connect(" | ||
| + | Serial.print(" | ||
| + | delay(1000); | ||
| + | } | ||
| + | Serial.println(" | ||
| + | // subscribe to the iotopenume topic | ||
| + | client.subscribe("/ | ||
| + | } | ||
| + | |||
| + | void messageReceived(String &topic, String & | ||
| + | Serial.println(" | ||
| + | lcd.setCursor(14, | ||
| + | lcd.print(payload); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == Step 3 == | ||
| + | Initialize the display and the Ethernet connection with IP and MAC address. Then launch the '' | ||
| + | |||
| + | We suggest doing all of the above in the '' | ||
| + | |||
| + | <code c> | ||
| + | void setup() { | ||
| + | lcd.begin(16, | ||
| + | lcd.print(" | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | Serial.begin(115200); | ||
| + | Ethernet.begin(mac, | ||
| + | client.begin(" | ||
| + | client.onMessage(messageReceived); | ||
| + | connect(); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | == Step 4 == | ||
| + | Implement '' | ||
| + | |||
| + | <code c> | ||
| + | void loop() { | ||
| + | client.loop(); | ||
| + | if (!client.connected()) { | ||
| + | connect(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === Result validation === | ||
| + | Observe on your LCD display, its hosting node acting as a subscriber, the value sampled by the temperature sensor attached to the publisher, as soon as you start the M1 sketch on the latter. | ||
| + | |||
| + | === Platformio.ini === | ||
| + | < | ||
| + | [env:uno] | ||
| + | platform = atmelavr | ||
| + | board = uno | ||
| + | framework = arduino | ||
| + | |||
| + | lib_ldf_mode=deep+ | ||
| + | lib_compat_mode=strict | ||
| + | |||
| + | lib_deps = | ||
| + | DHT sensor library@1.3.0 | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | lib_deps_external = | ||
| + | | ||
| + | </ | ||
| + | |||
| + | === IM2_sub.cpp === | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | LiquidCrystal lcd(7, 6, 5, 4, 13, 2); | ||
| + | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF}; | ||
| + | byte ip[] = {192, 168, 0, 99}; | ||
| + | EthernetClient net; | ||
| + | MQTTClient client; | ||
| + | |||
| + | void connect() { | ||
| + | Serial.print(" | ||
| + | while (!client.connect(" | ||
| + | Serial.print(" | ||
| + | delay(1000); | ||
| + | } | ||
| + | Serial.println(" | ||
| + | client.subscribe("/ | ||
| + | } | ||
| + | |||
| + | void messageReceived(String &topic, String & | ||
| + | Serial.println(" | ||
| + | lcd.setCursor(14, | ||
| + | lcd.print(payload); | ||
| + | } | ||
| + | |||
| + | void setup() { | ||
| + | lcd.begin(16, | ||
| + | lcd.print(" | ||
| + | lcd.setCursor(0, | ||
| + | lcd.print(" | ||
| + | Serial.begin(115200); | ||
| + | Ethernet.begin(mac, | ||
| + | client.begin(" | ||
| + | client.onMessage(messageReceived); | ||
| + | connect(); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | client.loop(); | ||
| + | if (!client.connected()) { | ||
| + | connect(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||