Outils pour utilisateurs

Outils du site


issue170:micro-ci_micro-la

Ceci est une ancienne révision du document !


I want to apologize if this month’s article seems a bit dis-jointed. I’ve been having multiple medical issues the last few weeks, and I REALLY wanted to get this article out to you.

Last month, I told you that we will be looking at the SparkFun Thing Plus ESP-32 WROOM (https://www.sparkfun.com/products/15663) and we will. However, I do want to bring some news of the RP2040 world and the Raspberry Pi Pico first.

Arduino has finally released their Nano RP2040 Connect which costs about $26 USD. It supports Wi-Fi, Bluetooth, and Bluetooth Low-Energy (v4.2), a built-in microphone for sound or voice activation, an RGB LED, and a six-axis motion sensor.

Seeed Studio has released the Wio RP2040 mini development board which includes 2.4 GHz 802.11b/g/n WiFi 4 supporting AP & station modes, but no Bluetooth support. The estimated price will be about $13 USD and is available only for pre-order at this point. It’s still unclear when the board will start shipping.

Finally, there is really big news from Adafruit. I’ll quote the headline from Tom’s Hardware site…”CircuitPython Libraries Slither Into MicroPython on the Raspberry Pi Pico”. That’s right. This is a big thing for the RPi Pico, since eventually the entire CircuitPython library should be available to MicroPython users. Currently, there are many drivers that don’t work, but it’s early days yet. Congratulations to Adafruit for making this move! You can find more about it at https://learn.adafruit.com/circuitpython-libraries-on-micropython-using-the-raspberry-pi-pico .

Sparkfun Thing Plus 32 Wroom

On to the real subject of my article for this month. The SparkFun Thing Plus 32 Wroom is an awesome little microcontroller and easily runs MicroPython. The price of the board is a bit steep, about $21 USD. If the cost is too high for your budget, you can find a similar generic board with the Wroom WiFi chip on the web. I found an Aokin ESP32 Development board from Amazon in a three-pack for just under $17 USD. The pinout and form factor between the two are different, but the same MicroPython firmware works on both. Just be sure to find a pinout for whichever board you choose. In addition, the generic ESP32 Development board might not fit properly on your breadboard.

For the Generic board, you can get the pinout at https://circuits4you.com/2018/12/31/esp32-devkit-esp32-wroom-gpio-pinout/, as well as other places. For the Sparkfun Thing Plus, see the link at the top of this article.

Both boards support 2.4 Ghz WiFi and Bluetooth as well as capacitive touch sensors, Hall sensor (magnetic sensor) and other things.

Below is the pinout for the Sparkfun Thing Plus 32 WROOM…

Above is the ESP32 Generic Dev board…

Now, let’s get into our projects for this month…

Project Program #1 - TouchPad

Both the SparkFun Thing Plus and the generic ESP32 development board come with multiple touchpad sensor inputs. For the Thing Plus, there are 8 that are broken out to the external pins. For the Generic that I am using, there are 9 broken out and one that is set to GPIO 0. Just which pins are which input, will be shown on the pinout diagram for your board. When you specify the pin number, be sure to use the GPIO pin number, not the touch number or the physical pin. In this case, we will simply use a male-to-male jumper connected to GPIO pin #27 as our input “pad” denoted as Touch7. The import and setup will be as follows…

import machine

import utime

touch7 = machine.TouchPad(machine.Pin(27))

Now (top right), we’ll create a variable called lowvalue and initialize it to 1000. When running, the touch pad actually floats between 700 and 600. Then we start a “forever loop”. We’ll read the value of the touch pin and compare that with the last recorded lowvalue figure. If the current touchvalue is lower than the last lowvalue, we set lowvalue to the touchvalue. We also print the lowvalue at this point. Finally we sleep for 100 milliseconds and loop again. We can use the {Stop} button in Thonny to quit the program.

Here is what the printout looks like. It takes about 1 ½ to 2 seconds to get to the lowest value.

LowValue = 677 LowValue = 671 LowValue = 667 LowValue = 658 LowValue = 616 LowValue = 456 LowValue = 390 LowValue = 347 LowValue = 227 LowValue = 128 LowValue = 107 LowValue = 100 LowValue = 94 LowValue = 92 LowValue = 88 LowValue = 87

Now (below), we can modify the program a little bit to allow the program to self terminate…

Notice that we haven’t changed it that much. We just added an assignment to a variable called loopit, changed the while statement to “while loopit:” instead of “while True:”, and then check to see if the lowvalue is less than 90 as a “trigger”. If it is, then we set the loopit value to False to make the loop fail.

I have to hold and gently squeeze the male jumper to get the value to go low enough to trigger the exit.

Project Program #2 - Network Programming

This is a simple example of how to connect to your local router. All it really does is to connect to the router with your password, obtain an IP address, and print it. We’ll cover a more complete example in a future article.

First, we need to import the network library and set up the essid and password. Be sure to change these to your real values.

import network

essid = 'YourNetworkRouter'

passwd = 'YourNetworkPassword'

Now (top right) we’ll create the function to do the actual communications with the router (essid). We set the network object (wlan) to station mode, which is like a “normal” computer on the network. You could also set it up as an access point. Next, we set the network object to active and then try to connect. Once we have connected, we print out the network information (ip address, etc). The final line calls the connection function.

As I said, this is a very simple example that really does nothing other than connect to the local router and return an IP address. Using sockets will be covered in a future article.

Project Program #3 - Web Server and DHT-11/22

Our final project for this month involves using the ESP32/Sparkfun 32 Thing Plus as a web server as well as DHT-11/22 Temperature/Humidity sensor reader, and a touchpad and button display program. Below is what it looks like when it’s running…

The entire project is described in detail at: https://itywik.org/2018/10/30/eight-micropython-python-experiments-for-the-esp32/.

This project uses the PicoWeb library which can be found at https://github.com/pfalcon/picoweb.

Of course you need to add Picoweb and the dependencies. You can try to follow the instructions in the link for the project itself, or you can use upip within the Thonny REPL. You will need to install micropython-pkg_resources, ulogging, uasyncio and picoweb. You will need to do an import upip at the REPL command line to be able to use it.

import upip

upip.install('micropython-pkg_resources')

upip.install('micropython-ulogging')

upip.install('micropython-uasyncio')

upip.install('picoweb')

You can find out more about upip at: https://docs.micropython.org/en/latest/reference/packages.html

There is the code, diagrams and step-by-step projects leading up to the full webserver project. The code for the webserver project is pretty long and includes both the python file and the index.html file that displays the web information. I won’t duplicate it here, but I will point out a few changes that I had to make to get the project to work the way that I wanted it to.

I saved the main code to a file named ESP32-DHT-Touch-Picoweb.py

Lines 18, 19 and 20 define the pins for the LEDs. I decided to use only the onboard LED on the ESP32 Dev board which is on pin 2. I changed line 18 to: r_led = machine.Pin(2, machine.Pin.OUT)

And I left lines 19 and 20 the same.

Line 33 (in my code) needs to be modified to support your network. sta_if.connect('YourNetworkESSID', 'YourPasswordHere')

Line 58 defines the callback for the touchpad timer interrupt handler. I modified lines 62, 70 and 78. They each are an if statement testing the touchpad.read() value to be less than 100. Since I’m just using just wires, not true copper pads, the earlier tests showed that I can achieve a value of under 100, but it takes a number of seconds for it to get that low. So, I changed the test value to less than 200 for all three values. It still takes around 2 ½ seconds to 3 seconds and a fairly tight squeeze to get it to trigger, but it does. So line 62 was changed to: if t7 < 200:

Line 70 was changed to: If t8 < 200:

And line 78 was changed to: If t9 < 200:

Lines 87, 89 and 90 (my code) reference the DHT11 sensor. Since I’m using a DHT22, I had to change the three instances from DHT11 to DHT22.

d = dht.DHT22(machine.Pin(23))

dht22_timer = machine.Timer(0)

dht22_timer.init(period=1000, mode=machine.Timer.PERIODIC, callback=timerIntHandler_temperature)

Also, in the index.html file I made a change to make the temperature and humidity display text smaller. Around line 45, you will find the code:

     .table-value {
          font-size: 160px;
          color: #999;
}

I found that the font size was way too big for me. I changed it to:

      .table-value {
          font-size: 60px;
          color: #999;
      }

Which worked much better, at least for me. You can make this change if you wish.

My code can be found on my github repository at https://github.com/gregwa1953/FCM170_MicroThisAndThat The webserver project code on the repository has my changes in it.

Next time, we’ll take a look at NeoPixel devices and how to program them using the ESP-32 and the RPI Pico.

Until then, as always; stay safe, healthy, positive and creative!

issue170/micro-ci_micro-la.1624694904.txt.gz · Dernière modification : 2021/06/26 10:08 de auntiee