sohotcall

micropython esp8266 lampu.py

Aug 9th, 2019
139
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import machine, network
  2. import socket, json, time
  3. LED_COUNT = 8
  4. SCHEDULE_COUNT = 8
  5. sta = network.WLAN( network.STA_IF )
  6. schedules = SCHEDULE_COUNT * [ ( 0, 0, -1 ) ]
  7. lampus = LED_COUNT * [0]
  8. rtc = machine.RTC()
  9. server = "http://192.168.43.109:83/"
  10.  
  11. d0 = machine.Pin( 16, machine.Pin.OUT );
  12. d1 = machine.Pin( 5, machine.Pin.OUT );
  13. d2 = machine.Pin( 4, machine.Pin.OUT );
  14. d3 = machine.Pin( 0, machine.Pin.OUT );
  15. d4 = machine.Pin( 2, machine.Pin.OUT );
  16. d5 = machine.Pin( 14, machine.Pin.OUT );
  17. d6 = machine.Pin( 12, machine.Pin.OUT );
  18. d8 = machine.Pin( 15, machine.Pin.OUT );
  19. d7 = machine.Pin( 13, machine.Pin.IN, machine.Pin.PULL_UP );
  20. def lampout():
  21.     d0.value(not lampus[0])
  22.     d1.value(not lampus[1])
  23.     d2.value(not lampus[2])
  24.     d3.value(not lampus[3])
  25.     d4.value(not lampus[4])
  26.     d5.value(not lampus[5])
  27.     d6.value(not lampus[6])
  28.     d8.value(not lampus[7])
  29.     #d7.value(not lampus[8])
  30.    
  31.    
  32. def to_ts( d ):
  33.     y, m, d, h, i, s = tuple( d[0:6] )
  34.     return 24 * 3600 * (
  35.             365 * y + y // 4 - y // 100 + y // 400 + 30 * m
  36.             + ( ( m + ( 1 if m > 8 else 0 ) ) // 2 )
  37.             + (
  38.                 2 if
  39.                 ( 0 if y % 4 > 0 else 1 )
  40.                 - ( 0 if y % 100 > 0 else 1 )
  41.                 + ( 0 if y % 400 > 0 else 1 )
  42.                 == 0
  43.                 else 1
  44.             ) * ( 1 if m < 3 else 0) + d - 719560
  45.             ) + h * 3600 + i * 60 + s
  46.  
  47. def http_get( url ):
  48.     _, _, host, path = url.split( '/', 3 )
  49.     host, port, _ = ( host + ":80:" ).split( ':', 2 )
  50.     port = int( port )
  51.     addr = socket.getaddrinfo( host, port )[0][-1]
  52.     s = socket.socket()
  53.     s.connect( addr )
  54.     s.send( bytes( "GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % ( path, host), 'utf8' ))
  55.     data = bytes([])
  56.     last = 0
  57.     while True:
  58.         data = data + s.recv( 2048 )
  59.         if last == len(data) or len(data)>1000:
  60.             break
  61.         last = len(data)
  62.     while( s.recv( 128 ) ):
  63.         pass
  64.     s.close()
  65.     _, data = str( data, 'utf8' ).split( "\r\n\r\n", 2 )
  66.     return data
  67.  
  68. current = 'pins'
  69.  
  70. while True:
  71.     try:
  72.         if not d7.value():
  73.             break
  74.         if not sta.isconnected():
  75.             print( "Connecting" )
  76.             sta.active( True )
  77.             sta.connect( "test", "wifiwifi" )
  78.             time.sleep( 10 )
  79.         else:
  80.             now = json.loads( http_get( server + "?now=now" ) )
  81.             rtc.datetime( ( now[0], now[1], now[2], 0, now[3], now[4], now[5], 0 ) )
  82.             print( "Checking..." )
  83.             current = http_get( server + "?mode=alarm&current=%s" % ( current ) )
  84.             new = json.loads( current )
  85.             print( "Set lamps" )
  86.             for i in range(0, LED_COUNT):
  87.                 lampus[ i ] = new['lampus'][i]
  88.             for i in range(0, SCHEDULE_COUNT):
  89.                 schedule=new['schedules'][i]
  90.                 schedules[i]=(to_ts(schedule),int(schedule[6]),int(schedule[7]))
  91.             lampout()
  92.         now = rtc.datetime()
  93.         now = to_ts( ( now[0], now[1], now[2], now[4], now[5], now[6] ) ) #ignore now[3]
  94.         print("Set schedules")
  95.         for i in range(0, SCHEDULE_COUNT):
  96.             schedule = schedules[i]
  97.             if schedule[0] < now and schedule[2] >= 0:
  98.                 lampus[ schedule[1] ] = schedule[2]
  99.                 http_get( server + "?lampu=%d&value=%d" % ( schedule[1], schedule[2] ) )
  100.                 http_get( server + "?clear_schedule=%d" % ( i ) )
  101.                 lampout()
  102.         time.sleep(1)
  103.     except:
  104.         pass
RAW Paste Data