Comments (3)
The first pm_values = pms5003.read()
works fine and gets data from device and pushes to luftdaten. It is any subsequent device reads that cause this error. Ill try reseting the connection before reads
from enviroplus-python.
Doesn't seem to help to add a preemtive reset. It appears there is a try/except loop I am gettings stuck inside of the pms5003.read()
call.
Docker:
FROM balenalib/raspberry-pi-debian-python:3.7.3-stretch-build
WORKDIR /usr/src
RUN pip3 install numpy --user --no-binary :all:
RUN pip3 install enviroplus \
requests \
smbus
RUN install_packages python3-pil libatlas-base-dev python3-dev
RUN pip3 install spidev RPi.GPIO
WORKDIR /usr/src
COPY luftdaten.py luftdaten.py
CMD ["python3", "luftdaten.py"]
Slightly modifed luftdaten.py from example (ran into some issues with how Popen was being used, removed LCD outputs, not needed for use case).
#!/usr/bin/env python
import requests
import time
from bme280 import BME280
from pms5003 import PMS5003, ReadTimeoutError
from subprocess import PIPE, check_output, run
try:
from smbus2 import SMBus
except ImportError:
from smbus import SMBus
try:
bus = SMBus(1)
except Exception as e:
print(e)
# Create BME280 instance
try:
bme280 = BME280(i2c_dev=bus)
except Exception as e:
print(e)
# Create PMS5003 instance
try:
pms5003 = PMS5003()
except Exception as e:
print(e)
# Read values from BME280 and PMS5003 and return as dict
def read_values():
values = {}
cpu_temp = get_cpu_temperature()
raw_temp = bme280.get_temperature()
comp_temp = raw_temp - ((cpu_temp - raw_temp) / comp_factor)
values["temperature"] = "{:.2f}".format(comp_temp)
values["pressure"] = "{:.2f}".format(bme280.get_pressure() * 100)
values["humidity"] = "{:.2f}".format(bme280.get_humidity())
try:
pm_values = pms5003.read()
values["P2"] = str(pm_values.pm_ug_per_m3(2.5))
values["P1"] = str(pm_values.pm_ug_per_m3(10))
except ReadTimeoutError:
pms5003.reset()
pm_values = pms5003.read()
values["P2"] = str(pm_values.pm_ug_per_m3(2.5))
values["P1"] = str(pm_values.pm_ug_per_m3(10))
return values
# Get CPU temperature to use for compensation
def get_cpu_temperature():
process = run(['vcgencmd', 'measure_temp'], stdout=PIPE)
output = process.stdout.decode('utf-8')
return float(output.split('=')[1].split("'")[0])
# Get Raspberry Pi serial number to use as ID
def get_serial_number():
with open('/proc/cpuinfo', 'r') as f:
for line in f:
if line[0:6] == 'Serial':
return str(line.split(":")[1].strip())
# Check for Wi-Fi connection
def check_wifi():
if check_output(['hostname', '-I']):
return True
else:
return False
def send_to_luftdaten(values, id):
pm_values = dict(i for i in values.items() if i[0].startswith("P"))
temp_values = dict(i for i in values.items() if not i[0].startswith("P"))
resp_1 = requests.post("https://api.luftdaten.info/v1/push-sensor-data/",
json={
"software_version": "enviro-plus 0.0.1",
"sensordatavalues": [{"value_type": key, "value": val} for
key, val in pm_values.items()]
},
headers={
"X-PIN": "1",
"X-Sensor": id,
"Content-Type": "application/json",
"cache-control": "no-cache"
}
)
resp_2 = requests.post("https://api.luftdaten.info/v1/push-sensor-data/",
json={
"software_version": "enviro-plus 0.0.1",
"sensordatavalues": [{"value_type": key, "value": val} for
key, val in temp_values.items()]
},
headers={
"X-PIN": "11",
"X-Sensor": id,
"Content-Type": "application/json",
"cache-control": "no-cache"
}
)
if resp_1.ok and resp_2.ok:
return True
else:
return False
# Compensation factor for temperature
comp_factor = 1.2
# Raspberry Pi ID to send to Luftdaten
id = "raspi-" + get_serial_number()
# Main loop to read data and send to Luftdaten
while True:
values = None
try:
values = read_values()
print(values)
resp = send_to_luftdaten(values, id)
print("Response: {}\n".format("ok" if resp else "failed"))
time.sleep(10)
except Exception as e:
print(e)
from enviroplus-python.
Ok, it was the serial device not being disabled properly.
root@1c75adf:~# mount -o remount,rw /
root@1c75adf:~# systemctl mask [email protected]
root@1c75adf:~# reboot
did the trick on the BalenaOS... just need to figure out how to add to device configs.
from enviroplus-python.
Related Issues (20)
- NumPy Error when working with LCD HOT 1
- Install script does not install all required python libraries HOT 4
- all_in_one (and combined) needs to recalculate humidity based on compensated temperature HOT 3
- Gas sensor not returning values HOT 3
- Migrate Lufdaten.py to Sensors Community (luftdaten.py) HOT 1
- Install script is missing a of dependencies and simply does not work HOT 3
- PMS5003 best use HOT 6
- light.py sample fails to run on new Enviro+ HOT 3
- BME280 - Pressure / Humidity Incorrect Readings HOT 3
- Failed to read PMS5003 (fresh latest install) HOT 1
- Pillow 10 compatibility HOT 1
- install.sh not working when trying to install over SSH HOT 6
- Installing enviroplus-python library and running lcd.py does not work HOT 3
- Error `/usr/bin/raspi-config: 2909: do_serial_cons: not found` when running install.sh HOT 1
- Will not install. Tried multiple methods and multiple pi's and multiple pi os versions. HOT 7
- Suddenly not starting, reporting an issue with `get_pin` HOT 1
- GPIO24: not found HOT 7
- (pms5003) Not working on Pi Zero / Bookworm HOT 5
- Support for other boards besides raspberry Pi with 40 pin gpio HOT 1
- LCD does not show anything only backlight is on HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from enviroplus-python.