ESP8266上传乐联网失败问题
本帖最后由 qpanda 于 2015-8-11 00:42 编辑先说明问题,我用ESP8266+UNO上传温度数据到乐联网,一直出现怪异错误。
在代码前面我已经合成了需要post的值,但是在合成cmd的时候,竟然没有显示出来。麻烦大家看下代码
[{"Name":"Ft1","Value":"28"}]
29
POST /api/v1/gateway/updatesensors/02 HTTP/1.1
Host: www.lewei50.com
Accept: */*
U-ApiKey: 25b0f5c31a3e4c3491b5a73314d439ed
Content-Length: //此次无法显示post值大小
Connection: close
//此次无法显示post值
#include "uartWIFI.h"
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// for lewei50 api
#define APIKEY "25b0f5c31××××××××××××8888" // replace your yeelink api key here
//replace the device ID and sensor ID for temperature sensor.
#define DEVICEID0 "02" // replace your device ID
#define SENSORID0 "Ft1" // replace your sensor ID
char server[] = "www.lewei50.com"; // name address for lewei50 API
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 1 * 1000; // delay between 2 datapoints, 5s
String returnValue = "";
boolean ResponseBegin = false;
#define ONE_WIRE_BUS 12 //Connect 12 to data pin of DS18B20
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int tempVal = 0;
#define SSID "QWLan"
#define PASSWORD "a1b2c3d4e5"
WIFI wifi;
void setup(){
wifi.begin();
bool b = wifi.Initialize(STA, SSID, PASSWORD);
if (!b)
{
DebugSerial.println("Init error");
}
delay(8000);//make sure the module can have enough time to get an IP address
String ipstring= wifi.showIP();
//DebugSerial.println("My IP address:");
DebugSerial.println(ipstring); //show the ip address of module
String wifistring= wifi.showJAP();
DebugSerial.println(wifistring); //show the name of current wifi access port
}
void loop() {
char message;
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if ((millis() - lastConnectionTime > postingInterval)) {
sensors.requestTemperatures();
tempVal = sensors.getTempCByIndex(0);
DebugSerial.print("Temp C: ");
DebugSerial.println(tempVal);
sendData(APIKEY, DEVICEID0, SENSORID0, tempVal);
}
if (wifi.ReceiveMessage(message)){
DebugSerial.println(message);
}
delay(10);
}
// this method makes a HTTP connection to the server:
void sendData(String apikey,String device_id, String sensor_id, int sensor_val) {
String postVal;
postVal += "[{\"";
postVal += "Name\":\"";
postVal += sensor_id;
postVal += "\",";
postVal += "\"Value\":\"";
postVal += String(sensor_val);
postVal += "\"}]";
DebugSerial.println(postVal);
int postValLen = postVal.length();
DebugSerial.println(postValLen);
DebugSerial.println();
String cmd;
cmd = "POST /api/v1/gateway/updatesensors/";
cmd += device_id;
cmd += " HTTP/1.1\r\n";
cmd += "Host: www.lewei50.com\r\n";
cmd += "Accept: */* \r\n";
cmd += "U-ApiKey: ";
cmd += apikey;
cmd += "\r\n";
cmd += "Content-Length: ";
cmd += String(postValLen); //此次无法显示post值大小
cmd += "\r\n";
cmd += "Content-Type: application/x-www-form-urlencoded\r\n";
cmd += "Connection: close\r\n";
cmd += "\r\n";
cmd += postVal; //此次无法显示post值
cmd += "\r\n";
/*
DebugSerial.println(postVal);
DebugSerial.println(postValLen);
DebugSerial.println();
*/
// if there's a successful connection:
if (wifi.ipConfig(TCP, server, 80)) {
DebugSerial.println("connecting...");
DebugSerial.println(cmd);
wifi.Send(cmd);
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
DebugSerial.println("connection failed");
DebugSerial.println("disconnecting.");
wifi.closeMux();
}
} 这个问题我也遇到过,uartWifi这个库写的不够好,用在328芯片上基本跑不起来。我改了,等有时间整理了共享出来。目前你那个问题主要因为如下两个原因:
50.void loop() {
51.char message;
51行那个message改小一些,建议改成200。328的ram有限,一共只有2k,你这里就用去很多,导致loop中嵌套的函数调用没有更多栈空间。
116. wifi.Send(cmd);
116行改成指针,使用对象做为参数,执行时会深拷贝依次,导致栈空间不够,cmd对象无法传递给wifi.Send函数。 zhb1190 发表于 2015-8-11 09:05 static/image/common/back.gif
这个问题我也遇到过,uartWifi这个库写的不够好,用在328芯片上基本跑不起来。我改了,等有时间整理了共享出 ...
我用ESP8266和UNO也觉得不稳定,结果放弃了。如果有好的库期待中。
PS。我是上Yeelink 谢谢,我也发现,ESP8266和UNO非常不稳定,经常出现莫名其妙的问题 用W5100正常 qpanda 发表于 2015-8-11 11:29 static/image/common/back.gif
谢谢,我也发现,ESP8266和UNO非常不稳定,经常出现莫名其妙的问题
8266是最新版的吗?最近更新固件和AT指令很频繁,uno还没试过,2560今天调了一下午,稳定倒是很稳定,就是总有很怪的问题,一开始初始化的时候不是很靠谱。我没有用uartWIFI这个库,我是自己写了一个函数,但是问题总是不很稳定。每一次不一定能够顺利初始化。我主要是用UDP透传,但是一旦进入了透传模式,8266就工作的比较稳定。现在还是很头疼。 昨天晚上已经把问题都解决了,传数也成功了,过几天把bug改完吧项目发上来 求分享改完的项目 qpanda 发表于 2015-8-12 09:34 static/image/common/back.gif
昨天晚上已经把问题都解决了,传数也成功了,过几天把bug改完吧项目发上来
盼着你的成果,分享一下,谢谢了!
页:
[1]