Differences

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

Link to this comparison view

iothings:laboratoare:lab7 [2022/02/11 16:55]
laura.ruse removed
iothings:laboratoare:lab7 [2024/07/15 22:03] (current)
cosmin.chenaru Change IP hardcoding
Line 1: Line 1:
-====== Contiki OS and Sparrow ======+===== Laborator 07. CoAP și MQTT =====
  
-Contiki considers itself the "The Operating System for Connecting the Next Billion Devices - the Internet of Things."​ It's an Internet-connected multi-tasking OS for low-end platforms, like Microcontrollers. Contiki is great for the amazing level of connected functionality it will squeeze out of a five dollar chip. It runs on a vast array of platforms and CPU's.+În acest laborator vom folosi două protocoale de comunicație des întâlnite în domeniul IoT.
  
-===== Installing Contiki =====+==== CoAP ====
  
-==== Toolchain ====+CoAP (Constrained Application Protocol) este un protocol de comunicație ce rulează peste protocolul UDP/IP, fiind folosit în domenii cu resurse limitate de curent (putere consumată) și cu posibile pierderi de pachete de rețea (lossy networks).
  
-There are two approaches to the toolchainOne is to install all the necessary tools natively on your machineThe other is to get instant-contikiwhich is VM image that works on WindowsMac or Linux.+Există o sumedenie de implementări software a protocolului CoAP ([[https://​en.wikipedia.org/​wiki/​Constrained_Application_Protocol#​Implementations|Implementations]])dar în acest laborator vom folosi biblioteca "​[[https://​github.com/​hirotakaster/​CoAP-simple-library|CoAP simple library]]"​ pentru Arduino IDE pentru ​rula un server CoAPși implementarea [[https://​github.com/​Tanganelli/​CoAPthon|CoAPthon]] din Python pentru a rula un client CoAP.
  
-==== Download Contiki ====+O captură Wireshark arată cum protocolul CoAP rulează peste protocolul UDP:
  
-You need to download a special fork of Contiki to try these changes out. Open a Terminal, and then:+{{:iothings:​laboratoare:​lab7-coap-capture.png?​600|}} 
 +==== MQTT ====
  
-<code bash>$ cd ~  +MQTT este un protocol de transport a mesajelor de la un producător (publisher) către un consumator (subscriber)Spre deosebire de CoAP care are o arhitectură client-server, MQTT folosește un intermediar (broker) pentru a redirecționa mesajele de la producător la consumator.
-$ git clone https://​github.com/​narcisaam/​contiki-sparrow.git -b sparrow  +
-$ cd contiki-sparrow/​platform/​sparrow</​code> ​+
  
 +{{:​iothings:​laboratoare:​lab7-mqtt-flow.png|}}
 +==== Exerciții ====
  
-If Contiki has a problem with the AVR tools, you will need to follow these steps. It ships with an old version of the tools which does not support the Atmega128rfa1 found on the Sparrow, and then sets those as the default AVR tools even though the tools that ship with this version of Ubuntu are newer.+=== Ex1 - CoAP ===
  
-Open Terminal, and type these:+Download-ați biblioteca [[https://​github.com/​hirotakaster/​CoAP-simple-library|CoAP-simple-library]] și copiați fișierele in directorul cu biblioteci din Arduino IDE (de obicei în Documents/​Arduino/​libraries). Este nevoie de un restart al aplicației Arduino IDE după instalarea unei biblioteci direct în acel director, în loc de a folosi Library Manager.
  
-<code bash> +{{:​iothings:​laboratoare:​lab7-coap-server-png.png|}}
-$ sed -i '/​avr/​d'​ /​home/​user/​.profile  +
-$ sudo aptitude update  +
-$ sudo apt-get install avr-libc gcc-avr binutils-avr avrdude git libc6-i386</​code> ​+
  
-Then log out of the machine and back in again (or restart it).+Deschideți exemplul numit "​esp32"​ din biblioteca "CoAP simple library"​ și ajustați codul astfel încât LED-ul conectat la GPIO2 (sau GPIO5 pe unele plăcuțesă fie aprins prin CoAP. Notați IP-ul obținut de ESP32 pentru a-l folosi din clientul de CoAP.
  
-===== Setting Up =====+<​note>​ 
 +Portul definit implicit de CoAP este portul UDP 5683. În biblioteca CoAP_simple_library din Arduino IDE, portul este definit in fișierul "​libraries/​CoAP_simple_library/​coap-simple.h":​
  
-==== Default avrdude port ==== +<​code>​ 
- +#define COAP_DEFAULT_PORT 5683
-Connect the Sparrow board to your computer through USB. Once this is done, you'll likely see a new //ttyUSB// port appear in ///dev//. In the folder ///​contiki-sparrow/​platform/​sparrow//​ edit Makefile.sparrow,​ and change the default avrdude port to match your local settings. This only needs to be done once ever. From now on in this tutorial we'll assume the board is connected to //​ttyUSB0//,​ but you can modify according to your local settings. +
- +
- <​code ​bashAVRDUDE_PORT ?= /​dev/​ttyUSB0 </​code>​ +
- +
-==== MAC Address / Node Number ==== +
- +
-Each node needs a unique MAC address. The sparrow platform expects to find the MAC address as the first 8 bytes in EEPROM. +
- +
-There is a special program just to program the MAC address. In the ///​contiki-sparrow/​platform/​sparrow//​ folder, check up //​tools/​set-eeprom/​set-eeprom.c//​ - it should look like this: +
- +
-<code C> +
-typedef unsigned int uint8_t __attribute__((__mode__(__QI__)));​ +
-uint8_t rf_channel[2] __attribute__((section("​.eeprom"​))) = {26, ~26}; +
-uint8_t mac_address[8] __attribute__((section("​.eeprom"​))) = {0x02, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, NODE};+
 </​code>​ </​code>​
 +</​note>​
  
-It uses the same first 7 bytes, and lets you choose a different number for the last. From this point forward, we will refer to that last digit of the MAC address as the "Node number"​. To assign a node number to your node:+Instalați apoi biblioteca CoAPthon din Python cu ajutorul utilitarului PIP:
  
-<​code ​bash>$ cd tools/​set-eeprom ​ +<​code>​ 
-$ make NODE=3 AVRDUDE_PORT=/​dev/​ttyUSB0 </​code>​ +pip install CoAPthon
- +
- +
-===== Hello World ===== +
- +
-Go back in your terminal to ///​contiki-sparrow/​platform/​sparrow//​ and type... +
- +
-<code bash>$cd tests/​hello-world  +
-$make upload  +
-$make login </​code>​ +
- +
-Working correctly, you should see this: +
- +
-<​code>​*******Booting Contiki 2.5******* +
-MAC address 2:​11:​22:​ff:​fe:​33:​44:​3 +
-nullmac sicslowmac, channel 26, check rate 65535 Hz +
-Routing Enabled +
-Autostart other processes +
- +
-IP addresses [4 max] +
-fdfd::3 +
-fe80::​11:​22ff:​fe33:​4403 +
-Hello, world+
 </​code>​ </​code>​
-==== Ping ==== 
  
-Now that we know the node itself is working and printing OK, we want to make sure two nodes can talk. The best way to do this is with a ping.+<note important>​Pentru Python3 trebuie să instalați CoAPthon3</​note>​
  
-This requires two nodes. If you already have hello-world on one node, the easiest thing to do is to put //​ping-ipv6//​ on second node, and ping the other. So for this example, say we already have //hello-world// working on node 3 on ///​dev/​ttyUSB0//​.+Folosiți următoarea comandă pentru ​aprinde/stinge LED-ul:
  
-We'll connect a second node to the computer, and presume it's connected to ///​dev/​ttyUSB1//​ and it's already been configured with node address 1. For this node, we'll need to compile and upload the //​ping-ipv6//​ app and specify the node we'll want to ping.  +<​code>​ 
- +/usr/local/bin/coapclient.py -o PUT -p "coap://$ESP32_BOARD_IP/light" ​-P "​1"​
-<​code ​bash>$ cd tests/​ping-ipv6 ​ +
-$ make upload AVRDUDE_PORT=/dev/ttyUSB1 NODE=3  +
-$ make login AVRDUDE_PORT=/dev/ttyUSB1 </​code>​ +
- +
-Note that you have to pass the node number of the node you want to ping (NODE=3 in this example). +
-Working correctly, you will see this: +
- +
-<​code>​connecting to /​dev/​ttyUSB1 (57600) [OK] Power-on reset. External reset!  +
-*******Booting Contiki 2.5*******  +
-MAC address 2:11:​22:​ff:​fe:​33:​44:​1 nullmac sicslowmac, channel 26  +
-In Process PING6 Wait for DAD IP addresses [4]  +
-fdfd::1 fe80::​11:​22ff:​fe33:​4401  +
-Sending Echo Request to fdfd:​0000:​0000:​0000:​0000:​0000:​0000:​0003  +
-from fdfd:​0000:​0000:​0000:​0000:​0000:​0000:​0001  +
-Echo reply received. Sending Echo Request to fdfd:​0000:​0000:​0000:​0000:​0000:​0000:​0003  +
-from fdfd:​0000:​0000:​0000:​0000:​0000:​0000:​0001  +
-Echo reply received. Sending Echo Request to fdfd:​0000:​0000:​0000:​0000:​0000:​0000:​0003  +
-from fdfd:​0000:​0000:​0000:​0000:​0000:​0000:​0001  +
-Echo reply received.  +
-Sending Echo Request to fdfd:​0000:​0000:​0000:​0000:​0000:​0000:​0003  +
-from fdfd:​0000:​0000:​0000:​0000:​0000:​0000:​0001  +
-Echo reply received. Sending Echo Request to fdfd:​0000:​0000:​0000:​0000:​0000:​0000:​0003  +
-from fdfd:​0000:​0000:​0000:​0000:​0000:​0000:​0001  +
-Echo reply received. END PING6 </code> +
- +
-==== UDP ==== +
- +
-Finally, we will want to be sure that two nodes can exchange real application data between them. We can do so with the UDP client/server test. Again, this requires two nodes. One node contains the server program, the other contains the client. When making the client, we need to tell it where to find the server by specifying NODE= the Node# of the server. +
- +
-<​code>​cd tests/udp-ipv6  +
-$ make NODE=3 -j10  +
-$ make udp-client.sparrow.u AVRDUDE_PORT=/​dev/​ttyUSB1 NODE=3  +
-$ make udp-server.sparrow.u login AVRDUDE_PORT=/​dev/​ttyUSB0 ​+
 </​code>​ </​code>​
  
-Working correctly, you'll see something like the following. The exact numbers will depend on the timing of when you do the 'make login'.+=== Ex2 (Opțional) - Android "IoT CoAP" application ===
  
-<​code>​ connecting to /​dev/​ttyUSB0 (57600) [OK]  +Instalați aplicația "IoT CoAP" pentru a trimite mesaje către plăcuța ESP32.
-Power-on resetExternal reset!  +
-*******Booting Contiki 2.5*******  +
-MAC address 2:​11:​22:​ff:​fe:​33:​44:​3 nullmac sicslowmac, channel 26  +
-IP addresses [4] fdfd::3 fe80::​11:​22ff:​fe33:​4403  +
-UDP server started Server IPv6 addresses: fdfd::3 fe80::​11:​22ff:​fe33:​4403  +
-+READY Server received: 'Hello 2 from the client'​ from fdfd::1  +
-Responding with message: Hello from the server! (1)  +
-Server received: 'Hello 3 from the client'​ from fdfd::1  +
-Responding with message: Hello from the server! (2)  +
-Server received: 'Hello 4 from the client'​ from fdfd::1  +
-Responding with message: Hello from the server! (3)  +
-Server received: 'Hello 5 from the client'​ from fdfd::1  +
-Responding with message: Hello from the server! (4)  +
-Server received: 'Hello 6 from the client'​ from fdfd::1  +
-Responding with message: Hello from the server! (5)  +
-Server received: 'Hello 7 from the client'​ from fdfd::1 +OK PASS </​code>​+
  
-===== The Shell =====+{{:​iothings:​laboratoare:​lab7-iot-coap-app.png|}}
  
-The shell is a very powerful feature of ContikiWithin this OS lies a fully-featured command shell. The shell in the sparrow platform is a subset of the full Contiki shell, including only the shell modules which have been proven to work on this platform.+=== ExMQTT ===
  
-==== Building ====+După ce instalați biblioteca "​PubSubClient"​ din Library Manager al Arduino IDE, deschideți exemplul "​mqtt_esp8266"​.
  
-<​code>​$ cd tests/shell  +{{:​iothings:​laboratoare:​lab7-mqtt-esp8266.png|}}
-$ make upload  +
-$ make login</​code> ​+
  
-==== Running ====+Chiar dacă este scris pentru ESP8266, putem rula programul pe ESP32 modificând următoare linie:
  
-If successful, you'll see this: +<​code>​ 
- +#include <​ESP8266WiFi.h> 
-<​code>​connecting to /​dev/​ttyUSB1 (57600) [OK]  ++ #include <WiFi.h>
-Power-on resetExternal reset! ​ +
-*******Booting Contiki 2.5*******  +
-MAC address 2:​11:​22:​ff:​fe:​33:​44:​1 nullmac sicslowmac, channel 26  +
-Contiki command shell  +
-Type '?'​ and return for help 68.1:  +
-ContikiIP addresses [4] fdfd::1 fe80::​11:​22ff:​fe33:​4401 ​+
 </​code>​ </​code>​
  
-=== help ===+Plăcuța ESP32 va folosi clientul MQTT pentru a înregistra un "​subscriber"​ la topicul "​inTopic",​ iar de pe un calculator vom instala biblioteca "​paho-mqtt"​ pentru a instanția un client de MQTT în modul "​publisher"​. Pentru a instala "​paho-mqtt",​ folosiți următoarea comandă:
  
-Now, try some of the commands, like '​help':​ +<​code>​ 
- +pip install paho-mqtt
-<​code>​Available commands: ​ +
-?: shows this help  +
-binprint: print binary data in decimal format  +
-blink [num]: blink LEDs ([num] times)  +
-echo <​text>:​ print <​text>​  +
-exit: exit shell  +
-hd: print binary data in hexadecimal format  +
-help: shows this help  +
-kill <​command>:​ stop a specific command  +
-killall: stop all running commands  +
-netstat: show UDP and TCP connections  +
-null: discard input  +
-ping <​host>:​ ping an IP host  +
-ps: list all running processes  +
-quit: exit shell  +
-randwait <​maxtime>​ <​command>:​ wait for a random time before running a command  +
-repeat <num> <​time>​ <​command>:​ run a command every <​time>​ seconds  +
-size: print the size of the input  +
-time [seconds]: output time in binary format, or set time in seconds since 1970  +
-timestamp: prepend a timestamp to data  +
-68.1: Contiki> ​+
 </​code>​ </​code>​
  
-=== blink & kill === +Folosiți {{:​iothings:​laboratoare:​mypythonmqttclient.py.txt|acest}} program Python pentru a stinge/​aprinde ​LED-ul de pe plăcuța ESP32.
- +
-Try "blink 100 &" to start blinking the LED's for 100 timesNotice the '&'​ at the end of that! You should now see Sparrow'​s ​LED flash on and off repeatedly. +
- +
-Then type "kill blink" when you're done admiring your blinking lights.+
  
-=== ping ===+Vom folosi un broker de MQTT public, la adresa "​test.mosquitto.org",​ atât pentru clientul MQTT de pe ESP32 cât și pentru clientul Python. Din acest motiv, este important să schimbăm numele topic-urilor pentru a nu primi mesaje de la alte device-uri din Internet, care folosesc același server public.
  
-We can also ping from within the shell!+<note important>​ 
 +Din topicul "​inTopic"​ puteți schimba in "​iot-lab7-myLED-inTopic",​ iar din topicul "​outTopic"​ in "​iot-lab7-myTemperature-outTopic"​. 
 +</​note>​ 
 +===== Resurse =====
  
-<​code>​68.1:​ Contiki> ping fdfd::3 SEND 13 bytes ping fdfd::​3 ​ +  * https://​github.com/​Tanganelli/​CoAPthon 
-Sending Echo Request to fdfd:​0000:​0000:0000:​0000:​0000:​0000:​0003 ​ +  * https://​github.com/​hirotakaster/​CoAP-simple-library 
-from fdfd:0000:0000:​0000:​0000:​0000:​0000:​0001 ​ +  * https://​en.wikipedia.org/​wiki/​Constrained_Application_Protocol 
-Other ICMP6 message received.  +  * https://​play.google.com/​store/​apps/​details?​id=ch.buedev.iot_coap&​hl=en&​gl=US 
-Echo reply received</code>+  * https://www.digikey.de/en/​maker/​blogs/​2019/​how-to-use-mqtt-with-the-raspberry-pi
iothings/laboratoare/lab7.1644591341.txt.gz · Last modified: 2022/02/11 16:55 by laura.ruse
CC Attribution-Share Alike 3.0 Unported
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