0

REally need som help with wifi and RGB matrices
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

REally need som help with wifi and RGB matrices

by hrikken on Wed Feb 17, 2021 8:18 am

The Adafruit libraries drive me nuts.
I bought a Matrixportal M4 with a 64 x 32 RBG Led Matrix panel

I have a simple task which I can't get done:
- Connect o a WiFi network
- Read a time server
- Read BME280 values
- Display date, time, temperature and humidy on the 64 x 32 RGB Matrix panel

I've played around with the Matrixportal Library and others so far the tasks seperately is going fine.
So I can read a sensor, display some texts and connet to a wifi network.
But if I combine these task into 1 Python script it simply doesn't work.Errors which I can't resolve.
I can't get my head around all those libraries and how, where and when to use them.

Has anybody a Python script for me or a link to instructions and example code with code that carries out those tasks?

I've read tons of examples but none of them are sufficient.


What libraries
hrikken
 
Posts: 14
Joined: Wed Apr 14, 2010 5:12 am

Re: REally need som help with wifi and RGB matrices

by tannewt on Wed Feb 17, 2021 12:29 pm

Please post your complete code and the error you are getting. That way we can help you get your code working. Having separate pieces working is a really good start.

tannewt
 
Posts: 2189
Joined: Thu Oct 06, 2016 8:48 pm

Re: REally need som help with wifi and RGB matrices

by jerryn on Wed Feb 17, 2021 4:12 pm

In case it is helpful, here is an example that sets the locatime then displays the BME280 data every 2 seconds. I am very new to creating displays on the MatrixPortal myself so this may not be a "good" example, but it works ... Your mileage may vary.... ;-)

Here is the code.py file
Code: Select all | TOGGLE FULL SIZE
import time
import random
import board
import terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal
import adafruit_bme280

# --- Display setup ---
matrixportal = MatrixPortal(status_neopixel=board.NEOPIXEL, debug=True)
# sensor setup
bme280 = adafruit_bme280.Adafruit_BME280_I2C(board.I2C())
# change this to match the location's pressure (hPa) at sea level
bme280.sea_level_pressure = 1013.25


# Create labels for the lines of text
matrixportal.add_text(
    text_font=terminalio.FONT,
    text_position=(0, 4),
    scrolling=False,
)
matrixportal.add_text(
    text_font=terminalio.FONT,
    text_position=(0, 13),
    scrolling=False,
)

matrixportal.add_text(
    text_font=terminalio.FONT,
    text_position=(0, 22),
    scrolling=False,
)




matrixportal.get_local_time()

while True:
    now = time.localtime()
   
    text = "{0:04d}/{1:02d}/{2:02d}".format(now.tm_year,now.tm_mon,now.tm_mday)
    # Set the quote text
    matrixportal.set_text(text,0)
    # Set the text color
    matrixportal.set_text_color(0xff00ff,0)

    text = "{0:02d}:{1:02d}:{2:02d}".format(now.tm_hour,now.tm_min,now.tm_sec)
    # Set the quote text
    matrixportal.set_text(text,1)
    matrixportal.set_text_color(0x0000ff,1)

    text = "{0:.1f}C {1:.0f}%".format(bme280.temperature,bme280.relative_humidity)
    # Set the quote text
    matrixportal.set_text(text,2)
    matrixportal.set_text_color(0x00ffff,2)

    time.sleep(2)





these are the libraries I have on the board - some may not be necessary, but won't hurt
Code: Select all | TOGGLE FULL SIZE
adafruit_bitmap_font
adafruit_display_notification
adafruit_display_shapes
adafruit_esp32spi 
adafruit_io
adafruit_matrixportal
adafruit_portalbase   
adafruit_display_text         
adafruit_register
adadfruit_bme280.mpy
adafruit_fakerequests.mpy
adafruit_imageload   
adafruit_lis3dh.mpy 
adafruit_ntp.mpy
adafruit_requests.mpy
neopixel.mpy

jerryn
 
Posts: 1389
Joined: Sat Sep 14, 2013 9:05 am

Re: REally need som help with wifi and RGB matrices

by hrikken on Fri Feb 19, 2021 10:27 am

Hi Jerryn,

Thank you for the examples. This will help me further I think.
I'll reply again if I run into trouble.

I really appreciate your effort !
hrikken
 
Posts: 14
Joined: Wed Apr 14, 2010 5:12 am

Re: REally need som help with wifi and RGB matrices

by hrikken on Sat Feb 20, 2021 11:37 am

Hi Jerry,

My 5 cents:

I fiddled around with your example and added the features:
- Query an InfluxDB for CO2 values
- Made it all easier to configure

It resulted in the following code:
Code: Select all | TOGGLE FULL SIZE
import time
import rtc
from digitalio import DigitalInOut, Direction, Pull
import board
import terminalio
from adafruit_matrixportal.matrixportal import MatrixPortal
from adafruit_matrixportal.network import Network
from adafruit_matrixportal.matrix import Matrix
import adafruit_requests as requests
import adafruit_bme280
import adafruit_bh1750
import adafruit_ds1307

# Function to initialise the texts and lables
def setTextLabels():

    global dayLabel
    global timeLabel
    global tehuLabel
    global monthLabel
   
    # Month
    dayLabel = matrixportal.add_text(
        text_font=dayLabelParams[4],
        text_position=(dayLabelParams[0], dayLabelParams[1]),
        scrolling=dayLabelParams[2],
        text_scale=dayLabelParams[3],
    )

    # Day
    monthLabel = matrixportal.add_text(
        text_font=monthLabelParams[4],
        text_position=(monthLabelParams[0], monthLabelParams[1]),
        scrolling=monthLabelParams[2],
        text_scale=monthLabelParams[3],
    )

    # Time
    timeLabel = matrixportal.add_text(
        text_font=timeLabelParams[4], #terminalio.FONT,
        text_position=(timeLabelParams[0], timeLabelParams[1]),
        scrolling=timeLabelParams[2],
        text_scale=timeLabelParams[3],
    )

    # Temp. & Humid.
    tehuLabel = matrixportal.add_text(
        text_font=tehuLabelParams[4],
        text_position=(tehuLabelParams[0], tehuLabelParams[1]),
        scrolling=tehuLabelParams[2],
        text_scale=tehuLabelParams[3],
    )

