关注分享主机优惠活动
国内外VPS云服务器

【基于Arduino IDE平台开发ESP8266连接Bafa Cloud】

教程门户基于平台开发和连接。简单介绍实验准备,软硬件实验步骤,照明实验,发送温湿度指令,升级总结。关于巴法云,它专注于开源、智造、创新和共享。

Arduino教程门户??????

基于Arduino IDE平台1开发ESP8266连接Bafa Cloud。导言2。实验准备2.1硬件2.2软件3。实验步骤3.1 TCP光照实验3.2 MQTT发送温湿度3.3 OTA指令升级4。摘要

关于巴法云专注于开源、智造、创新、分享。注重硬件和创新,突破技术极限,面向未来。我们是认真的。提倡开源,探索未知。——巴法科技

1.介绍ESP8266-NodeMCU的环境配置已经在一篇文章中解释过了。接下来我们就开始巴法云的物联网开发吧!???

设计目标

实现esp8266自动网络分配;ESP8266通过TCP协议连接Bafa Cloud的TCP Maker Cloud,串口发送指令控制LED的开关;实现ESP8266的MQTT设备云通过MQTT协议连接到Bafa Cloud发送串行指令控制LED的亮灭,还实现了DHT11温湿度数据的采集,并将数据发送到Bafa云平台在线显示数据;Bafa云平台上ESP8266的OTA指令升级是基于前面两个实验。2.实验的准备工作。这次需要做三个实验,TCP光照实验,MQTT发送温湿度和OTA指令升级。准备工作包括硬件???还有软件???两部分。

2.1硬件实现巴法云的三个实验,需要准备以下材料

ESP8266-NodeMCU(加安卓数据线);DHT11温湿度传感器;三母女杜邦线;硬件连接:ESP8266-NodeMCU硬件连接非常简单,只需将DHT11数据线连接到D6引脚即可。这组连接在所有三个实验中都是常见的。具体连接如下???

ESP8266-node MCU -----> USBVCC -----> 3.3V GND ------> gnd out ------> D2

2.2软件Arduino IED 1 . 8 . 5以上(越高越好);登录浏览器巴法云平台。3.实验步骤:首先搭建Bafa云平台,创建产品相关主题,检测相关数据流,上传固件,发送指令,方便后期程序端口接入开发。???

3.1 TCP灯光实验登录巴法云物联网平台,选择控制台,点击TCP创客云,然后新建light002主题。然后配置Bafa云TCP?程序设计,程序有bemfa02、TCP、motion、update四个部分,其中:??文件名bemfa02程序初始化调用和主程序调用;向TCP服务器发送TCP数据,初始化wifi连接,并与服务器建立连接;查运动的数据,发心跳,查WiFi更新固件升级功能。bemfa02.ino

/* *智能语言控制控件,支持天猫、萧艾、小度、Google Assist同时控制*时间:20211127 *作者:2345VOR *项目示例:开关机命令开关灯*引用:https://bbs.bemfa.com/84/last */# include # include # Define server _ IP " BEM fa . com "//Bafa云服务器默认地址为#define server_port "8344" //服务器端口、 Tcp Maker云端口8344//* * * * * * * * * * * * * * *待修改部分* * * * * * * * * * * * * * * */# define WiFi _ name " J09 502 "//WiFi不要写错# define WiFi _ password " qwertuiop 111 "//WiFi密码字符串uid = " e 8882 AE 28 D5 bde 39766 c 330 ea 913 FD 46 "; //用户的私钥,可以在控制台中获取,修改为自己的UIDString TOPIC = " light002//主题名,可以在控制台新建一个const int LED _ Pin = D4;//MCU LED管脚值,D2是NodeMcu管脚的命名方法。其他esp8266机型将D2改为自己的pin string up URL = " http://bin . BEM fa . com/b/3 bccztg 4 odjhzti 4 zdvizguzotc 2 nmzzblytkxm 2 zndy = light 002 . bin ";//固件链接,在Bafa云控制台//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * String TCP client _ Buff = " ";//初始化字符串,用于接收服务器发送的数据unsigned int TCP client _ buff index = 0;无符号长整型TCP client _ pre tick = 0;无符号长整型preHeartTick = 0;//heart beat unsigned long preTCPStartTick = 0;//connect bool preTCPConnected = false;//初始化相关函数//连接WiFi voiddownvifick();void startSTA();//TCP初始化连接void dotcpclientick();void startTCPClient();void sendtoTCPServer(字符串p);//led控制功能。具体功能内容见下面# define tunled()digital write(LED _ pin,低);# define turnffled()digital write(LED _ Pin,高);//初始化,相当于主函数void setup(){ serial . begin(115200);pinMode(LED_Pin,输出);digitalWrite(LED_Pin,高电平);Serial.println("开始...");}//Loop void Loop(){ dowifick();doTCPClientTick();} TCP.ino

