45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
HOST = "192.168.2.160"
|
|
PORT = 4223
|
|
UID = "DYC"
|
|
URL = "http://192.168.2.30:8086/write?db=mydb"
|
|
|
|
import time
|
|
|
|
import requests
|
|
|
|
from tinkerforge.ip_connection import IPConnection
|
|
from tinkerforge.bricklet_outdoor_weather import BrickletOutdoorWeather
|
|
|
|
def asdf(type, identifier, **kwargs):
|
|
print(time.strftime("%c"), type, identifier, kwargs)
|
|
time_ns = time.time_ns()
|
|
for key in kwargs:
|
|
data = "{unit},type={type},identifier={id} value={value} {time_ns}".format(unit=key, type=type, id=identifier, value=kwargs[key], time_ns=time_ns)
|
|
try:
|
|
r = requests.post(URL, data=data)
|
|
print(r, r.status_code)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
def cb_station(identifier, temperature, humidity, wind_speed, gust_speed, rain, wind_direction, battery_low):
|
|
asdf(type="station", identifier=identifier, temperature=temperature, humidity=humidity, wind_speed=wind_speed, gust_speed=gust_speed, rain=rain, wind_direction=wind_direction, battery_low=battery_low)
|
|
def cb_sensor(identifier, temperature, humidity):
|
|
asdf(type="sensor", identifier=identifier, temperature=temperature, humidity=humidity)
|
|
|
|
if __name__ == "__main__":
|
|
print("starting…")
|
|
ipcon = IPConnection()
|
|
ow = BrickletOutdoorWeather(UID, ipcon)
|
|
ipcon.connect(HOST, PORT)
|
|
ow.set_station_callback_configuration(True)
|
|
ow.set_sensor_callback_configuration(True)
|
|
ow.register_callback(ow.CALLBACK_STATION_DATA, cb_station)
|
|
ow.register_callback(ow.CALLBACK_SENSOR_DATA, cb_sensor)
|
|
print("now we play the waiting game…")
|
|
while True:
|
|
try:
|
|
time.sleep(60*60)
|
|
except Exception:
|
|
print("…")
|
|
ipcon.disconnect()
|