BarnyardBarnyard
Posts: 2
Joined: Wed Jan 08, 2020 8:28 pm

Help writing to DB as part of Raspi.orgs build your weather station project

Thu Jan 16, 2020 11:08 am

I purchased a Raz pi 4 B to make a weather station. My first sensor is the BME280 which was recommended as part of the raspi.org project : https://projects.raspberrypi.org/en/pro ... er-station

I've been able to connect the sensor, test my code in Thonny Python IDE and see results coming back. The problem I have now, is I'm unable to figure out how to write these results to the mariadb I built which follows the general instructions in the project described above. I've triple checked that my db was build and there "shouldn't" be a problem. I'm out of ideas for solving this one.

Admittedly, I probably tried to simplify the code in the above project which includes many different sensors and suposedly writes to a db no problem. Here is my code which doesn't appear to write to the table. Looking for help...my guess is that this is a simple one :).

Code: Select all

import time
import math
import bme280_sensor
import statistics
import database 

interval = 60
SECS_IN_AN_HOUR = 3600
store_speeds = []
store_directions = []

database = database.weather()
while True:
    start_time = time.time()
while time.time() - start_time >= interval:
    humidity, pressure, temperature = bme280_sensor.read_all()
    db.INSERT(AMBIENT_TEMPERATURE, AIR_PRESSURE, HUMIDITY)

print (humidity, pressure, temperature)
Last edited by BarnyardBarnyard on Thu Jan 16, 2020 8:01 pm, edited 1 time in total.

User avatar
joelostinspace
Posts: 39
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: Help writing to DB as part of Raspi.orgs build your weather station project

Thu Jan 16, 2020 2:46 pm

first please the Code button when adding any code it retains spacing and it easier to read,
you have assigned the data base to database here:
database = database.weather()
then your write to the data base is db here:
db.INSERT(AMBIENT_TEMPERATURE, AIR_PRESSURE, HUMIDITY)
try changing it database:

Code: Select all

database.INSERT(AMBIENT_TEMPERATURE, AIR_PRESSURE, HUMIDITY)
untested....

I would rather live in a world where my life is surrounded by mystery than a world so small that my mind could comprehend it.


--- Harry Emerson Fosdick

BarnyardBarnyard
Posts: 2
Joined: Wed Jan 08, 2020 8:28 pm

Re: Help writing to DB as part of Raspi.orgs build your weather station project

Thu Jan 16, 2020 4:40 pm

Hi Joe.

Thanks for the advice and suggestion. I tried your recommendation and am still unable to see sensor values and the dB. In python do I have to connect /login to Maria dB in order for this insert request to be valid?

User avatar
joelostinspace
Posts: 39
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: Help writing to DB as part of Raspi.orgs build your weather station project

Fri Jan 17, 2020 1:29 am

your while statements:
while True:
start_time = time.time()
while time.time() - start_time >= interval:
humidity, pressure, temperature = bme280_sensor.read_all()
db.INSERT(AMBIENT_TEMPERATURE, AIR_PRESSURE, HUMIDITY)
the program never gets the checking the interval and never reading your sensor, etc.
humidity, pressure, temperature = bme280_sensor.read_all()
then you change it in the insert

Code: Select all

database.INSERT(temperature,pressure,humidity)
here's an example:

Code: Select all

import time
interval= 10
start_time = time.time()
while True:    
    if time.time() - start_time >= interval:
        print('hello world')#place your read sensor and data base entry
        start_time = time.time()

I would rather live in a world where my life is surrounded by mystery than a world so small that my mind could comprehend it.


--- Harry Emerson Fosdick

Return to “Python”