def main(): #Set DATA pin DHT = 4 try: global temp subprocess.call(['mosquitto_pub', '-t', 'pooz/measuring', '-h', 'adveisorgroup2.lsr.ei.tum.de', '-m', "1"]) while True: if GPIO.input(18) == False: subprocess.call(['mosquitto_pub', '-t', 'pooz/fireflag', '-h', 'adveisorgroup2.lsr.ei.tum.de', '-m', "0"]) bmp = BMP085(0x77) temp = bmp.readTemperature() pressure = bmp.readPressure() altitude = bmp.readAltitude() lightLevel=bh1750.readLight() now = datetime.datetime.now() uv_value = uv.veml6070_sensor.getUVIntensity() print ("Temperature: %.2f C" % temp) print ("Pressure: %.2f hPa" % (pressure / 100.0)) print ("Altitude: %.2f" % altitude) h,t = dht.read_retry(dht.DHT22, DHT) print('Temp={0:0.1f}*C \nHumidity={1:0.1f}%\n\n'.format(t,h)) print("Light Level : " + format(lightLevel,'.2f') + " lx") print("UV Value: {0}".format(round(uv_value,2))) mine.write(h, temp, pressure, lightLevel) # f.write("Date: {}, Temperature: {}, Humidity: {}, Light: {}".format(now.strftime("%Y-%m-%d %H:%M:%S"),t, h, lightLevel)) print("Publishing message to topic","pooz/") subprocess.call(['mosquitto_pub', '-t', 'pooz/temperature', '-h', 'adveisorgroup2.lsr.ei.tum.de', '-m', str(round(temp, 2))]) subprocess.call(['mosquitto_pub', '-t', 'pooz/humidity', '-h', 'adveisorgroup2.lsr.ei.tum.de', '-m', str(round(h,2))]) subprocess.call(['mosquitto_pub', '-t', 'pooz/pressure', '-h', 'adveisorgroup2.lsr.ei.tum.de', '-m', str(round(pressure,2))]) subprocess.call(['mosquitto_pub', '-t', 'pooz/lightlevel', '-h', 'adveisorgroup2.lsr.ei.tum.de', '-m', str(round(lightLevel,2))]) subprocess.call(['mosquitto_pub', '-t', 'pooz/uv_value', '-h', 'adveisorgroup2.lsr.ei.tum.de', '-m', str(round(uv_value,2))]) #subprocess.call(['mosquitto_pub', '-t', 'pooz/fireflag', '-h', 'adveisorgroup2.lsr.ei.tum.de', '-m', "0"]) sleep(300) except KeyboardInterrupt: subprocess.call(['mosquitto_pub', '-t', 'pooz/measuring', '-h', 'adveisorgroup2.lsr.ei.tum.de', '-m', "0"]) print("Ended") client.loop_stop() #stop the loop GPIO.cleanup()
def getLumiance(): ''' get the lumiance from the drive ''' try: lumiance = bh1750.readLight() ## print('lumiance = %.02f' % lumiance) except: print('read lumiance error') lumiance = None log.recordError(log.BH1750ERR) return lumiance
def getI2cSensors(devices): tstamp = int(time.time()) newObject = {"time": tstamp} if not isinstance(devices, list): newObject['message'] = 'No devices specified.' return (False, newObject) if 'bh1750' in devices: ###### Get luminosity ## lux = bh1750.readLight() # Drop the first (usually bad) lux = bh1750.readLight() newObject['lum'] = int(lux) debug("Light Level : " + str(newObject['lum']) + " lx") if 'si7021' in devices: ###### Get temperature & humidity from si7021 ## T = si7021.readTemperature() newObject['temp'] = round(T, 1) RH = si7021.readHumidity() newObject['hum'] = int(RH) debug("Temperature : " + str(newObject['temp']) + " °C") debug("Humidity : " + str(newObject['hum']) + " %") if 'bme280' in devices: ###### Get temperature, pressure & humidity from bme280 ## T, P, RH = bme280.readBME280All() newObject['temp'] = round(T, 1) newObject['hum'] = int(RH) newObject['pres'] = int(P) debug("Temperature : " + str(newObject['temp']) + " °C") debug("Humidity : " + str(newObject['hum']) + " %") debug("Pressure : " + str(newObject['pres']) + " hPa") return (True, newObject)
try: wt = waterTemp.read_temp() params["watertemp"] = wt except Exception as ex: astro_logger.exception(ex) # Log exception info and continue… pass try: CO2 = co2.mh_z19() params["CO2"] = CO2 except Exception as ex: astro_logger.exception(ex) # Log exception info and continue… pass try: light = bh1750.readLight() params["light"] = light except Exception as ex: astro_logger.exception(ex) # Log exception info and continue… pass entry = "" for field in csv_fields: entry += str(params.get(field, "")) + "," entry = entry[:-1] + "\n" # Replace last comma by newline with open(filename, 'a') as csv: csv.write(entry) # print(entry) # call the MQTT function with the parameters to send this data to SURF cloud platform
temp = None # variable for temparature light = None # variable for light level i = 0 # counter for debugging light_state = False light_last_state = False try: while True: # Always call this method every time to send data properly client.loop() # Read data from DHT11 and BH1750 result = dht_pin.read() if result.is_valid(): temp = result.temperature if BH1750.readLight() is not None: light = BH1750.readLight() # Setup data for sending to cloud server via MQTT client.celsiusWrite(1, temp) client.luxWrite(2, light) # Turn light On-Off according to environment light if light < 25: light_state = False else: light_state = True GPIO.output(16, light_state) # Notify user when light state change if light_state is not light_last_state:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) _accesskey = '************************' _secretkey = '*****************************' _hostname = 'api.beebotte.com' bbt = BBT(_accesskey, _secretkey, hostname=_hostname) ### Alternatively you can authenticate using the channel token _token = '**********************************' bbt = BBT(token=_token, hostname=_hostname) while True: if humidity is not None and temperature is not None: print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format( temperature, humidity)) print("Light " + str(bh1750.readLight())) bbt.writeBulk("TestBeebotte", [{ "resource": "temp", "data": temperature }, { "resource": "humid", "data": humidity }, { "resource": "light", "data": bh1750.readLight() }]) else: print('Failed to get reading. Try again!') sys.exit(1)
""" assign event callbacks """ mqttc.on_connect = on_connect mqttc.on_publish = on_publish """ connect """ mqttc.connect(host, port, 60) while True: """ read sensors """ payload_temp = htu21d.readTemp() payload_humid = htu21d.readHumid() payload_light = int(bh1750.readLight()) payload_temp2 = bmp180.readTemp() payload_pres = bmp180.readPressure() payload_alt = bmp180.readAltitude() """ log """ print(host, port, topic_temp, payload_temp) print(host, port, topic_humid, payload_humid) print(host, port, topic_light, payload_light) print(host, port, topic_temp2, payload_temp2) print(host, port, topic_pres, payload_pres) print(host, port, topic_alt, payload_alt) """ publish """
client = mqtt.Client("Pooz") #create new instance client.on_message=on_message #attach function to callback print("connecting to broker") client.connect(broker_address) #connect to broker client.loop_start() #start the loop """ try: while True: bmp = BMP085(0x77) temp = bmp.readTemperature() pressure = bmp.readPressure() altitude = bmp.readAltitude() lightLevel = bh1750.readLight() print("Temperature: %.2f C" % temp) print("Pressure: %.2f hPa" % (pressure / 100.0)) print("Altitude: %.2f" % altitude) h, t = dht.read_retry(dht.DHT22, DHT) print('Temp={0:0.1f}*C \nHumidity={1:0.1f}%\n\n'.format(t, h)) print("Light Level : " + format(lightLevel, '.2f') + " lx") if int(temp) > 30: print( "FIRE DETECTED!\n CALL EMERGENCY!\nFIRST SAVE YOUR LIFE THEN OTHERS!" ) mine.write(h, temp, pressure, lightLevel) # f.write("Temperature: {}, Pressure: {}, Altitude: {}, Humidity: {}, Light: {}".format(temp, pressure, altitude, h, lightLevel))