极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 19585|回复: 8

ESP8266上传乐联网失败问题

[复制链接]
发表于 2015-8-11 00:41:24 | 显示全部楼层 |阅读模式
本帖最后由 qpanda 于 2015-8-11 00:42 编辑

先说明问题,我用ESP8266+UNO上传温度数据到乐联网,一直出现怪异错误。
在代码前面我已经合成了需要post的值,但是在合成cmd的时候,竟然没有显示出来。麻烦大家看下代码


  1. [{"Name":"Ft1","Value":"28"}]
  2. 29

  3. POST /api/v1/gateway/updatesensors/02 HTTP/1.1
  4. Host: www.lewei50.com
  5. Accept: */*
  6. U-ApiKey: 25b0f5c31a3e4c3491b5a73314d439ed
  7. Content-Length:           //此次无法显示post值大小
  8. Connection: close

  9.                                      //此次无法显示post值
复制代码

  1. #include "uartWIFI.h"
  2. #include <SoftwareSerial.h>
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>

  5. // for lewei50 api
  6. #define APIKEY         "25b0f5c31××××××××××××8888" // replace your yeelink api key here

  7. //replace the device ID and sensor ID for temperature sensor.
  8. #define DEVICEID0 "02" // replace your device ID
  9. #define SENSORID0 "Ft1" // replace your sensor ID

  10. char server[] = "www.lewei50.com";   // name address for lewei50 API

  11. unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
  12. boolean lastConnected = false;                 // state of the connection last time through the main loop
  13. const unsigned long postingInterval = 1 * 1000; // delay between 2 datapoints, 5s
  14. String returnValue = "";
  15. boolean ResponseBegin = false;

  16. #define ONE_WIRE_BUS 12                      //Connect 12 to data pin of DS18B20

  17. OneWire oneWire(ONE_WIRE_BUS);
  18. DallasTemperature sensors(&oneWire);

  19. int tempVal = 0;

  20. #define SSID       "QWLan"
  21. #define PASSWORD   "a1b2c3d4e5"

  22. WIFI wifi;

  23. void setup(){
  24.   
  25.   wifi.begin();
  26.   bool b = wifi.Initialize(STA, SSID, PASSWORD);
  27.   if (!b)
  28.   {
  29.     DebugSerial.println("Init error");
  30.   }
  31.   delay(8000);  //make sure the module can have enough time to get an IP address
  32.   String ipstring  = wifi.showIP();
  33.   //DebugSerial.println("My IP address:");
  34.   DebugSerial.println(ipstring);                //show the ip address of module

  35.   String wifistring  = wifi.showJAP();
  36.   DebugSerial.println(wifistring);          //show the name of current wifi access port
  37. }

  38. void loop() {
  39.   char message[400];
  40.   // if you're not connected, and ten seconds have passed since
  41.   // your last connection, then connect again and send data:
  42.   if ((millis() - lastConnectionTime > postingInterval)) {

  43.       sensors.requestTemperatures();
  44.       tempVal = sensors.getTempCByIndex(0);
  45.       DebugSerial.print("Temp C: ");
  46.       DebugSerial.println(tempVal);
  47.       
  48.       sendData(APIKEY, DEVICEID0, SENSORID0, tempVal);
  49.   }

  50.   if (wifi.ReceiveMessage(message)){
  51.     DebugSerial.println(message);
  52.   }

  53.   delay(10);
  54. }

  55. // this method makes a HTTP connection to the server:
  56. void sendData(String apikey,String device_id, String sensor_id, int sensor_val) {
  57.   
  58.     String postVal;
  59.     postVal += "[{"";
  60.     postVal += "Name":"";
  61.     postVal += sensor_id;
  62.     postVal += "",";
  63.     postVal += ""Value":"";
  64.     postVal += String(sensor_val);
  65.     postVal += ""}]";
  66.    
  67.     DebugSerial.println(postVal);
  68.     int postValLen = postVal.length();
  69.     DebugSerial.println(postValLen);   
  70.     DebugSerial.println();
  71.   
  72.     String cmd;
  73.     cmd = "POST /api/v1/gateway/updatesensors/";
  74.     cmd += device_id;
  75.     cmd += " HTTP/1.1\r\n";
  76.     cmd += "Host: www.lewei50.com\r\n";
  77.     cmd += "Accept: */* \r\n";
  78.     cmd += "U-ApiKey: ";
  79.     cmd += apikey;
  80.     cmd += "\r\n";
  81.     cmd += "Content-Length: ";
  82.     cmd += String(postValLen);          //此次无法显示post值大小
  83.     cmd += "\r\n";
  84.     cmd += "Content-Type: application/x-www-form-urlencoded\r\n";
  85.     cmd += "Connection: close\r\n";
  86.     cmd += "\r\n";
  87.     cmd += postVal;      //此次无法显示post值
  88.     cmd += "\r\n";
  89.    
  90.     /*
  91.     DebugSerial.println(postVal);
  92.     DebugSerial.println(postValLen);
  93.     DebugSerial.println();
  94.     */
  95.   
  96.   // if there's a successful connection:
  97.   if (wifi.ipConfig(TCP, server, 80)) {
  98.     DebugSerial.println("connecting...");
  99.     DebugSerial.println(cmd);
  100.     wifi.Send(cmd);
  101.     // note the time that the connection was made:
  102.     lastConnectionTime = millis();
  103.   }
  104.   else {
  105.     // if you couldn't make a connection:
  106.     DebugSerial.println("connection failed");
  107.     DebugSerial.println("disconnecting.");
  108.     wifi.closeMux();
  109.   }
  110. }
