步骤1:原理图:
您可以使用pcbgogo制造pcb。/p》
步骤2:ubidots设备和变量创建。
a。转到您的ubidots帐户的“设备”部分,然后创建一个名为“ wifiswitch”的新设备。
b。在您的“ wifiswitch”设备内,创建一个名为“ light”的变量。
步骤3:ubidots仪表盘和小部件创建。
一个。创建设备和变量后,我们可以创建仪表板和小部件以控制来自web或移动仪表板的灯光。要创建新的仪表板,请按“数据》仪表板”。然后按加号图标,然后根据需要完成仪表板配置。
b。现在,创建一个控件小部件以设置与变量“ light”关联的灯泡的状态。要创建控件小部件,请选择页面右上方的加号图标。然后按“切换”作为窗口小部件类型,选择所需的变量进行控制,并根据需要完成窗口小部件的配置。
c。然后即可开始编程和测试项目。
步骤4:使用arduino ide进行编程。
1。如果尚未完成,请下载arduino ide。
1a上。打开arduino ide,然后选择文件-》首选项
1b。将以下网址添加到“其他董事会经理网址”字段中。您可以使用逗号分隔多个url。
http://arduino.esp8266.com/stable/package_esp8266.。.
2。在boards manager中打开并安装esp8266板:工具-》 board-》 boards manager
2a。您可以通过在搜索栏中键入“ esp8266”来轻松找到该板。
3。现在,从工具-》板菜单中选择通用esp8266板
4。定义或仔细检查设备正在与之通信的pc的端口。转到工具-》端口:-》选择端口。
4b。通过转到工具-》上传速度-》 115200
5,确保您的ide上传速度为115200。如果尚未下载ubidotsespmqtt库,请下载它。现在,单击sketch –》 include library –》 add .zip library并选择ubidots esp8266 mqtt库。
如果上传正确,您将得到响应:“库已添加到库中。”
8。关闭并再次打开arduino ide。
步骤5:编程esp8266:
设置好esp8266之后,我们就可以开始从ubidots发布数据和向ubidots订购数据,以控制wifi开关。
1.将以下代码复制并粘贴到arduino ide中。不要忘记自定义wi-fi ssid和密码以及您的ubidots令牌。
2。在此处下载代码:
/****************************************
* libraries
****************************************/
#include “ubidotsespmqtt.h”
/****************************************
* define constants
****************************************/
#define token “。..。..。..。..。..。..。..。..。..” // your ubidots token
#define wifiname “。..。..。..” //your ssid
#define wifipass “。..。..。..” // your wifi pass
#define device_label “wifiswitch” // name of the device
#define variable_label1 “light” // name of the ubidots variable
const int error_value = 65535; // error value
const uint8_t number_of_variables = 2; // cantidad de variables a las que el programa se va a suscribir
char * variable_labels[number_of_variables] = {“light”}; // variables names
#define luz 0
#define boton 2
int seguro=0;
int ledstate = low; // the current state of the output pin
int buttonstate; // the current reading from the input pin
int lastbuttonstate = high; // the previous reading from the input pin
int reading;
unsigned long lastdebouncetime = 0; // the last time the output pin was toggled
unsigned long debouncedelay = 50;
float estadoluz; // variable to be used in the code
float value; // variable to store input data
uint8_t variable; // to use with the switch case
ubidots ubiclient(token);
wificlient client;
/****************************************
* auxiliar functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
char* variable_label = (char *) malloc(sizeof(char) * 30);
get_variable_label_topic(topic, variable_label);
value = btof(payload, length);
set_state(variable_label);
execute_cases();
free(variable_label);
/////////////////light////////////////////
digitalwrite(luz, estadoluz);
/////////////////light////////////////////
}
// parse topic to extract the variable label which changed value
void get_variable_label_topic(char * topic, char * variable_label) {
serial.print(“topic:”);
serial.println(topic);
sprintf(variable_label, “”);
for (int i = 0; i 《 number_of_variables; i++) {
char * result_lv = strstr(topic, variable_labels[i]);
if (result_lv != null) {
uint8_t len = strlen(result_lv);
char result[100];
uint8_t i = 0;
for (i = 0; i 《 len - 3; i++) {
result[i] = result_lv[i];
}
result[i] = ‘’;
serial.print(“label is: ”);
serial.println(result);
sprintf(variable_label, “%s”, result);
break;
}
}
}
// cast from an array of chars to float value.
float btof(byte * payload, unsigned int length) {
char * demo = (char *) malloc(sizeof(char) * 10);
for (int i = 0; i 《 length; i++) {
demo[i] = payload[i];
}
float value = atof(demo);
free(demo);
return value;
}
// state machine to use switch case
void set_state(char* variable_label) {
variable = 0;
for (uint8_t i = 0; i 《 number_of_variables; i++) {
if (strcmp(variable_label, variable_labels[i]) == 0) {
break;
}
variable++;
}
if (variable 》= number_of_variables) variable = error_value; // not valid
}
// function with switch case to determine which variable changed and assigned the value accordingly to the code variable
void execute_cases() {
switch (variable) {
case 0:
estadoluz = value;
serial.print(“luz: ”);
serial.println(estadoluz);
serial.println();
break;
case error_value:
serial.println(“error”);
serial.println();
break;
default:
serial.println(“default”);
serial.println();
}
}
/****************************************
* funcion principal
****************************************/
void setup() {
// put your setup code here, to run once:
pinmode(luz, output);
pinmode(boton, input);
ubiclient.ubidotssetbroker(“industrial.api.ubidots.com”); // sets the broker properly for the business account
ubiclient.setdebug(true); // pass a true or false bool value to activate debug messages
serial.begin(115200);
ubiclient.wificonnection(wifiname, wifipass);
ubiclient.begin(callback);
if(!ubiclient.connected()){
ubiclient.reconnect();
}
char* devicestatus = getubidotsdevice(device_label);
if (strcmp(devicestatus, “404”) == 0) {
ubiclient.add(“light”, 0); //insert your variable labels and the value to be sent
ubiclient.ubidotspublish(device_label);
ubiclient.loop();
}
ubiclient.ubidotssubscribe(device_label,variable_label1); //insert the device and variable‘s labels
serial.println(variable_labels[1]);
}
void loop() {
// put your main code here, to run repeatedly:
if(!ubiclient.connected()){
ubiclient.reconnect();
ubiclient.ubidotssubscribe(device_label,variable_label1); //insert the device and variable’s labels
}
ubiclient.loop();
read();
debounce();
// save the reading. next time through the loop, it‘ll be the lastbuttonstate:
lastbuttonstate = reading;
}
void read(){
// read the state of the switch into a local variable:
reading = digitalread(boton);
if (reading != lastbuttonstate) {
// reset the debouncing timer
lastdebouncetime = millis();
}
}
void debounce(){
if ((millis() - lastdebouncetime) 》 debouncedelay) {
// whatever the reading is at, it’s been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonstate) {
buttonstate = reading;
toggle();
}
}
}
void toggle(){
// only toggle the led if the new button state is low
if (buttonstate == low) {
ledstate = !ledstate;
// set the led:
digitalwrite(luz, ledstate);
ubiclient.add(“light”, ledstate); //insert your variable labels and the value to be sent
ubiclient.ubidotspublish(device_label);
}
}
char* getubidotsdevice(char* devicelabel) {
char* data = (char *) malloc(sizeof(char) * 700);
char* response = (char *) malloc(sizeof(char) * 400);
sprintf(data, “get /api/v1.6/devices/%s/”, devicelabel);
sprintf(data, “%s http/1.1 ”, data);
sprintf(data, “%shost: industrial.api.ubidots.com user-agent:wifiswitch/1.0 ”, data);
sprintf(data, “%sx-auth-token: %s connection: close ”, data, token);
char* data1 = data;
free(data);
if (client.connect(“industrial.api.ubidots.com”, 80)) {
client.println(data1);
}
else {
free(data);
return “e”;
}
int timeout = 0;
while(!client.available() && timeout 《 5000) {
timeout++;
if (timeout 》= 4999){
free(data);
return “e”;
}
delay(1);
}
int i = 0;
while (client.available()) {
response[i++] = (char)client.read();
if (i 》= 399){
break;
}
}
char * pch;
char * statuscode;
int j = 0;
pch = strtok (response, “ ”);
while (pch != null) {
if (j == 1 ) {
statuscode = pch;
}
pch = strtok (null, “ ”);
j++;
}
free(response);
return statuscode;
}
步骤6:使用google助手配置语音命令:
要使用google home控制您的“ wifi交换机”,首先我们需要配置一个称为ifttt的中介平台,将我们的switch与google assistant配对。要正确配置,请按照以下步骤操作。
如果您没有帐户,请注册。
单击“我的小程序”。
然后,单击“ new applet”。
单击“ + this”以配置条件触发条件。
搜索“ google助手”服务,然后单击它。
单击“说一个简单的短语”。
完成触发字段中包含您要用来控制灯光,响应和语言的短语,然后单击“创建触发”。
然后,单击“ + that”配置操作。
搜索“ webhooks”操作服务。
单击“发出web请求”
完整的操作字段:
url ----》 http://things.ubidots.com/api/v1.6/devices/wifisw 。..成为您的ubidots令牌)
方法----》 post
内容类型----》 application/json
正文----》要打开 {“灯光”:1} ,请关闭 {“轻”:0}
11。最后,单击“完成”。
注意:重复所有操作,以正确的body语句设置“关闭灯光”小程序。
步骤7:测试会话:
由于图中所示,将模块正确连接到交流电
line ---》 l
neutral ---》 n
light line‘---》 b
在接线端子sw中添加您喜欢的瞬时按钮。
识别电源线,中性线和轻型电缆:
进行连接并放置按钮,拧紧螺钉并进行测试
如果您是视觉学习者,请查看以下视频教程。您可以找到我精心构建的用于构建此项目的所有步骤:
步骤8:摘要:
在本指南中,我们刚刚学习了如何建立一个可以通过语音,手机应用程序或pc进行internet控制的wifi开关,让您控制卧室或其他任何地方的灯泡。该设备基于esp8266 wifi模块工作,该模块是一个很小的模块,可让项目轻松上线。而且该设备可用于控制许多不同的设备,例如风扇,电机,窗帘,灯,led灯条等。
不同封装技术下单个元件的贴装面积
电容麦噪音很大的原因和解决方案
还肾没长好呢 iPhone8就来了!发布时间已确认 售价1000美元还双卡双待?
如何用调试器JLink来打印信息
什么是高速电路?“高频”和“高速”有什么区别
如何使用ESP模块构建自己的WiFi交换机以及如何控制任何设备
人工智能越来越无所不能,人类该如何自处?
介绍两种方法来避免正在执行中的任务中断
数字孪生智慧工厂及3d可视化数据大屏展示系统介绍
Xilinx-FPGA的DNA的读取方法
玻璃将逐步取代PCB而成为MiniLED背光背板的首选
思特威推出全新5000万像素图像传感器SC580XS赋能手机影像系统
x86主板工业级主板与工控级主板之间的区别是什么
新汽车显示屏需要合适的背光照明LED驱动器
用Raspberry Pi和SensorMedal制作IoT跳绳设备 第四部分:在设备上安装显示器以增加动力
25亿!优必选科技在国际市场签订史上最大AI教育项目
NB-IoT模组使用中的常见问题及解决办法
OPPO R9s红色为什么还那么火?
DIN11系列交流信号隔离变送器概述及特性
甘肃民航飞机维修公司获得了2018年度国航金牌机务航站奖牌