issue106:arduino
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 | ||
issue106:arduino [2016/03/06 16:34] – d52fr | issue106:arduino [2016/04/08 16:30] (Version actuelle) – créée andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | **So, with the basic plan thought out, it’s now time to actually pick up some components | + | **WARNING: This month we’re heading into the big league with controlling household voltage devices. It should go without saying, but I’ll say it anyway, you must make sure your 110/240V devices are unplugged before chopping cables, |
- | Temperature | + | ATTENTION : ce mois-ci, nous passons en première division en pilotant des appareils sous tension domestique. Il va sans dire, mais je le dis quand même, que vous devez vous assurer que vos appareils sont débranchés avant de couper des câbles et vérifier deux fois votre câblage avant de remettre la fiche dans la prise murale. |
- | Temperature sensing is highly important to this as it will determine when the heat mat should be switched | + | **In previous articles, I used an LCD screen which, while it worked, would need about a dozen wires and a potentiometer to control |
- | 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.** | + | These newer LCD screens |
- | Maintenant que nous avons réfléchi au plan de base, il est temps de choisir quelques composants pour commencer le montage. | + | Before adding LCD code, we need to do a scan of the LCD screen to get its I2C address. Different models have different addresses. So, grab the code from: http:// |
- | Température | + | Dans les articles précédents, |
- | La détection | + | Ces nouveaux écrans LCD sont I2C ce qui signifie qu'ils ont une petite carte de pilotage au dos. Cela signifie aussi qu'ils ont besoin d'une nouvelle bibliothèque (https:// |
- | Plutôt que d'utiliser | + | Avant d'ajouter |
- | **Build 1 | + | **LCD Code |
- | 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. | + | I need to include that new library: |
- | 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 | + | #include < |
- | That’s the basic circuit for this first part. | + | Define various pins (I2C stuff that you shouldn’t need to touch), but insert |
- | The code for the DHT22 is pretty straightforward, | + | #define I2C_ADDR |
+ | < | ||
- | Assemblage n°1 | + | #define BACKLIGHT_PIN |
+ | #define En_pin | ||
+ | #define Rw_pin | ||
+ | #define Rs_pin | ||
+ | #define D4_pin | ||
+ | #define D5_pin | ||
+ | #define D6_pin | ||
+ | #define D7_pin | ||
- | J'ai connecté le module DHT22 en utilisant trois picots. Un fil au +5V, un à la masse et l' | + | LiquidCrystal_I2C |
- | À 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. | + | In my setup, I initialise the screen as 16X2, turn on the backlight, print a message, and use a delay to allow it to be read.** |
- | C'est le circuit de base de la première partie. | + | Code LCD |
- | 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 | + | J'ai besoin |
- | **Code | + | #include < |
- | I’ll be adding all my code for this project to this Github gist: https:// | + | Définissez plusieurs picots (des éléments I2C que vous n' |
- | 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. | + | #define I2C_ADDR |
+ | < | ||
- | For the DHT22, I’m using the two libraries from here: http:// | + | #define BACKLIGHT_PIN |
+ | #define En_pin | ||
+ | #define Rw_pin | ||
+ | #define Rs_pin | ||
+ | #define D4_pin | ||
+ | #define D5_pin | ||
+ | #define D6_pin | ||
+ | #define D7_pin | ||
- | 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. | + | LiquidCrystal_I2C |
- | I start by including the DHT library and define the pin for the DHT22. I then define my wifi SSID and password.** | + | Dans mon paramétrage, |
- | Code | + | **lcd.begin (16, |
+ | lcd.setBacklightPin(BACKLIGHT_PIN, | ||
- | J' | + | lcd.setBacklight(HIGH); |
- | 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. | + | lcd.home(); |
- | Pour le DHT22, j' | + | lcd.print(" |
- | Vous aurez besoin de copier/ | + | delay(2000); |
- | 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. | + | Most of those commands are pretty self explanatory. |
- | **Now we’re on to the main setup. I use two serial begin commands: | + | I now have my temperature, |
- | Serial.begin(115200); | + | lcd.begin (16, |
+ | lcd.setBacklightPin(BACKLIGHT_PIN, | ||
- | Serial2.begin(9600); | + | lcd.setBacklight(HIGH); |
- | 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. | + | lcd.home(); |
- | < | + | lcd.print("Screen |
- | Serial2.println("AT" | + | |
- | + | ||
- | Serial.println(" | + | |
- | + | ||
- | delay(5000); | + | |
- | + | ||
- | char okcheck[]=" | + | |
- | + | ||
- | if(Serial2.find(okcheck)){ | + | |
- | Serial.println(" | + | |
- | connectWiFi(); | + | |
- | Serial.println(" | + | |
- | else{ | + | |
- | Serial.println(" | + | |
- | }</ | + | |
- | Maintenant, nous en venons au réglage principal. J' | + | delay(2000); |
- | Serial.begin(115200); | + | La plupart de ces commandes sont explicites. |
- | Serial2.begin(9600); | + | Maintenant, j'ai la température, |
- | Le Serail.begin sont pour le DHT22 ; de cette façon, j' | + | **lcd.setCursor(0, |
- | < | + | lcd.print("Checking... "); |
- | Serial2.println("AT" | + | |
- | + | ||
- | Serial.println(" | + | |
- | + | ||
- | delay(5000); | + | |
- | + | ||
- | char okcheck[]=" | + | |
- | + | ||
- | if(Serial2.find(okcheck)){ | + | |
- | Serial.println(" | + | |
- | connectWiFi(); | + | |
- | Serial.println(" | + | |
- | else{ | + | |
- | Serial.println(" | + | |
- | }</ | + | |
- | **When using Serial2, I’m talking to the ESP8266. When using just Serial, I’m talking | + | lcd.setCursor() is for placing |
- | Connecting | + | NOTE: if you don’t see anything on your screen on first run, try turning the potentiometer on the back of the LCD screen |
- | boolean connectWiFi(){ | + | lcd.setCursor(0,2); |
- | Serial2.println(" | + | |
- | delay(2000); | + | |
- | String cmd=" | + | |
- | < | + | |
- | cmd+=SSID; | + | |
- | cmd+=" | + | |
- | cmd+=PASS; | + | |
- | cmd+=" | + | |
- | // send string | + | |
- | Serial2.println(cmd); | + | |
- | delay(5000); | + | |
- | // was the login accepted? | + | lcd.print("Checking... "); |
- | char okcheck[]=" | + | |
- | if(Serial2.find(okcheck)){ | + | |
- | Serial.println("Login accepted" | + | |
- | return true; | + | |
- | }else{ | + | |
- | Serial.println(" | + | |
- | 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=\”, | + | lcd.setCursor() sert à positionner le texte à un endroit précis de l' |
- | For the main loop, I first jump to TempHum() to get my temperature info from the DHT22. | + | NOTE : si, au premier démarrage, vous ne voyez rien sur votre écran, essayez de tourner le potentiomètre au dos de l' |
- | Serial.print(" | + | **Solid State Relay |
- | int chk = DHT.read22(DHT22_PIN); | + | |
- | switch (chk) | + | |
- | { | + | |
- | … | + | |
- | } | + | |
- | < | + | |
- | Serial.print(DHT.humidity, | + | |
- | Serial.print(", | + | |
- | Serial.println(DHT.temperature, | + | |
- | **This is simply creating an integer called chk, and reading the DHT22 pin. I check the status of the DHT22 (the switch, | + | First, you can use a relay switch |
- | String SendTempLevel = String((float)DHT.temperature, 0); | + | Before I chop into the cable of my beloved heat mat, I want to make sure this circuit will work. So, instead, I’m going to chop into an unused desk lamp. |
- | String SendHumLevel = String((float)DHT.humidity, 0); | + | With it unplugged, I chopped through the cable and paired off the two inner cables |
- | The two Strings are holders for the temperature and humidity and are what I’ll send to ThingSpeak. | + | Relais statique |
- | 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 | + | Disons d' |
- | String cmd = " | + | Avant de couper le câble de ma chère couverture chauffante, je veux être sûr que le circuit fonctionne. Aussi, à la place, je vais sacrifier une lampe de bureau inutilisée. |
- | cmd += "184.106.153.149"; | + | |
- | 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. | + | Après l' |
- | Again, I do a check to see if there’s an error or not. | + | **The solid state relay (SSR for short) has two DC inputs (in the photo, at the bottom left, is the red +5v and blue ground). It also has (depending on your SSR) one or more channels. My SSR has two channels. This is where your Arduino (yellow wire in the photo) will tell the SSR to go HIGH or LOW. The final two inputs are for the cable you wish to open/close. In this case the live (top wires in the photo) from my lamp. Now, it didn’t help that my inputs are labelled wrongly. The one marked ‘Ch1’ is actually for SSR channel two. So if you try this and it doesn’t work, try switching your wire from Ch1 to Ch2. You should see a light come on on the SSR to show which channel is HIGH/LOW. |
- | String getStr = "GET / | + | With those things in place it’s time to write some code. |
- | getStr += " | + | |
- | getStr += "& | + | |
- | getStr += String(SendTempLevel); | + | |
- | getStr += "& | + | |
- | getStr += String(SendHumLevel); | + | |
- | getStr += " | + | |
- | A new string, getStr, is created with a GET command – with my ThingSpeak API key, and temperature and humidity. | + | To test the SSR I’ll flash the lamp on/off. The first new lines of code are: |
- | 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.** | + | #define ssr1 53 |
- | **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. | + | int state = LOW; |
- | Uploading the gist code to the MEGA should be error-free, and, when running, the serial monitor will tell you what is happening. | + | unsigned long previousMillis= 0; |
- | 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. | + | const long interval= 1000;** |
- | 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.** | + | Le relais statique (RS pour faire court) a deux entrées en courant continu (sur la photo, en bas à gauche, le +5V est rouge et la masse bleue). Il comporte aussi (suivant votre RS) un ou plusieurs canaux. Le mien en a deux. C'est par ici que votre Arduino (fil jaune sur la photo) alimentera ou non votre RS. Les deux dernières entrées sont pour le câble que vous souhaitez ouvrir/ |
- | **Famous last words! | + | Une fois tout ceci en place, le moment est venu d' |
- | 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. | + | Pour tester le RS, je vais faire clignoter la lampe. Les premières nouvelles lignes de code sont : |
- | Next month we’ll add an LCD screen, and test the relay switch.** | + | #define ssr1 53 |
+ | |||
+ | int state = LOW; | ||
+ | |||
+ | unsigned long previousMillis= 0; | ||
+ | |||
+ | const long interval= 1000; | ||
+ | |||
+ | **This defines pin 53 on the Arduino as the control pin for the SSR. The state is to keep track of whether the lamp is currently on/off. The previousMillis and interval are for the flash. I’m going to try and use millis rather than delays as millis is more preferable to the program halting delay command. | ||
+ | |||
+ | I begin with the usual pinMode and then set the SSR to the initial state of LOW. In other words, off. This SSR is ‘low level trigger’ which means LOW is on, and HIGH is off. | ||
+ | |||
+ | pinMode(ssr1, | ||
+ | |||
+ | digitalWrite(ssr1, | ||
+ | |||
+ | In the setup I create: | ||
+ | |||
+ | unsigned long currentMillis = millis(); | ||
+ | |||
+ | as this will be used in the if/then to switch on/off the lamp.** | ||
+ | |||
+ | Ceci définit le picot 53 de l' | ||
+ | |||
+ | Je commence avec l' | ||
+ | |||
+ | pinMode(ssr1, | ||
+ | |||
+ | digitalWrite(ssr1, | ||
+ | |||
+ | Dans le paramétrage, | ||
+ | |||
+ | unsigned long currentMillis = millis(); | ||
+ | |||
+ | car il sera utilisé dans la boucle si/alors pour commuter la lampe. | ||
+ | |||
+ | **The only other additional code of note is the switching (top right). CurrentMillis and previousMillis will keep track of how many milliseconds the loop is running for. When it reaches the interval (set up at the start) then the loop ends. Note that this means the code never grinds to a halt like it would with a delay. I can still put stuff to do inside that HIGH/LOW if statement. | ||
+ | |||
+ | The inner if/else is just checking to execute: | ||
+ | • if it’s currently off, let’s put it on, | ||
+ | • if it’s currently on, let’s put it off. | ||
+ | |||
+ | And, finally, do the on/off. | ||
+ | |||
+ | With the interval set at 1000 this means the lamp will turn on/off every second.** | ||
+ | |||
+ | La seule addition notable de code est la commutation (en haut à droite). CurrentMillis et PreviousMillis conservent les durées en millisecondes de parcours de la boucle. Quand « interval » est atteint (fixé au début), | ||
+ | |||
+ | Le if/else intérieur contrôle juste l' | ||
+ | • si l' | ||
+ | • si l' | ||
+ | |||
+ | Et, enfin, exécute le Allumé/ | ||
+ | |||
+ | Avec un intervalle réglé à 1000, ça signifie que la lampe va clignoter chaque seconde. | ||
+ | |||
+ | **The only downside of removing the delays is that I’m now hammering ThingSpeak every minute or less, but I’ll fix that later. It’s working. That’s the main thing! | ||
+ | |||
+ | The code for this is a gist on my Github at: https:// | ||
+ | |||
+ | To check for a specific temperature, | ||
+ | |||
+ | if (DHT.temperature > 24 ) { | ||
+ | state=LOW; | ||
+ | } | ||
+ | |||
+ | if (DHT.temperature < 24) { | ||
+ | state=HIGH; | ||
+ | } | ||
+ | |||
+ | If the temperature goes above 24, the lamp comes on. If the temperature falls below 24, the lamp goes off.** | ||
+ | |||
+ | Le seul inconvénient de retirer les délais est que je vais maintenant solliciter ThingSpeak toutes les minutes ou moins, mais je le réglerai plus tard. Ça marche. C'est le principal ! | ||
+ | |||
+ | Le code correspondant est un gist sur mon Github à : https:// | ||
+ | |||
+ | Pour surveiller une température particulière, | ||
+ | |||
+ | if (DHT.temperature > 24 ) { | ||
+ | state=LOW; | ||
+ | } | ||
+ | |||
+ | if (DHT.temperature < 24) { | ||
+ | state=HIGH; | ||
+ | } | ||
+ | |||
+ | Si la température dépasse 24 °C, la lampe s' | ||
+ | |||
+ | |||
+ | |||
+ | **Mike K. - qualified electrical engineer (true!); and who has an uncle who was electrocuted notes: | ||
+ | |||
+ | Ronnie says: listen to this man. He knows FAR more than me! | ||
+ | |||
+ | Timer-overflow: | ||
+ | |||
+ | You may need to allow for that event in your code, otherwise, your code will stop working when it occurs. For example. if a “long” is 4 bytes, and if the time unit is actual millisecs, then this code will fail after 49 days operation (at most!).** | ||
+ | |||
+ | Notes de Mike K., ingénieur qualifié en électricité (vrai !) et dont un oncle a été électrocuté : | ||
+ | |||
+ | Ronnie écrit : écoutez ce gars. Il en connaît beaucoup plus que moi ! | ||
+ | |||
+ | Dépassement de la temporisation : dans le monde réel, vous ne devriez pas avoir besoin de déterminer la valeur maximale de la variable « currentMillis » parce que, tôt ou tard, elle sera remise à zéro. | ||
+ | |||
+ | Vous pourriez avoir besoin de prévoir cet événement dans votre code ; autrement, votre code s' | ||
+ | |||
+ | **Temperature triggers: In the real world, your temp sensor might be “noisy”, | ||
+ | • In the initialisation code,set the Device to OFF, and set the “Device_State” to OFF (for simplicity). | ||
+ | • If the new temperature is at/below the lower value, and the Device_State is currently OFF, then set it ON, and set Device_State to ON. | ||
+ | • If the new temperature is at/above the upper value, and the Device_State is currently ON, then set it OFF, and set Device_State to OFF. | ||
+ | • If we can read the current state of the device, then the above “Device_State” variable is not needed. And, reverse the above references to ON/OFF, if appropriate.** | ||
+ | |||
+ | Seuils de température : dans le monde réel, votre détecteur de température pourrait être « bruité » et, dans ce cas, quand la température est « autour de 24 », la lecture pourrait tourner autour de 23/24/25, avec de nombreux changements. Si ça arrive, le code pourrait devenir fou, commutant le relais à toute vitesse. Pour éviter cela, on peut utiliser deux seuils, juste en dehors de cette zone perturbée ; peut-être 22 et 26 degrés. L' | ||
+ | • Dans le code d' | ||
+ | • Si la nouvelle température est à la valeur basse (ou en-dessous), | ||
+ | • Si la nouvelle température est à la valeur haute (ou au-dessus), et que l' | ||
+ | • Si vous pouvez lire l' | ||
+ | |||
+ | **Wiring: When cutting the mains cable, strip back the outer insulation very slowly and carefully, and do not damage or cut into the wires which are not used by the relay circuit. If there is an earth cable, and if there is an earthing connector in the SSR circuit, then, do not cut the earth cable, but strip about 1-2 cm of it, fold it, and insert it into the earth connector. | ||
+ | |||
+ | For safety, the SSR circuit must be enclosed in a suitable protective box. If the relays get hot when being used, then ensure there is adequate ventilation in the box, and ensure the box is not placed on soft surfaces such as carpets, duvets, etc - which would inhibit ventilation.** | ||
+ | |||
+ | Câblage : Quand vous coupez les câbles d' | ||
+ | |||
+ | Par sécurité, le circuit du RS doit être placé dans une enveloppe protectrice adaptée. Si les relais deviennent chauds à l' | ||
+ | |||
+ | **A user might install two relays - one on the positive and one on the negative lines. If a single relay is used - as in this project - it must be connected into the LIVE wire, so that, when the device (lamp, etc) is " | ||
+ | |||
+ | Rating of Relays: Ensure that the relay can easily handle the mains voltage (110V AC or 240V AC), and the maximum current that will be used by the devices. For example, a light bulb might use 1.0 Amp current, whereas an electric room heater might use 20+ Amps. | ||
+ | |||
+ | Overall, we should expect that Arduino fans might have kits in their bedrooms, and might decide to control the mains heating, lighting, ventilation, | ||
+ | |||
+ | L' | ||
+ | |||
+ | Caractéristiques du relais : Assurez-vous que le relais peut facilement supporter la tension secteur (110 V AC ou 240 V AC), ainsi que la courant maximal absorbé par les appareils. Par exemple, une lampe à incandescence pourrait absorber 1 A, alors qu'un radiateur électrique pourrait utiliser 20 A ou plus. | ||
+ | |||
+ | En général, vous pouvez vous attendre à ce que des fans d' |
issue106/arduino.1457278458.txt.gz · Dernière modification : 2016/03/06 16:34 de d52fr