/* *将数据发送到TCP服务器*/void发送到TCP服务器(string p) {if(!TCP Client . connected()){ serial . println("客户端未就绪");返回;} TCP client . print(p);}/* *初始化wifi连接*/void start sta(){ wifi . disconnect();WIFI . mode(WIFI _ STA);WiFi.begin(wifi_name,wifi _ password);}/* *初始化与服务器的连接:style="value.online?"订阅设备在线":"无订阅设备" "颜色:# 9 a9 a9 a;*/void startTCPClient(){ if(TCP client . connect(server _ IP,atoi(server _ port)){ serial . print("/n连接到服务器:");Serial.printf("%s:%d/r/n ",server_ip,atoi(server _ port));字符串tcpTemp =//初始化字符串tcptemp = " cmd = 1 & uid = "+uid+" & topic = "+topic+"/r/n ";//建立订阅指令sendtoTCPServer(tcpTemp);//发送订阅指令tcpTemp =//Clear/*//如果需要订阅多个主题,可以再次发送订阅指令tcptemp = " cmd = 1 & uid = "+uid+" & topic = "+topic zhujipindao"/r/n ";//建立订阅指令sendtoTCPServer(tcpTemp);//发送订阅指令tcpTemp =//empty */pretpconnected = true;preHeartTick = millis();TCP client . setnodelay(true);} else{ Serial.print("连接服务器失败:");serial . println(server _ IP);TCP client . stop();preTCPConnected = false} preTCPStartTick = millis();} motion.ino

/* *检查数据发送heart beat */void doTCPClientTick(){//检查是否断开,然后重新连接if(WiFi.status())!= WL _已连接)返回;如果(!TCP client . connected()){////断开并重新连接if(pretpconnected = = true){ pretpconnected = false;preTCPStartTick = millis();serial . println();Serial.println("TCP客户端已断开连接。");TCP client . stop();} else if(Millis()-pretcpstarttick > 1 * 1000)//重新连接startTCPClient();} else { if(TCP client . available()){//data char c = TCP client . read();TCP client _ Buff+= c;TCP client _ buff index++;TCP client _ pre tick = millis();if(TCP client _ buff index > = MAX _ packet size -1){ TCP client _ buff index = MAX _ packet size -2;TCP client _ pre tick = TCP client _ pre tick -200;} preHeartTick = millis();} if(millis()-preheartbeat > = keepalive atime){//保持心跳preHeartTick = millis();serial . println(" --保活:");sendtoTCPServer(" ping/r/n ");//发送心跳,指令需要以/r/n结尾,具体请参考access文档介绍} } if((TCP client _ buff . length()> = 1)&(millis()-TCP client _ pre tick > = 200)){ TCP client . flush();Serial.print("版本字符串:");TCP client _ buff . trim();//去掉第一个空格serial . println(TCP client _ Buff);//打印接收到的消息字符串getTopic =String getMsg =If (TcpClient_Buff。length()> 15){//注意TCP client _ buff只是一个字符串,初始化字符串TcpClient_Buff = " "是在它的开头做的;//此时会收到push指令,大概是cmd = 2 & uid = XXX & topic = light 002 & msg = off int topic index = TCP client _ buff . index of(" & topic = ")+7;//c语言字符串搜索,搜索&topic= position,移动7位数。如果不明白,可以搜索int msg index = TCP client _ buff . index of(" & msg = ");//c语言字符串查找,lookup & msg = location gettopic = TCP client _ buff . substring(主题索引,msgindex);//c语言字符串拦截,拦截到题目,不懂就拦截百度C语言字符串。getmsg = TCP client _ buff . substring(msgindex+5);//c语言字符串截取到消息serial . print(" topic:-----");serial . println(getTopic);//打印截取的主题值serial . Print(" msg:--------");serial . println(getMsg);//打印截获的消息值} if(getMsg == "on"){ //如果收到指令,on==打开已打开的灯on();}else if(getMsg == "off"){ //如果收到指令off==关灯turn ffled();}else if(getMsg == "update"){ //如果收到指令update bin();//执行升级函数} TcpClient _ Buff =TCP client _ buff index = 0;} }/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * WIFI勾选检查WIFI是否需要初始化。如果连接成功,启动TCP客户端控制指示器*/voiddownifick(static bool task started = false;静态uint 32 _ t lastWiFiCheckTick = 0;如果(!startSTAFlag){ startSTAFlag = true;startSTA();}//1s未连接则重新连接(wifi.status()!= WL _连接){ if(millis()-lastwifichecktck > 1000){ lastwifichecktck = millis();} }//连接成功建立else { if(task started = = false){ task started = true;serial . print("/r/nGet IP Address:");serial . println(wifi . localip());startTCPClient();} }}更新

/* * *固件升级功能*只需在需要升级的地方添加这个功能即可,比如设置中添加的update bin();*原理:通过http请求获取远程固件进行升级*/void update bin(){ serial . println(" start update ");WiFiClient更新客户端;t _ httpUpdate _ return ret = esphttpupdate . update(update client,up URL);switch(ret){ case http _ update _ failed://当升级失败时serial . println("[update]update failed。");打破;http _ update _ no _ updates://当没有升级serial . println("[update]update no update。");打破;http _ update _ ok://当升级成功时。);打破;}}}修改bemfa02主程序对应的wifi和Bafa云参数,upUrl参数可以暂时不变,实验三再修改???

编译程序,然后上传esp8266,观察esp8266和巴法云的TCP控制台????

实验效果:在订阅的“light002”主题下,发送on或off,可以看到esp8266板载灯会分别打开和关闭。?????

3.2 MQTT发送温湿度登录Bafa Cloud物联网平台,选择控制台,点击MQTT设备云,然后新建led002控制端和temp004状态端的主题,那么Bafa Cloud MQTT设备云就配置好了?

程序设计,程序有四个部分:dht11_led_OTA1.0,OTA(更新)PubSubClient.cpp和PubSubClient.h,其中:??

文件名dht11_led_OTA1.0程序初始化调用和主程序调用,自动连接目标wifi,重新连接,led002的回调函数处理;OTA(更新)固件升级功能。dht11_led_OTA1.0.ino

/*时间:2021127作者:2345 VOR项目示例:通过MQTT协议发送开关量控制开关,并将温湿度上传到Bafayun参考:https://www . cnblogs . com/BEM fa/p/14590133 . html */#include //默认加载WIFI头文件#include "PubSubClient.h"//默认加载MQTT库文件# include//自动升级库https://bbs.bemfa.com/84#include//DHT11库文件string up URL = "http://bin .const char * ssid = " J09 502//修改,修改为你的路由const char * password = " qwertuiop 111 "的WIFI名称;//改成你的WIFI密码const char * MQTT _ server = " BEM fa . com ";//默认情况下,MQTT服务器地址const int mqtt _ server _ port = 9501//默认情况下,MQTT服务器端口# define ID _ MQTT " e 8882 AE 28 D5 bde 39766 c 330 ea 913 FD 46 "//MQTT客户端ID修改为您的开发者key const char * topic = " led002//Led主题名,可以在巴法云控制台自己创建。该名称是可选的const char * dhttopic = " temp004//可以在巴法云的mqtt控制台中创建温湿度主题的名称,其中int pin DHT 11 = D2;//input int B _ led =//DHT 11传感器引脚的D4;//控件的led引脚long time val = 3 * 1000;//上传传感器时间间隔默认为3秒# definedstatus!digital read(B _ led);//默认//led状态为0long lastMsg = 0//时间戳simple DHT 11 DHT 11(pin DHT 11);//dht11初始化WiFiClient espClientPubSubClient客户端(espClient);//mqtt初始化//灯功能和引脚定义#定义TURNONLED()数字写(B _ LED,低);# define turnffled()digital write(B _ led,高);//自动连接目标wifi void setup _ wifi(){ delay(10);serial . println();Serial.print("连接到");serial . println(ssid);WiFi.begin(ssid,密码);while (WiFi.status()!= WL _连接){延迟(500);Serial.print(" . ");} serial . println(" ");Serial.println("WiFi已连接");Serial.println("IP地址:");serial . println(wifi . localip());}//reconnect void reconnect(){//循环直到我们“reconnect while(!client . connected()){ serial . print("尝试MQTT连接...");//尝试连接if(client . connect(id _ mqtt)){//connect mqtt serial . println(" connected ");client.subscribe(主题);//修改成你的主题} else {Serial.print("failed,RC = ");serial . print(client . state());serial . println(" 5秒后重试");//重试延迟前等待5秒(5000);} }}void setup() { pinMode(B_led,OUTPUT);serial . begin(115200);setup _ wifi();client.setServer(mqtt_server,mqtt _ server _ port);client.setCallback(回调);digitalWrite(B_led,高);}void loop() { if(!client . connected()){///确定mqtt是否连接到reconnect();} client . loop();//mqtt客户端long now = millis();//获取当前时间戳if (now - lastMsg > timeval) {//如果达到

未经允许不得转载:主机频道 » 【基于Arduino IDE平台开发ESP8266连接Bafa Cloud】

评论 抢沙发

评论前必须登录!