diff --git a/src/boot.py b/src/boot.py index 7b00fde..fe411f4 100644 --- a/src/boot.py +++ b/src/boot.py @@ -2,4 +2,9 @@ #import esp #esp.osdebug(None) import webrepl + +def clear_main(): + import os + os.remove('main.py') + webrepl.start() diff --git a/src/main.py b/src/main.py index c505a39..108ced9 100644 --- a/src/main.py +++ b/src/main.py @@ -1,62 +1,133 @@ +import time + import network -from mqtt import MQTTClient -import machine -import time - -def sub_cb(topic, msg): - print(msg) - -wlan = network.WLAN(network.STA_IF) +from mqtt import MQTTClient +import machine +import tsl2591 +import tcs34725 + +i2c = machine.I2C(-1, machine.Pin(33), machine.Pin(32)) +sensor = tcs34725.TCS34725(i2c) + +tsl = tsl2591.Tsl2591(0) +full, ir = tsl.get_full_luminosity() # read raw values (full spectrum and ir spectrum) +lux = tsl.calculate_lux(full, ir) # convert raw values to lux +print(lux, full, ir) + +running = True + + +def sub_cb(topic, msg): + print(msg) + global running + if msg == b'stop': + print('Stopping execution') + running = False + if msg == b'start' and running is False: + print('Resuming execution') + running = True + main() + + +wlan = network.WLAN(network.STA_IF) wlan.active(True) -#wlan.connect("c-base-botnet", auth=(WLAN.WPA2, "wifipassword"), timeout=5000) - -while not wlan.isconnected(): - machine.idle() -print("Connected to Wifi\n") - -client = MQTTClient("windsensor_0", "192.168.178.98",user="", password="", port=1883) -#client = MQTTClient("windsensor_0", "mqtt.cbrp3.c-base.org",user="", password="", port=1883) -client.set_callback(sub_cb) +# wlan.connect("c-base-botnet", auth=(WLAN.WPA2, "wifipassword"), timeout=5000) + +while not wlan.isconnected(): + machine.idle() + +print("Connected to Wifi\n") + +client = MQTTClient("bioreactor_0", "192.168.178.98", user="", password="", port=1883) +# client = MQTTClient("windsensor_0", "mqtt.cbrp3.c-base.org",user="", password="", port=1883) +client.set_callback(sub_cb) client.connect() -client.subscribe(topic="/hackerfleet/sensors/windsensor/control") +client.subscribe(topic="/hackerfleet/sensors/bioreactor/control") +topic_pub = "/hackerfleet/sensors/bioreactor/status" print("Connected to MQTT") -speedInterrupts = 0 -directionInterrupts = 0 +configuration = { + 'pin_light': 3, + 'pin_rotor': 4, + 'process': [ + { + 'action': 'sleep', + 'duration': 1 + }, + { + 'action': 'start_measurement', + }, + { + 'action': 'spin_rotor', + 'duration': 1, + }, + { + 'action': 'sleep', + 'duration': 0 + }, + { + 'action': 'stop_measurement', + }, + # { + # 'action': 'stop_program' + # } + ] +} -speedTicks = 0 -directionTicks = 0 +colour_sensor_address = 0x29 -def callbackSpeed(pin): - global speedInterrupts - speedInterrupts += 1 -def callbackDirection(pin): - global directionInterrupts - directionInterrupts += 1 +def wait(duration): + print('Sleeping %s seconds' % duration) + time.sleep(duration) -speedPin = machine.Pin(25, machine.Pin.IN, machine.Pin.PULL_UP) -directionPin = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_UP) -speedPin.irq(trigger=machine.Pin.IRQ_FALLING, handler=callbackSpeed) -directionPin.irq(trigger=machine.Pin.IRQ_FALLING, handler=callbackSpeed) +def switch_pin(pin, state): + print('Switching pin %i to %s' % (pin, state)) -print("Pin setup done") -while True: +def start_measurement(): + print('Starting measurement') + data = sensor.read() + print(data) - if speedInterrupts > 0 or directionInterrupts > 0: - state = machine.disable_irq() - if speedInterrupts > 0: - speedTicks += 1 - speedInterrupts -= 1 - if directionInterrupts > 0: - directionTicks += 1 - directionInterrupts -= 1 - machine.enable_irq(state) +def stop_measurement(): + print('Stopping measurement') - print("Direction: ", str(directionTicks), " Speed: ", str(speedTicks)) +def spin_rotor(duration): + print('Stirring') + switch_pin(configuration['pin_rotor'], True) + wait(duration) + print('Stopping') + switch_pin(configuration['pin_rotor'], False) + + +def main(): + global running + + while running: + print('Restarting process') + client.check_msg() + client.publish(topic_pub, "Restarting") + + data = [] + for item in configuration['process']: + client.publish(topic_pub, str(item)) + if item['action'] == 'sleep': + wait(item['duration']) + if item['action'] == 'start_measurement': + start_measurement() + if item['action'] == 'stop_measurement': + stop_measurement() + if item['action'] == 'spin_rotor': + spin_rotor(item['duration']) + if item['action'] == 'stop_program': + print('Stopping program') + running = False + + +main() diff --git a/src/tcs34725.py b/src/tcs34725.py new file mode 100644 index 0000000..6374a62 --- /dev/null +++ b/src/tcs34725.py @@ -0,0 +1,171 @@ +import time +import ustruct + +#const = lambda x:x + +_COMMAND_BIT = const(0x80) + +_REGISTER_ENABLE = const(0x00) +_REGISTER_ATIME = const(0x01) + +_REGISTER_AILT = const(0x04) +_REGISTER_AIHT = const(0x06) + +_REGISTER_ID = const(0x12) + +_REGISTER_APERS = const(0x0c) + +_REGISTER_CONTROL = const(0x0f) + +_REGISTER_SENSORID = const(0x12) + +_REGISTER_STATUS = const(0x13) +_REGISTER_CDATA = const(0x14) +_REGISTER_RDATA = const(0x16) +_REGISTER_GDATA = const(0x18) +_REGISTER_BDATA = const(0x1a) + +_ENABLE_AIEN = const(0x10) +_ENABLE_WEN = const(0x08) +_ENABLE_AEN = const(0x02) +_ENABLE_PON = const(0x01) + +_GAINS = (1, 4, 16, 60) +_CYCLES = (0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60) + + +class TCS34725: + def __init__(self, i2c, address=0x29): + self.i2c = i2c + self.address = address + self._active = False + self.integration_time(2.4) + sensor_id = self.sensor_id() + if sensor_id not in (0x44, 0x10): + raise RuntimeError("wrong sensor id 0x{:x}".format(sensor_id)) + + def _register8(self, register, value=None): + register |= _COMMAND_BIT + if value is None: + return self.i2c.readfrom_mem(self.address, register, 1)[0] + data = ustruct.pack('