当前位置:网站首页>Communication between WEMOS and raspberry pie
Communication between WEMOS and raspberry pie
2022-07-17 23:04:00 【The guest court carries the theory】
List of articles
Raspberry pie operation
Use Python establish Http The server
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(host = "0.0.0.0", port = '3009')
- Define the return contents of the root directory
Make with raspberry pie WeMos The background of
Raspberry pie http The server
- effect
- Use flask, Create a simple... On raspberry pie web The server
- wemos Put forward HTTP Of get Request to get the report data
Code
# Default access ‘/data’ Path will call subordinate functions
""" pi_backend.py This is a REST relay server running on the Pi for the Arduino to relay data to on the way to the Cloud. Effectively functions as a gateway for the edge devices. """
from flask import Flask, request, render_template, Response
#import client #uncomment when integrating with the client.py API
# Create the flask app
app = Flask(__name__)
# This is for the callback, which should be defined to
# with the following signature:
# def callback(temp, humid, light)
#
_callback_ = None
# Create just a single route to read data from the Arduino
@app.route('/data', methods = ['GET'])
def addData():
''' The one and only route. It extracts the data from the request, converts to float if the data is not None, then calls the callback if it is set '''
global _callback_
tempStr = request.args.get('temp')
humidStr = request.args.get('humid')
lightStr = request.args.get('light')
temp = float(tempStr) if tempStr else None
humid = float(humidStr) if humidStr else None
light = float(lightStr) if lightStr else None
if _callback_:
_callback_(temp = temp, humid = humid, light = light)
return "OK", 200
def set_callback(callback):
''' Sets the callback. @param callback The callback with signature def callback(temp, humid, light) '''
global _callback_
_callback_ = callback
def demo_callback(temp, humid, light):
''' Demo callback that just prints the data passed in. '''
if temp is not None:
print("\nTemperature = %3.2f" % temp)
if humid is not None:
print("Humidity = %3.2f" % humid)
if light is not None:
print("Light = %3.2f" % light)
if temp is None and humid is None and light is None:
print("\n*** Called without any parameters. ***")
print("\n")
def main():
set_callback(demo_callback)
app.run(host = "0.0.0.0", port = '3009')
if __name__ == '__main__':
main()
WeMos send out HTTP request
- explain
- WeMos and Pi It can realize interworking through LAN , Raspberry pie has been set up as a server above , The current part needs to pass WeMos send out HTTP request
WeMos Connect wifi
- Burn the following code into wemos modular
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
const char* ssid = "ChinaNet-G3GN201"; //Your Wifi's SSID
const char* password = "qq7729577855"; //Wifi Password
void setup(void){
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(void){
}

WeMos send out HTTP request
- In the previous example , establish http The relevant code of the request , In order to simulate the process of data generation by actual sensors , Every time 5 Send a data per second .
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
const char* ssid = "ChinaNet-G3GN201"; //Your Wifi's SSID
const char* password = "qq7729577855"; //Wifi Password
WiFiClient wifiClient;
const char* piAt = "http://192.168.1.248:3009/"; //change to your Pi's IP
void setup(void){
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(void){
Serial.print("Sending...");
if (WiFi.status() == WL_CONNECTED){
HTTPClient http;
String url = piAt;
url += "data?temp=31.2&humid=76&light=87"; //hardcoded values for example only
http.begin(wifiClient,url);
int returnCode = http.GET(); //perform a HTTP GET request
if (returnCode > 0){
String payload = http.getString();
Serial.println(payload);
}
http.end();
} else {
Serial.println("WiFi disconnected");
}
delay(5000); //Five second delay
}
边栏推荐
- 联通实时计算平台演进与实践
- Sword finger offer interview question: 2 Search in two-dimensional array
- The difference between step into and force step into in idea debugging
- 信息系统项目管理师必背核心考点(四十二)SWOT分析法
- 从0开始的 TypeScriptの十三:infer、extends、keyof、typeof、in
- Search - binary sort tree (I)
- Apache Flink 在斗鱼的应用与实践
- Wechat applet - get element positioning and width in applet custom components
- Force deduction solution summary 745 prefix and suffix search
- PolarDB for PostgreSQL的存储接口支持什么形式的磁盘存储?
猜你喜欢

TFTLCD thin film transistor liquid crystal display -- Taking Explorer as an example

Proxmox VE 7.2 iso 镜像重置 pve root密码

STM32F407外部SRAM

Slow SQL analysis and optimization

How do Internet companies implement paging and limit MySQL?

Proxmox VE 7.2 ESXi OVA 导入

AI 之 OpenCvSharp 大图找小图(案例版)

Fundamentals of C language: operators

VMware Photon OS 4 Install

Proxmox ve 7.2 install grafana+prometheus monitoring PVE exporter
随机推荐
source insight4 配色仿vscode
【重识云原生】第四章云网络4.9.4.1节——智能网卡SmartNIC方案综述
Wechat applet - life cycle approach
28、 Raid (redundant array of Inexpensive Disks) and crash consistency; Fsck and log)
Comment les entreprises Internet réalisent - elles la pagination, prenez MySQL Strength limit?
Proxmox VE 7.2 Install SMB 服务
ML:机器学习可解释性之特征置换重要性之机器学习模型中哪些特征很重要?
221. Largest square
Understanding and operation of pointer
Government organizations improve the efficiency, transparency and control of information management through content management
C language emptying input residual content
动画优化
AttributeError: ‘AvgPool2d’ object has no attribute ‘divisor_ override’
VMware Photon OS 4 Install
TFTLCD 薄膜晶体管液晶显示器——探索者为例
面向流批一体的 Flink Runtime 新进展
How do Internet companies implement paging and limit MySQL?
List collection
Arkui FAQ summary [series 3]
2022年了,我该如何入门智能家居