esp8266_贝壳物联_arduino
Posted _WILLPOWER_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了esp8266_贝壳物联_arduino相关的知识,希望对你有一定的参考价值。
功能:
接收串口数据,将串口数据上报到贝壳物联的数据接口
此处为接收0和1数据,上报到贝壳物联
贝壳物联平台通讯协议
ArduinoJson解析
ArduinoJson Assistant非常好用的工具
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <Ticker.h> // 使用Ticker库,需要包含头文件
#include <WiFiClient.h>
//Ticker timer1; // 创建一个定时器对象
const char* ssid = "xxxxx"; //wifi name
const char* password = "xxxxxx"; //wifi passwd
const char* device_id = "xxxxx"; //设备ID
const char* device_key = "xxxxxxx"; //设备key
const char* interface_id = "xxxxxx"; //接口id
String heart_string; //心跳信息
const char* host = "www.bigiot.net"; //网站
const int port = 8282; //服务器向客户端发送心跳包的端口
WiFiClient client;
#define bufferSize 8192
uint8_t buf1[bufferSize];
uint16_t i1=0;
uint8_t buf2[bufferSize];
uint16_t i2=0;
bool tcp_ok = 0; //是否是首次连接(首次连接发送心跳包)
#define packTimeout 5 // ms (if nothing more on UART, then send packet)
void WiFi_Connect()
Serial.begin(9600);
// We start by connecting to a WiFi network
Serial.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 cause
network-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());
void Heart_Task()
//心跳包任务
StaticJsonDocument<96> heart_json;
heart_json["M"] = "checkin";
heart_json["ID"] = device_id;
heart_json["K"] = device_key;
serializeJson(heart_json, heart_string);
//设置周期性定时10s
//timer1.attach(10, Heart_Live);
void setup()
WiFi_Connect();
//心跳包服务
Heart_Task();
void loop()
//TCP客户端
//如果没有连接
if(!client.connected()) // if client not connected
if (!client.connect(host, port)) //连接失败退出再次连接
Serial.println("connection failed");
delay(5000);
tcp_ok = 0; //连接失败
return;
//如果是第一次上线
if(tcp_ok == 0)
tcp_ok = 1;
//发送登陆消息
client.println(heart_string);
//如果收到来自客户端的消息
if(client.available())
while(client.available())
buf1[i1] = (uint8_t)client.read(); // read char from client (RoboRemo app)
if(i1<bufferSize-1) i1++;
//解析消息
StaticJsonDocument<128> receive_json;
DeserializationError error = deserializeJson(receive_json, buf1, i1);
if (error)
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
// 串口发送出来:
serializeJson(receive_json, Serial);
const char* M = receive_json["M"]; // "heart beat"
if(*M == 'b') //如果是心跳信号
Serial.println(" receive heart beat");
client.println(heart_string); //发送上线通知
//Serial.write(buf1, i1);
i1 = 0;
//串口接收数据
if(Serial.available())
// read the data until pause:
while(1)
if(Serial.available())
buf2[i2] = (char)Serial.read(); // read char from UART
if(i2<bufferSize-1) i2++;
else
//delayMicroseconds(packTimeoutMicros);
delay(packTimeout);
if(!Serial.available())
break;
// now send to WiFi:
//client.write((char*)buf2, i2);
Serial.write(buf2, i2);
bool send_flag = 0;
StaticJsonDocument<96> send_json;
//发送数据
if(buf2[0] == '0') //如果是没人
send_flag = 1;
send_json["V"][interface_id] = "0";
else if(buf2[0] == '1')//有人
send_flag = 1;
send_json["V"][interface_id] = "1";
if(send_flag == 1)
String send_string;
send_json["M"] = "update";
send_json["ID"] = device_id; //设备ID
serializeJson(send_json, send_string);
client.println(send_string); //发送上线通知
i2 = 0;
send_flag = 0;
以上是关于esp8266_贝壳物联_arduino的主要内容,如果未能解决你的问题,请参考以下文章