def __init__(self, ssid, password, mqtt_host, mqtt_root_topic, pin_soil_power, pin_pump, pin_scl, pin_sda, i2c_addr_bme280, event_periods, debug): """Setup the application""" self._events = [] self.should_bail = False self.debug = debug self.event_periods = event_periods # ensure network is up do_network_connect(ssid, password) # configure mqtt client self.mqtt_root_topic = mqtt_root_topic self.mqtt_client = MQTTClient("umqtt_client", mqtt_host) self.mqtt_client.set_callback(self.mqtt_recieve) self.mqtt_client.connect() # configure output pins self.pin_soil_power = machine.Pin(pin_soil_power, machine.Pin.OUT) self.pin_pump = machine.Pin(pin_pump, machine.Pin.OUT) # set up i2c bus and sensors self.i2c = machine.I2C(scl=machine.Pin(pin_scl), sda=machine.Pin(pin_sda)) self.sensor_bme280 = bme280.BME280(i2c=self.i2c, address=i2c_addr_bme280) self.sensor_si1145 = si1145.SI1145(i2c=self.i2c) self.sensor_adc = ads1x15.ADS1015(self.i2c) # topic to trigger event loop end self.mqtt_client.subscribe(self.mqtt_make_topic("halt")) self.mqtt_client.subscribe(self.mqtt_make_topic("water_plant")) # fire off initial events. These are self submitting so each one # will submit the next job to the event queue. self.event_update_ntp(utime.time()) current_time = utime.time() self.event_temperature(current_time) self.event_light(current_time) self.event_soil_moisture(current_time) self.schedule_pump_on(current_time)
import ads1x15 from machine import I2C, Pin i2c = I2C(scl=Pin(5), sda=Pin(4)) ads = ads1x15.ADS1015(i2c=i2c) while True: print(ads.read(0), ads.read(1))
from array import array import time addr = 72 gain = 1 _BUFFERSIZE = const(512) data = array("h", (0 for _ in range(_BUFFERSIZE))) timestamp = array("L", (0 for _ in range(_BUFFERSIZE))) #i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000) i2c = I2C(-1, Pin(14), Pin(2),freq=400000) # for the Pycom branch, use: # i2c = I2C() ads = ads1x15.ADS1015(i2c, addr, gain) while True: value = ads.read() volts = ads.raw_to_v(value) print(value,volts) time.sleep(1) # # Interrupt service routine for data acquisition # called by a timer interrupt # """ def sample(x, adc = ads.alert_read, data=data, timestamp = timestamp):
pin_s2 = machine.Pin(5, machine.Pin.OUT) pin_s3 = machine.Pin(17, machine.Pin.OUT) muxer = hp4067.HP4067(pin_en, pin_s0, pin_s1, pin_s2, pin_s3) muxer.enable() muxer.set_channel(0) # ADC Setup ads_address = 72 adc_gain = 0 adc_rate = 6 pin_scl = machine.Pin(22) pin_sda = machine.Pin(23) i2c = machine.I2C(sda=pin_sda, scl=pin_scl, freq=400000) adc = ads1x15.ADS1015(i2c, ads_address, adc_gain) # Functions def voltage_shunt(): return adc.raw_to_v(adc.read(get_adc_rate(), 0)) def total_current(v_shunt): return v_shunt / R_mux[muxer.get_channel()] def voltage_sensor(v_shunt): total_voltage = get_total_voltage() return total_voltage - v_shunt