复制代码
回复

使用道具 举报

发表于 2015-8-11 09:05:06 | 显示全部楼层
这个问题我也遇到过,uartWifi这个库写的不够好,用在328芯片上基本跑不起来。我改了,等有时间整理了共享出来。目前你那个问题主要因为如下两个原因:

50.void loop() {
51.  char message[400];


51行那个message[400]改小一些,建议改成200。328的ram有限,一共只有2k,你这里就用去很多,导致loop中嵌套的函数调用没有更多栈空间。

116. wifi.Send(cmd);
116行改成指针,使用对象做为参数,执行时会深拷贝依次,导致栈空间不够,cmd对象无法传递给wifi.Send函数。
回复 支持 反对

使用道具 举报

发表于 2015-8-11 10:43:19 | 显示全部楼层
zhb1190 发表于 2015-8-11 09:05
这个问题我也遇到过,uartWifi这个库写的不够好,用在328芯片上基本跑不起来。我改了,等有时间整理了共享出 ...

我用ESP8266和UNO也觉得不稳定,结果放弃了。如果有好的库期待中。
PS。我是上Yeelink
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-11 11:29:48 | 显示全部楼层
谢谢,我也发现,ESP8266和UNO非常不稳定,经常出现莫名其妙的问题
回复 支持 反对

使用道具 举报

发表于 2015-8-11 21:58:38 | 显示全部楼层
                  用W5100正常
回复 支持 反对

使用道具 举报

发表于 2015-8-11 23:11:31 | 显示全部楼层
qpanda 发表于 2015-8-11 11:29
谢谢,我也发现,ESP8266和UNO非常不稳定,经常出现莫名其妙的问题

8266是最新版的吗?最近更新固件和AT指令很频繁,uno还没试过,2560今天调了一下午,稳定倒是很稳定,就是总有很怪的问题,一开始初始化的时候不是很靠谱。我没有用uartWIFI这个库,我是自己写了一个函数,但是问题总是不很稳定。每一次不一定能够顺利初始化。我主要是用UDP透传,但是一旦进入了透传模式,8266就工作的比较稳定。现在还是很头疼。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-8-12 09:34:06 | 显示全部楼层
昨天晚上已经把问题都解决了,传数也成功了,过几天把bug改完吧项目发上来
回复 支持 反对

使用道具 举报

发表于 2015-11-18 13:42:50 | 显示全部楼层
求分享改完的项目
回复 支持 反对

使用道具 举报

发表于 2015-11-19 10:57:56 | 显示全部楼层
qpanda 发表于 2015-8-12 09:34
昨天晚上已经把问题都解决了,传数也成功了,过几天把bug改完吧项目发上来

盼着你的成果,分享一下,谢谢了!
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 23:19 , Processed in 0.060943 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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