编译通过,但是下载程序时会超时。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.错误跳转,观看错误发生的位置
附上全套代码以及库文件
- #include <TimerOne.h>
- #include <HttpPacket.h>
- #include <ArduinoJson.h>
- #include <dht11.h>
- dht11 DHT11;
- #define DHT11PIN 8 //DHT11 连接ARDUINO 8
- HttpPacketHead packet;
- #define DebugSerial Serial
- #define ESP8266Serail Serial3
- #define Success 1U
- #define Failure 0U
- int L = 13; //LED指示灯引脚
- unsigned long Time_Cont = 0; //定时器计数器
- const unsigned int esp8266RxBufferLength = 600;
- char esp8266RxBuffer[esp8266RxBufferLength];
- unsigned int ii = 0;
- char OneNetServer[] = "api.heclouds.com"; //不需要修改
- const char ssid[] = "PushPull"; //修改为自己的路由器用户名
- const char password[] = "********"; //修改为自己的路由器密码
- char device_id[] = "600911"; //修改为自己的设备ID
- char API_KEY[] = "R9xO5NZm6oVI4YBHvCPKEqtwYtMA"; //修改为自己的API_KEY
- char sensor_id1[] = "TEMP";
- char sensor_id2[] = "HUMI";
- void setup() {
- pinMode(L, OUTPUT);
- digitalWrite(L, LOW);
- DebugSerial.begin(9600);
- ESP8266Serail.begin(115200);
- Timer1.initialize(1000);
- Timer1.attachInterrupt(Timer1_handler);
- initEsp8266();
- DebugSerial.println("setup end!");
- }
- void loop() {
- //获取温湿度数据
- int chk = DHT11.read(DHT11PIN); //读取温湿度值
- //串口调试DHT11输出信息
- DebugSerial.print("Read sensor: ");
- switch (chk)
- {
- case DHTLIB_OK:
- DebugSerial.println("OK");
- break;
- case DHTLIB_ERROR_CHECKSUM:
- DebugSerial.println("Checksum error");
- break;
- case DHTLIB_ERROR_TIMEOUT:
- DebugSerial.println("Time out error");
- break;
- default:
- DebugSerial.println("Unknown error");
- break;
- }
- //发送数据到Onenet
- postDataToOneNet(API_KEY,device_id,sensor_id1,DHT11.temperature);
- delay(100);
- postDataToOneNet(API_KEY,device_id,sensor_id2,DHT11.humidity);
- delay(5000);
- }
- void postDataToOneNet(char* API_VALUE_temp,char* device_id_temp,char* sensor_id_temp,double thisData)
- {
- //合成POST请求
- StaticJsonBuffer<200> jsonBuffer;
- JsonObject& value = jsonBuffer.createObject();
- value["value"] = thisData;
- JsonObject& id_datapoints = jsonBuffer.createObject();
- id_datapoints["id"] = sensor_id_temp;
- JsonArray& datapoints = id_datapoints.createNestedArray("datapoints");
- datapoints.add(value);
- JsonObject& myJson = jsonBuffer.createObject();
- JsonArray& datastreams = myJson.createNestedArray("datastreams");
- datastreams.add(id_datapoints);
- char p[200];
- int num = myJson.printTo(p, sizeof(p));
- packet.setHostAddress(OneNetServer);
- packet.setDevId(device_id_temp); //device_id
- packet.setAccessKey(API_VALUE_temp); //API_KEY
- // packet.setDataStreamId("<datastream_id>"); //datastream_id
- // packet.setTriggerId("<trigger_id>");
- // packet.setBinIdx("<bin_index>");
- /*create the http message about add datapoint */
- packet.createCmdPacket(POST, TYPE_DATAPOINT, p);
- // if (strlen(packet.content))
- // Serial.print(packet.content);
- // Serial.print(p);
- int httpLength = strlen(packet.content) + num;
-
- //连接服务器
- char cmd[400];
- memset(cmd, 0, 400); //清空cmd
- strcpy(cmd, "AT+CIPSTART="TCP","");
- strcat(cmd, OneNetServer);
- strcat(cmd, "",80\r\n");
- if (sendCommand(cmd, "CONNECT", 7, 10000, 5) == Success);
- else ESP8266_ERROR(1);
- //发送数据
- memset(cmd, 0, 400); //清空cmd
- sprintf(cmd, "AT+CIPSEND=%d\r\n", httpLength);
- if (sendCommand(cmd, ">", 1, 3000, 1) == Success);
- else ESP8266_ERROR(2);
- memset(cmd, 0, 400); //清空cmd
- strcpy(cmd, packet.content);
- strcat(cmd, p);
- if (sendCommand(cmd, ""succ"}", 7, 3000, 3) == Success);
- else ESP8266_ERROR(3);
- if (sendCommand("AT+CIPCLOSE\r\n", "CLOSED", 6, 3000, 1) == Success);
- else ESP8266_ERROR(4);
- }
- void initEsp8266()
- {
- if (sendCommand("AT\r\n", "OK", 2, 3000, 10) == Success);
- else ESP8266_ERROR(5);
- if (sendCommand("AT+RST\r\n", "ready", 5, 10000, 10) == Success);
- else ESP8266_ERROR(6);
- if (sendCommand("AT+CWMODE=1\r\n", "OK", 2, 3000, 10) == Success);
- else ESP8266_ERROR(7);
- char cmd[50];
- strcpy(cmd, "AT+CWJAP="");
- strcat(cmd, ssid);
- strcat(cmd, "","");
- strcat(cmd, password);
- strcat(cmd, ""\r\n");
- if (sendCommand(cmd, "OK", 2, 20000, 10) == Success);
- else ESP8266_ERROR(8);
- if (sendCommand("AT+CIPMUX=0\r\n", "OK", 2, 3000, 10) == Success);
- else ESP8266_ERROR(9);
- if (sendCommand("AT+CIFSR\r\n", "OK", 2, 20000, 10) == Success);
- else ESP8266_ERROR(10);
- }
- void(* resetFunc) (void) = 0; //制造重启命令
- void ESP8266_ERROR(int num)
- {
- DebugSerial.print("ERROR");
- DebugSerial.println(num);
- while (1)
- {
- digitalWrite(L, HIGH);
- delay(300);
- digitalWrite(L, LOW);
- delay(300);
- if (sendCommand("AT\r\n", "OK", 2, 100, 10) == Success)
- {
- DebugSerial.print("\r\nRESET!!!!!!\r\n");
- resetFunc();
- }
- }
- }
- unsigned int sendCommand(char *Command, char *Response, unsigned int Res_Length, unsigned long Timeout, unsigned char Retry)
- {
- clrEsp8266RxBuffer();
- for (unsigned char n = 0; n < Retry; n++)
- {
- DebugSerial.print("\r\nsend AT Command:\r\n----------\r\n");
- DebugSerial.write(Command);
- ESP8266Serail.write(Command);
- Time_Cont = 0;
- while (Time_Cont < Timeout)
- {
- esp8266ReadBuffer();
- if ((mystrstr(esp8266RxBuffer, Response, ii, Res_Length)) != NULL)
- {
- DebugSerial.print("\r\nreceive AT Command:\r\n==========\r\n");
- DebugSerial.print(esp8266RxBuffer); //输出接收到的信息
- clrEsp8266RxBuffer();
- return Success;
- }
- }
- Time_Cont = 0;
- }
- DebugSerial.print("\r\nreceive AT Command:\r\n==========\r\n");
- DebugSerial.print(esp8266RxBuffer);//输出接收到的信息
- clrEsp8266RxBuffer();
- return Failure;
- }
- unsigned char mystrstr(char *s, char *t, unsigned int Length_s, unsigned int Length_t)
- { char x = 0; char *p; p = t;
- int i = 0, j = 0;
- for (; i < Length_s; s++, i++)
- {
- while (*t == *s)
- { s++; t++; i++; j++;
- if (j >= Length_t) return 1;
- }
- s -= j;
- t = p; j = 0;
- }
- return NULL;
- }
- void Timer1_handler(void)
- {
- Time_Cont++;
- }
- void esp8266ReadBuffer() {
- while (ESP8266Serail.available())
- {
- esp8266RxBuffer[ii++] = ESP8266Serail.read();
- if (ii == esp8266RxBufferLength)clrEsp8266RxBuffer();
- }
- }
- void clrEsp8266RxBuffer(void)
- {
- memset(esp8266RxBuffer, 0, esp8266RxBufferLength); //清空
- ii = 0;
- }
复制代码 |