作者:zls121zls | 更新时间:2016-12-27 | 浏览量:3673
一:前期准备
1、贝壳物联平台通讯协议:http://www.bigiot.net/help/1.html
2、eBox支持贝壳物联,共享一个API接口库:http://www.bigiot.net/talk/97.html
2、机智云Gokit2代功能板ESP-12F直接刷AT固件透传方法:http://www.bigiot.net/talk/113.html
4、ESP8266透传设置脚本,设置成功后自动连接贝壳物联平台:http://www.bigiot.net/talk/1.html
5、机智云开发板在线稳定性测试的详细介绍:http://www.bigiot.net/info/458.html
二、相关参考资料:
百度云盘资料下载:http://pan.baidu.com/s/1nvfO2LV
三:展示效果
四、硬件资料:
整机外观
STM32底板
功能驱动板
WIFI透传模块ESP8266
五、相关源码分析:
//STM32 RUN IN eBox
#include "ebox.h"
#include "cJSON.h"
#include "Dht11.h"
#include "ESP8266.h"
#include "esp8266_tcp.h"
#include "wstring.h"
#include "bigiot.h"
BigIot bigiot(&wifi);
//================================
#define HOST "www.bigiot.net"
#define postingInterval 10000
uint16_t remote_port = 8181;
uint16_t local_port = 4321;
//================================
String DEVICEID = "911";
String APIKEY = "cdd8d4c44";
String temp_input_id="851";
String hum_input_id="1129";
String WiFiSSID = "TP-LINK_3772A8";
String WiFiPASSWORD = "zzzzzzzz";
//====================================
//STM32 RUN IN eBox
#include "ebox.h"
#include "cJSON.h"
#include "Dht11.h"
Iwdg dog;
Dht11 sensor(&PB3);
char uart1_rx_str[1024];
int i=0;
char uart2_rx_str[1024];
int j=0;
uint32_t last_beat_time = 0;
uint32_t last_say_time = 0;
bool checkinok = false;
String checkin="{\"M\":\"checkin\",\"ID\":\""+DEVICEID+"\",\"K\":\""+APIKEY+"\"}\n";
String checkout="{\"M\":\"checkout\",\"ID\":\""+DEVICEID+"\",\"K\":\""+APIKEY+"\"}\n";
void say(char *toID, char *content)
{
uart2.printf("{\"M\":\"say\",\"ID\":\"%s\",\"C\":\"%s\"}\n",toID,content);
}
int processMessage(char *msg){
cJSON *jsonObj = cJSON_Parse(msg);
if(!jsonObj)
{
uart1.printf("json string wrong!");
return 0;
}
cJSON *method = cJSON_GetObjectItem(jsonObj, "M");
char *m = method->valuestring;
if(strncmp(m,"b",1) == 0 || strncmp(m,"WELCOME",7) == 0)
{
uart1.printf("sending checkout...\r\n");
uart2.print(checkout);
//delay_ms(500);
uart1.printf("sending checkin...\r\n");
uart2.print(checkin);
}
if(strncmp(m,"checkinok",9) == 0)
{
checkinok=true;
}
if(strncmp(m,"connected",9) == 0)
{
checkinok=false;
uart1.printf("sending checkout...\r\n");
uart2.print(checkout);
//delay_ms(500);
uart1.printf("sending checkin...\r\n");
uart2.print(checkin);
}
if(strncmp(m,"checked",7) == 0)
{
checkinok=true;
}
if(strncmp(m,"login",5) == 0)
{
char *from_id = cJSON_GetObjectItem(jsonObj, "ID")->valuestring;
if(strncmp(from_id,"G",1) == 0)
{
uart1.printf("saying...\r\n");
char new_content[] = "Dear friend, welcome to BIGIOT and zls121zls !";
say(from_id,new_content);
}
}
if(strncmp(m,"say",3) == 0 && millis() - last_say_time > 10)
{
last_say_time = millis();
char *content = cJSON_GetObjectItem(jsonObj, "C")->valuestring;
char *from_id = cJSON_GetObjectItem(jsonObj, "ID")->valuestring;
if(strncmp(content,"play",4) == 0)
{
//do something here....
PB15.write(HIGH);
uart1.printf("play:PB15 turn on!\r\n");
char new_content[] = "play:PB15 turn on!";
say(from_id,new_content);
}
else if(strncmp(content,"stop",4) == 0)
{
//do something here....
PB15.write(LOW);
uart1.printf("stop:PB15 turn off!\r\n");
char new_content[] = "stop:PB15 turn off!";
say(from_id,new_content);
}
else if(strncmp(content,"up",2) == 0)
{
//do something here....
PB13.write(HIGH);
uart1.printf("up:PB13 turn on!\r\n");
char new_content[] = "up:PB13 turn on!";
say(from_id,new_content);
}
else if(strncmp(content,"plus",4) == 0)
{
//do something here....
PB13.write(LOW);
uart1.printf("plus:PB13 turn off!\r\n");
char new_content[] = "plus:PB13 turn off!";
say(from_id,new_content);
}
else if(strncmp(content,"pause",5) == 0)
{
//do something here....
PB11.write(HIGH);
uart1.printf("pause:PB11 turn on!\r\n");
char new_content[] = "pause:PB11 turn on!";
say(from_id,new_content);
}
else if(strncmp(content,"right",5) == 0)
{
//do something here....
PB11.write(LOW);
uart1.printf("right:PB11 turn off!\r\n");
char new_content[] = "right:PB11 turn off!";
say(from_id,new_content);
}
else if(strncmp(content,"down",4) == 0)
{
//do something here....
PB1.write(HIGH);
uart1.printf("down:PB11 turn on!\r\n");
char new_content[] = "down:PB11 turn on!";
say(from_id,new_content);
}
else if(strncmp(content,"forward",7) == 0)
{
//do something here....
PB1.write(LOW);
uart1.printf("forward:PB1 turn off!\r\n");
char new_content[] = "forward:PB1 turn off!";
say(from_id,new_content);
}
else if(strncmp(content,"offOn",5) == 0)
{
//do something here....
PB15.write(LOW);
PB13.write(LOW);
PB11.write(LOW);
PB1.write(LOW);
uart1.printf("offOn:PB15-13-11-1 LED ALL off!\r\n");
char new_content[] = "offOn:LED ALL off";
say(from_id,new_content);
}
else if(strncmp(content,"minus",5) == 0)
{
//do something here....
PB15.write(HIGH);
PB13.write(HIGH);
PB11.write(HIGH);
PB1.write(HIGH);
uart1.printf("minus!PB15-13-11-1 LED ALL ON\r\n");
char new_content[] = "minus:LED ALL ON\r\n";
say(from_id,new_content);
}
else if(strncmp(content,"left",4) == 0)
{
//do something here....
//PB11.write(LOW);
uart1.printf("^~^left^~^\r\n");
char new_content[] = "^~^left^~^";
say(from_id,new_content);
}
else if(strncmp(content,"backward",8) == 0)
{
//do something here....
//PB11.write(HIGH);
uart1.printf("^~^backward^~^\r\n");
char new_content[] = "^~^backward^~^";
say(from_id,new_content);
}
}
if(jsonObj)cJSON_Delete(jsonObj);
return 1;
}
void uart2_rx_event()
{
uint16_t c;
c = uart2.read();
uart1.write(c);
uart2_rx_str[j]=c;
uart2_rx_str[j+1] = '\0';
if (c == '\n')
{
if(uart2_rx_str[j-1] == '}')
{
processMessage(uart2_rx_str);
}
if(strncmp(uart2_rx_str,"ERROR",5) == 0)
{
checkinok = false;
}
j=0;
}else{
j++;
}
}
void update_dht11()
{
int temp,hum;
switch (sensor.read())
{
case Dht11::OK:
temp=sensor.getTemperature();
hum=sensor.getHumidity();
uart1.printf("{\"M\":\"update\",\"ID\":\"%s\",\"V\":{\"%s\":\"%d\",\"%s\":\"%d\"}}\n",DEVICEID.c_str(),temp_input_id.c_str(),temp,hum_input_id.c_str(),hum);
uart2.printf("{\"M\":\"update\",\"ID\":\"%s\",\"V\":{\"%s\":\"%d\",\"%s\":\"%d\"}}\n",DEVICEID.c_str(),temp_input_id.c_str(),temp,hum_input_id.c_str(),hum);
break;
case Dht11::ERROR_CHECKSUM:
uart1.printf("Checksum error\r\n");
break;
case Dht11::ERROR_TIMEOUT:
uart1.printf("Timeout error\r\n");
break;
default:
uart1.printf("Unknown error\r\n");
break;
}
}
void uart1_rx_event()
{
uint16_t c;
c = uart1.read();
uart1_rx_str[i]=c;
uart1_rx_str[i+1] = '\0';
if (c == '\n')
{
uart2.printf(uart1_rx_str);
i=0;
}else if(strncmp(uart1_rx_str,"+++",3) == 0)
{
uart2.printf(uart1_rx_str);
i=0;
}else
{
i++;
}
}
void setup()
{
ebox_init();
uart1.begin(115200);
uart2.begin(115200);
dog.begin(10000);
uart1.printf("Working start!\r\n");
uart1.attach(uart1_rx_event,RxIrq);
uart2.attach(uart2_rx_event,RxIrq);
//Button.begin();
PB15.mode(OUTPUT_PP); //设置 PB15为推挽输出模式
PB13.mode(OUTPUT_PP); //设置 PB13为推挽输出模式
PB11.mode(OUTPUT_PP); //设置 PB11为推挽输出模式
PB1.mode(OUTPUT_PP); //设置 PB1为推挽输出模式
}
int main(void)
{
uint32_t last_update_time = 0;
uint32_t last_status_time = 0;
setup();
while(1)
{
if(millis() - last_update_time > 6310 && checkinok)
{
update_dht11();
last_update_time = millis();
dog.feed();
}
if(millis() - last_status_time > 120000 || last_status_time == 0)
{
uart2.printf("{\"M\":\"status\"}\n");
last_status_time = millis();
}
}
}
--------------------------------------------------------------------------------------------
六、串口输出记录
[00:20:24.324] +++AT
[00:20:25.322] OK
[00:20:25.322] AT+RESTORE
[00:20:26.498] OK
[00:20:26.602] ets Jan 8 2013,rst cause:2, boot mode:(3,6)
[00:20:26.602] load 0x40100000, len 1856, room 16
[00:20:26.613] tail 0
[00:20:26.613] chksum 0x63
[00:20:26.613] load 0x3ffe8000, len 776, room 8
[00:20:26.613] tail 0
[00:20:26.619] chksum 0x02
[00:20:26.619] load 0x3ffe8310, len 552, room 8
[00:20:26.619] tail 0
[00:20:26.635] chksum 0x79
[00:20:26.635] csum 0x79
[00:20:26.635] 2nd boot version : 1.5
[00:20:26.635] SPI Speed : 40MHz
[00:20:26.635] SPI Mode : QIO
[00:20:26.635] SPI Flash Size & Map: 32Mbit(1024KB+1024KB)
[00:20:26.635] jump to run user1 @ 1000
[00:20:26.635] rl恘専bbl宐r|`l挏brlb靝~抧no鈧lbl€l寧*l鞂€l`€n尀bcllb尀bcl宐|slsl
[00:20:27.181] ready
[00:20:27.181] AT+CWMODE=1
[00:20:31.415] OK
[00:20:31.415] AT+CWJAP="TP-LINK_3772A8","zzzzzzzz"
[00:20:32.416] WIFI CONNECTED
[00:20:35.686] AT+CIPMUX=0
[00:20:42.322] busy p...
[00:20:42.322] WIFI GOT IP
[00:20:43.014] AT+CIPMODE=1
[00:20:43.322] busy p...
[00:20:43.322]
[00:20:43.515] OK
[00:20:43.515] AT+SAVETRANSLINK=1,"121.42.180.30",8282,"TCP"
[00:20:44.407] OK
[00:20:44.407] AT+RST
[00:20:49.320] OK
[00:20:49.320] WIFI DISCONNECT
[00:20:49.429] ets Jan 8 2013,rst cause:2, boot mode:(3,6)
[00:20:49.429] load 0x40100000, len 1856, room 16
[00:20:49.429] tail 0
[00:20:49.440] chksum 0x63
[00:20:49.440] load 0x3ffe8000, len 776, room 8
[00:20:49.440] tail 0
[00:20:49.440] chksum 0x02
[00:20:49.440] load 0x3ffe8310, len 552, room 8
[00:20:49.440] tail 0
[00:20:49.456] chksum 0x79
[00:20:49.456] csum 0x79
[00:20:49.456] 2nd boot version : 1.5
[00:20:49.456] SPI Speed : 40MHz
[00:20:49.456] SPI Mode : QIO
[00:20:49.456] SPI Flash Size & Map: 32Mbit(1024KB+1024KB)
[00:20:49.456] jump to run user1 @ 1000
[00:20:49.456] rl恘専鉩
[00:20:49.906] >
[00:20:49.906] ready
[00:20:49.906] {"M":"WELCOME TO BIGIOT"}
[00:20:55.685] sensending checkin...
[00:20:55.685] {"M":"checkinok","ID":"D911","CID":"7f0000011f4100000466","NAME":"\u673a\u667a\u4e91GOKIT2","T":"1482164455"}
[00:21:03.925] [ESP]cmd TIMEOUT
[00:21:06.933] [ESP]set sta fail!
[00:21:06.933] [ESP]pleas wait, join wifi will take about 10 seconds.
[00:21:06.933] [ESP]cmd TIMEOUT
[00:21:16.932] [ESP]join ap failed!
[00:21:16.932] {"M":"update","ID":"911","V":{"851":"0","1129":"0"}}
[00:21:16.957] {"M":"update","ID":"911","V":{"851":"21","1129":"36"}}
[00:21:23.288] {"M":"update","ID":"911","V":{"851":"21","1129":"36"}}
-------------------------------------------------------------------------------------
[21:49:25.742] {"M":"update","ID":"911","V":{"851":"19","1129":"34"}}
[21:49:25.742] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"offOn","T":"1482846567"}
[21:49:28.488] offOn:PB15-13-11-1 LED ALL off!
[21:49:28.498] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"minus","T":"1482846568"}
[21:49:29.507] minus!PB15-13-11-1 LED ALL ON
[21:49:29.507] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"play","T":"1482846570"}
[21:49:31.042] play:PB15 turn on!
[21:49:31.042] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"up","T":"1482846571"}
[21:49:31.865] up:PB13 turn on!
[21:49:31.865] {"M":"update","ID":"911","V":{"851":"18","1129":"35"}}
[21:49:32.072] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"pause","T":"1482846571"}
[21:49:32.479] pause:PB11 turn on!
[21:49:32.479] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"down","T":"1482846572"}
[21:49:33.017] down:PB11 turn on!
[21:49:33.025] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"stop","T":"1482846573"}
[21:49:33.646] stop:PB15 turn off!
[21:49:33.646] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"plus","T":"1482846573"}
[21:49:34.123] plus:PB13 turn off!
[21:49:34.123] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"right","T":"1482846574"}
[21:49:34.646] right:PB11 turn off!
[21:49:34.646] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"forward","T":"1482846574"}
[21:49:35.184] forward:PB1 turn off!
[21:49:35.184] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"left","T":"1482846575"}
[21:49:35.805] ^~^left^~^
[21:49:35.805] {"M":"say","ID":"P7f00000109c4000015cc","NAME":"guest","C":"backward","T":"1482846575"}
[21:49:36.396] ^~^backward^~^
[21:49:36.396] {"M":"checked"}
[21:49:36.928] {"M":"update","ID":"911","V":{"851":"18","1129":"35"}}
-----------------------------------------------------------------------------------------
七、扩展知识阅读:
c语言解析json数据:http://stevengw.blog.163.com/blog/static/5834475120124643458695/
c语言解析json数据
我使用的是cJSON:http://sourceforge.net/projects/cjson/
先看json的数据结构
c中没有对象,所以json数据是采用链表存储的
C代码
typedef struct cJSON {
struct cJSON *next,*prev; // 数组 对象数据中用到
struct cJSON *child; // 数组 和对象中指向子数组对象或值
int type; // 元素的类型,如是对象还是数组
char *valuestring; // 如果是字符串
int valueint; // 如果是数值
double valuedouble; // 如果类型是cJSON_Number
char *string; // The item's name string, if this item is the child of, or is in the list of subitems of an object.
} cJSON;
比如你有一个json数据
Javascript代码
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}
那么你可以
1:讲字符串解析成json结构体。
C代码
cJSON *root = cJSON_Parse(my_json_string);
2:获取某个元素
C代码
cJSON *format = cJSON_GetObjectItem(root,"format");
int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;
3:讲json结构体转换成字符串
C代码
char *rendered=cJSON_Print(root);
4:删除
C代码
cJSON_Delete(root);
5:构建一个json结构体
C代码
cJSON *root,*fmt;
root=cJSON_CreateObject();
cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());
cJSON_AddStringToObject(fmt,"type", "rect");
cJSON_AddNumberToObject(fmt,"width", 1920);
cJSON_AddNumberToObject(fmt,"height", 1080);
cJSON_AddFalseToObject (fmt,"interlace");
cJSON_AddNumberToObject(fmt,"frame rate", 24);
----------------------------------------------------------------------------
八、参考文章
机智云实验室 / GoKit_2_MCU_STM
https://git.oschina.net/dantang/GoKit_2_MCU_STM
极客堂
GoKit2代资源汇总