esp8266 wemos D1 wifi 板子:
USB线 插入PC对应COM5
对应的代码为:
/**
* BasicHTTPClient.ino
*
* Created on: 24.05.2015
*
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial
ESP8266WiFiMulti WiFiMulti;
void setup() {
USE_SERIAL.begin(115200);
USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("HelloWifi", "123ab");
}
void loop() {
// wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
//http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
http.begin("http://118.190.215.49:5001/order?id=123&name=XiaoMing"); //HTTP
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(10000);
}
http://118.190.215.49:5001/order?id=123&name=XiaoMing
为在 阿里云上搭建的 web服务器:使用的是 python 2.7 加 Flask,作用是将发送的数据,原样返回。
(“HelloWifi”, “123ab”) 为 能上网的 wifi路由器
log:
SDK:2.2.1(cfd48f3)/Core:2.4.1/lwIP:2.0.3(STABLE-2_0_3_RELEASE/glue:arduino-2.4.1)
[SETUP] WAIT 4...
[SETUP] WAIT 3...
[SETUP] WAIT 2...
[SETUP] WAIT 1...
scandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 3
connected with HelloWifi, channel 1
dhcp client start...
cnt
ip:192.168.0.104,mask:255.255.255.0,gw:192.168.0.1
[HTTP] begin...
[HTTP] GET...
[HTTP] GET... code: 200
{'id': u'123', 'name': u'XiaoMing'}
pm open,type:2 0
[HTTP] begin...
[HTTP] GET...
[HTTP] GET... code: 200
{'id': u'123', 'name': u'XiaoMing'}
[HTTP] begin...
[HTTP] GET...
[HTTP] GET... code: 200
{'id': u'123', 'name': u'XiaoMing'}
python 服务器代码:
Esp8266_WebServer.py
# -*- 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中的参数
# to something here
return str(data) # 注意,不管什么问题,一定要返回,就算是返回None
if __name__=='__main__':
app.debug=True
#app.run(host='127.0.0.1',port=5000)
app.run(host="172.31.120.108",port="5001")
Fiddle调试: