Azazel
August 10, 2019, 7:02am
1
Hi Guys,
I’m sorry, I’m very new to the python and sensors game :)
I’ve recently purchased Enviro+ and would like to plot out the data. How are you guys plotting your sensors outputs?
I did some Googling and looks like you can try to use Plotly, Adafruit OI and more manual like this one Data Collection, Logging, Plotting & Serving - A Simple Guide
I have tried to adopt the python from examples and the guide but ran into issues in the exporting to CSV part:
#!/usr/bin/env python
import time
import csv
from bme280 import BME280
from datetime import datetime
try:
from smbus2 import SMBus
except ImportError:
from smbus import SMBus
dt = datetime.now()
full_date = dt.strftime("%Y-%m-%d")
full_time = dt.strftime("%H:%M:%S")
full_day = dt.strftime("%A")
year = dt.year
month = dt.month
date = dt.day
hour = dt.hour
bus = SMBus(1)
bme280 = BME280(i2c_dev=bus)
temperature = bme280.get_temperature()
pressure = bme280.get_pressure()
humidity = bme280.get_humidity()
csvWrite = full_date, full_time, full_day, year, month, date, hour, temperature, pressure, humidity
csvFile = open("/home/pi/enviroplot.csv", "a")
while True:
with csvFile:
writer = csv.writer(csvFile)
writer.writerow(csvWrite)
time.sleep(1)
Errors:
Traceback (most recent call last):
File "Weather_Plot_Test.py", line 35, in <module>
with csvFile:
ValueError: I/O operation on closed file
I signed up for an account at emoncms.org and am sending my data every 30s there. It’s an interim solution, I just wanted a way to start saving & graphing the output!
Azazel
August 10, 2019, 11:04pm
3
Thank you! I will have a look. I was hoping to set up something locally on the Pi
I come from Networking world and I’m surprised there is no PRTG equivalent solution to these sensors :)
I should mention that I run the Pi headless so i cant use the matplotlib option.
#Azazel try this code
import time
import smbus2
import bme280
bus = smbus2.SMBus(1)
address = 0x77
calibration_params = bme280.load_calibration_params(bus, address)
layout = ‘{0:5d}: {1}, {2:0.3f} deg C, {3:0.2f} hPa, {4:0.2f} %’
counter = 1
while True:
data = bme280.sample(bus, address, calibration_params)
with open(“sample.log”,“a+”) as f:
f.write(layout.format(counter, data.timestamp, data.temperature, data.pressure, data.humidity) + “\n”)
f.close()
counter += 1
time.sleep(5)
Azazel
August 17, 2019, 11:57pm
5
kflmiami420:
import time
import smbus2
import bme280
bus = smbus2.SMBus(1)
address = 0x77
calibration_params = bme280.load_calibration_params(bus, address)
layout = ‘{0:5d}: {1}, {2:0.3f} deg C, {3:0.2f} hPa, {4:0.2f} %’
counter = 1
while True:
data = bme280.sample(bus, address, calibration_params)
with open(“sample.log”,“a+”) as f:
f.write(layout.format(counter, data.timestamp, data.temperature, data.pressure, data.humidity) + “\n”)
f.close()
counter += 1
time.sleep(5)
Hey Mate,
Thank you for the advise!
I am getting the following error though:
pi@RaspberryPiEnviro:~ $ /usr/bin/python3 /home/pi/export.py
File "/home/pi/export.py", line 13
data = bme280.sample(bus, address, calibration_params)
^
IndentationError: expected an indented block
It OK I’ve started on working on Crontab idea.
Any tips on how to get python to show the light sensors?
It seems that you need to run it for few seconds to get the reading, so the single “lux = ltr559.get_lux()” will no do.
I have tried the following but no go:
#Light
lux = ltr559.get_lux()
def get_lux_avg():
while lux != 0:
time.sleep(1)
return lux
avg_lux = get_lux_avg()
I give up trying to post the indented code in this forum. This forum
is _ _ _ _ _ _ _ piece of _ _ _ _ !