天天看點

esp8266 arduino 發送資料 到 遠端 阿裡雲 web伺服器

esp8266 wemos D1 wifi 闆子:

USB線 插入PC對應COM5

esp8266 arduino 發送資料 到 遠端 阿裡雲 web伺服器
esp8266 arduino 發送資料 到 遠端 阿裡雲 web伺服器

對應的代碼為:

/**
 * 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();
    USE_SERIAL.setDebugOutput(true);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();

    for(uint8_t t = ; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay();
    }

    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 > ) {
            // 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();
}
           

http://118.190.215.49:5001/order?id=123&name=XiaoMing

為在 阿裡雲上搭建的 web伺服器:使用的是 python 2.7 加 Flask,作用是将發送的資料,原樣傳回。

(“HelloWifi”, “123ab”) 為 能上網的 wifi路由器

log:

SDK:(cfd48f3)/Core:/lwIP:(STABLE-_0_3_RELEASE/glue:arduino-)



[SETUP] WAIT ..
[SETUP] WAIT ..
[SETUP] WAIT ..
[SETUP] WAIT ..
scandone
scandone
state:  ->  (b0)
state:  ->  ()
state:  ->  ()
add 
aid 

connected with HelloWifi, channel 
dhcp client start...
cnt 
ip:,mask:,gw:
[HTTP] begin...
[HTTP] GET...
[HTTP] GET... code: 
{'id': u'123', 'name': u'XiaoMing'}
pm open,type:2 0
[HTTP] begin...
[HTTP] GET...
[HTTP] GET... code: 
{'id': u'123', 'name': u'XiaoMing'}
[HTTP] begin...
[HTTP] GET...
[HTTP] GET... code: 
{'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調試:

esp8266 arduino 發送資料 到 遠端 阿裡雲 web伺服器

繼續閱讀