Outils pour utilisateurs

Outils du site


issue106:arduino

Ceci est une ancienne révision du document !


**!!ATTENTION : lors du scribage, rechercher et éliminer les <nowiki> et </nowiki> !!**

So, with the basic plan thought out, it’s now time to actually pick up some components and get building. Temperature Temperature sensing is highly important to this as it will determine when the heat mat should be switched on or off. Rather than use the old DHT11 module, I’m using a DHT22 module this time. These are more sensitive and give a more precise reading. I’m also going to use an Arduino MEGA for this project.

Maintenant que nous avons réfléchi au plan de base, il est temps de choisir quelques composants pour commencer le montage.

Température

La détection de température est très importante dans notre cas, car elle déterminera quand la couverture chauffante doit être mise sous ou hors tension.

Plutôt que d'utiliser le vieux module DHT11, cette fois, j'utilise un module DHT22. Il est plus sensible et donne une meilleure lecture. Je vais aussi utiliser un Arduino MEGA pour ce projet.

Build 1 I’ve connected the DHT22 module using the three pins. One wire to +5V, one to ground, and the other to pin 6 on the MEGA. Other than the serial monitor, there’s no way to see the temperature. I prefer visuals, so I’m going to hook up the ESP8266 (WiFi) module and send my results to ThingSpeak. The ESP8266 uses five of the eight pins that it has. One for +3.3V (NOT +5V or you will fry it) shown as a yellow wire in the diagram, one for the ground (black wire), one each for RX and TX (green and blue) and a reset pin (white wire) which also goes to +3.3V That’s the basic circuit for this first part. The code for the DHT22 is pretty straightforward, so we’ll look at that next. The ESP8266 is more tricky, and is quite often prone to not responding. If you think your code is right, and the ESP8266 isn’t responding, try unplugging/plugging the two 3.3V wires from the breadboard. I find that gives a good reset to the module. When the code is running you’ll see the RX/TX lights flash on the Arduino and at that same time you should see a blue light on the ESP8266 flash.

Assemblage n°1

J'ai connecté le module DHT22 en utilisant trois picots. Un fil au +5V, un à la masse et l'autre au picot 6 de MEGA.

En dehors du moniteur série, il n'y a pas moyen de voir la température. Je préfère les graphiques, aussi je vais brancher le module WiFi ESP8366 et envoyer mes résultats à ThingSpeak. Le ESP8266 utilise 5 des ses 8 picots. Un pour le +3,3V (PAS de +5V ou vous le grillez),le fil jaune visible sur le schéma, un pour la masse (fil noir), un pour RX et un pour TX (vert et bleu) et un fil de r.a.z. (fil blanc) qui va aussi au + 3,3V.

C'est le circuit de base de cette première partie.

Le code pour le DHT22 est assez évident ; aussi, nous ne le verrons qu'au paragraphe suivant. Le ESP8266 est plus délicat et il lui arrive souvent de ne pas répondre. Si vous pensez que le code est bon et que le ESP8266 ne répond pas, essayez de débrancher les deux fils 3,3V de la plaque d'essais. Il me semble que ça réinitialise bien le module. Quand le code tourne, vous verrez les voyants RX/TX clignoter sur l'Arduino et, en même temps, une lampe bleue qui clignote sur le ESP8266.

Code I’ll be adding all my code for this project to this Github gist: https://gist.github.com/ronnietucker/7fc62df161107116cf93 By the time you read this, there will be many revisions of the code added to the gist, but this is revision 2 that I’m discussing here. For the DHT22, I’m using the two libraries from here: http://playground.arduino.cc/Main/DHTLib You’ll need to copy/paste the text for the two files to new files, and name them accordingly. All the instructions for doing this are at that link. I start by including the DHT library and define the pin for the DHT22. I then define my wifi SSID and password.

Code

J'ajouterai tout mon code pour ce projet sur ce gist Github : https://gist.github.com/ronnietucker/7fc62df161107116cf93

Au moment où vous lirez ceci, de nombreuses révisions du code auront été ajoutées sur gist, mais c'est la révision 2 que je présente ici.

Pour le DHT22, j'utilise deux bibliothèques de cette adresse : http://playground.arduino.cc/Main/DHTLib

Vous devrez copier/coller le texte des deux fichiers dans de nouveaux fichiers et de les nommer respectivement. Toutes les instructions pour le faire sont sur le lien.

Je commence par inclure la bibliothèque DHT et définis les picots pour le DHT22. Puis, je définis le SSID et le mot de passe de mon WiFi.

Now we’re on to the main setup. I use two serial begin commands: Serial.begin(115200); Serial2.begin(9600); The Serial.begin is for the DHT22, so that I could get results to the serial monitor before I added the wifi module. The Serial2.begin is for the wifi module. If you’re using an Arduino other than the Mega, then you may have only one serial RX/TX and need to modify the code accordingly. Now I set up the ESP8266. //WiFi setup Serial2.println("AT"); Serial.println("AT sent - checking..."); delay(5000); char okcheck[]="OK"; if(Serial2.find(okcheck)){ Serial.println("OK, found. Connecting"); connectWiFi(); Serial.println("Yay! Should be connected now.");} else{ Serial.println("NOT CONNECTED TO WIFI"); }

