赞
踩
具体操作参考这篇文章,非常好用!但是注意最下面的postData()函数中,url中还要改成自己的设备id。
(1条消息) arduino通过esp8266模块发送数据到云服务器_arduino esp8266 上传数据_代码小白101的博客-CSDN博客
由于我的传感器数据比较多(6个),所以在使用次代码时出现了上传失败的问题,于是我把6个数据拆成了3次发送,代码如下:
主要修改了updateTemp()函数、postData()函数。
- #include <stdlib.h>
- #include <SoftwareSerial.h>
-
- #define SSID "VIVO X27" //wifi名
- #define PASS "l123321000" //wifi密码
- #define IP "api.heclouds.com" // 连接thingspeak.com服务器
-
- const char OneNetServer[] = "api.heclouds.com";
- //String GET = "GET /update?key=ylZJctZfVULDM4NvVRfec97Axxw="; //输入前面记下的API
- const char APIKEY[] = "Zxc7y94ESpWDm=uu310H8CHWlME=";
- int32_t DeviceId = 1062145212;
- const char DS_ax[] = "ax";
- const char DS_ay[] = "ay";
- const char DS_az[] = "az";
- const char DS_press[] = "F";
- const char DS_temp[] = "C";
- const char DS_v[] = "V";
- SoftwareSerial ESP8266(8, 9); // 定义软串口
- const int tcpPort = 80;
- //初始化-----------------------------------------
- void setup()
- {
- ESP8266.begin(115200);
- Serial.begin(115200);
- ESP8266.println("AT");//指令测试
- delay(1000);
- if (ESP8266.find("OK")) //接收指令正常则返回OK
- {
- Serial.println("Wifi module connection is normal");
- connectWiFi();
- }
- else {
- Serial.println("Wifi module connection failed");
- }
- }
-
- //主循环-----------------------------------------
- void loop()
- {
- //Serial.println((float)DHT11.temperature);
- //Serial.println((float)DHT11.humidity);
- float ax= 111.11;
- float ay = 222.22;
- float az = 333.33;
- float press = 444.44;
- float temp = 555.55;
- float v = 666.66;
- char buffer[10];
- String ax_str = dtostrf(ax, 4, 2, buffer);
- String ay_str = dtostrf(ay, 4, 2, buffer);
- String az_str = dtostrf(az, 4, 2, buffer);
- String press_str = dtostrf(press, 4, 2, buffer);
- String temp_str = dtostrf(temp, 4, 2, buffer);
- String v_str = dtostrf(v, 4, 2, buffer);
- updateTemp( 1,ax_str, ay_str);
- updateTemp( 2,az_str, press_str);
- updateTemp( 3, temp_str, v_str);
- delay(10);
- }
-
-
- void updateTemp(int a,String data1, String data2)
- {
- String cmd = "AT+CIPSTART=\"TCP\",\"";
- cmd += IP;
- cmd += "\",80";
- //sendDebug(cmd); //发送指令,链接服务器
- ESP8266.println(cmd);
- delay(500);
- if (ESP8266.find("Error"))
- {
- Serial.print("Connection to server failed");
- return;
- }
- //cmd = GET + "&field1=" + temph + "&field2=" + tempt + "\r\n"; //记录T和H的值
- cmd = postData(DeviceId, a, data1, data2);
- ESP8266.print("AT+CIPSEND=");
- ESP8266.println(cmd.length());
- if (ESP8266.find(">"))
- {
- Serial.print(">");
- ESP8266.print(cmd);
- Serial.print(cmd);
- }
- else
- {
- Serial.println("Data transmission failure");
- }
- if (ESP8266.find("OK"))
- {
- Serial.println("RECEIVED: OK");
- ESP8266.println("AT+CIPCLOSE");
- }
- else
- {
- Serial.println("Data transmission failure");
- }
- }
-
- boolean connectWiFi()
- {
- //Serial.println("AT+CIPMUX=0");
- ESP8266.println("AT+CWMODE=1");
- ESP8266.println("AT+RST");
- delay(2000);
- String cmd = "AT+CWJAP=\"";
- cmd += SSID;
- cmd += "\",\"";
- cmd += PASS;
- cmd += "\"";
- ESP8266.println(cmd);
- delay(1000);
- if (ESP8266.find("OK"))
- {
- Serial.println("Wifi connection successful");
- return true;
- } else
- {
- Serial.println("Wifi connection failed");
- return false;
- }
- }
-
- String postData(int dId,int a, String data1, String data2)
- {
- // 创建发送请求的URL -- We now create a URI for the request
- String url = "/devices/1062145212";//此处修改为你的设备id
- //url += String(dId);
- url += "/datapoints?type=3";
- String data;
- if(a==1){
- data = "{\"" +
- String(DS_ax) + "\":" + data1 + ",\"" +
- String(DS_ay) + "\":" + data2 + "}";}
- else if(a==2){
- data = "{\"" +
- String(DS_az) + "\":" + data1 + ",\"" +
- String(DS_press) + "\":" +data2 + "}";}
- if(a==3){
- data = "{\"" +
- String(DS_temp) + "\":" + data1 + ",\"" +
- String(DS_v) + "\":" + data2 + "}";}
-
- // 创建发送指令 -- We now combine the request to the server
- String post_data = "POST " + url + " HTTP/1.1\r\n" +
- "api-key:" + APIKEY + "\r\n" +
- "Host:" + OneNetServer + "\r\n" +
- "Content-Length: " + String(data.length()) + "\r\n" + //发送数据长度
- "Connection: close\r\n\r\n" +
- data;
- return post_data;
- }
如果有错误的或者让大家有更好地方法,欢迎下方评论留言!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。