当前位置:   article > 正文

Arduino通过ESP8266模块发送数据到ONENET云服务器(HTTP)_esp8266向onenet发送数据

esp8266向onenet发送数据

1、具体操作

具体操作参考这篇文章,非常好用!但是注意最下面的postData()函数中,url中还要改成自己的设备id。

(1条消息) arduino通过esp8266模块发送数据到云服务器_arduino esp8266 上传数据_代码小白101的博客-CSDN博客

2、代码修改

由于我的传感器数据比较多(6个),所以在使用次代码时出现了上传失败的问题,于是我把6个数据拆成了3次发送,代码如下:

主要修改了updateTemp()函数、postData()函数。

  1. #include <stdlib.h>
  2. #include <SoftwareSerial.h>
  3. #define SSID "VIVO X27" //wifi名
  4. #define PASS "l123321000" //wifi密码
  5. #define IP "api.heclouds.com" // 连接thingspeak.com服务器
  6. const char OneNetServer[] = "api.heclouds.com";
  7. //String GET = "GET /update?key=ylZJctZfVULDM4NvVRfec97Axxw="; //输入前面记下的API
  8. const char APIKEY[] = "Zxc7y94ESpWDm=uu310H8CHWlME=";
  9. int32_t DeviceId = 1062145212;
  10. const char DS_ax[] = "ax";
  11. const char DS_ay[] = "ay";
  12. const char DS_az[] = "az";
  13. const char DS_press[] = "F";
  14. const char DS_temp[] = "C";
  15. const char DS_v[] = "V";
  16. SoftwareSerial ESP8266(8, 9); // 定义软串口
  17. const int tcpPort = 80;
  18. //初始化-----------------------------------------
  19. void setup()
  20. {
  21. ESP8266.begin(115200);
  22. Serial.begin(115200);
  23. ESP8266.println("AT");//指令测试
  24. delay(1000);
  25. if (ESP8266.find("OK")) //接收指令正常则返回OK
  26. {
  27. Serial.println("Wifi module connection is normal");
  28. connectWiFi();
  29. }
  30. else {
  31. Serial.println("Wifi module connection failed");
  32. }
  33. }
  34. //主循环-----------------------------------------
  35. void loop()
  36. {
  37. //Serial.println((float)DHT11.temperature);
  38. //Serial.println((float)DHT11.humidity);
  39. float ax= 111.11;
  40. float ay = 222.22;
  41. float az = 333.33;
  42. float press = 444.44;
  43. float temp = 555.55;
  44. float v = 666.66;
  45. char buffer[10];
  46. String ax_str = dtostrf(ax, 4, 2, buffer);
  47. String ay_str = dtostrf(ay, 4, 2, buffer);
  48. String az_str = dtostrf(az, 4, 2, buffer);
  49. String press_str = dtostrf(press, 4, 2, buffer);
  50. String temp_str = dtostrf(temp, 4, 2, buffer);
  51. String v_str = dtostrf(v, 4, 2, buffer);
  52. updateTemp( 1,ax_str, ay_str);
  53. updateTemp( 2,az_str, press_str);
  54. updateTemp( 3, temp_str, v_str);
  55. delay(10);
  56. }
  57. void updateTemp(int a,String data1, String data2)
  58. {
  59. String cmd = "AT+CIPSTART=\"TCP\",\"";
  60. cmd += IP;
  61. cmd += "\",80";
  62. //sendDebug(cmd); //发送指令,链接服务器
  63. ESP8266.println(cmd);
  64. delay(500);
  65. if (ESP8266.find("Error"))
  66. {
  67. Serial.print("Connection to server failed");
  68. return;
  69. }
  70. //cmd = GET + "&field1=" + temph + "&field2=" + tempt + "\r\n"; //记录T和H的值
  71. cmd = postData(DeviceId, a, data1, data2);
  72. ESP8266.print("AT+CIPSEND=");
  73. ESP8266.println(cmd.length());
  74. if (ESP8266.find(">"))
  75. {
  76. Serial.print(">");
  77. ESP8266.print(cmd);
  78. Serial.print(cmd);
  79. }
  80. else
  81. {
  82. Serial.println("Data transmission failure");
  83. }
  84. if (ESP8266.find("OK"))
  85. {
  86. Serial.println("RECEIVED: OK");
  87. ESP8266.println("AT+CIPCLOSE");
  88. }
  89. else
  90. {
  91. Serial.println("Data transmission failure");
  92. }
  93. }
  94. boolean connectWiFi()
  95. {
  96. //Serial.println("AT+CIPMUX=0");
  97. ESP8266.println("AT+CWMODE=1");
  98. ESP8266.println("AT+RST");
  99. delay(2000);
  100. String cmd = "AT+CWJAP=\"";
  101. cmd += SSID;
  102. cmd += "\",\"";
  103. cmd += PASS;
  104. cmd += "\"";
  105. ESP8266.println(cmd);
  106. delay(1000);
  107. if (ESP8266.find("OK"))
  108. {
  109. Serial.println("Wifi connection successful");
  110. return true;
  111. } else
  112. {
  113. Serial.println("Wifi connection failed");
  114. return false;
  115. }
  116. }
  117. String postData(int dId,int a, String data1, String data2)
  118. {
  119. // 创建发送请求的URL -- We now create a URI for the request
  120. String url = "/devices/1062145212";//此处修改为你的设备id
  121. //url += String(dId);
  122. url += "/datapoints?type=3";
  123. String data;
  124. if(a==1){
  125. data = "{\"" +
  126. String(DS_ax) + "\":" + data1 + ",\"" +
  127. String(DS_ay) + "\":" + data2 + "}";}
  128. else if(a==2){
  129. data = "{\"" +
  130. String(DS_az) + "\":" + data1 + ",\"" +
  131. String(DS_press) + "\":" +data2 + "}";}
  132. if(a==3){
  133. data = "{\"" +
  134. String(DS_temp) + "\":" + data1 + ",\"" +
  135. String(DS_v) + "\":" + data2 + "}";}
  136. // 创建发送指令 -- We now combine the request to the server
  137. String post_data = "POST " + url + " HTTP/1.1\r\n" +
  138. "api-key:" + APIKEY + "\r\n" +
  139. "Host:" + OneNetServer + "\r\n" +
  140. "Content-Length: " + String(data.length()) + "\r\n" + //发送数据长度
  141. "Connection: close\r\n\r\n" +
  142. data;
  143. return post_data;
  144. }

如果有错误的或者让大家有更好地方法,欢迎下方评论留言!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/699275
推荐阅读
相关标签
  

闽ICP备14008679号