8266获取18B20温度,网络同步到巴法云。最后通过手机软件或天猫精灵就可以获取实时温度。
这里我使用的8266是ESP01S。这个板子导出接口太少,如果大家有多操作需求最好买其它型号,在这里顺带说一下,8266不是一个具体的板子,它只是一个芯片型号。一般板子的区别也就是用的flash和导出的io口多少不一样,当然有的板子还自带了USB转串口芯片,这样直接使用数据线接电脑USB口便可下载程序更方便,如果是学习个人觉得这种串品芯片它更方便,flash也最好选大点的,以免有的例子跑不了。
1、打开arduino,工具-》管理库 在过滤里输入 onewire 我使用的下面这个库
2、再在过滤里搜索 18b20 找到下面这个库安装
注意 库2依赖库1,所以其实库2不要也可以,那就要自己调用或者封装库1 。
3、在巴法云上下载8266的例程 放上地址 https://cloud.bemfa.com/zip/ap.zip
4、上面安装包是一个ino文件,解压或者直接打开复制到自己的工程就好。
5、在把这个SSID 和PSK改成自己wifi的帐号密码
6、这里的UID改成自己巴法云的密钥和设备名,设备名我已经之前建立好了,如果没有建立,那么就先到巴法云的控制台建立新主题,最后三位使用004代表传感器。
7、添加一个获取18B20的函数,注意18B20是可以并联多个的,如果要读取多个则需要使用注释里的while获取多个,因为我这里只有一个就不用了。
#define DHTPIN 2DS18B20 ds(DHTPIN);float GetDS18B02Temperature()
{//只有一个,如果多个需要while(ds.selectNext())return ds.getTempC();
}
8、添加一个上传温度的函数
void upload()
{String upstr = "";upstr = "cmd=2&uid=" + UID + "&topic=" + TOPIC + "&msg=on#" + String(GetDS18B02Temperature()) + "\r\n";sendtoTCPServer(upstr);Serial.println("upload....");Serial.println(upstr);
}
9、修改上传接收逻辑,其中bIsOn 为一全局bool变量。
void doTCPClientTick()
{//检查是否断开,断开后重连if (WiFi.status() != WL_CONNECTED)return;if (!TCPclient.connected()){ //断开重连if (preTCPConnected == true){preTCPConnected = false;preTCPStartTick = millis();Serial.println();Serial.println("TCP Client disconnected.");TCPclient.stop();}else if (millis() - preTCPStartTick > 1 * 1000) //重新连接startTCPClient();}else{if (TCPclient.available()){ //收数据char c = TCPclient.read();TcpClient_Buff += c;TcpClient_BuffIndex++;TcpClient_preTick = millis();if (TcpClient_BuffIndex >= MAX_PACKETSIZE - 1){TcpClient_BuffIndex = MAX_PACKETSIZE - 2;TcpClient_preTick = TcpClient_preTick - 200;}preHeartTick = millis();}if (isOpen && (millis() - preHeartTick >= 10 * 1000)){preHeartTick = millis();upload();}else if (millis() - preHeartTick >= KEEPALIVEATIME){ //保持心跳preHeartTick = millis();Serial.println("--Keep alive:");sendtoTCPServer("ping\r\n"); //发送心跳,指令需\r\n结尾,详见接入文档介绍}}if ((TcpClient_Buff.length() >= 1) && (millis() - TcpClient_preTick >= 200)){TCPclient.flush();Serial.print("Rev string: ");TcpClient_Buff.trim(); //去掉首位空格Serial.println(TcpClient_Buff); //打印接收到的消息String getTopic = "";String getMsg = "";if (TcpClient_Buff.length() > 15){ //注意TcpClient_Buff只是个字符串,在上面开头做了初始化 String TcpClient_Buff = "";//此时会收到推送的指令,指令大概为 cmd=2&uid=xxx&topic=light002&msg=offint topicIndex = TcpClient_Buff.indexOf("&topic=") + 7; // c语言字符串查找,查找&topic=位置,并移动7位,不懂的可百度c语言字符串查找int msgIndex = TcpClient_Buff.indexOf("&msg="); // c语言字符串查找,查找&msg=位置getTopic = TcpClient_Buff.substring(topicIndex, msgIndex); // c语言字符串截取,截取到topic,不懂的可百度c语言字符串截取getMsg = TcpClient_Buff.substring(msgIndex + 5); // c语言字符串截取,截取到消息Serial.print("topic:------");Serial.println(getTopic); //打印截取到的主题值Serial.print("msg:--------");Serial.println(getMsg); //打印截取到的消息值}if (String(getMsg).startsWith("on")){ //如果是消息==打开// upload();isOpen = true;}else if (String(getMsg).startsWith("off")){ //如果是消息==关闭isOpen = false;}TcpClient_Buff = "";TcpClient_BuffIndex = 0;}
}
10、上一个整个代码
/*This sketch establishes a TCP connection to a "quote of the day" service.It sends a "hello" message, and then prints received data.
*/#include <ESP8266WiFi.h>
#include <DS18B20.h>#ifndef STASSID
#define STASSID "你的wifi帐号"
#define STAPSK "你的wifi密码"
#endif#define server_ip "bemfa.com" //巴法云服务器地址默认即可
#define server_port "8344" //服务器端口,tcp创客云端口8344String UID = "你的UID"; //用户私钥,可在控制台获取,修改为自己的UID
String TOPIC = "t004"; //主题名字,可在控制台新建const char *ssid = STASSID;
const char *password = STAPSK;#define DHTPIN 2DS18B20 ds(DHTPIN);float GetDS18B02Temperature()
{//只有一个,如果多个需要while(ds.selectNext())return ds.getTempC();
}//最大字节数
#define MAX_PACKETSIZE 512
//设置心跳值30s
#define KEEPALIVEATIME 30 * 1000
// tcp客户端相关初始化,默认即可
WiFiClient TCPclient;
String TcpClient_Buff = ""; //初始化字符串,用于接收服务器发来的数据
unsigned int TcpClient_BuffIndex = 0;
unsigned long TcpClient_preTick = 0;
unsigned long preHeartTick = 0; //心跳
unsigned long preTCPStartTick = 0; //连接
bool preTCPConnected = false;
bool isOpen = false;
// TCP初始化连接
void doTCPClientTick();
void startTCPClient();
int sendtoTCPServer(String p);/**发送数据到TCP服务器*/
int sendtoTCPServer(String p)
{if (!TCPclient.connected()){Serial.println("Client is not readly");return 0;}return TCPclient.print(p);
}/**初始化和服务器建立连接*/
void startTCPClient()
{if (TCPclient.connect(server_ip, atoi(server_port))){Serial.print("\nConnected to server:");Serial.printf("%s:%d\r\n", server_ip, atoi(server_port));String tcpTemp = ""; //初始化字符串tcpTemp = "cmd=1&uid=" + UID + "&topic=" + TOPIC + "\r\n"; //构建订阅指令sendtoTCPServer(tcpTemp); //发送订阅指令tcpTemp = ""; //清空/*//如果需要订阅多个主题,可再次发送订阅指令tcpTemp = "cmd=1&uid="+UID+"&topic="+主题2+"\r\n"; //构建订阅指令sendtoTCPServer(tcpTemp); //发送订阅指令tcpTemp="";//清空*/preTCPConnected = true;preHeartTick = millis();TCPclient.setNoDelay(true);}else{Serial.print("Failed connected to server:");Serial.println(server_ip);TCPclient.stop();preTCPConnected = false;}preTCPStartTick = millis();
}void upload()
{String upstr = "";upstr = "cmd=2&uid=" + UID + "&topic=" + TOPIC + "&msg=on#" + String(GetDS18B02Temperature()) + "\r\n";sendtoTCPServer(upstr);Serial.println("upload....");Serial.println(upstr);
}
/**检查数据,发送心跳*/
void doTCPClientTick()
{//检查是否断开,断开后重连if (WiFi.status() != WL_CONNECTED)return;if (!TCPclient.connected()){ //断开重连if (preTCPConnected == true){preTCPConnected = false;preTCPStartTick = millis();Serial.println();Serial.println("TCP Client disconnected.");TCPclient.stop();}else if (millis() - preTCPStartTick > 1 * 1000) //重新连接startTCPClient();}else{if (TCPclient.available()){ //收数据char c = TCPclient.read();TcpClient_Buff += c;TcpClient_BuffIndex++;TcpClient_preTick = millis();if (TcpClient_BuffIndex >= MAX_PACKETSIZE - 1){TcpClient_BuffIndex = MAX_PACKETSIZE - 2;TcpClient_preTick = TcpClient_preTick - 200;}preHeartTick = millis();}if (isOpen && (millis() - preHeartTick >= 10 * 1000)){preHeartTick = millis();upload();}else if (millis() - preHeartTick >= KEEPALIVEATIME){ //保持心跳preHeartTick = millis();Serial.println("--Keep alive:");sendtoTCPServer("ping\r\n"); //发送心跳,指令需\r\n结尾,详见接入文档介绍}}if ((TcpClient_Buff.length() >= 1) && (millis() - TcpClient_preTick >= 200)){TCPclient.flush();Serial.print("Rev string: ");TcpClient_Buff.trim(); //去掉首位空格Serial.println(TcpClient_Buff); //打印接收到的消息String getTopic = "";String getMsg = "";if (TcpClient_Buff.length() > 15){ //注意TcpClient_Buff只是个字符串,在上面开头做了初始化 String TcpClient_Buff = "";//此时会收到推送的指令,指令大概为 cmd=2&uid=xxx&topic=light002&msg=offint topicIndex = TcpClient_Buff.indexOf("&topic=") + 7; // c语言字符串查找,查找&topic=位置,并移动7位,不懂的可百度c语言字符串查找int msgIndex = TcpClient_Buff.indexOf("&msg="); // c语言字符串查找,查找&msg=位置getTopic = TcpClient_Buff.substring(topicIndex, msgIndex); // c语言字符串截取,截取到topic,不懂的可百度c语言字符串截取getMsg = TcpClient_Buff.substring(msgIndex + 5); // c语言字符串截取,截取到消息Serial.print("topic:------");Serial.println(getTopic); //打印截取到的主题值Serial.print("msg:--------");Serial.println(getMsg); //打印截取到的消息值}if (String(getMsg).startsWith("on")){ //如果是消息==打开// upload();isOpen = true;}else if (String(getMsg).startsWith("off")){ //如果是消息==关闭isOpen = false;}TcpClient_Buff = "";TcpClient_BuffIndex = 0;}
}void setup()
{Serial.begin(115200);// We start by connecting to a WiFi networkSerial.println();Serial.println();Serial.print("Connecting to ");Serial.println(ssid);/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,would try to act as both a client and an access-point and could causenetwork-issues with your other WiFi-devices on your WiFi-network. */WiFi.mode(WIFI_STA);WiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED){delay(500);Serial.print(".");}Serial.println("");Serial.println("WiFi connected");Serial.println("IP address: ");Serial.println(WiFi.localIP());Serial.println(GetDS18B02Temperature());startTCPClient();
}void loop()
{doTCPClientTick();
}
注意修改代码里的你的信息。运行之后还不能得到温度。
11、在巴法云 控制台,向设备推送了一个 on的消息,这后开始定时上传温度。当然如果你有天猫精灵,直接对它说“打开温度传感器”就行了。
至此基本完成,此外巴法云也提供了开发安卓的AI2Offline文件。这个可以自行阅读开发文档。
也上传一个效果
当然使用TCP自己开发一个也很简单。