简体   繁体   English

如何从 Raspberry Pi 向 PC 发送读数?

[英]How to send readings to PC from Raspberry Pi?

So I have a Raspberry Pi Model 3B+ that currently runs a script to collect temperature, humidity, and pressure values.所以我有一个树莓派 Model 3B+,它当前运行一个脚本来收集温度、湿度和压力值。 How would I send these values from the Pi to my PC and then have my PC read the values and store them say every 60 seconds?我如何将这些值从 Pi 发送到我的 PC,然后让我的 PC 每 60 秒读取一次值并存储它们?

My end goal is to: Read data on Pi ==> Send data to PC ==> Send Data to Database ==> Display on website updating every 60s我的最终目标是:在 Pi 上读取数据 ==> 将数据发送到 PC ==> 将数据发送到数据库 ==> 在网站上显示,每 60 秒更新一次

I currently have this code on my Pi:我目前在我的 Pi 上有这个代码:

import bme680
import time
import socket
import sys
from struct import pack

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

host, port = '?????', 65000
server_address = (host, port)

try:
    sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except (RuntimeError, IOError):
    sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)

sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680,OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)

print('Polling:')
try:
    while True:
        if sensor.get_sensor_data():
            output = '{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH'.format(
                sensor.data.temperature,
                sensor.data.pressure,
                sensor.data.humidity)
            print(output)
            time.sleep(60)
except KeyboardInterrupt:
    pass

This reads data and displays it fine... However, I want to send this data to my PC so that my PC can read the values and send them to a database etc.这可以读取数据并正常显示...但是,我想将此数据发送到我的 PC,以便我的 PC 可以读取这些值并将它们发送到数据库等。

Which IP would I send to and any other help would be greatly appreciated!!我将发送给哪个 IP 以及任何其他帮助将不胜感激!

You could construct a simple Flask API from your Pi and pass the variables to it from your Pi using a POST request, and then a separate script on your PC that queries the endpoint with a GET request and processes the data for storing in your db.您可以从您的 Pi 构建一个简单的 Flask API并使用 POST 请求从您的 Pi 将变量传递给它,然后在您的 PC 上使用一个单独的脚本,使用 GET 请求查询端点并处理数据以存储在您的数据库中。

This is not a final answer, but too long for the comments so i write it in here.这不是最终答案,但评论太长了,所以我把它写在这里。 To get your ip on windows simply start cmd or Powershell and type ipconfig somewhere in the response is you ipv4.要在 windows 上获取 ip,只需启动cmdipconfig中的响应类型为 70183ACF36790。

I would suggest to keep the database (and probably also the website) on the Pi and then simply retrieve the data with your PC.我建议将数据库(可能还有网站)保留在 Pi 上,然后简单地用您的 PC 检索数据。 This way you can also get the data from other divices (like your phone).通过这种方式,您还可以从其他设备(例如您的手机)获取数据。 Also in my opinion its easier to get an Pi (with probably some sort of Debian) to act like a server than a Windows PC.同样在我看来,让 Pi(可能带有某种 Debian)比 Windows PC 更容易像服务器一样工作。

As @RMA Dev suggests Flask would be nice for the API either way and if you really want a Database for the values i would recommend sqlite正如@RMA Dev 建议的那样,Flask 对 API 来说都很好,如果你真的想要一个数据库的值,我会推荐sqlite

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Raspberry Pi捕获jpeg图像并将其发送到PC套接字python? - Capture and send jpeg images from Raspberry Pi to PC socket python? 如何使用 python 将实时数据从计算机(PC)发送到 Raspberry pi 4? - How to send Live Data from Computer(PC) to Raspberry pi 4 using python? 如何将数据从树莓派发送到网页? - how to send data from raspberry pi to a webpage? 每10分钟从Raspberry Pi获取一次温度读数 - Take temperature readings every 10 minutes from a Raspberry Pi 如何从单独的PC控制Raspberry Pi的GPIO引脚 - How to control Raspberry Pi's GPIO pins from separate PC 将图像通过 tcp 从树莓派发送到 pc 中的程序并在 pc 上显示 - Send image over tcp from raspberry pi to program in pc and display it on pc 如何通过蓝牙一次从树莓派 4 向多个树莓派发送数据? - How to send data to multiple raspberry pi's at a time from raspberry pi 4 via Bluetooth? 通过蓝牙发送传感器数据和流视频(Raspberry Pi 到 PC) - Send Sensor Data & Stream Video Over Bluetooth (Raspberry Pi to PC) 在(Raspberry pi上的Python)和(PC上的Matlab)之间发送数据并获取响应 - Send Data And Get Response between (Python on Raspberry pi) and (Matlab on PC) 使用 python 将 CSV 从树莓派 pi3 发送到树莓派 3 - Send CSV from raspberry pi3 to raspberry pi 3 with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM