该项目主要基于Arduino D1 mini(wemos)模块和MQTT协议和移动Onenet物联网云平台实现远程控制
1、选插座一定要选择这种花里胡哨,内部空间大的,这样便于后期改造
2、线路改造,可以看到进来两根线一根线经过开关,结构简单粗暴,便于改造
3、寻找合适的安装位置,我这里放了两个3.3V的一路继电器,和一个Arduino D1 MINI这种某宝一大堆,(本来准备把那个四路遥控器也安装上去,但是无奈这个信号输出是5V的所以不能用,用三极管改造一上午居然还给我搞坏了,气死直接丢垃圾桶)
图中画圈区域就是改造区域,选用这个接线板的好处就是板载自带5V电源,所以就不需要自己再接220VAC转5V模块了,直接上电测出5V电源连接到Arduino上的5V个GND即可,完成本项目所需的电源供应。因为没用到四路遥控模块所以这里连线非常简单,就是继电器信号线连接到D1和D2上即可。
4、打上热熔胶,固定住元器件,(注意:热熔胶在高温下是导电的,如果你的插座比较好或者后期要用到大功率电器上,那么尽量别用热熔胶,选用其它胶水比如502这种),编写程序,烧写代码,把螺丝拧上即可。
5、测试——效果完美,达到预期效果
代码:在这里插入代码片
由于部分代码涉及隐私信息已用XXXXX代替,写程序的时候注意修改即可
/*
Name: smartchazuo.ino
Created: 2019/12/2 17:26:00
Author: 60480
*/
#include
#include
#include
#include
#include
#include
#include
#define DebugSerial Serial
#define JDQ1Pin D1
#define JDQ2Pin D2
#define BZLED D4 //D1mini是D4
ESP8266WiFiMulti WiFiMulti;
Ticker ticker1;
WiFiClient espClient;
PubSubClient PSClient(espClient);
const char* mqtt_server = “183.230.40.39”;
const char* DeviceID = “xxxxxx”;//设备ID
const char* ProductID = “xxxxxx”;//产品ID
const char* AuthInfo = “xxxxxx”;//鉴权信息
long lastMsg = 0;
char msg[100];
char tmp[100];
char d[3];
void setup() {
DebugSerial.begin(115200);
PSClient.setServer(mqtt_server, 6002);
PSClient.setCallback(callback);
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("********", "**************");//WIFI账号密码
pinMode(BZLED, OUTPUT);
pinMode(JDQ1Pin, OUTPUT);
pinMode(JDQ2Pin, OUTPUT);
setup_wifi();
ticker1.attach(5, pubtoOnenet);//每隔发送数据到MQTT平台
}
void loop() {
if (!PSClient.connected()) {
reconnect();
}
PSClient.loop();
}
//WIFI相关
void setup_wifi() {
unsigned long startTime = millis();
while (WiFiMulti.run() != WL_CONNECTED && millis() - startTime < 10000) {
digitalWrite(BZLED, HIGH);
delay(50);
digitalWrite(BZLED, LOW);
delay(50);
}
digitalWrite(BZLED, LOW);
}
void wificonncheck() {
// 检查连接状态
if (WiFi.status() == WL_CONNECTED)
{ //如果WiFi连接成功
digitalWrite(BZLED, LOW);
}
else
{ //WiFi连接不成功
digitalWrite(BZLED, HIGH);
setup_wifi();
}
}
//MQTT相关
void callback(char topic, byte payload, unsigned int length) {
if (payload[0] == ‘1’) {
if (payload[1] == ‘1’) {
digitalWrite(JDQ1Pin, LOW);
}
else
{
digitalWrite(JDQ1Pin, HIGH);
}
}
else if(payload[0]==‘2’)
{
if (payload[1] == ‘1’) {
digitalWrite(JDQ2Pin, LOW);
}
else
{
digitalWrite(JDQ2Pin, HIGH);
}
}
}
void pubtoOnenet() {
wificonncheck();//WIFI状态检测
snprintf(tmp, sizeof(tmp), "{\"JDQ1State\":1%d,\"JDQ2State\":2%d}", !digitalRead(JDQ1Pin),!digitalRead(JDQ2Pin));
uint16_t streamLen = strlen(tmp);
d[0] = '\x03';
d[1] = (streamLen >> 8);
d[2] = (streamLen & 0xFF);
snprintf(msg, sizeof(msg), "%c%c%c%s", d[0], d[1], d[2], tmp);
PSClient.publish("$dp", (uint8_t*)msg, streamLen + 3, false); //上传onenet云的语句
}
void reconnect() {
while (!PSClient.connected()) {
Serial.print(“Attempting MQTT connection…”);
// Attempt to connect
if (PSClient.connect("xxxxxx", "xxxxxx", "xxxxxx")) {//设备ID;产品ID;鉴权信息
Serial.println("connected");
PSClient.subscribe("inTopic");
}
else {
Serial.print("failed, 重连中...");
Serial.print(PSClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}