Diana Bernabei
Published

Flip&Click LEDMatrix8x8 Controlled Using Zerynth and Ubidots

How to control with Ubidots dashboard a Flip & Click LED Matrix 8x8.

BeginnerFull instructions provided533

Things used in this project

Hardware components

MikroE Flip & Click
×1
MikroE WiFi 4 click
×1
MikroE 8x8 R click
×1

Software apps and online services

Zerynth Studio
Zerynth Studio
Ubidots
Ubidots

Story

Read more

Schematics

board

Pay attention with the slot:
- Wifi module Click on slot A aka pin D16
- 8x8 Click on slot B aka pin D25

Code

main.py

Python
# Flip & Click LED matrix 8x8 controlled by Ubidots

# NOTE
# wifi module Click on slot A of a Flip n Click device
# 8x8 Click on slot B of a Flip n Click device

# Flip & Click LED matrix 8x8 controlled by Ubidots

import streams                          # import class definitions for streams 
import json                             # import functions to serialize and deserialize objects to and from JSON format
from wireless import wifi               # import a generic wifi interface
from stm.spwf01sa import spwf01sa as wifi_driver # import a module that supports one tcp socket at a time (multiple sockets in future updates) in client and server mode
from ubidots.iot import iot             # import ubidots iot module
import helpers                          # import helpers functions to easily device configuration
from maxim.max7219 import max7219       # import MAX7219 library to handle 8x8 led matrix
import fonts                            # import fonts file where matrix numbers are defined

streams.serial()

display = max7219.MAX7219(SPI0, D25)
display.shutdown(False)
new_resource('device.conf.json') 

def callback(value):
	print("Send number: ", value['value'])
	set_led(int(value['value'])) 

def set_led(num):
    for row in range(8):
        for col in range(8):
            if fonts.numbers[num][row][col]:
                display.set_led(row, col, 1)  
            else:
                display.set_led(row, col, 0)

try:
    wifi_driver.init(SERIAL1,D16, baud=9600) 
except Exception as e:
    print(e)
    
for i in range(0,5):
    try:
        wifi.link("SSID",wifi.WIFI_WPA2,"password")
        print("Connect to wifi")
        break
    except Exception as e:
        print("I can't connect",e)
else:
    print("Impossibile to connect!")
    while True:
        sleep(1000)

print("Start connection to Ubidots")        
device_conf = helpers.load_device_conf()
publish_period = 3000
print("I'm connecting with mqtt broker...")

try:
    print("I'm trying to connect device on Ubidots...")
    device = iot.Device(device_conf['device_label'], device_conf['user_type'], device_conf['api_token'])
    device.mqtt.connect()
    print("Connect with mqtt broker")
except Exception as e:
   print("Ops, something went wrong :(", e)
   while True:
       sleep(1000)
       
device.on_variable_update(device_conf['device_label'],'output-matrix', callback, json=True)
device.mqtt.loop()

while True:
    print("print...")
    sleep(publish_period)  
    
    

device.conf.json

Python
{
    "user_type": "business",
    "device_label": "DEVICE-NAME-ON-UBIDOTS",
    "api_token": "DEFAULT-TOKEN-ON-UBIDOTS"
}

fonts.py

Python
numbers = [
    [
    [0,0,1,1,1,1,0,0],#0
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,0,1,1,1,1,0,0],
    ],
    [
    [0,0,0,1,1,0,0,0],#1
    [0,0,1,1,1,0,0,0],
    [0,1,1,1,1,0,0,0],
    [0,0,0,1,1,0,0,0],
    [0,0,0,1,1,0,0,0],
    [0,0,0,1,1,0,0,0],
    [0,0,0,1,1,0,0,0],
    [0,1,1,1,1,1,1,0]
    ],    
    [
    [0,0,1,1,1,1,0,0],#2
    [0,1,1,0,0,1,1,0],
    [0,0,0,0,0,1,1,0],
    [0,0,0,0,1,1,0,0],
    [0,0,0,1,1,0,0,0],
    [0,0,1,1,0,0,0,0],
    [0,1,1,0,0,0,0,0],
    [0,1,1,1,1,1,1,0]
    ],
    [
    [0,0,1,1,1,1,0,0],#3
    [0,1,1,0,0,1,1,0],
    [0,0,0,0,0,1,1,0],
    [0,0,0,1,1,1,0,0],
    [0,0,0,0,0,1,1,0],
    [0,0,0,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,0,1,1,1,1,0,0]
    ],
    [
    [0,0,0,0,0,1,1,0],#4
    [0,0,0,0,1,1,1,0],
    [0,0,0,1,1,1,1,0],
    [0,0,1,1,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,1,1,1,1,1,1,0],
    [0,0,0,0,0,1,1,0],
    [0,0,0,0,0,1,1,0]
    ],
    [
    [0,1,1,1,1,1,1,0],#5
    [0,1,1,0,0,0,0,0],
    [0,1,1,0,0,0,0,0],
    [0,1,1,1,1,1,0,0],
    [0,0,0,0,0,1,1,0],
    [0,0,0,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,0,1,1,1,1,0,0]
    ],    
    [
    [0,0,1,1,1,1,0,0],#6
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,0,0,0],
    [0,1,1,1,1,1,0,0],
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,0,1,1,1,1,0,0]
    ],
    [
    [0,1,1,1,1,1,1,0],#7
    [0,1,1,0,0,1,1,0],
    [0,0,0,0,1,1,0,0],
    [0,0,0,1,1,0,0,0],
    [0,0,0,1,1,0,0,0],
    [0,0,0,1,1,0,0,0],
    [0,0,0,1,1,0,0,0],
    [0,0,0,1,1,0,0,0]
    ],
    [
    [0,0,1,1,1,1,0,0],#8
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,0,1,1,1,1,0,0],
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,0,1,1,1,1,0,0],
    ],
    [
    [0,0,1,1,1,1,0,0],#9
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,0,1,1,1,1,1,0],
    [0,0,0,0,0,1,1,0],
    [0,1,1,0,0,1,1,0],
    [0,0,1,1,1,1,0,0],
    ]    
    ]

helpers.py

Python
# -*- coding: utf-8 -*-
# @Author: Lorenzo
# @Date:   2017-10-03 10:56:02
# @Last Modified by:   lorenzo
# @Last Modified time: 2017-10-26 10:44:43

import json

def load_device_conf():
    confstream = open('resource://device.conf.json')
    conf = ''
    while True:
        line = confstream.readline()
        if not line:
            break
        conf += line
    return json.loads(conf)

Credits

Diana Bernabei

Diana Bernabei

2 projects • 1 follower

Comments