本帖最后由 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[400];
- // 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();
- }
- }
复制代码 |