节日彩灯 物联网控
本帖最后由 云天 于 2023-1-14 20:07 编辑【项目背景】
临近春节,家里的灯也该有它应有的样子,准备使用Beetle-ESP32控制器,有4个数字口,我只用了D2引脚,集成WIFI无线协议,可以用物联网控制。电源使用旧手机充电器改造,遥控器使用“鳄霸王”。
【改造充电器】
拆解手机充电器,焊上电源线,给ESP32供电。
【测试ESP32】
1、连接物联网,Easy Iot
2、物联网点灯程序
主控板选择FireBeetle ESP32
网络扩展选择WIFI、MQTT
图形化编程
【灯带连接】
灯带数据引脚接ESP32板的D2引脚,灯带与ESP32单独供电,且共地。
【遥控灯测试】
1、鳄霸王程序(掌控板)
同micropython程序
# MindPlus
# mpython
from umqtt.simple import MQTTClient
from mpython import *
import network
import usocket
import music
import time
def hostgetIP(host):
return usocket.getaddrinfo(host,8080,0,0,usocket.SOL_SOCKET)
# 自定义函数
def BoFangXianShi(NaRong):
global my_variable, BiaoShi
music.play(music.BA_DING, wait=False, loop=False)
oled.fill(0)
my_wifi = wifi()
p0=MPythonPin(0,PinMode.ANALOG)
p1=MPythonPin(1,PinMode.ANALOG)
my_wifi.connectWiFi("sxs","smj080823")
while not (my_wifi.sta.isconnected()):
pass
mqtt = MQTTClient("", hostgetIP("iot.dfrobot.com.cn"), 1883, "X8jykxFnR", "u8jskbFngz", keepalive=30)
try:
mqtt.connect()
print('MQTT Connected Successful')
except:
print('MQTT Connection Failed')
BiaoShi = 0
oled.DispChar("初始化成功", 42, 22, 1)
oled.show()
my_clock = Clock(oled, 64, 32, 30)
time.sleep(5)
while True:
if (p0.read_analog() > 4000):
if (not (BiaoShi == 1)):
BiaoShi = 1
mqtt.publish(str("jBarN574g"), str("R").encode('utf-8'))
BoFangXianShi("红灯")
elif (p1.read_analog() > 4000):
if (not (BiaoShi == 2)):
BiaoShi = 2
mqtt.publish(str("jBarN574g"), str("G").encode('utf-8'))
BoFangXianShi("绿灯")
elif (p0.read_analog() < 100):
if (not (BiaoShi == 3)):
BiaoShi = 3
mqtt.publish(str("jBarN574g"), str("B").encode('utf-8'))
BoFangXianShi("蓝灯")
elif (p1.read_analog() < 100):
if (not (BiaoShi == 4)):
BiaoShi = 4
mqtt.publish(str("jBarN574g"), str("M").encode('utf-8'))
BoFangXianShi("彩灯")
elif button_a.value() == 0:
if (not (BiaoShi == 5)):
BiaoShi = 5
mqtt.publish(str("jBarN574g"), str("O").encode('utf-8'))
BoFangXianShi("开灯")
elif button_b.value() == 0:
if (not (BiaoShi == 6)):
BiaoShi = 6
mqtt.publish(str("jBarN574g"), str("S").encode('utf-8'))
BoFangXianShi("关灯")
time.sleep(5)
elif ((BiaoShi == 6) or (BiaoShi == 0)):
oled.fill(0)
my_clock.settime()
my_clock.drawClock()
oled.show()
2、ESP32程序
同micropython程序
/*!
* MindPlus
* firebeetleesp32
*
*/
#include <DFRobot_Iot.h>
#include <DFRobot_NeoPixel.h>
// 动态变量
volatile float mind_n_BiaoShi, mind_n_YunXingShiJian;
// 函数声明
void obloqMqttEventT0(String& message);
// 静态常量
const String topics = {"jBarN574g","","","",""};
const MsgHandleCb msgHandles = {obloqMqttEventT0,NULL,NULL,NULL,NULL};
// 创建对象
DFRobot_Iot myIot;
DFRobot_NeoPixel neoPixel_D2;
// 主程序开始
void setup() {
myIot.setMqttCallback(msgHandles);
Serial.begin(9600);
myIot.wifiConnect("sxs", "smj080823");
while (!myIot.wifiStatus()) {yield();}
Serial.println(myIot.getWiFiLocalIP());
myIot.init("iot.dfrobot.com.cn","X8jykxFnR","","u8jskbFngz",topics,1883);
myIot.connect();
while (!myIot.connected()) {yield();}
digitalWrite(D9, HIGH);
delay(5000);
digitalWrite(D9, LOW);
neoPixel_D2.begin(D2, 120);
neoPixel_D2.clear();
mind_n_BiaoShi = 0;
mind_n_YunXingShiJian = millis();
neoPixel_D2.showRainbow(0, 119, 1, 360);
}
void loop() {
mind_n_YunXingShiJian = millis();
if ((mind_n_BiaoShi==7)) {
neoPixel_D2.rotate(1);
}
else if ((mind_n_BiaoShi==1)) {
neoPixel_D2.setRangeColor(0, 119, 0xFFFFFF);
mind_n_BiaoShi = 0;
}
else if ((mind_n_BiaoShi==2)) {
mind_n_BiaoShi = 0;
neoPixel_D2.clear();
}
else if ((mind_n_BiaoShi==3)) {
mind_n_BiaoShi = 0;
neoPixel_D2.setRangeColor(0, 119, 0xFF0000);
}
else if ((mind_n_BiaoShi==4)) {
mind_n_BiaoShi = 0;
neoPixel_D2.setRangeColor(0, 119, 0x00FF00);
}
else if ((mind_n_BiaoShi==5)) {
mind_n_BiaoShi = 0;
neoPixel_D2.setRangeColor(0, 119, 0x0000FF);
}
else if ((mind_n_BiaoShi==6)) {
mind_n_BiaoShi = 7;
neoPixel_D2.showRainbow(0, 119, 1, 360);
}
}
// 事件回调函数
void obloqMqttEventT0(String& message) {
if ((message=="O")) {
mind_n_BiaoShi = 1;
}
else if ((message=="S")) {
mind_n_BiaoShi = 2;
}
else if ((message=="R")) {
mind_n_BiaoShi = 3;
}
else if ((message=="G")) {
mind_n_BiaoShi = 4;
}
else if ((message=="B")) {
mind_n_BiaoShi = 5;
}
else if ((message=="M")) {
mind_n_BiaoShi = 6;
}
}
【安装吸顶灯】
【演示视频】
https://www.bilibili.com/video/BV1oT41127ca/?share_source=copy_web&vd_source=98855d5b99ff76982639c5ca6ff6f528
厉害厉害 放上去不打算拆了吗? 还是吸顶灯的......感觉这种彩色灯带容易坏............. 赞赞云天老师 三春牛-创客 发表于 2023-1-14 23:01
放上去不打算拆了吗?
这个灯平时基本不用,弄成彩灯,烘托节日气氛。 云天 发表于 2023-1-15 17:11
这个灯平时基本不用,弄成彩灯,烘托节日气氛。
哦,原来是这样 厉害厉害 赞赞!! 灯挺漂亮的。 新年快乐!
页:
[1]