return {'c': cTemp, 'f': fTemp} def writehumidity(self): """Select the relative humidity command from the given provided values""" bus.write_byte(HTU21D_DEFAULT_ADDRESS, HTU21D_MEAS_RH_NOHOLD) def readhumidity(self): """Read data back from the device address, 2 bytes, humidity MSB, humidity LSB""" data0 = bus.read_byte(HTU21D_DEFAULT_ADDRESS) data1 = bus.read_byte(HTU21D_DEFAULT_ADDRESS) # Convert the data humidity = ((data0 * 256 + data1) * 125 / 65536.0) - 6 return {'h': humidity} from HTU21D import HTU21D HTU21D = HTU21D() while True: HTU21D.writehumidity() hum = HTU21D.readhumidity() print "Relative Humidity : %.2f %%" % (hum['h']) HTU21D.writetemperature() temp = HTU21D.readtemperature() print "Temperature in Celsius : %.2f C" % (temp['c']) print "Temperature in Fahrenheit : %.2f F" % (temp['f']) print " ************************************* " time.sleep(1)
# Acquire temperature/pressure data from HTU21D and do stuff # # Patrick O'Keeffe <*****@*****.**> # Laboratory for Atmospheric Research at Washington State University from __future__ import print_function import os, os.path as osp import time import logging from logging.handlers import TimedRotatingFileHandler import sys sys.path.append('/home/pi/weather-station') from HTU21D import HTU21D htu = HTU21D() #### read config file import ConfigParser as configparser c = configparser.ConfigParser() c.read('/etc/wsn/htu21d-logger.conf') interval = c.getint('main', 'interval') log_dir = c.get('logging', 'log_dir') log_file = c.get('logging', 'log_file') ####################################### #### logging setup try: os.makedirs(log_dir) except OSError:
def parse_gps(NMEA_sentence, flightmode): global TESTALTITUDE satellites = 0 lats = 0 northsouth = 0 lngs = 0 westeast = 0 altitude = 0 if TESTING: altitude = TESTALTITUDE time = 0 latitude = 0 longitude = 0 temp = 0 humidity = 0 temp2 = 0 pressure2 = 0 alt2 = 0 temp3 = 0 temp4 = 0 global counter if NMEA_sentence.startswith("$GPGGA"): print NMEA_sentence data = NMEA_sentence.split(",") if data[6] == "0": print "No Lock" pass else: for Value in Devices: if Value == "HTU21D" and Devices[Value]: print "HTU21D Enabled" start = time_.time() elap = 0 while elap < 5: elap = time_.time() - start temp = float(HTU21D.read_temperature()) humidity = float(HTU21D.read_humidity()) temp = "%.2f" % temp humidity = "%.2f" % humidity print "HTU21D -> {}C, {}%".format(temp, humidity) if temp and humidity: print "Time elapsed for HTU21D: {}".format(elap) break if Value == "BMP085" and Devices[Value]: print "BMP085 Enabled" start = time_.time() elap = 0 while elap < 5: try: elap = time_.time() - start temp2 = "%.2f" % bmp.readTemperature() pressure2 = "%.1f" % float( bmp.readPressure() / 100.0) alt2 = "%.1f" % bmp.readAltitude() print "BMP085 -> {}C, {}HPa, {}m".format( temp2, pressure2, alt2) if temp2 == "12.8" and pressure2 == "125.1" and alt2 == "14557.3": temp2 = pressure2 = alt2 = 0 break if temp2 and pressure2 and alt2: print "Time elapsed for BMP085: {}".format( elap) break except: pass if Value == "TMP102" and Devices[Value]: print "TMP102 Enabled" start = time_.time() while elap < 5: elap = time_.time() - start temp3 = "%.2f" % gettmp(0x49) print "TMP102 -> {}".format(temp3) if temp3: print "Time elapsed for TMP102: {}".format(elap) break if Value == "DS18B20" and Devices[Value]: "DS18B20 Enabled" start = time_.time() elap = 0 while elap < 5: elap = time_.time() - start temp4 = read_B18() print "DS18B20 -> {}".format(temp4) if temp4: print "Time elapsed for DS18B20: {}".format(elap) break raw_time = data[1] lats = data[2] northsouth = data[3] lngs = data[4] westeast = data[5] satellites = data[7] altitude = data[9] if TESTING: altitude = TESTALTITUDE # This will set the altitude for comparing / sending etc. if time_set == False: set_time(raw_time) time = float(raw_time) string = "%06i" % time hours = string[0:2] minutes = string[2:4] seconds = string[4:6] time = "{}:{}:{}".format( hours, minutes, seconds ) # Could also use time.strftime("%H:%M:S") which will output HOURS:MINUTES:SECONDS latitude = convert( lats, northsouth ) # Full list can be found at https://docs.python.org/2/library/time.html ( time.strftime() ) longitude = convert(lngs, westeast) global HasTriggered global DropFinished global DropStart global DropDelay global CutOff global TopAlt if HasTriggered and DropFinished == False: if (time_.time() - DropStart) > DropDelay: #print "Drop has finished. GPIO {} to Off.".format(CutOff) GPIO.output(CutOff, 0) DropFinished = True if float(altitude) >= TopAlt and not HasTriggered and not DropFinished: #print "Drop Starting! GPIO {} Triggered!".format(CutOff) GPIO.output(CutOff, 1) HasTriggered = True DropStart = time_.time() #flightmode = int(flightmode) # Accidental overwriting? logstring = "{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}".format( callsign, time, counter, latitude, longitude, satellites, int(flightmode), altitude, temp, humidity, temp2, pressure2, alt2, temp3, temp4, int(HasTriggered)) if Args.s: string = "{},{},{},{},{},{},{},{},{},{}".format( callsign, time, counter, latitude, longitude, satellites, int(flightmode), temp2, altitude, int(HasTriggered)) else: string = logstring if not os.path.isfile('log.txt'): with open('log.txt', 'w+') as File: File.write("Log Text:\n") with open('log.txt', 'r') as File: Read = File.read() Read = Read.split("\n") with open('log.txt', 'w+') as File: Out = Read.append('{}'.format(logstring)) for A in Read: File.write(A + "\n") csum = str(hex(crc16f(string))).upper()[2:] csum = csum.zfill(4) datastring = str("$$$$$$" + string + "*" + csum + "\n") counter += 1 print "Sending >> ", datastring time_.sleep(0.1) send(datastring) if Args.p: print "Taking Picture!" Capture() print "Finished Taking Picture."
import random from Relay import Relay from Controls import BangBang from threading import Timer import threading import requests from HTU21D import HTU21D from mySqlTools import mySqlSenderAnalog from HeaterControlTools import printCurrentTime print(printCurrentTime()+"Imports complete") setpoint = 16 zone = 0.2 radiator = Relay(12) rumsensor = HTU21D() controller = BangBang(setpoint, zone) temp = round(rumsensor.read_temperature(),2) print(printCurrentTime()+"Setup complete") temp = 0 def controlLoop(): ct=Timer(15.0, controlLoop) #t.daemon = True ct.start() print(printCurrentTime()+"Regulering aktiv !") #print(time.clock()) controller.control(radiator, temp) def logLoop():
# Delay between pictures = 5 minutes, Total runtime = 3 days ##--------------## TESTING = False ## If you want to test the Cut Off, Enable this (True). ##--------------## GPIO.setmode(11) GPIO.setwarnings(False) GPIO.setup(CutOff, GPIO.OUT) Devices = {"BMP085": True, "TMP102": True, "HTU21D": True, "DS18B20": False} StartTime = time.time() if Devices["HTU21D"]: HTU21D = HTU21D() if Devices["BMP085"]: bmp = BMP085(0x77, 3) time_set = False gps_set_success = False bus = smbus.SMBus(1) DNull = open('/dev/null', 'w') os.system("chmod +x ./DHT") Parser = argparse.ArgumentParser( description= "Parse and decipher GPS signals from serial. Output to Tx with flightmode enabled." ) Parser.add_argument('-c', nargs='?', help='Custom Callsign for the Tx.') Parser.add_argument('-p',
from w1_Temperature import W1Temperature import signal import sys import time def signal_handler(signal, frame): print "Exiting..." scheduler.stop() sys.exit(0) historian_url = "http://127.0.0.1:5000/" w1_temps = W1Temperature() humidity = HTU21D() w1_sensors = w1_temps.connected_sensors() scheduler = Scheduler(historian_url) interval = 30 # add sensors to be recorded to the scheduler for sensor in w1_sensors: scheduler.add_sensor(interval, w1_temps, sensor, sensor) scheduler.add_sensor(interval, humidity, "Temperature", "HTU21D_Temp") scheduler.add_sensor(interval, humidity, "Humidity", "HTU21D_Hum") signal.signal(signal.SIGINT, signal_handler) scheduler.start() while True:
def parse_gps(NMEA_sentence, flightmode): global TESTALTITUDE satellites = 0 lats = 0 northsouth = 0 lngs = 0 westeast = 0 altitude = 0 if TESTING: altitude = TESTALTITUDE time = 0 latitude = 0 longitude = 0 temp = 0 humidity = 0 temp2 = 0 pressure2 = 0 alt2 = 0 temp3 = 0 temp4 = 0 global counter if NMEA_sentence.startswith("$GPGGA"): print NMEA_sentence data = NMEA_sentence.split(",") if data[6] == "0": print "No Lock" pass else: for Value in Devices: if Value == "HTU21D" and Devices[Value]: print "HTU21D Enabled" start = time_.time() elap = 0 while elap < 5: elap = time_.time() - start temp = float(HTU21D.read_temperature()) humidity = float(HTU21D.read_humidity()) temp = "%.2f" % temp humidity = "%.2f" % humidity print "HTU21D -> {}C, {}%".format(temp, humidity) if temp and humidity: print "Time elapsed for HTU21D: {}".format(elap) break if Value == "BMP085" and Devices[Value]: print "BMP085 Enabled" start = time_.time() elap = 0 while elap < 5: try: elap = time_.time() - start temp2 = "%.2f" % bmp.readTemperature() pressure2 = "%.1f" % float(bmp.readPressure() / 100.0) alt2 = "%.1f" % bmp.readAltitude() print "BMP085 -> {}C, {}HPa, {}m".format(temp2, pressure2, alt2) if temp2 == "12.8" and pressure2 == "125.1" and alt2 == "14557.3": temp2 = pressure2 = alt2 = 0 break if temp2 and pressure2 and alt2: print "Time elapsed for BMP085: {}".format(elap) break except: pass if Value == "TMP102" and Devices[Value]: print "TMP102 Enabled" start = time_.time() while elap < 5: elap = time_.time() - start temp3 = "%.2f" % gettmp(0x49) print "TMP102 -> {}".format(temp3) if temp3: print "Time elapsed for TMP102: {}".format(elap) break if Value == "DS18B20" and Devices[Value]: "DS18B20 Enabled" start = time_.time() elap = 0 while elap < 5: elap = time_.time() - start temp4 = read_B18() print "DS18B20 -> {}".format(temp4) if temp4: print "Time elapsed for DS18B20: {}".format(elap) break raw_time = data[1] lats = data[2] northsouth = data[3] lngs = data[4] westeast = data[5] satellites = data[7] altitude = data[9] if TESTING: altitude = TESTALTITUDE # This will set the altitude for comparing / sending etc. if time_set == False: set_time(raw_time) time = float(raw_time) string = "%06i" % time hours = string[0:2] minutes = string[2:4] seconds = string[4:6] time = "{}:{}:{}".format( hours, minutes, seconds ) # Could also use time.strftime("%H:%M:S") which will output HOURS:MINUTES:SECONDS latitude = convert( lats, northsouth ) # Full list can be found at https://docs.python.org/2/library/time.html ( time.strftime() ) longitude = convert(lngs, westeast) global HasTriggered global DropFinished global DropStart global DropDelay global CutOff global TopAlt if HasTriggered and DropFinished == False: if (time_.time() - DropStart) > DropDelay: # print "Drop has finished. GPIO {} to Off.".format(CutOff) GPIO.output(CutOff, 0) DropFinished = True if float(altitude) >= TopAlt and not HasTriggered and not DropFinished: # print "Drop Starting! GPIO {} Triggered!".format(CutOff) GPIO.output(CutOff, 1) HasTriggered = True DropStart = time_.time() # flightmode = int(flightmode) # Accidental overwriting? logstring = "{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}".format( callsign, time, counter, latitude, longitude, satellites, int(flightmode), altitude, temp, humidity, temp2, pressure2, alt2, temp3, temp4, int(HasTriggered), ) if Args.s: string = "{},{},{},{},{},{},{},{},{},{}".format( callsign, time, counter, latitude, longitude, satellites, int(flightmode), temp2, altitude, int(HasTriggered), ) else: string = logstring if not os.path.isfile("log.txt"): with open("log.txt", "w+") as File: File.write("Log Text:\n") with open("log.txt", "r") as File: Read = File.read() Read = Read.split("\n") with open("log.txt", "w+") as File: Out = Read.append("{}".format(logstring)) for A in Read: File.write(A + "\n") csum = str(hex(crc16f(string))).upper()[2:] csum = csum.zfill(4) datastring = str("$$$$$$" + string + "*" + csum + "\n") counter += 1 print "Sending >> ", datastring time_.sleep(0.1) send(datastring) if Args.p: print "Taking Picture!" Capture() print "Finished Taking Picture."
def __init__(self) -> None: self.htu = HTU21D()
#!/usr/bin/python import time import sys from classes.idleloop import IdleLoop from MCP9808 import MCP9808 as TemperatureSensor from HTU21D import HTU21D as HumiditySensor from MPL3115A2 import MPL3115A2 as BarometricSensor from HT16K33 import HT16K33 as DisplayController from MCP23017 import MCP23017 as PortExpander from py2cbus import i2c thermometer = TemperatureSensor(i2c(1, TemperatureSensor.BASE_ADDRESS)) hygrometer = HumiditySensor(i2c(1, HumiditySensor.BASE_ADDRESS)) barometer = BarometricSensor(i2c(1, BarometricSensor.BASE_ADDRESS)) ports = PortExpander(i2c(1, PortExpander.BASE_ADDRESS)) ports.writePortA(0x00) display = DisplayController(i2c(1, DisplayController.BASE_ADDRESS)) display.turnOnOscillator() display.turnOnDisplay() display.setDimming(0) for loop in range(1024): t = thermometer.read_sensor() thermometer.print_temperature(t) display.writeTemperature(t['fahrenheit']) h = hygrometer.read_sensor() print("Humidity: {:5.1f}, Temperature: {:6.1f}".format(