esp8266 arduino 发送数据 到 远程 阿里云 web服务器

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调试:

这里写图片描述

如果您打算使用Arduino运行Web服务器,那么这里是您的理想选择。我们将在Web服务器上显示传感器数据。 硬件部件: Arduino UNO和Genuino UNO×1个 Arduino以太网修订版3×1个 跳线(通用)×1个 DHT22温度传感器×1个 软件应用程序和在线服务: Arduino IDE 仅作记录,Arduino本身不能充当Web服务器。与具有完整功能的嵌入式系统Raspberry Pi不同,Arduino需要以太网屏蔽才能连接到Internet或ESP8266芯片。 Arduino以太网屏蔽是一种电路板,可让Arduino连接到互联网。它基于Wiznet W5xxx系列以太网芯片。这些芯片具有能够同时使用TCP-IP和UDP的网络堆栈。但是,屏蔽层仅允许通过RJ45连接器进行有线连接。因此,如果您正在寻找无线互联网解决方案,则可以改用ESP8266开发板。Arduino以太网防护罩具有集成的microSD卡读卡器,可用于存储网页文件。 您仍然可以使用以太网屏蔽的母头引脚与Arduino的大多数引脚接口。只需将屏蔽罩和Arduino的引脚对齐,然后向下压直到它们舒适地合上即可。不能使用的引脚是引脚10 (SS),11 (MOSI),12 (MISO)和13 (SCK)。这些是SPI(串行外围设备接口)引脚,Arduino用来与以太网屏蔽进行通信。 准备硬件 对于此项目,我们将显示来自Arduino的DHT22温湿度传感器数据和与网络页面配对的以太网屏蔽,您可以在世界任何地方访问该页面。为此,请连接以下组件,如图2所示: 组装完组件后,将LAN电缆插入RJ45连接器。另一端必须在连接到互联网的路由器上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值