Ceci est une ancienne révision du document !
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/humidity sensor and a 128×32 SSD1306 display. The overall cost of a single unit is about $6.00 USD, since I bought all the parts in bulk a number of months ago. Eventually, each unit can have a code upgrade to enable it to talk to a MQTT server running somewhere in the house and have all the temperatures to display on a monitor. Now that you know the idea behind the project, you will recognize that we have already dealt with various parts of this project before. So, let’s jump right into the project.
Here is the breadboard layout of the project. Please notice that I used a 128×64 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. 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.
Here’s the import section… 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 You should have all of the support files from previous projects. The DHT library is part of the ESP8266 Micropython library. Just in case, I’ve included all the files that you need in the repository for this project. You’ll find the link at the end of this article. 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, False) # wri = Writer(ssd, font10) wri = Writer(ssd, font6) Writer.set_textpos(ssd, 0, 0)
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) 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.
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, SSD1306_I2C WIDTH = const(128) HEIGHT = const(64) # 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 128×64 and 128×32 SSD1306 displays around, and want to be able to swap quickly. Just so you know, if you are using a 128×64 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, so I’ve removed that code from that shown below, but it still is in the repository file. Notice that I’ve commented out the original setup function definition and replaced the soft parameter set to False. This keeps us from using the software I2C library. I have done that, since we’re be forcing the I2C pins to SDA on pin 4 and SCL to pin 5. I’ve included the SPI code here, just to provide you a landmark for the code.
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), and usually you can just ignore the warning for now. 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://github.com/gregwa1953/FCM-175_MicroThisMicroThat. Ensure you place them all on your ESP8266 board. 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!