Created
January 8, 2022 22:11
-
-
Save depfryer/e5f127f4f94ed44da9b10e6c32e66638 to your computer and use it in GitHub Desktop.
get the temperature and power consumtion and save it in influxdb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from datetime import datetime, time | |
from time import sleep | |
from requests import exceptions | |
import pymodbus | |
import serial | |
import math | |
from os import error, system | |
from pymodbus.pdu import ModbusRequest | |
from pymodbus.client.sync import ModbusSerialClient as ModbusClient | |
from pymodbus.transaction import ModbusRtuFramer | |
from influxdb.client import InfluxDBClient | |
from influxdb import SeriesHelper | |
clientInfluxDb = InfluxDBClient(host='192.168.0.240', port='8086' ) | |
clientInfluxDb.switch_database('telegraf') | |
class maisonGeneral(SeriesHelper): | |
class Meta: | |
# Meta class stores time series helper configuration. | |
series_name = 'consommationElectrique_global' | |
# Series name must be a string, curly brackets for dynamic use. | |
tags = ['alias', 'HC'] | |
fields = ['voltage_v', 'current_a', 'power_w', 'total_wh', 'used_wh','frequence','ratio_efficacite','erreur'] | |
# Defines all the fields in this time series. | |
### Following attributes are optional. ### | |
client = clientInfluxDb | |
# Client should be an instance of InfluxDBClient. | |
#warning: Only used if autocommit is True. | |
bulk_size = 5 | |
# Defines the number of data points to write simultaneously. | |
# Only applicable if autocommit is True. | |
autocommit = True | |
# If True and no bulk_size, then will set bulk_size to 1. | |
# Specify the retention policy for the data points | |
# time_precision = "ns" | |
# Default is ns (nanoseconds) | |
# Setting time precision while writing point | |
# You should also make sure time is set in the given precision | |
def calc (registers, factor): | |
format = '%%0.%df' % int (math.ceil (math.log10 (factor))) | |
if len(registers) == 1: | |
return float(format % ((1.0 * registers[0]) / factor)) | |
elif len(registers) == 2: | |
return float(format % (((1.0 * registers[1] * 65535) + (1.0 * registers[0])) / factor)) | |
clientCapteur = ModbusClient (method = "rtu", port="/dev/ttyUSB0", stopbits = 1, bytesize = 8, parity = 'N', baudrate = 9600) | |
connection = clientCapteur.connect() | |
def getMeasurement(): | |
global clientCapteur | |
result = clientCapteur.read_input_registers (0x0000, 10, unit = 0x01) | |
return { | |
'Voltage' : calc (result.registers[0:1], 10), | |
'Current' : calc (result.registers[1:3], 1000), | |
'Power':calc (result.registers[3:5], 10), | |
'Total_wh':calc (result.registers[5:7], 1) , | |
'Frequence':calc (result.registers[7:8], 10), | |
'Ratio_efficacite':calc (result.registers[8:9], 100), | |
'Erreur':calc (result.registers[9:10], 1), | |
} | |
old_total_wh = -1 | |
while True: | |
heure = datetime.now().time() | |
HC = 1 | |
if time(hour= 6, minute = 38) <= heure <= time(hour= 22, minute = 38) : | |
HC = 0 | |
# print(hs110_sysinfo) | |
alias = 'home' | |
measurement = getMeasurement() | |
voltage_v = measurement['Voltage'] | |
current_a = measurement['Current'] | |
power_w = measurement['Power'] | |
total_wh = measurement['Total_wh'] | |
frequence = measurement['Frequence'] | |
ratio_efficacite = measurement['Ratio_efficacite'] | |
erreur = measurement['Erreur'] | |
if old_total_wh == -1: | |
print(f'select last(*) from "consommationElectrique_global" where alias = \'{alias}\'') | |
r = clientInfluxDb.query(f'select last(*) from "consommationElectrique_global" where alias = \'{alias}\'') | |
old_total_wh = list(r.get_points())[0]['last_total_wh'] | |
used_wh = total_wh - old_total_wh | |
old_total_wh = total_wh | |
maisonGeneral(alias =alias,HC=HC, voltage_v= voltage_v, current_a= current_a, power_w= power_w, total_wh= total_wh, used_wh= used_wh,frequence=frequence,ratio_efficacite=ratio_efficacite,erreur=erreur).commit() | |
sleep(.5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import board | |
import adafruit_dht | |
import psutil | |
from influxdb.client import InfluxDBClient | |
from influxdb import SeriesHelper | |
clientInfluxDb = InfluxDBClient(host='192.168.0.240', port='8086' ) | |
clientInfluxDb.switch_database('telegraf') | |
class maisonGeneral(SeriesHelper): | |
class Meta: | |
# Meta class stores time series helper configuration. | |
series_name = 'temperature' | |
# Series name must be a string, curly brackets for dynamic use. | |
tags = ['alias'] | |
# Defines all the fields in this time series. | |
fields = ['temperature', 'humidite'] | |
### Following attributes are optional. ### | |
client = clientInfluxDb | |
# Client should be an instance of InfluxDBClient. | |
#warning: Only used if autocommit is True. | |
bulk_size = 5 | |
# Defines the number of data points to write simultaneously. | |
# Only applicable if autocommit is True. | |
autocommit = True | |
# If True and no bulk_size, then will set bulk_size to 1. | |
# Specify the retention policy for the data points | |
# time_precision = "ns" | |
# Default is ns (nanoseconds) | |
# Setting time precision while writing point | |
# You should also make sure time is set in the given precision | |
# We first check if a libgpiod process is running. If yes, we kill it! | |
for proc in psutil.process_iter(): | |
if proc.name() == 'libgpiod_pulsein' or proc.name() == 'libgpiod_pulsei': | |
proc.kill() | |
sensor = adafruit_dht.DHT11(board.D22) | |
while True: | |
try: | |
temperature = sensor.temperature | |
humidity = sensor.humidity | |
if temperature and humidity: | |
maisonGeneral(alias ="home", temperature= temperature,humidite =humidity) | |
except RuntimeError as error: | |
print(error.args[0]) | |
time.sleep(0.5) | |
continue | |
except Exception as error: | |
sensor.exit() | |
raise error | |
time.sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment