import time import machine import onewire, ds18x20 import json import ubinascii import utils # the device is on GPIO12 class TempMon: def __init__(self, pin): dat = machine.Pin(pin) # create the onewire object self.ds = ds18x20.DS18X20(onewire.OneWire(dat)) # scan for devices on the bus self.roms = self.ds.scan() print('found devices:', self.roms) # loop 10 times and print all temperatures def read_temp(self): try: print('temperatures:', end=' ') self.ds.convert_temp() vals=[] time.sleep_ms(750) for rom in self.roms: vals.append(dict(id=ubinascii.hexlify(rom).decode('utf-8'), temp=self.ds.read_temp(rom))) #print('%s/%s' % (rom, self.ds.read_temp(rom), end=' ')) print(json.dumps(vals)) return vals except Exception: print("error encountered...continuing") class DweetTemp: def __init__(self, thing, pin): self.thing = thing self.t = TempMon(pin) def dweet(self, readings): self.url = 'http://dweet.io/dweet/for/%s?' % self.thing + '&'.join('%s=%s' % (e['id'], e['temp']) for e in readings) print(self.url) utils.http_get(self.url) def post_dweet(self, loop=1, interval=5): for i in range(loop): self.dweet(self.t.read_temp()) time.sleep(interval)