Maintenant, nous en venons au réglage principal. J'utilise deux commandes serial begin :

Serial.begin(115200);

Serial2.begin(9600);

Le Serial.begin est pour le DHT22 ; C'est de cette façon que j'obtenais les résultats sur le moniteur série avant que je n'ajoute le module WiFi. Le Serial2.begin est pour le module WiFi. Si vous utilisez un autre Arduino que le Mega, vous n'aurez alors qu'un seul RX/TX et vous aurez besoin de modifier le code en conséquence. Maintenant, je paramètre le ESP8266.

//WiFi setup - paramétrage du WiFi Serial2.println("AT"); Serial.println("AT sent - checking..."); delay(5000); char okcheck[]="OK"; if(Serial2.find(okcheck)){ Serial.println("OK, found. Connecting"); connectWiFi(); Serial.println("Yay! Should be connected now.");} else{ Serial.println("NOT CONNECTED TO WIFI"); }

When using Serial2, I’m talking to the ESP8266. When using just Serial, I’m talking (or printing to) the serial monitor. I’m sending the command AT to the ESP8266 and printing text to the serial monitor to show me what is happening in the background. I wait five seconds, then run a Serial2.find to see if I got a reply of OK. If I did, then I’m going to try and connect (by jumping to connectWiFi() ). If not, then I display the text to say ‘not connected’. Connecting to a wifi router needs the SSID, password and several commands. boolean connectWiFi(){ Serial2.println(“AT+CWMODE=1”); delay(2000); String cmd=“AT+CWJAP=\”“; // add SSID and password cmd+=SSID; cmd+="\",\""; cmd+=PASS; cmd+="\""; // send string Serial2.println(cmd); delay(5000); // was the login accepted? char okcheck[]="OK"; if(Serial2.find(okcheck)){ Serial.println("Login accepted"); return true; }else{ Serial.println("Login not accepted."); return false; }

Quand j'utilise Serial2, je suis en conversation avec le ESP8266. Quand j'utilise simplement Serial, je discute avec le moniteur série (ou j'imprime dessus). J'envoie la commande AT au ESP8266 et j'imprime le texte sur le moniteur série pour voir ce qui se passe en arrière-plan. J'attends cinq secondes puis je lance un Serial.find pour voir si l'obtiens une réponse OK. Si oui, alors j'essaie de me connecter (en sautant à connectWiFi()). Sinon, j'affiche alors le texte « non connecté ».

Pour la connexion au routeur WiFi, j'ai besoin d'un SSID, d'un mot de passe et de plusieurs commandes.

boolean connectWiFi(){ Serial2.println("AT+CWMODE=1"); delay(2000); String cmd="AT+CWJAP=\""; // add SSID and password - ajout du SSID et du mot de passe cmd+=SSID; cmd+="\",\""; cmd+=PASS; cmd+="\""; // send string - envoi de la chaîne Serial2.println(cmd); delay(5000); // was the login accepted? - le login est-il accepté ? char okcheck[]="OK"; if(Serial2.find(okcheck)){ Serial.println("Login accepted"); return true; }else{ Serial.println("Login not accepted."); return false; }

So, I send the command AT+CWMODE=1 to the ESP8266. Wait two seconds, create a string called cmd, and start with AT+CWJAP=\”, then add to the string with the SSID, password, then send the completed string to the ESP8266. Again, I check for a reply of OK (or not) with an appropriate message to the serial monitor. For the main loop, I first jump to TempHum() to get my temperature info from the DHT22. Serial.print("DHT22, \t"); int chk = DHT.read22(DHT22_PIN); switch (chk) { … } // DISPLAY DATA Serial.print(DHT.humidity, 1); Serial.print(",\t\t"); Serial.println(DHT.temperature, 1);

Donc, j'envoie la commande AT+CWMODE=1 au ESP8266. J'attends deux secondes, je crée une chaîne nommée cmd, la commence avec AT+CWJAP=\”, puis la complète avec le SSID et le mot de passe ; ensuite, j'envoie la chaîne complète au ESP8266. À nouveau, je vérifie si la réponse est OK (ou pas) avec le message approprié au moniteur série.

Pour la boucle principale, je passe d'abord à TempHum() pour obtenir l'information de température du DHT22.

Serial.print("DHT22, \t"); int chk = DHT.read22(DHT22_PIN); switch (chk) { … } // DISPLAY DATA - AFFICHAGE DES DONNÉES Serial.print(DHT.humidity, 1); Serial.print(",\t\t"); Serial.println(DHT.temperature, 1);

This is simply creating an integer called chk, and reading the DHT22 pin. I check the status of the DHT22 (the switch, which I’ve snipped that code here to save space), and print the temperature and humidity to the serial monitor. String SendTempLevel = String((float)DHT.temperature, 0); String SendHumLevel = String((float)DHT.humidity, 0); The two Strings are holders for the temperature and humidity and are what I’ll send to ThingSpeak. I jump to updateTemp(), taking those two strings with me. Now it’s time to send to ThingSpeak. You’ll obviously need to create a free account with ThingSpeak, create a channel, have two fields (for temperature and humidity), and obtain your API key. String cmd = "AT+CIPSTART=\"TCP\",\""; cmd += "184.106.153.149"; // api.thingspeak.com cmd += "\",80"; Serial2.println(cmd);

Ceci crée simplement un entier appelé chk qui lit le picot du DHT22. je vérifie l'état du DHT22 (l'inter, dont j'ai omis le code ici pour gagner de la place) et j'imprime la température et l'humidité sur le moniteur série.

String SendTempLevel = String((float)DHT.temperature, 0);

String SendHumLevel = String((float)DHT.humidity, 0);

Les deux String mémorisent la température et l'humidité ; je les enverrai à ThingSpeak.

Je saute à updateTemp(), emportant ces deux chaînes avec moi. Maintenant, c'est le moment de l'envoi à ThingSpeak. Vous aurez évidemment l'obligation de vous créer un compte sur ThingSpeak, de créer un canal, d'avoir deux champs (pour la température et l'humidité) et d'obtenir votre clé API.

String cmd = "AT+CIPSTART=\"TCP\",\""; cmd += "184.106.153.149"; // api.thingspeak.com cmd += "\",80"; Serial2.println(cmd);

Like last time, I create a string called cmd and send it AT codes, add the ThingSpeak IP and port, then send it to the ESP8266. Again, I do a check to see if there’s an error or not. String getStr = "GET /update?api_key="; getStr += "8KS0CVMQ12XXD817"; getStr += "&field1="; getStr += String(SendTempLevel); getStr += "&field2="; getStr += String(SendHumLevel); getStr += "\r\n\r\n"; A new string, getStr, is created with a GET command – with my ThingSpeak API key, and temperature and humidity. String cmd is created again with an AT command, and the getStr.length will tell us now long the getStr is. This is required for sending to ThingSpeak and to the ESP8266.

Comme la fois précédente, je crée une chaîne appelée cmd et lui envoie les codes AT, j'ajoute l'IP et le port de ThingSpeak, puis je l'envoie à ThingSpeak.

À nouveau, je vérifie s'il y a une erreur ou non.

String getStr = "GET /update?api_key="; getStr += "8KS0CVMQ12XXD817"; getStr += "&field1="; getStr += String(SendTempLevel); getStr += "&field2="; getStr += String(SendHumLevel); getStr += "\r\n\r\n";

Une nouvelle chaîne, getStr, est créée avec la commande GET - avec ma clé API ThingSpeak, la température et l'humidité.

Une chaîne cmd est créée à nouveau avec une commande AT, et la commande getStr.length nous dira maintenant quelle est la longueur de la chaîne. Ceci est obligatoire pour un envoi à ThingSpeak et au ESP8266.

Like previous sends, we check for a reply. The greater than character (>) means good. Anything else is bad, and we send AT+CIPCLOSE to close the connection. Again, info is sent to the serial monitor to tell us what’s happening. Uploading the gist code to the MEGA should be error-free, and, when running, the serial monitor will tell you what is happening. If you’re seeing something like that shown in the serial monitor image here, then you’re good to go. ThingSpeak should be receiving your data. If you’ve gotten this far, then you’ve done great. As getting the ESP8266 to work, and sending data to ThingSpeak, are probably the most difficult parts of this project. The rest should be a breeze.

Comme pour les envois précédents, nous vérifions la réponse. Le caractère plus grand que (>) signifie que c'est bon, tout autre signe indiquerait que c'est mauvais, nous envoyons AT+CIPCLOSE pour fermer la connexion. À nouveau, l'info est envoyée au moniteur série pour nous dire ce qui arrive.

L'envoi du code gist au MEGA devrait être sans erreur et, quand il tourne, le moniteur série vous dira ce qui se passe.

Si vous voyez quelque chose sur le moniteur série comme ce qui est montré ici sur l'image, alors tout marche bien. ThingSpeak devrait recevoir vos données.

Si vous êtes arrivé jusque-là, c'est que vous avez très bien travaillé. Obtenir du ESP8266 qu'il marche et envoyer des données à ThingSpeak sont probablement les parties les plus difficiles de ce projet. Le reste devrait être un plaisir.

Famous last words! I should add that I’m currently sending data to ThingSpeak every 20-25 seconds. This is obviously for testing at the moment. Before I use the Brewduino, I will change the delays to maybe every 30 minutes or so. Next month we’ll add an LCD screen, and test the relay switch.

Ah ! Les fameux derniers mots !

Je devrais ajouter que j'envoie des données à ThingSpeak environ toutes les 20-25 secondes. C'est bien sûr pour faire des tests actuellement. Avant d'utiliser le brasse-duino, je modifierai les intervalles pour quelque chose comme une trentaine de minutes.

Le mois prochain, nous ajouterons un écran LCD et testerons le relais.

issue106/arduino.1457737245.txt.gz · Dernière modification : 2016/03/12 00:00 de erlevo