issue133:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue133:python [2018/05/27 11:08] – christo.2so | issue133:python [2018/05/31 15:43] (Version actuelle) – d52fr | ||
---|---|---|---|
Ligne 6: | Ligne 6: | ||
**This month, we'll continue our discussion of MQTT, especially how MQTT can support quality of service and then we'll modify our original program from Full Circle issue #109 to support MQTT.** | **This month, we'll continue our discussion of MQTT, especially how MQTT can support quality of service and then we'll modify our original program from Full Circle issue #109 to support MQTT.** | ||
- | Ce mois-ci, nous poursuivrons notre discussion sur MQTT, en particulier sur la façon dont MQTT peut assurer la bonne qualité d'un service, puis nous modifierons notre programme original du numéro 109 de Full Circle pour supporter MQTT. | + | Ce mois-ci, nous poursuivrons notre discussion sur MQTT, en particulier sur la façon dont MQTT peut assurer la bonne qualité d'un service, puis nous modifierons notre programme original du numéro 109 du Full Circle pour supporter MQTT. |
**QoS | **QoS | ||
QoS stands for Quality of Service. MQTT supports three levels, numbered 0, 1 and 2. If you don't have to be sure that those who are subscribed to your topic messages, then you simply use a QoS of 0. Here is a breakdown of the levels: | QoS stands for Quality of Service. MQTT supports three levels, numbered 0, 1 and 2. If you don't have to be sure that those who are subscribed to your topic messages, then you simply use a QoS of 0. Here is a breakdown of the levels: | ||
- | |||
0 - At most once | 0 - At most once | ||
Ligne 17: | Ligne 16: | ||
2 - Exactly once** | 2 - Exactly once** | ||
- | When the broker transfers a message to a subscribing client, it uses the QoS of the subscription made by the receiving client | + | QoS |
- | Level 0 | + | QoS signifie Qualité de service. MQTT supporte trois niveaux, numérotés 0, 1 et 2. Si vous ne devez pas être certain de savoir ceux qui sont abonnés à vos messages thématiques, |
+ | |||
+ | 0 - Au plus une fois. | ||
+ | 1 - Au moins une fois. | ||
+ | 2 - Exactement une fois. | ||
+ | |||
+ | |||
+ | **When the broker transfers a message to a subscribing client, it uses the QoS of the subscription made by the receiving client** | ||
+ | |||
+ | Lorsque l' | ||
+ | |||
+ | |||
+ | **Level 0 | ||
Level 0 guarantees a best effort delivery. The message won't be acknowledged by the receiver (broker or client), nor will it be stored and re-delivered by the sender. | Level 0 guarantees a best effort delivery. The message won't be acknowledged by the receiver (broker or client), nor will it be stored and re-delivered by the sender. | ||
Ligne 29: | Ligne 40: | ||
Level 2 | Level 2 | ||
- | As you can see from the diagram, the client sends a publish message to the broker and the broker sends back a PUBREC. The sender then stores a reference to the packet identifier, which is then stored and then sends out a PUBREL message. If the receiver responds with a PUBCOMP message, the sender then knows it can safely discard the message and reference. If a message gets lost somewhere along the way, the sender (either the publishing client or the broker) is responsible for re-sending the last message. | + | As you can see from the diagram, the client sends a publish message to the broker and the broker sends back a PUBREC. The sender then stores a reference to the packet identifier, which is then stored and then sends out a PUBREL message. If the receiver responds with a PUBCOMP message, the sender then knows it can safely discard the message and reference. If a message gets lost somewhere along the way, the sender (either the publishing client or the broker) is responsible for re-sending the last message.** |
- | Of course, QoS Level 1 is faster than Level 2, since there are only two messages to deal with where Level 2 has four and QoS Level 0 is faster than Level 1. | + | Niveau 0 |
+ | |||
+ | Le niveau 0 fournit la meilleure garantie de livraison. Le message ne fera pas l' | ||
+ | |||
+ | Niveau 1 | ||
+ | |||
+ | L' | ||
+ | |||
+ | Niveau 2 | ||
+ | |||
+ | Comme vous pouvez le voir sur le diagramme, le client envoie un message de publication à l' | ||
+ | |||
+ | |||
+ | **Of course, QoS Level 1 is faster than Level 2, since there are only two messages to deal with where Level 2 has four and QoS Level 0 is faster than Level 1. | ||
When the broker sends a message to a subscribing client, the QoS used is what the client used when subscribing to the topic. Therefore, the QoS can be downgraded from what the publishing client intends. | When the broker sends a message to a subscribing client, the QoS used is what the client used when subscribing to the topic. Therefore, the QoS can be downgraded from what the publishing client intends. | ||
- | When should you use one level over another? Basically if your publishing client doesn' | + | When should you use one level over another? Basically if your publishing client doesn' |
- | We will be using a Qos Level 1 for our communications in our demo, just for practice. | + | Bien sûr, le niveau |
- | The Hardware | + | Lorsque l' |
+ | |||
+ | A quel moment doit-on utiliser un niveau plutôt qu'un autre ? Fondamentalement, | ||
+ | |||
+ | |||
+ | **We will be using a Qos Level 1 for our communications in our demo, just for practice.** | ||
+ | |||
+ | Nous utiliserons le niveau 1 de Qos pour les communications dans notre démo d' | ||
+ | |||
+ | |||
+ | **The Hardware | ||
If you remember the original project we did back in FCM 109, we used a DHT11 connected to our Raspberry Pi. We used the Adafruit DHT22 python library and slightly modified their sample code. | If you remember the original project we did back in FCM 109, we used a DHT11 connected to our Raspberry Pi. We used the Adafruit DHT22 python library and slightly modified their sample code. | ||
Ligne 45: | Ligne 79: | ||
I’ve recreated the Fritzing diagram showing the hardware connections… | I’ve recreated the Fritzing diagram showing the hardware connections… | ||
- | Once you have the hardware all set up, now we have to download and install the library. If you don’t currently have the library, you can find it on GitHub at https:// | + | Once you have the hardware all set up, now we have to download and install the library. If you don’t currently have the library, you can find it on GitHub at https:// |
- | cd /home/pi | + | Le matériel |
- | git clone https:// | + | Si vous vous souvenez du projet original réalisé dans le FCM n° 109, nous avons utilisé un DHT11 connecté à notre Raspberry Pi. Nous avons utilisé la bibliothèque DHT22 python d' |
- | cd Adafruit_Python_DHT | + | J'ai recréé le diagramme de Fritzing en montrant les connexions matérielles : |
- | sudo apt-get update | + | Une fois tout le matériel installé, nous devons télécharger et installer la bibliothèque. Si vous n'avez pas la bibliothèque, |
- | sudo apt-get install build-essential python-dev python-openssl | ||
- | sudo python setup.py install | + | * cd /home/pi |
+ | * git clone https:// | ||
- | The sample code is in the /examples folder, called simpletest.py. Be sure to change the line that defines which pin your sensor is on to match your setup. The original code assumes your are putting the data pin of the sensor on GPIO 23 (line 35). In the setup I present, use GPIO 4. Also be sure to comment out line 31 if you are using a Raspberry Pi instead of a Beaglebone board. | + | * cd Adafruit_Python_DHT |
- | Here is our updated code (right, and continuing on to the next page). You can try modifying the original code, or just start over. It’s not that long. | + | * sudo apt-get update |
- | In those last lines (next page), we check to make sure that we got data from the sensor, and if so, we create two messages, one containing the humidity value and one containing the temperature value. In the next two lines, we actually publish the messages. Notice that we are using a QoS value of 1 with a retain value of True. This should ensure delivery if the broker is there and if there are any clients listening for our data. Finally, we sleep for 5 seconds and repeat the loop. Feel free to change the sleep value to whatever you wish over 5. The DHT11/22 doesn’t like to be accessed more than about once every 5 seconds. A realistic value would be somewhere between 30 and 60 seconds. | + | * sudo apt-get install build-essential python-dev python-openssl |
+ | |||
+ | * sudo python setup.py install | ||
+ | |||
+ | |||
+ | **The sample code is in the /examples folder, called simpletest.py. Be sure to change the line that defines which pin your sensor is on to match your setup. The original code assumes your are putting the data pin of the sensor on GPIO 23 (line 35). In the setup I present, use GPIO 4. Also be sure to comment out line 31 if you are using a Raspberry Pi instead of a Beaglebone board. | ||
+ | |||
+ | Here is our updated code (right, and continuing on to the next page). You can try modifying the original code, or just start over. It’s not that long.** | ||
+ | |||
+ | Le code d' | ||
+ | |||
+ | Voici notre code mis à jour (à droite, puis il continue à la page suivante). Vous pouvez essayer de modifier le code original ou simplement recommencer à zéro. Ce n'est pas si long. | ||
+ | |||
+ | |||
+ | **In those last lines (next page), we check to make sure that we got data from the sensor, and if so, we create two messages, one containing the humidity value and one containing the temperature value. In the next two lines, we actually publish the messages. Notice that we are using a QoS value of 1 with a retain value of True. This should ensure delivery if the broker is there and if there are any clients listening for our data. Finally, we sleep for 5 seconds and repeat the loop. Feel free to change the sleep value to whatever you wish over 5. The DHT11/22 doesn’t like to be accessed more than about once every 5 seconds. A realistic value would be somewhere between 30 and 60 seconds.** | ||
+ | |||
+ | Dans ces dernières lignes (page suivante), nous vérifions que nous avons bien reçu les données du capteur et, si c'est le cas, nous créons deux messages, l'un contenant la valeur d' | ||
client.publish(MQTT_PATH1, | client.publish(MQTT_PATH1, | ||
Ligne 76: | Ligne 126: | ||
sleep(5) | sleep(5) | ||
- | Of course, I’m assuming that your broker is running on the same Raspberry Pi. If not, be sure the change the IP address that MQTT_SERVER pointing to. | + | **Of course, I’m assuming that your broker is running on the same Raspberry Pi. If not, be sure the change the IP address that MQTT_SERVER pointing to. |
- | Now you can run your program (be sure the broker is running BEFORE you start the program). Next, run mosquitto_sub on the RPi subscribing to one or both of the topics we set up in our program. You should see a message each time the loop runs. | + | Now you can run your program (be sure the broker is running BEFORE you start the program). Next, run mosquitto_sub on the RPi subscribing to one or both of the topics we set up in our program. You should see a message each time the loop runs.** |
- | I’ve put our new program on Pastebin at https:// | + | Bien sûr, je suppose que votre intermédiaire utilise le même Raspberry Pi. Si ce n'est pas le cas, assurez-vous de changer l' |
+ | |||
+ | Vous pouvez maintenant exécuter votre programme (assurez-vous que l' | ||
+ | |||
+ | |||
+ | **I’ve put our new program on Pastebin at https:// | ||
Next month, our project will be a GUI subscriber client to monitor our sensors. This client can be either on the RPi or on another computer on your network. In fact, if you use a public broker, you can monitor it from anywhere in the world. | Next month, our project will be a GUI subscriber client to monitor our sensors. This client can be either on the RPi or on another computer on your network. In fact, if you use a public broker, you can monitor it from anywhere in the world. | ||
- | Until then, have a great month. | + | Until then, have a great month.** |
+ | |||
+ | J'ai mis notre nouveau programme sur Pastebin à https:// | ||
+ | |||
+ | Le mois prochain, notre projet sera un client abonné GUI pour surveiller nos capteurs. Ce client peut être sur le RPi ou sur un autre ordinateur de votre réseau. En fait, si vous utilisez un intermédiaire public, vous pouvez le surveiller de n' | ||
+ | |||
+ | D'ici là, passez un bon mois. | ||
issue133/python.1527412129.txt.gz · Dernière modification : 2018/05/27 11:08 de christo.2so