Outils pour utilisateurs

Outils du site


issue106:arduino

Ceci est une ancienne révision du document !


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.

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.

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

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.1457270317.txt.gz · Dernière modification : 2016/03/06 14:18 de d52fr