def setLedColors():
    global ledColor
    global ledColorDim

    # Color definitions
    ledColor = {"red" : 0xff0000, "green" : 0x00ff00, "blue" : 0x0000ff,
                "white" :0xffffff, "black": 0x000000, "cyan" : 0x00ffff,
                "yellow" : 0xffff00, "orange" : 0xffa500, "gray" : 0x808080}

    ledColorDim = {"red" : 0x880000, "green" : 0x008800, "blue" : 0x000088,
                "white" :0x888888, "black": 0x000000, "cyan" : 0x008888,
                "yellow" : 0x888800, "orange" : 0x885200, "gray" : 0x808080}


# Human readable month names
iMonths = ["Ja", "Fe", "Ma", "Ap", "Me", "Jn", "Jl", "Au", "Se", "Ok", "No", "De"]

# --- Display setup ---
matrixportal = MatrixPortal(status_neopixel=board.NEOPIXEL,debug=False)
network = matrixportal.network

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise
print("    M4 RTC Clock init")
print("Time will be set for {}".format(secrets["timezone"]))

# --- Network connect ---
network.connect() # Connect to WiFi with valid credentials
 
# sensor setup
bme280 = adafruit_bme280.Adafruit_BME280_I2C(board.I2C())
bh1750 = adafruit_bh1750.BH1750(board.I2C())
rtcTime = adafruit_ds1307.DS1307(board.I2C())
# change this to match the location's pressure (hPa) at sea level
bme280.sea_level_pressure = 1013.25

# Setup switches
# The code runs until the down-switch is pressed
upswitch = DigitalInOut(board.BUTTON_UP)
downswitch = DigitalInOut(board.BUTTON_DOWN)
upswitch.direction = Direction.INPUT
upswitch.pull = Pull.UP
downswitch.direction = Direction.INPUT
downswitch.pull = Pull.UP

# Set the RTC as the source of localtime()
rtc.set_time_source(rtcTime)

# Create labels for the lines of text
# Set parameters x,y,scale,scroll,font for labels
dayLabelParams = [52,4,0,False,terminalio.FONT]
monthLabelParams = [52,13,0,False,terminalio.FONT]
timeLabelParams = [0,9,0,False,"/fonts/ter-u20b.bdf"]
tehuLabelParams = [0,22,1,False,terminalio.FONT]
dayLabel = None
timeLabel = None
tehuLabel = None
monthLabel = None
ledColor = None
ledColorDim = None


# Initialise labels
setLedColors()
setTextLabels()

# Semaphore init
last_check = None
co2_check = None
co2Value = 0
influxQuery = 'http://192.168.88.38:8086/query?u=testuser&p=Geheim01&db=sensordata&q=SELECT%20last(%22CO2%22)%20FROM%20%22environment_MQTT_telegraf%22%20WHERE%20(%22deviceid%22%20=%20%272310179%27)'


# Init COLON display
colon = True
colonChar = ':'

# Until the down-switch is pressed, loop...
while downswitch.value == True:
    now = time.localtime()

    # Set month
    text = iMonths[now.tm_mon-1]
    matrixportal.set_text(text,monthLabel)
    matrixportal.set_text_color(ledColor["green"],monthLabel)

    # Set day
    text = "{0:02d}".format(now.tm_mday)
    matrixportal.set_text(text,dayLabel)
    matrixportal.set_text_color(ledColor["blue"],dayLabel)

    # Set time and colon
    if last_check == None or time.monotonic() > last_check + 1:
        text = "{0:02d}:{1:02d}".format(now.tm_hour,now.tm_min)
#        if colon:
#            text = "{0:02d}:{1:02d}".format(now.tm_hour,now.tm_min)
#        else:
#            text = "{0:02d} {1:02d}".format(now.tm_hour,now.tm_min)
        colon = not colon
        last_check = time.monotonic()

    # Get CO2 value
    if co2_check == None or time.monotonic() > co2_check + 30:
        co2_check = time.monotonic()
        # Fire the query at the InfluxDB API
        r=requests.get(influxQuery)
        # Get the JSON
        data = r.json()
        co2Value = data['results'][0]['series'][0]['values'][0][1]
        co2_check = time.monotonic()

    # Set temperature and humidity
    matrixportal.set_text(text,timeLabel)
    matrixportal.set_text_color(ledColor["yellow"],timeLabel)

    # Set temperature and humidity
    text = "Tmp.:{0:.1f}C   Hum.:{1:.0f}%   CO2:{2:.0f}ppm".format(bme280.temperature,bme280.relative_humidity,co2Value)
    matrixportal.set_text(text,tehuLabel)
    matrixportal.set_text_color(ledColor["cyan"],tehuLabel)
    matrixportal.scroll_text(0.02)
hrikken
 
Posts: 14
Joined: Wed Apr 14, 2010 5:12 am

Re: REally need som help with wifi and RGB matrices

by jerryn on Sat Feb 20, 2021 12:06 pm

Thanks for the update. I’m glad to hear it is working for you!

jerryn
 
Posts: 1389
Joined: Sat Sep 14, 2013 9:05 am

Please be positive and constructive with your questions and comments.