下载地址:
https://www.telerik.com/download/fiddler/fiddler4
内容1:
http://api.heclouds.com/devices/35902210/datapoints?type=3
内容2:
User-Agent: Fiddler
api-key: qHi1Pa=Hr=7RSPiX6Y9PJGmk1Dc=
Host: api.heclouds.com
Content-Length: 11
Request Body:
{"temp":77}
内容如下:
POST http://api.heclouds.com/devices/35902210/datapoints?type=3 HTTP/1.1
User-Agent: Fiddler
api-key: qHi1Pa=Hr=7RSPiX6Y9PJGmk1Dc=
Host: api.heclouds.com
Content-Length: 11
{"temp":66}
接收到的信息:
HTTP/1.1 200 OK
Date: Tue, 17 Jul 2018 03:05:39 GMT
Content-Type: application/json
Content-Length: 26
Connection: keep-alive
Server: Apache-Coyote/1.1
Pragma: no-cache
{"errno":0,"error":"succ"}
数据查看 :
https://open.iot.10086.cn/device/data?pid=156176&did=35902210
在线调试:
https://open.iot.10086.cn/datasm?pid=156176
选择TCP Client
Server IP:183.230.40.34
Server Port: 80
arduino中的代码:
/*
* This sketch sends data via HTTP GET requests to data.sparkfun.com service.
*
* You need to get streamId and privateKey at data.sparkfun.com and paste them
* below. Or just customize this script to talk to other HTTP servers.
*
*/
#include <ESP8266WiFi.h>
const char* ssid = "HelloWifi";
const char* password = "123ab";
const char* host = "183.230.40.33";
char device_id[] = "35902210";
char API_KEY[] = "qHi1Pa=Hr=7RSPiX6Y9PJGmk1Dc=";
char sensor[] = "temp";
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/devices/";
url += device_id;
url += "/datapoints?type=3 HTTP/1.1\r\n";
url += "api-key:";
url +=API_KEY;
url +="\r\n";
url +="Host:";
url +=host;
url +="\r\n";
url +="Content-Length:";
url +="14";
url +="\r\n\r\n";
url +="{\"temp\":56.25}";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("POST ") + url);
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
log:
Connecting to HelloWifi
scandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 2
cnt
connected with HelloWifi, channel 4
dhcp client start...
ip:192.168.88.94,mask:255.255.255.0,gw:192.168.88.1
.
WiFi connected
IP address:
192.168.88.94
connecting to 183.230.40.33
Requesting URL: /devices/35902210/datapoints?type=3 HTTP/1.1
api-key:qHi1Pa=Hr=7RSPiX6Y9PJGmk1Dc=
Host:183.230.40.33
Content-Length:14
{"temp":56.25}
HTTP/1.1 200 OK
Date: Tue, 17 Jul 2018 06:51:56 GMT
Content-Type: application/json
Content-Length: 26
Connection: keep-alive
Server: Apache-Coyote/1.1
Pragma: no-cache
pm open,type:2 0
{"errno":0,"error":"succ"}
closing connection
研究一下:
char sensor_temp[] = "temp";
char text[100] = {
0 };
char tmp[25] = {
0 };
double data_value = 5.2 ;
char value_str[15] = {
0 };
dtostrf(data_value, 3, 2, value_str);
sprintf(tmp, "Content-Length:%d\r\n\r\n", strlen(text));
strcat
strstr
sprintf(text, "{\"%s\":86.25}", sensor_temp);
另外一种格式:
发送给服务器:
POST http://api.heclouds.com/devices/35902210/datapoints HTTP/1.1
api-key: qHi1Pa=Hr=7RSPiX6Y9PJGmk1Dc=
Host: api.heclouds.com
Connection: close
Content-Type: application/json
Content-Length: 59
{"datastreams":[{"id":"temp","datapoints":[{"value":50}]}]}
返回的数据
HTTP/1.1 200 OK
Date: Wed, 25 Jul 2018 12:49:58 GMT
Content-Type: application/json
Content-Length: 26
Connection: close
Server: Apache-Coyote/1.1
Pragma: no-cache
{"errno":0,"error":"succ"}
本地调试
python 代码:
# -*- coding:utf-8 -*-
from flask import Flask
from flask import request
import datetime
import time
app=Flask(__name__)
@app.route('/')
def index():
"""index.html主页返回"""
return 'I have receive you request !'
@app.route('/order', methods=['GET', 'POST', 'DELETE'])
def order():
"""/order.html接口,接收get请求,解析url中的参数"""
print(request.url) # 请求的http网址
data = request.args.to_dict() # 解析http中的参数
print request.headers
print request.data
return str(data) # 注意,不管什么问题,一定要返回,就算是返回None
if __name__=='__main__':
app.debug=True
app.run(host='127.0.0.1',port=5000)
Fiddle发送数据:
POST http://127.0.0.1:5000/order HTTP/1.1
api-key: qHi1Pa=Hr=7RSPiX6Y9PJGmk1Dc=
Connection:close
Content-Type: application/json
Content-Length:59
{"datastreams":[{"id":"temp","datapoints":[{"value":50}]}]}
打印的消息:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
http://127.0.0.1:5000/order
Content-Length: 59
Connection: close
Api-Key: qHi1Pa=Hr=7RSPiX6Y9PJGmk1Dc=
Host: 127.0.0.1:5000
Content-Type: application/json
{"datastreams":[{"id":"temp","datapoints":[{"value":50}]}]}