Outils pour utilisateurs

Outils du site


issue106:arduino

Ceci est une ancienne révision du document !


!!ATTENTION : lors du scribage, rchercher et éliminer les et !!

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 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.

À part avec le moniteur série, il n'y a pas moyen de voir la température. Je préfère les visuels, 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 +5V ou vous le grillez),le fil jaune visible sur le schéma, un pour la masse (fil noir), un pour chaque RX et 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 la première partie.

Le code pour le DHT22 est assez évident ; aussi, nous le verrons au paragraphe suivant. Le ESP8266 est plus difficile 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. Je trouve que ça fait une bonne r.a.z. du module. Quand le code tourne, vous verrez les voyants RX/TX qui flashent sur l'Arduino et, en même temps, une lampe bleue qui flashe 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'ajouerai tout mon code pour ce projet à ce gist Github : https://gist.github.com/ronnietucker/7fc62df161107116cf93

Au moment où vous lisez ceci, il y aura eu beaucoup de révisions du code ajoutées sur gist, mais c'est la revision 2 que je présente ici.

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

Vous aurez besoin de copier/coller le texte des deux fichiers dans des 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éfinir 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 des commandes serial begin :

Serial.begin(115200);

Serial2.begin(9600);

Le Serial.begin sont pour le DHT22 ; de cette façon, j'obtenais les résultats sur le moniteur série avant que j'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 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 Serail2, je suis en conversation avec ESP8266. Quand j'utilise simplement Serial, je discute avec le moniteur série (ou j'imprime dessus). J'envoie la commande AT au ESP8266 imprime un texte sur le moniteur série pour me montrer 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 qui indique « non connecté ».

Pour se connecter 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=\"";
<nowiki>// 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;
}</nowiki>

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);

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);

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.

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.

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.

issue106/arduino.1457278978.txt.gz · Dernière modification : 2016/03/06 16:42 de d52fr