issue175:micro-ci_micro-la
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue175:micro-ci_micro-la [2021/11/28 13:21] – créée auntiee | issue175:micro-ci_micro-la [2021/12/03 18:27] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | It’s November and I can’t believe we’ve already gone through eight MicroThis MicroThat articles. We’ve all come a long way in our understanding of microcontrollers and MicroPython. Back in part 4 (FCM#170), we covered a number of small parts of a large project using the ESP32. Due to a number of things, the article that I wanted to present didn't work out, so I will move a project from my “stuff to do” list. With any luck, I will be able to present it next month. | + | **It’s November and I can’t believe we’ve already gone through eight MicroThis MicroThat articles. We’ve all come a long way in our understanding of microcontrollers and MicroPython. Back in part 4 (FCM#170), we covered a number of small parts of a large project using the ESP32. Due to a number of things, the article that I wanted to present didn't work out, so I will move a project from my “stuff to do” list. With any luck, I will be able to present it next month.** |
- | A while ago, I had an idea about creating a number of standalone temperature sensors for each room in our apartment. The problem (like most apartments) is that the central heating and air conditioning thermostat is placed in an area that really bears no resemblance to the actual needs of the various rooms in the apartment. One room (the living area, where I spend 80% of my waking time) is ice cold all the time – at least to me. Summer or winter, it doesn’t matter. It’s ALWAYS cold. The farthest bedroom is always hot. Since we have decided that we will be moving to a house sooner than later, it would be helpful to have these standalone modules in each room of the house so we can glance at the display and see what the room temperature is at any time. The devices are built with the ESP8266, a DHT22 temperature/ | + | Nous sommes en novembre et je n' |
- | Here is the breadboard layout of the project. Please notice that I used a 128x64 SSD1306 in the diagram, but the code will work with the smaller display. Just be sure that you get the pinout for whichever OLED that you are using in your project to properly line with the wiring. Sometimes the pins are different from one manufacturer to another. | + | |
+ | **A while ago, I had an idea about creating a number of standalone temperature sensors for each room in our apartment. The problem (like most apartments) is that the central heating and air conditioning thermostat is placed in an area that really bears no resemblance to the actual needs of the various rooms in the apartment. One room (the living area, where I spend 80% of my waking time) is ice cold all the time – at least to me. Summer or winter, it doesn’t matter. It’s ALWAYS cold. The farthest bedroom is always hot. Since we have decided that we will be moving to a house sooner than later, it would be helpful to have these standalone modules in each room of the house so we can glance at the display and see what the room temperature is at any time. The devices are built with the ESP8266, a DHT22 temperature/ | ||
+ | |||
+ | *Il y a quelque temps, j'ai eu l' | ||
+ | |||
+ | |||
+ | **Here is the breadboard layout of the project. Please notice that I used a 128x64 SSD1306 in the diagram, but the code will work with the smaller display. Just be sure that you get the pinout for whichever OLED that you are using in your project to properly line with the wiring. Sometimes the pins are different from one manufacturer to another. | ||
Now we’ll look into the code. | Now we’ll look into the code. | ||
- | The main file will be named boot.py, and therefore every time the device is booted or reset, the program will start. This is what we want since we won’t be using a computer with Thonny to control it once we get ready to deploy it. Once we plug it in, the program should start running. | + | The main file will be named boot.py, and therefore every time the device is booted or reset, the program will start. This is what we want since we won’t be using a computer with Thonny to control it once we get ready to deploy it. Once we plug it in, the program should start running.** |
+ | |||
+ | Voici le schéma de la plaque d' | ||
+ | |||
+ | Maintenant, nous allons nous pencher sur le code. | ||
+ | |||
+ | Le fichier principal sera nommé boot.py, et donc chaque fois que le dispositif est démarré ou réinitialisé, | ||
- | Here’s the import section… | + | **Here’s the import section… |
from machine import Pin | from machine import Pin | ||
Ligne 22: | Ligne 35: | ||
Next we have the setup section. Here, we set up the DHT temperature sensor and the OLED device. | Next we have the setup section. Here, we set up the DHT temperature sensor and the OLED device. | ||
+ | |||
+ | sensor = dht.DHT22(Pin(14)) | ||
+ | ssd = setup(False, | ||
+ | # wri = Writer(ssd, font10) | ||
+ | wri = Writer(ssd, font6) | ||
+ | Writer.set_textpos(ssd, | ||
+ | |||
+ | Voici la section d' | ||
+ | |||
+ | from machine import Pin | ||
+ | from time import sleep | ||
+ | import dht | ||
+ | from ssd1306_setup import WIDTH, HEIGHT, setup | ||
+ | from writer import Writer | ||
+ | # import font10 | ||
+ | import font6 | ||
+ | |||
+ | Vous devriez avoir tous les fichiers de support des projets précédents. La bibliothèque DHT fait partie de la bibliothèque Micropython ESP8266. Juste au cas où, j'ai inclus tous les fichiers dont vous avez besoin dans le dépôt de ce projet. Vous trouverez le lien à la fin de cet article. | ||
+ | |||
+ | Ensuite, nous avons la section de configuration. Ici, nous configurons le capteur de température DHT et le dispositif OLED. | ||
sensor = dht.DHT22(Pin(14)) | sensor = dht.DHT22(Pin(14)) | ||
Ligne 29: | Ligne 62: | ||
Writer.set_textpos(ssd, | Writer.set_textpos(ssd, | ||
- | Notice that I’ve commented out the use of the font10 file. I find that the font6 is clear enough to see from about 3-4 feet, which should be enough. Use whichever font file you find better for your application. | ||
- | Now we get to the main loop of the program (top right). We start a “forever” loop, use a try|except call (just in case the sensor fails to read), and use a sleep(2) statement to give the DHT22 a two-second delay before we query it. This is the minimum “safe” time, since the DHT22 is a little slower than some of the more expensive Temperature sensors. We then call the measure method to get the temperature and humidity values, and place them into temporary variables. As I’ve often said, my old brain can’t think in Celsius temperatures. I convert it to Farenheit and then send the values (both temperature and humidity) | + | **Notice that I’ve commented out the use of the font10 file. I find that the font6 is clear enough |
- | That’s the entirety of the boot.py code. Now we’ll take a quick look at the ssd1306_setup.py file. You should remember that this is part of the writer library that Peter Hinch wrote. We used the writer library back in issue #172 (article part 6). As I usually do, I’ll start with the import and global setup sections. | + | Now we get to the main loop of the program (top right). We start a “forever” loop, use a try|except call (just in case the sensor fails to read), and use a sleep(2) statement to give the DHT22 a two-second delay before we query it. This is the minimum “safe” time, since the DHT22 is a little slower than some of the more expensive Temperature sensors. We then call the measure method to get the temperature and humidity values, and place them into temporary variables. As I’ve often said, my old brain can’t think in Celsius temperatures. I convert it to Farenheit and then send the values (both temperature and humidity) to the OLED display, and finally call the show method of the SSD library. We end with the ‘except’ portion of the code to try to recover politely.** |
+ | |||
+ | Remarquez que j'ai commenté l' | ||
+ | |||
+ | Nous arrivons maintenant à la boucle principale du programme (en haut à droite). Nous démarrons une boucle « éternelle », utilisons un appel try|except (juste au cas où le capteur n' | ||
+ | |||
+ | |||
+ | **That’s the entirety of the boot.py code. Now we’ll take a quick look at the ssd1306_setup.py file. You should remember that this is part of the writer library that Peter Hinch wrote. We used the writer library back in issue #172 (article part 6). As I usually do, I’ll start with the import and global setup sections. | ||
+ | |||
+ | |||
+ | import machine | ||
+ | from machine import Pin | ||
+ | from ssd1306 import SSD1306_SPI, | ||
+ | |||
+ | WIDTH = const(128) | ||
+ | HEIGHT = const(64) | ||
+ | # HEIGHT = const(32)** | ||
+ | |||
+ | C'est l' | ||
Ligne 44: | Ligne 94: | ||
# HEIGHT = const(32) | # HEIGHT = const(32) | ||
- | You will want to make sure that the WIDTH and HEIGHT constants are the same for your display (see below). I commented out the last HEIGHT line, since I have both 128x64 and 128x32 SSD1306 displays around, and want to be able to swap quickly. Just so you know, if you are using a 128x64 SSD1306, you can still set the HEIGHT to 32. It shouldn’t matter having a larger display using a smaller HEIGHT value. It doesn’t work the other way around though. The original code was written for the Pyboard microcontroller, | ||
- | This is to make sure that we all use the same pin values for the I2C. You will probably get a depreciation warning (I’m not using the ESP8266 Micropython firmware 1.16 from 2021-06-18), | + | **You will want to make sure that the WIDTH and HEIGHT constants are the same for your display (see below). I commented out the last HEIGHT line, since I have both 128x64 and 128x32 SSD1306 displays around, and want to be able to swap quickly. Just so you know, if you are using a 128x64 SSD1306, you can still set the HEIGHT to 32. It shouldn’t matter having a larger display using a smaller HEIGHT value. It doesn’t work the other way around though. The original code was written for the Pyboard microcontroller, |
+ | |||
+ | Vous devez vous assurer que les constantes WIDTH (largeur) et HEIGHT (hauteur) sont les mêmes pour votre écran (voir ci-dessous). J'ai commenté la dernière ligne HEIGHT, car j'ai à la fois des écrans SSD1306 128 x 64 et 128 x 32, et je veux être capable de les échanger rapidement. Pour votre information, | ||
+ | |||
+ | |||
+ | **This is to make sure that we all use the same pin values for the I2C. You will probably get a depreciation warning (I’m not using the ESP8266 Micropython firmware 1.16 from 2021-06-18), | ||
All the other code files are support files. You should have them from the previous projects, but I’ve included them in the github repository. You can find it at https:// | All the other code files are support files. You should have them from the previous projects, but I’ve included them in the github repository. You can find it at https:// | ||
Ligne 52: | Ligne 106: | ||
I’ve kept the article for this month fairly short, since my Python article ran so long. I want to make sure that the other authors have room for their good work. | I’ve kept the article for this month fairly short, since my Python article ran so long. I want to make sure that the other authors have room for their good work. | ||
- | Until next time, as always; stay safe, healthy, positive and creative! | + | Until next time, as always; stay safe, healthy, positive and creative!** |
+ | |||
+ | C'est pour s' | ||
+ | |||
+ | Tous les autres fichiers de code sont des fichiers de support. Vous devriez les avoir dans les projets précédents, | ||
+ | |||
+ | J'ai gardé assez court l' | ||
+ | |||
+ | Jusqu' |
issue175/micro-ci_micro-la.1638102061.txt.gz · Dernière modification : 2021/11/28 13:21 de auntiee