Saturday 26 June 2021

Temperature measuring Web Server

So, having had the Pimoroni Breakout Garden for a while and having used it as a temperature measurement device with a BME680 and display, I needed to measure the temperature and compare it with the temperature measured  by the analogue sensor of an Adafruit PyPortal and the CPU temperature.

The BME680 results are affected by the heat generated by the CPU, and a correction formula based on the CPU temperature was used to compensate for the CPU temperature.

An alternative is to separate the temperature sensor from the CPU.

This was achieved using a Breakout Garden to Stemma QT/QWIIC adapter and STEMMA QT / Qwiic to Breakout Garden Adapter  joined by a JST to JST cable. A BMP280 breakout was plugged in at one end, and the other plugged into the Breakout Garden. This should provide sufficient thermal isolation from the hot CPU.


Software installation

The Python library for the BMP280 temperature, pressure and altitude sensor is available via PIP. As with all installations, check if pip is for the now obsolete Python 2.x or Python 3.x. If the former, then use pip3 (that is the case on my Raspberry Pi Zero development systerm).

sudo pip3 install bmp280

The easiest way to make the data available to an Internet connectable device is by making the temperature measuring Raspberry Pi a web server.

The Flask library is similarly installed with pip. Note Flask starts with a capital letter
sudo pip3 install -U Flask

Web Server

This is a very simple web server, it has two  pages - /temperature and /json.

from flask import Flask, render_template
import time
import datetime
import json
from bmp280 import BMP280

try:
    from smbus2 import SMBus
except ImportError:
    from smbus import SMBus
app = Flask(__name__)

@app.route('/temperature')
def temperature():
    now = datetime.datetime.now()
    timeString = now.strftime("%Y-%m-%d %H:%M")
    temperature = bmp280.get_temperature()
    temp = '{:05.2f}*C'.format(temperature)
    templateData = {
      'temperature' : temp,
      'time': timeString
      }
    return render_template('temperature.html', **templateData)

@app.route('/json')
def tempjson():
    now = datetime.datetime.now()
    timeString = now.strftime("%Y-%m-%d %H:%M")
    temperature = bmp280.get_temperature()
    temp = '{:05.2f}'.format(temperature)
    templateData = {
      'temperature' : temp,
      'time': timeString
      }

    return json.dumps(templateData)

if __name__ == '__main__':
    bus = SMBus(1)
    bmp280 = BMP280(i2c_dev=bus)
    app.run(debug=True, host='0.0.0.0')

References


Saturday 12 June 2021

Raspberry Pi Pico and MicroPython

Installing MicroPython



To install or upgrade MicroPython on a Pico there are three steps.

1. Download the UF2 file. There is a vanilla version available from micropython.org, or if you are using one of Pimoroni's boards then you can select one of the UF2 files with all their libraries preinstalled here.

2. Put the Pico into BootLoader mode by unplugging it, hold the BOOTSEL button down and then plugging it back in. The Pico will then appear as a USB device on the host machine.

3. Copy the UF2 file into the new USB device

Once the file has been copied over, the USB device will disappear.

Testing the MicroPython

As the Pico has no built-in display, it can only communicate via the USB. The MicroPython firmware has a built-in serial port available over the USB.

To access it, the popular Minicom package is used.

Install it on your Pi using:

sudo apt install minicom

Next, identify the serial port.

The Pico should appear as a port of the form /dev/ttyACM.

Use 

ls /dev/tty*

If it is unclear which port is the Pico, unplug it and try again, identifying which device disappears.

Connect to the MicroPython running on the Pico using:

minicom -o -D /dev/ttyACM0

This should display the Minicom opening text:

Welcome to minicom 2.7.1
OPTIONS: I18n 
Compiled on Aug 13 2017, 15:25:34.
Port /dev/ttyACM0, 07:39:31
Press CTRL-A Z for help on special keys

You now need to soft reboot, press Control and D. This will display information about the installed MicroPython and the prompt >>>.

MPY: soft reboot

MicroPython v1.15 on 2021-04-18; Raspberry Pi Pico with RP2040

Type "help()" for more information.

>>>

To test the MicoPython, try the classic:

>>> print("hello")

hello

>>>

This shows that MicroPython is running successfully on your Pico.

Control A then Z brings up help.

Control X closes Minicom.

References

https://micropython.org/download/rp2-pico/rp2-pico-latest.uf2

https://github.com/pimoroni/pimoroni-pico/releases

https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-python-sdk.pdf

https://en.wikipedia.org/wiki/Minicom