Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
vany5921
Published © MIT

M5Stack with Blynk

Using UIFlow and Blynk to complete the interaction experiment

BeginnerFull instructions provided1 hour3,791
M5Stack with Blynk

Things used in this project

Hardware components

ESP32 Basic Core IoT Development Kit
M5Stack ESP32 Basic Core IoT Development Kit
×1
LED (generic)
LED (generic)
×1

Software apps and online services

Blynk
Blynk

Story

Read more

Schematics

Source Code

Source Code2

Source Code3

Code

M5Stack Blynk Demo1

Python
We implement a simple blynk interaction: two buttons are designed on the blynk app. When clicking, two virtual pins V2 / v6 are activated. After clicking, the results are transmitted to the m5stack end. After parsing, two LEDs are driven to flash.
from m5stack import *
from m5ui import *
from uiflow import *
import wifiCfg
 
import BlynkLib
import network
import machine
import time
from machine import Pin
led1=Pin(2,Pin.OUT)        #create LED object from pin16,Set Pin16 to output
led2=Pin(5,Pin.OUT)        #create LED object from pin17,Set Pin17 to output
 
WIFI_SSID = 'T*************'
 
WIFI_PASS = 'x*************m'
 
BLYNK_AUTH = 'z*************b'
 
setScreenColor(0x222222)
 
title0 = M5Title(title="Blynk Demo", x=3 , fgcolor=0xFFFFFF, bgcolor=0x0000FF)
rectangle0 = M5Rect(34, 192, 60, 30, 0xa9a504, 0xFFFFFF)
rectangle1 = M5Rect(219, 189, 60, 30, 0x187b06, 0xFFFFFF)
rectangle2 = M5Rect(129, 191, 60, 30, 0xf40808, 0xFFFFFF)
label0 = M5TextBox(49, 200, "Text", lcd.FONT_Default,0xFFFFFF, rotate=0)
label1 = M5TextBox(140, 199, " ", lcd.FONT_Default,0xFFFFFF, rotate=0)
label2 = M5TextBox(231, 198, " ", lcd.FONT_Default,0xFFFFFF, rotate=0)
label3 = M5TextBox(85, 88, "wait...", lcd.FONT_Default,0xFFFFFF, rotate=0)
 
label3.setText('Hello M5')
 
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASS)
 
while not wifi.isconnected():
    pass
 
#print('IP:', wifi.ifconfig()[0])
 
label3.setText('Connecting to Blynk...')
blynk = BlynkLib.Blynk(BLYNK_AUTH)
tmr_start_time = time.time()
 
@blynk.on("connected")
def blynk_connected(ping):
    #print('Blynk ready. Ping:', ping, 'ms')
    label3.setText('Blynk ready')
def runLoop():
    while True:
        blynk.run()
        machine.idle()
 
@blynk.on("V6")
def v3_write_handler(value):
    
    label3.setText(str(int(value[0])))
    led1.value(int(value[0]))
    label2.setText('   ')
    if int(value[0])==1:
      label2.setText('click')
    else:
      label2.setText('   ')
 
@blynk.on("V2")
def v2_write_handler(value):
    
    label3.setText(str(int(value[0])))
    led2.value(int(value[0]))
    label1.setText('   ')
    if int(value[0])==1:
      label1.setText('click')
    else:
      label1.setText('   ')
 
# Run blynk in the main thread:
while True:
    #runLoop()
    wait(0)  #为了在vscode中连续run
    blynk.run()

M5Stack Blynk Demo2

Python
A uiflow blynk plug-in is used for development. After the UI is developed in uiflow, it can also be redeveloped and improved in vscode.
In this paper, we design a simple interaction function of blynk: associate the virtual rocker on blynk app with the m5stack end through v-pin to drive a white ball on m5stack
from m5stack import *
from m5ui import *
from uiflow import *
import machine
import wifiCfg
 
setScreenColor(0x222222)
 
title0 = M5Title(title="conntect.....", x=3 , fgcolor=0xfdfcfc, bgcolor=0x0000FF)
label0 = M5TextBox(61, 88, "Pin2=", lcd.FONT_Default,0xFFFFFF, rotate=0)
label1 = M5TextBox(108, 87, "x", lcd.FONT_Default,0xFFFFFF, rotate=0)
 
x = None
 
#led function
def led(x):
  label1.setText(str(str(x)))
  pin2.value(int(x))
 
pin2 = machine.Pin(2, mode=machine.Pin.OUT, pull=machine.Pin.PULL_UP)
wifiCfg.doConnect('*************', '*************')
wifiCfg.screenShow()
wifiCfg.autoConnect(lcdShow = True)
import BlynkLib
BLYNK_AUTH = 'z*************b'
blynk = BlynkLib.Blynk(BLYNK_AUTH)
 
@timerSch.event('__blynk_timer')
def __blink_timer():
  wait(0)
  blynk.run()
timerSch.run('__blynk_timer', 2, 0x00)
 
@blynk.on('connected')
def __blynk_on_connected():
  global x
  title0.setBgColor(0x33cc00)
  title0.setTitle('Connected')
  pass
@blynk.on('disconnected')
def __blynk_on_disconnected():
  global x
  title0.setBgColor(0xff0000)
  title0.setTitle('DisConnected')
 
@blynk.VIRTUAL_WRITE(6)
def v6_write_handler(args):
    globals()["led"](*args)

source

Python
No preview (download only).

Credits

CangHai

Posted by vany5921

Comments