极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 21919|回复: 6

mega2560+esp8266+dht11遇到烧录程序超时

[复制链接]
发表于 2017-5-13 12:57:17 | 显示全部楼层 |阅读模式
编译通过,但是下载程序时会超时。IDE换了好几个版本的也不行,确定板子和串口是对的,io口全部没接也不能下载,其他程序可以正常下载。如果把这个程序中的#define DebugSerial Serial 相关代码注释掉,也能正常下载,不知道怎么回事


就是一个mega2560+dht11+esp8266,读取温湿度数据上传到onenet
所需库文件链接:http://pan.baidu.com/s/1miJEIFI 密码:y8ks


ESP8266 ---> Serial3(TX3/RX3) 用户发送和读取WIFI指令
DHT11 ---> D8 单总线读取温湿度数据值
DEBUG接口---> Serial(D0/D1)
1.把发送给ESP8266的数据,也在DEBUG串口输出,观看指令运行到哪一步,
2.ESP8266接收的数据,也通过DEBUG口转发出来,观看反馈数据是否正确。
3.错误跳转,观看错误发生的位置

附上全套代码以及库文件

  1. #include <TimerOne.h>
  2. #include <HttpPacket.h>
  3. #include <ArduinoJson.h>
  4. #include <dht11.h>
  5. dht11 DHT11;
  6. #define DHT11PIN 8 //DHT11 连接ARDUINO 8


  7. HttpPacketHead packet;


  8. #define DebugSerial Serial
  9. #define ESP8266Serail Serial3

  10. #define Success 1U
  11. #define Failure 0U

  12. int L = 13; //LED指示灯引脚

  13. unsigned long  Time_Cont = 0;       //定时器计数器

  14. const unsigned int esp8266RxBufferLength = 600;
  15. char esp8266RxBuffer[esp8266RxBufferLength];
  16. unsigned int ii = 0;
  17. char OneNetServer[] = "api.heclouds.com";       //不需要修改

  18. const char ssid[] = "PushPull";     //修改为自己的路由器用户名
  19. const char password[] = "********"; //修改为自己的路由器密码



  20. char device_id[] = "600911";    //修改为自己的设备ID
  21. char API_KEY[] = "R9xO5NZm6oVI4YBHvCPKEqtwYtMA";    //修改为自己的API_KEY
  22. char sensor_id1[] = "TEMP";
  23. char sensor_id2[] = "HUMI";



  24. void setup() {
  25.     pinMode(L, OUTPUT);
  26.     digitalWrite(L, LOW);
  27.     DebugSerial.begin(9600);
  28.     ESP8266Serail.begin(115200);

  29.     Timer1.initialize(1000);
  30.     Timer1.attachInterrupt(Timer1_handler);

  31.     initEsp8266();


  32.     DebugSerial.println("setup end!");
  33. }

  34. void loop() {
  35.     //获取温湿度数据
  36.     int chk = DHT11.read(DHT11PIN);                             //读取温湿度值

  37.       //串口调试DHT11输出信息
  38.       DebugSerial.print("Read sensor: ");
  39.       switch (chk)
  40.       {
  41.       case DHTLIB_OK:
  42.         DebugSerial.println("OK");
  43.         break;
  44.       case DHTLIB_ERROR_CHECKSUM:
  45.         DebugSerial.println("Checksum error");
  46.         break;
  47.       case DHTLIB_ERROR_TIMEOUT:
  48.         DebugSerial.println("Time out error");
  49.         break;
  50.       default:
  51.         DebugSerial.println("Unknown error");
  52.         break;
  53.       }

  54.       //发送数据到Onenet
  55.       postDataToOneNet(API_KEY,device_id,sensor_id1,DHT11.temperature);
  56.           delay(100);      
  57.       postDataToOneNet(API_KEY,device_id,sensor_id2,DHT11.humidity);

  58.       delay(5000);
  59. }

  60. void postDataToOneNet(char* API_VALUE_temp,char* device_id_temp,char* sensor_id_temp,double thisData)
  61. {
  62.         //合成POST请求
  63.     StaticJsonBuffer<200> jsonBuffer;



  64.     JsonObject& value = jsonBuffer.createObject();
  65.     value["value"] = thisData;

  66.     JsonObject& id_datapoints = jsonBuffer.createObject();
  67.     id_datapoints["id"] = sensor_id_temp;
  68.     JsonArray& datapoints = id_datapoints.createNestedArray("datapoints");
  69.     datapoints.add(value);

  70.     JsonObject& myJson = jsonBuffer.createObject();
  71.     JsonArray& datastreams = myJson.createNestedArray("datastreams");
  72.     datastreams.add(id_datapoints);

  73.     char p[200];
  74.     int num = myJson.printTo(p, sizeof(p));


  75.     packet.setHostAddress(OneNetServer);
  76.     packet.setDevId(device_id_temp);   //device_id
  77.     packet.setAccessKey(API_VALUE_temp);  //API_KEY
  78.     // packet.setDataStreamId("<datastream_id>");    //datastream_id
  79.     // packet.setTriggerId("<trigger_id>");
  80.     // packet.setBinIdx("<bin_index>");

  81.     /*create the http message about add datapoint */
  82.     packet.createCmdPacket(POST, TYPE_DATAPOINT, p);
  83.     // if (strlen(packet.content))
  84.     //  Serial.print(packet.content);
  85.     // Serial.print(p);
  86.     int httpLength = strlen(packet.content) + num;

  87.    

  88.         //连接服务器
  89.     char cmd[400];
  90.     memset(cmd, 0, 400);    //清空cmd
  91.     strcpy(cmd, "AT+CIPSTART="TCP","");
  92.     strcat(cmd, OneNetServer);
  93.     strcat(cmd, "",80\r\n");
  94.     if (sendCommand(cmd, "CONNECT", 7, 10000, 5) == Success);
  95.     else ESP8266_ERROR(1);

  96.     //发送数据
  97.     memset(cmd, 0, 400);    //清空cmd
  98.     sprintf(cmd, "AT+CIPSEND=%d\r\n", httpLength);
  99.     if (sendCommand(cmd, ">", 1, 3000, 1) == Success);
  100.     else ESP8266_ERROR(2);

  101.     memset(cmd, 0, 400);    //清空cmd
  102.     strcpy(cmd, packet.content);
  103.     strcat(cmd, p);
  104.     if (sendCommand(cmd, ""succ"}", 7, 3000, 3) == Success);
  105.     else ESP8266_ERROR(3);

  106.     if (sendCommand("AT+CIPCLOSE\r\n", "CLOSED", 6, 3000, 1) == Success);
  107.     else ESP8266_ERROR(4);
  108. }

  109. void initEsp8266()
  110. {
  111.     if (sendCommand("AT\r\n", "OK", 2, 3000, 10) == Success);
  112.     else ESP8266_ERROR(5);

  113.     if (sendCommand("AT+RST\r\n", "ready", 5, 10000, 10) == Success);
  114.     else ESP8266_ERROR(6);

  115.     if (sendCommand("AT+CWMODE=1\r\n", "OK", 2, 3000, 10) == Success);
  116.     else ESP8266_ERROR(7);

  117.     char cmd[50];
  118.     strcpy(cmd, "AT+CWJAP="");
  119.     strcat(cmd, ssid);
  120.     strcat(cmd, "","");
  121.     strcat(cmd, password);
  122.     strcat(cmd, ""\r\n");

  123.     if (sendCommand(cmd, "OK", 2, 20000, 10) == Success);
  124.     else ESP8266_ERROR(8);

  125.     if (sendCommand("AT+CIPMUX=0\r\n", "OK", 2, 3000, 10) == Success);
  126.     else ESP8266_ERROR(9);

  127.     if (sendCommand("AT+CIFSR\r\n", "OK", 2, 20000, 10) == Success);
  128.     else ESP8266_ERROR(10);
  129. }

  130. void(* resetFunc) (void) = 0; //制造重启命令

  131. void ESP8266_ERROR(int num)
  132. {
  133.     DebugSerial.print("ERROR");
  134.     DebugSerial.println(num);
  135.     while (1)
  136.     {
  137.         digitalWrite(L, HIGH);
  138.         delay(300);
  139.         digitalWrite(L, LOW);
  140.         delay(300);

  141.         if (sendCommand("AT\r\n", "OK", 2, 100, 10) == Success)
  142.         {
  143.             DebugSerial.print("\r\nRESET!!!!!!\r\n");
  144.             resetFunc();
  145.         }
  146.     }
  147. }



  148. unsigned int sendCommand(char *Command, char *Response, unsigned int Res_Length, unsigned long Timeout, unsigned char Retry)
  149. {
  150.     clrEsp8266RxBuffer();
  151.     for (unsigned char n = 0; n < Retry; n++)
  152.     {
  153.         DebugSerial.print("\r\nsend AT Command:\r\n----------\r\n");
  154.         DebugSerial.write(Command);

  155.         ESP8266Serail.write(Command);

  156.         Time_Cont = 0;
  157.         while (Time_Cont < Timeout)
  158.         {
  159.             esp8266ReadBuffer();
  160.             if ((mystrstr(esp8266RxBuffer, Response, ii, Res_Length)) != NULL)
  161.             {
  162.                 DebugSerial.print("\r\nreceive AT Command:\r\n==========\r\n");
  163.                 DebugSerial.print(esp8266RxBuffer); //输出接收到的信息
  164.                 clrEsp8266RxBuffer();
  165.                 return Success;
  166.             }
  167.         }
  168.         Time_Cont = 0;
  169.     }
  170.     DebugSerial.print("\r\nreceive AT Command:\r\n==========\r\n");
  171.     DebugSerial.print(esp8266RxBuffer);//输出接收到的信息
  172.     clrEsp8266RxBuffer();
  173.     return Failure;
  174. }

  175. unsigned char mystrstr(char *s, char *t, unsigned int Length_s, unsigned int Length_t)
  176. {   char x = 0; char *p; p = t;
  177.     int i = 0, j = 0;
  178.     for (; i < Length_s; s++, i++)
  179.     {
  180.         while (*t == *s)
  181.         {   s++; t++; i++; j++;
  182.             if (j >= Length_t) return 1;
  183.         }
  184.         s -= j;
  185.         t = p; j = 0;
  186.     }
  187.     return NULL;
  188. }


  189. void Timer1_handler(void)
  190. {
  191.     Time_Cont++;
  192. }



  193. void esp8266ReadBuffer() {
  194.     while (ESP8266Serail.available())
  195.     {
  196.         esp8266RxBuffer[ii++] = ESP8266Serail.read();
  197.         if (ii == esp8266RxBufferLength)clrEsp8266RxBuffer();
  198.     }
  199. }

  200. void clrEsp8266RxBuffer(void)
  201. {
  202.     memset(esp8266RxBuffer, 0, esp8266RxBufferLength);      //清空
  203.     ii = 0;
  204. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2017-5-16 00:17:12 | 显示全部楼层
你板子插一下电脑看会不会自动复位,如果不会可能是丢Bootloader了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-19 19:22:58 | 显示全部楼层
赤云 发表于 2017-5-16 00:17
你板子插一下电脑看会不会自动复位,如果不会可能是丢Bootloader了

奇怪的是,烧其他程序一切正常,就烧这个程序会超时。这也是丢bootloader?
回复 支持 反对

使用道具 举报

发表于 2017-5-19 21:58:32 | 显示全部楼层
再见依然13 发表于 2017-5-19 19:22
奇怪的是,烧其他程序一切正常,就烧这个程序会超时。这也是丢bootloader?

那应该不是BootLoader的问题了,我一般遇到下不了程序,要么就BootLoader,要么就arduino串口连其他东西了,现在看来都不是额
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-24 00:46:47 | 显示全部楼层
赤云 发表于 2017-5-19 21:58
那应该不是BootLoader的问题了,我一般遇到下不了程序,要么就BootLoader,要么就arduino串口连其他东西 ...

是的,很奇怪,尴尬了,这个程序注释掉debug就能下载,但是程序还有问题,就是两个小时后数据就传不了, 得重启arduino才行,目前就是想看debug信息看不了
回复 支持 反对

使用道具 举报

发表于 2017-6-9 08:35:03 | 显示全部楼层
可能是!!!问题导致,,,这句 DebugSerial.print("\r\nRESET!!!!!!\r\n");  中含有!!!,,
回复 支持 反对

使用道具 举报

发表于 2017-6-9 09:15:07 | 显示全部楼层
tx  rx 有个口被占用!导致的!
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 19:23 , Processed in 0.043134 second(s), 22 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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