def __init__(self): if hasattr(config, 'DEVICE_ID'): self.device_id = config.DEVICE_ID else: self.device_id = 'mp-' + str(esp.flash_id()) if hasattr(config, 'SEND_INTERVAL_SECONDS'): self.sendIntervalSeconds = config.SEND_INTERVAL_SECONDS else: self.sendIntervalSeconds = DEFAULT_SEND_INTERVAL_SECONDS try: from display import Display self.display = Display() except ImportError: self.display = None try: from sht30 import SHT30 self.sensor = SHT30() except ImportError: self.sensor = None try: from moisture import SoilMoistureSensor self.soilMoistureSensor = SoilMoistureSensor() except ImportError: self.soilMoistureSensor = None try: from mhz19 import MHZ19 self.mhz19 = MHZ19() self.mhz19.start() except ImportError: self.mhz19 = None disable_access_point() do_connect(WLAN_SSID, WLAN_PASSWORD) self.ntptimeWhenZero = 0
def TempSensReport(): from sht30 import SHT30 sensor = SHT30() url = "http://%s:8086/write?db=%s" % (API, DB) for i in range(0, LENGTH + 1): print('Reboot in %d minutes' % (LENGTH - i)) # turn on the WeMOS LED p2.off() # measure values temperature, humidity = sensor.measure() temp = "temp_C,host=%s value=%.02f" % (HOST, temperature) hum = "rel_humidity,host=%s value=%.02f" % (HOST, humidity) # send to InfluxDB API urequests.post(url, data=temp) urequests.post(url, data=hum) # print on uart for warm fuzzy feeling print('Temperature:', temperature, ' C, RH:', humidity, '%') # turn off LED p2.on() sleep(LENGTH) # reboot every range*LENGTH reset()
if device == bmx_address1: bmx_present1 = True if device == light_address1: light_present1 = True if device == display_address2: display_present2 = True if device == bmx_address2: bmx_present2 = True if device == light_address2: light_present2 = True if display_present1: oled = SSD1306_I2C(width, height, i2c, addr=display_address1) if display_present2: oled = SSD1306_I2C(width, height, i2c, addr=display_address2) sensor = SHT30(scl_pin=pin_scl, sda_pin=pin_sda) if bmx_present1: sensor2 = BME280(mode=BME280_OSAMPLE_16, address=bmx_address1, i2c=i2c) if bmx_present2: sensor2 = BME280(mode=BME280_OSAMPLE_16, address=bmx_address2, i2c=i2c) topic1 = b'TempDHT22' topic2 = b'HumidDHT22' topic3 = b'Press' topic4 = b'Light' topic5 = b'LPG' topic6 = b'CO' topic7 = b'ME' client_id = hexlify(unique_id())
from machine import I2C, Pin from sht30 import SHT30 import utime as time PIN_CLK = 29 # PD6, get the pin number from get_pin_number.py PIN_SDA = 30 # PC1 clk = Pin(("clk", PIN_CLK), Pin.OUT_OD) # Select the PIN_CLK as the clock sda = Pin(("sda", PIN_SDA), Pin.OUT_OD) # Select the PIN_SDA as the data line i2c = I2C(-1, clk, sda, freq=100000) sensor = SHT30(i2c, 0x44) #print(senser.measure()) #print(senser.measure_int()) while True: temperature, xx, humidity, yy = sensor.measure_int() print(temperature) print(humidity) time.sleep_ms(500)
from sht30 import SHT30 import sys,time print("Reading out the SHT30 temperature and humidity sensor") print("Program written for the workshop on IoT at the") print("African Internet Summit 2019") print("Copyright: U.Raich") print("Released under the Gnu Public License") if sys.platform == "esp8266": print("Running on ESP8266") pinScl = 5 #ESP8266 GPIO5 (D1 pinSda = 4 #ESP8266 GPIO4 (D2) else: print("Running on ESP32") pinScl = 22 # SCL on esp32 pinSda = 21 # SDA ON ESP32 sht30=SHT30(scl_pin=pinScl, sda_pin=pinSda) if not sht30.is_present(): print("Could not find SHT30 board. Please connect it") sys.exit() else: print("Found SHT-30, let's go on") sht30.reset() while True: temperature, humidity = sht30.measure() print('Temperature',temperature,'°C, RH:',humidity,' %') time.sleep(2)
from sht30 import SHT30, SHT30Error sensor = SHT30() if sensor.is_present(): try: temperature, humidity = sensor.measure() print('SHT30: Temperature:', temperature, 'ºC, RH:', humidity, '%') except SHT30Error as e: print('Error reading SHT30 sensor:', e) else: print('SHT30 sensor is not connected')
from utime import sleep_ms from umqtt_simple import MQTTClient from machine import Pin, unique_id from micropython import schedule, alloc_emergency_exception_buf from esp_init import ESP_init import uheapq from max6675 import MAX6675 from sht30 import SHT30 from ds3231_time import cl_id import gc alloc_emergency_exception_buf(100) sensor_k = MAX6675() sensor3 = SHT30() button = Pin(0, Pin.IN) SERVER = "192.168.100.101" CLIENT_ID = cl_id() TOPIC = b"esp001log" DATA_TP = b"data_" + CLIENT_ID sub_flag = 100 i_pum = 0 i_snd = 0 i_data = [] uheapq.heapify(i_data) class Foo(): def __init__(self): self.push_ref = self.push p2 = Pin(2, Pin.IN) p2.irq(trigger=Pin.IRQ_RISING, handler=self.cb)
# MSG_TXT = "{\"Warehouse_Demo\": \"myPythonDevice\",\"windSpeed\": %.2f,\"temperature\": %.2f,\"humidity\": %.2f}" try: # Get I2C bus, I'm using I2C port 1 bus = smbus.SMBus(1) kwargs = {'address': 0x70} tca9546 = tca9546.TCA9546(bus, kwargs) ## ENVIRONMENTAL MONITOR DEMO START ## time.sleep(.1) tca9546.change_channel(3) time.sleep(.1) kwargs = {'address': 0x44} sht30 = SHT30(bus, kwargs) kwargs = {'address': 0x39, 'wait_enable': 0x08, 'als_enable': 0x02, 'power_on': 0x01, 'als_integration_time': 0xFF, 'wait_time': 0xFF, 'gain': 0x20, 'glass_attenuation': 0x01} tsl2571 = tsl2571.TSL2571(bus, kwargs) kwargs = {'address': 0x50} mq131 = mq131.MQ131(bus, kwargs) kwargs = {'address': 0x77} hp203b = hp203b(bus, kwargs) ## ENVIRONMENTAL MONITOR DEMO END ## ## INDUSTRIAL EQUIPMENT MONITOR DEMO START ## time.sleep(.1) tca9546.change_channel(2)
def getTemp(): sensor = SHT30() return sensor.measure()
def ESP_init(cl_s=b'esp001data'): f_log = StringIO() f_log.write('{"ESP_init":{\n') atemp = '"DATA_topic":"%s",\n' % (cl_s.decode()) print(atemp) f_log.write(atemp) try: if ds_tm.lost_time(): atemp = '"DS3231":"lost_time",\n' print(atemp) f_log.write(atemp) raise else: ds_tm.settime() atemp = '"DS3231 SetTime":"OK",\n' print(atemp) f_log.write(atemp) try: ntptime_cn.time() utime.sleep_ms(200) ntptime_cn.settime() ds_tm.sync_rtc2ntp() ds_tm.settime() atemp = '"NTP & RTC SyncTime":"OK",\n' print(atemp) f_log.write(atemp) except: pass except: ntptime_cn.time() utime.sleep(3) ntptime_cn.settime() atemp = '"NTP SetTime":"OK",\n' print(atemp) f_log.write(atemp) try: ds_tm.sync_rtc2ntp() ds_tm.settime() atemp = '"NTP & RTC SyncTime":"OK",\n' print(atemp) f_log.write(atemp) except: pass finally: atemp = '"Time":"%d-%02d-%02d %02d:%02d:%02d GMT",\n"Day_W":%d,"Day_Y":%d,\n' % ( utime.localtime()) print(atemp) f_log.write(atemp) try: atemp = '"DS3231 SQW":"1Hz Out Start%d",\n' % (ds_tm.sqw_1hz()) print(atemp) f_log.write(atemp) except: atemp = '"DS3231 SQW":"1Hz Out ERROR",\n' print(atemp) f_log.write(atemp) try: sensor3 = SHT30() temperature, humidity = sensor3.measure() del sensor3 atemp = '"ambient Temperature":"%f ºC","ambient Humidity":"%f %%",\n' % ( temperature, humidity) print(atemp) f_log.write(atemp) atemp = '"Sensor SHT30":"OK",\n' print(atemp) f_log.write(atemp) except: pass try: sensor_k = MAX6675() atemp = '"K Thermocouple":"%f ºC",\n"Sensor MAX6675":"OK"}}..' % ( sensor_k.measure()) del sensor_k print(atemp) f_log.write(atemp) except: pass f_str = f_log.getvalue() f_log.close() return f_str
if device == bmx_address2: bmx_present2 = True if device == light_address2: light_present2 = True if device == ds1307_address: ds1307_present = True if display_present2: oled = SSD1306_I2C(width, height, i2c, addr=display_address2) if display_present1: oled = SSD1306_I2C(width, height, i2c, addr=display_address1) if ahtx0_present: sensor = AHT20(i2c) if sht3x_present2: sensor = SHT30(scl_pin=pin_scl, sda_pin=pin_sda, i2c_address=sht3x_address2) if sht3x_present1: sensor = SHT30(scl_pin=pin_scl, sda_pin=pin_sda, i2c_address=sht3x_address1) if bmx_present2: sensor2 = BME280(mode=BME280_OSAMPLE_16, address=bmx_address2, i2c=i2c) if bmx_present1: sensor2 = BME280(mode=BME280_OSAMPLE_16, address=bmx_address1, i2c=i2c) if ds1307_present: rtc = DS1307(i2c) topic1 = b'TempDHT22' topic2 = b'HumidDHT22' topic3 = b'Press'
def __init__(self, i2c): self.sht = SHT30()
def do_measurements(): if my_config_dict['node_version'] == 0x02: try: addr68_ch0 = MCP342x(i2c_irt, 0x68, channel=0, resolution=18, gain=8, scale_factor=1000.0) addr68_ch1 = MCP342x(i2c_irt, 0x68, channel=1, resolution=18, gain=8, scale_factor=1000.0) addr68_ch2 = MCP342x(i2c_irt, 0x68, channel=2, resolution=18, gain=8, scale_factor=1000.0) addr68_ch3 = MCP342x(i2c_irt, 0x68, channel=3, resolution=18, gain=8, scale_factor=1000.0) float_values[0] = addr68_ch0.convert_and_read() float_values[1] = addr68_ch1.convert_and_read() float_values[2] = addr68_ch2.convert_and_read() float_values[3] = addr68_ch3.convert_and_read() float_values[4] = float_values[0] * 5.0 float_values[5] = 0.0 except Exception as ex: rgb_blink(100, 0x13f2ab) print("Couldn't find ADC") print(ex) float_values[0] = -1.0 float_values[1] = -1.0 float_values[2] = -1.0 float_values[3] = -1.0 float_values[4] = -1.0 float_values[5] = 0.0 else: print("Waking up I2C sensors...") ### IRT Sensor irt = None try: irt = MLX90614(i2c_irt, 90) print("Waking IRT...OK") time.sleep(1) float_values[0] = irt.read_ambient_temp() float_values[1] = irt.read_object_temp() rgb_blink(100, 0xa013f2) except Exception as error: red_blink(1000) irt = None print("Couldn't find IRT") float_values[0] = -1.0 float_values[1] = -1.0 wdt.feed() ### Air sensor if my_config_dict["air_sensor"] == NONE: print("Ignoring air sensor...") float_values[2] = 0.0 float_values[3] = 0.0 float_values[4] = 0.0 elif my_config_dict["air_sensor"] == BME280: bme = None try: bme = BME280(address=BME280_I2CADDR, i2c=i2c_air) print("Waking BME280...OK") time.sleep(1) float_values[2] = bme.read_temperature() / 100.0 float_values[3] = bme.read_humidity() / 1024.0 float_values[4] = bme.read_pressure() / 256.0 / 100.0 rgb_blink(100, 0x0000ff) except Exception as error: red_blink(1000) print("Couldn't find BME") float_values[2] = -1.0 float_values[3] = -1.0 float_values[4] = -1.0 elif my_config_dict["air_sensor"] == SHT3x: sht30 = None try: sht30 = SHT30(i2c_air) print("Waking SHT3x...OK") time.sleep(1) float_values[2], float_values[3] = sht30.measure() float_values[4] = 0.0 green_blink(100) except Exception as error: red_blink(1000) print("Couldn't find SHT30") float_values[2] = -1.0 float_values[3] = -1.0 float_values[4] = 0.0 elif my_config_dict["air_sensor"] == SHT3x_single: sht30 = None try: sht30 = SHT30(i2c_air, i2c_address=0x44) print("Waking SHT3x...OK") time.sleep(1) float_values[2], float_values[3] = sht30.measure() float_values[4] = 0.0 green_blink(100) except Exception as error: rgb_blink(100, 0x13f2ab) print("Couldn't find SHT30") float_values[2] = -1.0 float_values[3] = -1.0 float_values[4] = 0.0 else: print("Sensor not supported") wdt.feed() ### Soil sensor temp = None try: ow_devices = ow.scan() if len(ow_devices) > 0: print("Waking OWD...OK") if len(ow_devices) > 0: ow_id = ubinascii.hexlify(ow_devices[0]).decode('ascii') else: ow_id = "None" print("OW ID: {}".format(ow_id)) temp = DS18X20(ow) time.sleep(1) temp.start_convertion() time.sleep(1) float_values[5] = temp.read_temp_async() if float_values[5] is None: float_values[5] = -100 rgb_blink(100, 0xf2b313) else: print("Soil temp not found") red_blink(1000) float_values[5] = -1.0 #time.sleep(1) except Exception as error: print(error) red_blink(1000) print("Couldn't find OWD") float_values[5] = -100.0 wdt.feed() # Battery sensing adc = machine.ADC(0) batt = adc.channel(pin='P16', attn=3) float_values[6] = (batt.value() / 4096.0) * 354.8 / 31.6 print("Battery: {}V".format(float_values[6]))
cayenne.mqttConnect() print("Successfully connected to myDevices MQTT broker") def senddata(): temperature, humidity = sht30.measure() dataString = 'temp,c=%6.3f' % temperature print("Publishing temperature: %s" % dataString) cayenne.publish(str(temperatureChannel), dataString) time.sleep(5) dataString = 'rel_hum,p=%6.3f' % humidity print("Publishing humidity: %s" % dataString) cayenne.publish(str(humidityChannel), dataString) time.sleep(5) sht30 = SHT30() if not sht30.is_present(): print("Could not find SHT30 board. Please connect it") sys.exit() else: print("Found SHT-30, let's go on") sht30.reset() while True: try: senddata() except OSError: pass
from ssd1306 import SSD1306_I2C from sht30 import SHT30 try: i = I2C(-1, Pin(5), Pin(4)) devices = i.scan() if not devices: print("No I2C devices detected") if 60 in devices and 49 in devices: s = SSD1306_I2C(64, 48, i) s.text("Pycon", 0, 5) s.text("Limerick", 0, 20) s.text("March 23", 0, 35) s.show() else: print("OLED shield not found") if 69 in devices: t = SHT30() if not t.is_present(): print("SHT not present") else: t.measure() else: print("SHT30 shield not found") except Exception: p = Pin(2, Pin.OUT) raise