Esempio n. 1
0
 def temperature(self):
     if runtime() - self.last_poll > 2000:
         self.last_poll = runtime()
         try:
             self.weather.measure()
         except Exception:
             pass
     return self.weather.temperature()
Esempio n. 2
0
 def humidity(self):
     if runtime() - self.last_poll > 2000:
         self.last_poll = runtime()
         try:
             self.weather.measure()
         except Exception:
             pass
     return self.weather.humidity()
Esempio n. 3
0
 def _handler(self, source):
     state = self.button.value()
     now = runtime()
     if state == self.last_state:
         return
     self.last_state = state
     self.his.append(runtime())
     if len(self.his) < 2:
         return
     if self.his[-1] - self.his[-2] > 500:
         print('hold for ', (self.his[-1] - self.his[-2]) // 1000,
               'seconds')
         self.execute('hold', (self.his[-1] - self.his[-2]) // 1000)
         self.his.clear()
Esempio n. 4
0
 def __init__(self,
              client_id,
              server,
              port=0,
              user=None,
              password=None,
              keepalive=0,
              ssl=False,
              ssl_params={}):
     if port == 0:
         port = 8883 if ssl else 1883
     self.client_id = client_id
     self.sock = None
     self.addr = None
     self.ssl = ssl
     self.ssl_params = ssl_params
     self.pid = 0
     self.cb = None
     self.user = user
     self.pswd = password
     self.keepalive = keepalive
     self.lw_topic = None
     self.lw_msg = None
     self.lw_qos = 0
     self.lw_retain = False
     self.server = server
     self.port = port
     self.last = runtime()
Esempio n. 5
0
 async def _async_handler(self):
     while True:
         await asyncio.sleep_ms(500)
         if self.button.value() == 0:
             if len(self.his) and (runtime() - self.his[-1]) > 500:
                 print('pressed ', len(self.his) // 2)
                 self.execute('pressed', len(self.his) // 2)
                 self.his.clear()
Esempio n. 6
0
 def publish(self, topic, msg, retain=False, qos=0):
     if runtime() - self.last < 20:
         sleep_ms(1)
     self.last = runtime()
     try:
         pkt = bytearray(b"\x30\0\0\0")
         pkt[0] |= qos << 1 | retain
         sz = 2 + len(topic) + len(msg)
         if qos > 0:
             sz += 2
         assert sz < 2097152
         i = 1
         while sz > 0x7f:
             pkt[i] = (sz & 0x7f) | 0x80
             sz >>= 7
             i += 1
         pkt[i] = sz
         #print(hex(len(pkt)), hexlify(pkt, ":"))
         self.sock.write(pkt, i + 1)
         self._send_str(topic)
         if qos > 0:
             self.pid += 1
             pid = self.pid
             struct.pack_into("!H", pkt, 0, pid)
             self.sock.write(pkt, 2)
         self.sock.write(msg)
         if qos == 1:
             while 1:
                 op = self.wait_msg()
                 if op == 0x40:
                     sz = self.sock.read(1)
                     assert sz == b"\x02"
                     rcv_pid = self.sock.read(2)
                     rcv_pid = rcv_pid[0] << 8 | rcv_pid[1]
                     if pid == rcv_pid:
                         return
         elif qos == 2:
             assert 0
     except Exception:
         from Blocky.Network import network
         network.connect()
Esempio n. 7
0
 def __init__(self, port, module='DHT11'):
     pin = getPin(port)
     if (pin[0] == None):
         return
     if module == 'DHT11': self.weather = DHT11(Pin(pin[0]))
     elif module == 'DHT22': self.weather = DHT22(Pin(pin[0]))
     elif module == 'DHTBase': self.weather = DHTBase(Pin(pin[0]))
     else:
         raise NameError
     self.last_poll = runtime()
     self.cb_humidity = []
     self.cb_temperature = []
     loop = asyncio.get_event_loop()
     loop.call_soon(self.handler())
Esempio n. 8
0
 def __init__(self, port):
     self.p = getPin(port)
     if self.p[0] == None:
         return
     self.last_time = runtime()
     self.last_state = 0
     self.number = 0
     self.ButtonTaskList = {}
     self.his = []
     self.button = Pin(self.p[0], Pin.IN, Pin.PULL_DOWN)
     self.button.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING,
                     handler=self._handler)
     loop = asyncio.get_event_loop()
     loop.create_task(Cancellable(self._async_handler)())
Esempio n. 9
0
 def __init__(self, port, module='DHT11'):
     pin = getPin(port)
     if (pin[0] == None):
         from machine import reset
         reset()
     if module == 'DHT11': self.weather = DHT11(Pin(pin[0]))
     elif module == 'DHT22': self.weather = DHT22(Pin(pin[0]))
     elif module == 'DHTBase': self.weather = DHTBase(Pin(pin[0]))
     else:
         raise NameError
     self.last_poll = runtime()
     self.cb_humidity = None
     self.cb_temperature = None
     self._handler = False
Esempio n. 10
0
def sync_ntp():
    try:
        import usocket, ustruct
        NTP_QUERY = bytearray(48)
        NTP_QUERY[0] = 0x1b
        s = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
        s.settimeout(2)
        res = s.sendto(NTP_QUERY,
                       usocket.getaddrinfo("pool.ntp.org", 123)[0][-1])
        msg = s.recv(48)
        s.close()
        ntp = ustruct.unpack("!I", msg[40:44])[0]
        ntp -= 3155673600
        if ntp > 0:  # Correct time
            from time import localtime
            ntp += gmt * 3600  #GMT time
            global NTP_PAIR
            NTP_PAIR = [runtime(), ntp]
            print('Synced ', localtime(ntp))

    except Exception as err:

        print('ntp-sync->', err)
        return
Esempio n. 11
0
	def time(self):
		return runtime()