把一个废柴插座转眼变身远程控制智能插座-Arduino中文社区 - Powered by Discuz! Archiver

IOT专业户 发表于 2019-12-4 17:39

把一个废柴插座转眼变身远程控制智能插座

本帖最后由 IOT专业户 于 2019-12-4 17:47 编辑

把一个废柴插座转眼变身远程控制智能插座该项目主要基于Arduino D1 mini(wemos)模块和MQTT协议和移动Onenet物联网云平台实现远程控制1、选插座一定要选择这种花里胡哨,内部空间大的,这样便于后期改造https://www.arduino.cn/forum.php?mod=image&aid=59657&size=300x300&key=19046f633bb68183&nocache=yes&type=fixnone 2、线路改造,可以看到进来两根线一根线经过开关,结构简单粗暴,便于改造https://www.arduino.cn/forum.php?mod=image&aid=59658&size=300x300&key=9e4ccebfe829e1a4&nocache=yes&type=fixnone
https://www.arduino.cn/forum.php?mod=image&aid=59663&size=300x300&key=55f97b960621aa07&nocache=yes&type=fixnone3、寻找合适的安装位置,我这里放了两个3.3V的一路继电器,和一个Arduino D1 MINI这种某宝一大堆,(本来准备把那个四路遥控器也安装上去,但是无奈这个信号输出是5V的所以不能用,用三极管改造一上午居然还给我搞坏了,气死直接丢垃圾桶)https://www.arduino.cn/forum.php?mod=image&aid=59664&size=300x300&key=f61851d6ee136838&nocache=yes&type=fixnone图中画圈区域就是改造区域,选用这个接线板的好处就是板载自带5V电源,所以就不需要自己再接220VAC转5V模块了,直接上电测出5V电源连接到Arduino上的5V个GND即可,完成本项目所需的电源供应。因为没用到四路遥控模块所以这里连线非常简单,就是继电器信号线连接到D1和D2上即可。
4、打上热熔胶,固定住元器件,(注意:热熔胶在高温下是导电的,如果你的插座比较好或者后期要用到大功率电器上,那么尽量别用热熔胶,选用其它胶水比如502这种),编写程序,烧写代码,把螺丝拧上即可。5、测试——效果完美,达到预期效果 https://www.arduino.cn/forum.php?mod=image&aid=59659&size=300x300&key=9af11eb241acc87e&nocache=yes&type=fixnone
https://www.arduino.cn/forum.php?mod=image&aid=59660&size=300x300&key=d994d7f2f57232cc&nocache=yes&type=fixnone
https://www.arduino.cn/forum.php?mod=image&aid=59661&size=300x300&key=b3aa81ff586b07fc&nocache=yes&type=fixnone
https://www.arduino.cn/forum.php?mod=image&aid=59662&size=300x300&key=2aa98bb53fb06e42&nocache=yes&type=fixnone代码:由于部分代码涉及隐私信息已用XXXXX代替,写程序的时候注意修改即可
/*
Name: smartchazuo.ino
Created: 2019/12/2 17:26:00
Author: 60480
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <Ticker.h>
#include <PubSubClient.h>
#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;
char tmp;
char d;
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 == ‘1’) {
if (payload == ‘1’) {
digitalWrite(JDQ1Pin, LOW);
}
else
{
digitalWrite(JDQ1Pin, HIGH);
}
}
else if(payload==‘2’)
{
if (payload == ‘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 = '\x03';
d = (streamLen >> 8);
d = (streamLen & 0xFF);
snprintf(msg, sizeof(msg), "%c%c%c%s", d, d, d, 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);
        }
}
}604803482

页: [1]
查看完整版本: 把一个废柴插座转眼变身远程控制智能插座