极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14012|回复: 1

机智云开发板(stm32+esp8266)连接贝壳物联在线稳定性测试(含代码)

[复制链接]
发表于 2016-12-5 22:36:16 | 显示全部楼层 |阅读模式
本帖最后由 sxmwhl 于 2016-12-6 23:39 编辑

一、设备功能
机智云gokit开发板实时上传DHT11温湿度数据,接受控制并进行反馈,保持断线重连,可以通过开发板的usb口,用串口工具调试工具,直接控制esp8266。
二、硬件
gokit开发板2.0v,esp8266网络模块。
三、实现方法
1、gokit扩展板上的esp8266刷AT固件,参见:机智云Gokit2代功能板ESP-12F直接刷AT固件透传方法
2、下载基于ebox系统的stm32,工程文件,点击下载
3、复制下方代码,粘贴替换下载工程文件中的mian.cpp文件内容;
4、修改文件中的DEVICEID、APIKEY、temp_input_id、hum_input_id,分别为设备id,设备密码,属于该设备下的温度接口和湿度接口id;
5、编译烧录;
6、进行透传设置,直接通过gokit的usb口,使用串口调试工具,向esp8266发送命令进行透传设置,可参见:ESP8266透传设置脚本,设置成功后自动连接贝壳物联平台
四、代码
  1. //STM32 RUN IN eBox
  2. #include "ebox.h"
  3. #include "cJSON.h"
  4. #include "Dht11.h"
  5. //================================
  6. String DEVICEID = "xxx";
  7. String APIKEY = "xxxxxxxxx";
  8. String temp_input_id="xxxx";
  9. String hum_input_id="xxxx";
  10. //====================================
  11. Dht11 sensor(&PB3);
  12. char uart1_rx_str[1024];
  13. int i=0;
  14. char uart2_rx_str[1024];
  15. int j=0;
  16. uint32_t last_beat_time = 0;
  17. bool checkinok = false;
  18. String checkin="{"M":"checkin","ID":""+DEVICEID+"","K":""+APIKEY+""}\n";
  19. void say(char *toID, char *content)
  20. {
  21.     uart2.printf("{"M":"say","ID":"%s","C":"%s"}\n",toID,content);
  22. }
  23. int processMessage(char *msg){
  24.     cJSON *jsonObj = cJSON_Parse(msg);
  25.     if(!jsonObj)
  26.     {
  27.         uart1.printf("json string wrong!");
  28.         return 0;
  29.     }
  30.     cJSON *method = cJSON_GetObjectItem(jsonObj, "M");
  31.     char *m = method->valuestring;
  32.     if(strncmp(m,"b",1) == 0 || strncmp(m,"WELCOME",7) == 0)
  33.     {
  34.         uart1.printf("sending checkin...\r\n");
  35.         uart2.print(checkin);
  36.     }
  37.     if(strncmp(m,"checkinok",9) == 0)
  38.     {
  39.         checkinok=true;
  40.     }
  41.     if(strncmp(m,"connected",9) == 0)
  42.     {
  43.         checkinok=false;
  44.         uart1.printf("sending checkin...\r\n");
  45.         uart2.print(checkin);
  46.     }
  47.     if(strncmp(m,"checked",7) == 0)
  48.     {
  49.         checkinok=true;
  50.     }
  51.     if(strncmp(m,"login",5) == 0)
  52.     {
  53.         char *from_id = cJSON_GetObjectItem(jsonObj, "ID")->valuestring;
  54.         uart1.printf("saying...");
  55.         char new_content[] = "Dear friend, welcome to BIGIOT !";
  56.         say(from_id,new_content);
  57.     }
  58.     if(strncmp(m,"say",3) == 0)
  59.     {
  60.         char *content = cJSON_GetObjectItem(jsonObj, "C")->valuestring;
  61.         char *from_id = cJSON_GetObjectItem(jsonObj, "ID")->valuestring;
  62.         if(strncmp(content,"play",4) == 0)
  63.         {
  64.             //do something here....
  65.             uart1.printf("saying...");
  66.             char new_content[] = "played";
  67.             say(from_id,new_content);
  68.         }
  69.         else if(strncmp(content,"stop",4) == 0)
  70.         {
  71.             //do something here....
  72.             uart1.printf("saying...");
  73.             char new_content[] = "stoped";
  74.             say(from_id,new_content);
  75.         }
  76.         else{
  77.             //do something here....
  78.             uart1.printf("saying...");
  79.             char new_content[] = "I don't know what you have said...";
  80.             say(from_id,new_content);
  81.         }
  82.     }
  83.     if(jsonObj)cJSON_Delete(jsonObj);
  84.     return 1;
  85. }
  86. void uart2_rx_event()
  87. {
  88.     uint16_t c;
  89.     c = uart2.read();
  90.     uart1.write(c);
  91.     uart2_rx_str[j]=c;
  92.     uart2_rx_str[j+1] = '\0';
  93.     if (c == '\n')
  94.     {
  95.         if(uart2_rx_str[j-1] == '}')
  96.         {
  97.             processMessage(uart2_rx_str);
  98.         }
  99.         if(strncmp(uart2_rx_str,"ERROR",5) == 0)
  100.         {
  101.             checkinok = false;
  102.         }
  103.         j=0;
  104.     }else{
  105.         j++;
  106.     }
  107. }
  108. void update_dht11()
  109. {
  110.     int temp,hum;
  111.     switch (sensor.read())
  112.     {
  113.     case Dht11::OK:
  114.         temp=sensor.getTemperature();
  115.         hum=sensor.getHumidity();
  116.         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);
  117.         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);
  118.         break;
  119.     case Dht11::ERROR_CHECKSUM:
  120.         uart1.printf("Checksum error\r\n");
  121.         break;
  122.     case Dht11::ERROR_TIMEOUT:
  123.         uart1.printf("Timeout error\r\n");
  124.         break;
  125.     default:
  126.         uart1.printf("Unknown error\r\n");
  127.         break;
  128.     }
  129. }
  130. void uart1_rx_event()
  131. {
  132.     uint16_t c;
  133.     c = uart1.read();
  134.     uart1_rx_str<i>=c;
  135.     uart1_rx_str[i+1] = '\0';
  136.     if (c == '\n')
  137.     {
  138.         uart2.printf(uart1_rx_str);
  139.         i=0;
  140.     }else if(strncmp(uart1_rx_str,"+++",3) == 0)
  141.     {
  142.         uart2.printf(uart1_rx_str);
  143.         i=0;
  144.     }else
  145.     {
  146.         i++;
  147.     }
  148. }
  149. void setup()
  150. {
  151.     ebox_init();
  152.     uart1.begin(115200);
  153.     uart2.begin(115200);
  154.     uart1.printf("Working start!\r\n");
  155.     uart1.attach(uart1_rx_event,RxIrq);
  156.     uart2.attach(uart2_rx_event,RxIrq);
  157. }
  158. int main(void)
  159. {
  160.     uint32_t last_update_time = 0;
  161.     uint32_t last_status_time = 0;
  162.     setup();
  163.     while(1)
  164.     {
  165.         if(millis() - last_update_time > 6310 && checkinok)
  166.         {
  167.             update_dht11();
  168.             last_update_time = millis();
  169.         }
  170.         if(millis() - last_status_time > 120000 || last_status_time == 0)
  171.         {
  172.             uart2.printf("{"M":"status"}\n");
  173.             last_status_time = millis();
  174.         }
  175.     }
  176. }

复制代码


原文出自:http://www.bigiot.net/info/458.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-9 21:35 , Processed in 0.033454 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表