def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} moisture = Moisture(0) while True: addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) reported['state']['reported']['moisture'] = moisture.read() json = ujson.dumps(reported) cli.request_post(path, json) print(json) sleep(30) cli.close()
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} lsm6ds3 = LSM6DS3(0x6A, True, True) #device_addr, accel_mode, gyros_mode while True: addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) reported['state']['reported']['accel'] = lsm6ds3.accel_start() reported['state']['reported']['gyro'] = lsm6ds3.gyros_start() print(ujson.dumps(reported)) cli.request_post(path, ujson.dumps(reported)) time.sleep(5) cli.close()
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} loudness = Loudness(0) while True: addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) reported['state']['reported']['loudness'] = loudness.read() json = ujson.dumps(reported) cli.request_post(path, json) print(json) sleep(1) cli.close()
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} gps = GPS(0, GPS_BAUDRATE) while True: addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) reported['state']['reported'] = gps.read(("gprmc", "gpgsa")) json = ujson.dumps(reported) print(json) cli.request_post(path, json) time.sleep(1) cli.close()
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} air = AirQuality(0) while True: addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) reported['state']['reported']['air_quality'] = air.read() json = ujson.dumps(reported) cli.request_post(path, json) print(json) sleep(60) cli.close()
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) vib = Vibration(0) while True: reported['state']['reported']['vibration'] = "not detected" for i in range(100): if vib.detect(): reported['state']['reported']['detected'] = "detected" sleep(0.01) json = ujson.dumps(reported) cli.request_post(path, json) print(json)
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) bus = I2C(1) sht31 = SHT31(i2c=bus) values = sht31.get_temp_humi() while True: reported['state']['reported']['temp'] = values[0] reported['state']['reported']['humid'] = values[1] print(ujson.dumps(reported)) cli.request_post(path, ujson.dumps(reported)) time.sleep(60)
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) bus = I2C(1) bme = BME280(i2c=bus) while True: reported['state']['reported']['temp'] = bme.values[0] reported['state']['reported']['pres'] = bme.values[1] reported['state']['reported']['humid'] = bme.values[2] print(ujson.dumps(reported)) cli.request_post(path, ujson.dumps(reported)) time.sleep(60)
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) co2 = CO2() while True: concentration = co2.getConcentration() reported['state']['reported']['co2'] = { 'co2concentration': concentration[0], 'temperature': concentration[1], 'humidity': concentration[2] } json = ujson.dumps(reported) cli.request_post(path, json) print(json) sleep(60)
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) pm25 = PM25() while True: concentration = pm25.getConcentration() reported['state']['reported']['pollution'] = { 'pm1.0': concentration[0], 'pm2.5': concentration[1], 'pm10': concentration[2] } json = ujson.dumps(reported) cli.request_post(path, json) print(json) sleep(60)
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) colorSensor = ColorSensor() while True: color = colorSensor.readColor() reported['state']['reported']['color'] = { 'red': color[0], 'green': color[1], 'blue': color[2] } json = ujson.dumps(reported) cli.request_post(path, json) print(json) sleep(60)
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) pin = Pin(('GPIO_1', 5), Pin.OUT) led = Signal(pin, invert=True) led.off() led_status = 'OFF' while True: reported['state']['reported']['led'] = led_status cli.request_post(path, ujson.dumps(reported)) received = cli.request_get(path) if received: try: desired = ujson.loads(received) try: led_status = desired['state']['desired']['led'] except: pass if led_status == 'ON': led.on() else: led.off() except: pass time.sleep(1) cli.close()
import time import ujson def light_sensor(): ADC_REF = 0.6 ADC_RESOLUTION=4096 #12bit ain = ADC(0) ain.gain(ain.GAIN_1_6) #gain set to 1/6 raw = ain.read() v = (raw / ADC_RESOLUTION) * ADC_REF * 6 return v if __name__ == '__main__': path = 'thing/' + zcoap.eui64() reported = {'state':{'reported':{}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) while True: light = round(light_sensor(), 2) reported['state']['reported']['light'] = light print(ujson.dumps(reported)) cli.request_post(path, ujson.dumps(reported)) time.sleep(1) cli.close()