def main(): path = 'thing/' + zcoap.eui64() reported = {'state':{'reported':{}}} rtc = RTC() # RTC initialization rtc.setTime(2019, 5, 22, 17, 31, 30) days = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"] formatString = "{}-{:0>2}-{:0>2}({:0>2}) {:0>2}:{:0>2}:{:0>2}" while True: addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) time = rtc.getTime() reported['state']['reported']['time'] = {"time": formatString.format( time[0], time[1], time[2], days[time[3]], time[4], time[5], time[6] )} json = ujson.dumps(reported) cli.request_post(path, json) print(json) sleep(1) cli.close()
def main(): path = 'thing/' + zcoap.eui64() reported = {'state':{'reported':{}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) adxl345 = ADXL345(ADXL345_I2CADDR) while True: reported['state']['reported']['axis'] = adxl345.getAxes() print(ujson.dumps(reported)) cli.request_post(path, ujson.dumps(reported)) time.sleep(5)
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) gps = GPS(0, GPS_BAUDRATE) while True: reported['state']['reported'] = gps.read(("gprmc", "gpgsa")) json = ujson.dumps(reported) print(json) cli.request_post(path, json) time.sleep(1)
def main(): path = 'thing/' + zcoap.eui64() reported = {'state':{'reported':{}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) temp = Temperature(0) while True: reported['state']['reported']['temp'] = temp.read() 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)) air = AirQuality(0) while True: reported['state']['reported']['air_quality'] = air.read() 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)) lsm6ds3 = LSM6DS3(0x6A, True, True) #device_addr, accel_mode, gyros_mode while True: 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)
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) loudness = Loudness(0) while True: reported['state']['reported']['loudness'] = loudness.read() json = ujson.dumps(reported) cli.request_post(path, json) print(json) sleep(1)
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} collision = Collision(0) while True: addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) reported['state']['reported']['collision'] = collision.detect() json = ujson.dumps(reported) cli.request_post(path, json) print(json) sleep(1) cli.close()
def main(): path = 'thing/' + zcoap.eui64() reported = {'state': {'reported': {}}} addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) bus = I2C(1) bmp = BMP280(i2c=bus) while True: reported['state']['reported']['temp'] = bmp.values[0] reported['state']['reported']['pres'] = bmp.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': {}}} bma400 = BMA400(BMA400_I2CADDR) while True: addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) reported['state']['reported']['temp'] = bma400.temprature_read() reported['state']['reported']['axis'] = bma400.accel_axis_read() 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':{}}} mmc7660 = MMC7660(MMC7660_I2CADDR) while True: addr = zcoap.gw_addr() port = 5683 cli = zcoap.client((addr, port)) reported['state']['reported']['axis'] = mmc7660.getAxes() 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': {}}} 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': {}}} 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)) 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 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()