0

So I have a Raspberry Pi Model 3B+ that currently runs a script to collect temperature, humidity, and pressure values. 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?

My end goal is to: Read data on Pi ==> Send data to PC ==> Send Data to Database ==> Display on website updating every 60s

I currently have this code on my 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.

Which IP would I send to and any other help would be greatly appreciated!!

  • are both devices at you home with your own wifi? What router do you have (or do you happen to know if you have fixed ip adresses in your network)? What os does the pc have? Are both devices always on or what happens when one (probably the pc) is turned of? – Finn Jan 28 '22 at 14:17
  • @Finn Both at home on same Wi-Fi, IPs are fixed, OS is Windows 10, both devices on when testing (doesn't need to continuously run) Thanks – Jack Dickinson Jan 28 '22 at 14:24
  • 1
    Some ideas here... https://stackoverflow.com/a/64647027/2836621 – Mark Setchell Jan 28 '22 at 14:38

2 Answers2

0

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.

RMA Dev
  • 166
  • 5
0

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.

I would suggest to keep the database (and probably also the website) on the Pi and then simply retrieve the data with your 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.

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

Finn
  • 2,333
  • 1
  • 10
  • 21