def main(): sensor_data = { 'temperature': None, 'humidity': None, 'luminosity': None } ### SHT31: Temperature and Humidity Sensor ### sht31_instance = sht31.SHT31(SHT31_ADDRESS, SHT31_CHANNEL) sht31_result = sht31_instance.read() sensor_data['temperature'] = round(sht31_result[0], 2) sensor_data['humidity'] = round(sht31_result[1], 2) ### TSL2561: Luminosity Sensor ### tsl2561_instance = tsl2561.TSL2561(TSL2561_ADDRESS, TSL2561_CHANNEL) tsl2561_result = tsl2561_instance.read() sensor_data['luminosity'] = round(tsl2561_result[0], 1) client = boto3.client('cloudwatch') for i in sensor_data.keys(): metric_data = None metric_data = [ { 'MetricName': DEVICE_ID + '-' + i, 'Value': sensor_data[i], 'Unit': 'Count', 'Dimensions': [ { 'Name': 'Device', 'Value': DEVICE_ID } ] } ] client.put_metric_data(Namespace=NAMESPACE, MetricData=metric_data)
def create_message(mail_to, subject, device, smart_plug_ip_address): msg = MIMEMultipart() msg['To'] = mail_to msg['Subject'] = subject now = datetime.datetime.now() body = "Date: %s \n" % now.strftime("%Y-%m-%d %H:%M:%S") sht31_result = sht31.SHT31(SHT31_ADDRESS, SHT31_CHANNEL).read() tsl2561_result = tsl2561.TSL2561(TSL2561_ADDRESS, TSL2561_CHANNEL).read() body += "Temperature(C): %s \n" % round(sht31_result[0], 2) body += "Humidity(%%): %s \n" % round(sht31_result[1], 2) body += "Luminosity(lx): %s \n" % round(tsl2561_result[0], 1) body = MIMEText(body, 'plain', 'utf-8') msg.attach(body) command = ("pyhs100 --ip=%s on" % smart_plug_ip_address) os.system(command) time.sleep(3) command = ("fswebcam -d %s -r 1280x960 --jpeg 95 %s" % (device, IMAGE)) os.system(command) time.sleep(1) command = ("pyhs100 --ip=%s off" % smart_plug_ip_address) os.system(command) attachment = MIMEBase("image", "jpeg") file = open(IMAGE) attachment.set_payload(file.read()) file.close() command = "rm %s" % IMAGE os.system(command) Encoders.encode_base64(attachment) msg.attach(attachment) return msg
import machine import sht31 #AWS MQTT client cert example for esp8266 or esp32 running MicroPython 1.9 from umqtt.robust import MQTTClient import time # SHT31 Pin Init i = machine.I2C(sda=machine.Pin(5), scl=machine.Pin(4)) s = sht31.SHT31(i) # Temperature Vals (TRUE) tempLimit = 23.0 tMax = 24.0 # Humidity Vals (TRUE) humidLimit = 90.0 hMax = 97.0 # # Temperature Vals (FALSE) # tempLimit = 30.0 # tMax = 35.0 # # Humidity Vals (FALSE) # humidLimit = 95.0 # hMax = 100.0 # 2 Relay Module Pin Init heaterPin = machine.Pin(0, machine.Pin.OUT) humidiPin = machine.Pin(2, machine.Pin.OUT) #This works for either ESP8266 ESP32 if you rename certs before moving into /flash
"""Handles all sensor inputs for the XBee module.""" import machine import sht31 # setup I2C for SHT31 i2c = machine.I2C(id=1, freq=400000) sht31sensor = sht31.SHT31(i2c) def fahrenheit(celsius): """Converts celsius to fahrenheit""" return (celsius * (9 / 5)) + 32 def tempSHT31(): """Read temp and humidity from SHT31""" return sht31sensor.get_temp_humi() def read(xb, timeout=15): """Reads all sensor inputs from the XBee module. xb: an already initialized xbee module. timeout: the time to wait for all sensor inputs.""" readings = {} temp, humidity = tempSHT31() readings['temp'] = temp readings['humidity'] = humidity
sdaPin = 4 sclPin = 5 # For DHT22 - This sensor does not seem too accurate # Set up object for reading sensors dht22 = dht.DHT22(machine.Pin(dhtPin)) dhtTempUnit = 'C' dhtRHUnit = 'RH' dhtTempId = '01001' dhtHumId = '01002' # for sht31 set up i2c i2c = machine.I2C(sda=machine.Pin(sdaPin), scl=machine.Pin(sclPin)) shtBoard = sht31.SHT31(i2c) shtTempUnit = 'F' shtRHUnit = 'RH' shtTempId = '01003' shtHumId = '01004' # set up socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) server_address = ("192.168.1.210", 21567) # Set up message Header # # Standard message format to make reading multiple sensors easier # # Format is:
from machine import Pin, I2C import sht31 i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000) sensor = sht31.SHT31(i2c, addr=0x44) sensor.get_temp_humi()
#Thermostat test script This tests all functionality import wiringpi as io import time import Adafruit_CharLCD as LCD import sht31 sht31 = sht31.SHT31(0) LOW = 0 HIGH = 1 OUTPUT = 1 RELAY = 7 io.wiringPiSetup() io.pinMode(RELAY,OUTPUT) lcd = LCD.Adafruit_CharLCDPlate() # Test Relay io.digitalWrite(RELAY,HIGH) time.sleep(1) io.digitalWrite(RELAY,LOW) time.sleep(1) # activate screen lcd.set_color(1,1,1) lcd.clear() time.sleep(1) lcd.message('Hello!') time.sleep(1) # initialize temp temperature, humidity = sht31.get_temp_and_humidity()
# The feed_id parameter identifies the feed, and the payload parameter has # the new value. print 'Feed {0} received new value: {1}'.format(feed_id, payload) # Create an MQTT client instance. client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) # Setup the callback functions defined above. client.on_connect = connected client.on_disconnect = disconnected client.on_message = message # Connect to the Adafruit IO server. client.connect() # Client loop function client.loop_background() print 'Publishing a new message every 10 seconds (press Ctrl-C to quit)...' with sht31.SHT31(1) as sht31: while True: temperature, humidity = sht31.get_temp_and_humidity() print 'publishing {0} to Temperature Feed'.format( round(temperature * 9 / 5 + 32), 1) print 'Publishing {0} to Humidity Feed'.format(round(humidity), 1) client.publish('Temperature', temperature) client.publish('Humidity', humidity) time.sleep(10)
import sht31 from machine import Pin, I2C from ssd1306 import SSD1306_I2C i2c = I2C(sda=Pin("Y8"), scl=Pin("Y6")) oled = SSD1306_I2C(128, 64, i2c, addr=0x3c) sht = sht31.SHT31(i2c_address=0x44) rtc = pyb.RTC() while True: shtdata = sht.measure() t = rtc.datetime() oled.fill(0) oled.text("temp:" + str(shtdata[0]), 0, 10) oled.text("Humi:" + str(shtdata[1]), 0, 30) oled.text("Time:" + str(t[4]) + ":" + str(t[5]) + ":" + str(t[6]), 0, 45) print(str(t[6])) print(shtdata[0]) print(shtdata[1]) oled.show() pyb.delay(300)
# Timezone info just for outputting proper localtime format utc_offset_sec = time.altzone if time.localtime().tm_isdst else time.timezone utc_offset = datetime.timedelta(seconds=-utc_offset_sec) # Setup the GPIO pins GPIO.setmode(GPIO.BCM) # GPIO.BCM to use pin numbers like 17 for GPIO17 GPIO.setwarnings(True) ac = Appliance(PIN_AC) heat = Appliance(PIN_HEAT) humid = Appliance(PIN_HUMID) dehumid = Appliance(PIN_DEHUMID) light = Appliance(PIN_LIGHT) # Setup the temperature/RH sensor sht = sht31.SHT31(smbus.SMBus(1), addr_gpio=PIN_SHT31) try: while True: T, H = sht.read(rep='high', nofail=True) if sht.get_last_read_error() is not None: # read failed, delay and try again # @TCC NEED TO TRIGGER SENDING AN ALARM HERE print("T/RH READ FAILED") time.sleep(READ_FAILED_POLLING_TIME) continue ## Light ## nowtime = datetime.datetime.now() tmp = datetime.datetime.strptime(LIGHT_ON_TIME, "%H:%M") ontime = nowtime.replace(hour=tmp.hour, minute=tmp.minute) tmp = datetime.datetime.strptime(LIGHT_OFF_TIME, "%H:%M")
import sht31 import ds18b20 with open("weather.csv", "a") as csv: temp_sensors = ds18b20.get_devices() header = '# time, co2, humidity, int_temp' sensor_ids = [] for sensor_id, sensor_name in temp_sensors.iteritems(): header += ', %s' % sensor_name sensor_ids.append(sensor_id) print header csv.write(header + "\n") sht = sht31.SHT31() co2_sensor = k30.K30() while True: current_time = datetime.datetime.now() (int_temp, humidity) = sht.get_temp_and_humidity() co2 = co2_sensor.get_ppm() data_line = "%s, %d, %f, %f" % (current_time, co2, humidity, int_temp) for sensor_id in sensor_ids: data_line += ", " + ds18b20.read_temp(sensor_id) time.sleep(.1) print data_line csv.write(data_line + "\n")
from machine import I2C import time import sht31 i2c = I2C(0, I2C.MASTER, baudrate=100000) s31 = sht31.SHT31(i2c) while (True): data = s31.get_temp_humi(celsius=True) print(data) time.sleep(1)