sohotcall

lamp.py -- ESP8266, 74HC595, N5110 LCD, Relay

Feb 2nd, 2021 (edited)
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #SRVR: Lamp nodeJS server URL
  2. #SSID: Hotspot SSID
  3. #PSWD: Hotspot password
  4. #SCHC: Schedule data count (must be same as set in nodeJS server)
  5. #lamps: 64 bit unsigned, one bit for control each lamp (0=Off, 1=On)
  6. #schs: array of schedule data [(timestamp, lamp_index, state)]
  7. #      array length is SCHC
  8. #      timestamp using formula in tsy() function
  9. #      lamp_index is index of lamp to control (0 to 63)
  10. #      state: -1=clear/do nothing, 0=turn off, 1=turn on, 2=toggle on/off
  11. #      after the schedule task is executed, the state will be changed to -1
  12. #brk (pin GPIO5): pull low to break the program loop, exiting it (entering REPL maybe)
  13. #nCE (pin GPIO4): connect to Nokia display pin CS/CE (chip select/chip enable)
  14. #nDC (pin GPIO0): connect to Nokia display pin DC (data/command)
  15. #sST (pin GPIO2): connect to 74HC595 Serial register ST_CP pin
  16. #pin GPIO14 (SPI SCK): connect to Nokia display pin CLK and 74HC595 Serial register SH_CP pin
  17. #pin GPIO13 (SPI MOSI): connect to Nokia display pin DIN and74HC595 Serial register DS pin
  18. #pin GPIO12 (SPI MISO):
  19. #sn: Serial number, 8 char hexadecimal string
  20. #tsy: convert tuple (year, month, date, 0, hour, minute, second, 0) to timestamp
  21. #     this is simple timestamp, every day is 24 * 60 * 60 seconds
  22. #nTX(x): display 162 char string to Nokia display in 8 row and 21 col
  23. #sDI(x): send inverted 64 bit unsigned x to 74HC595 Serial register (inverted because relay pins are active low)
  24. #        display information about lamp states on Nokia display
  25. #htG(u): http get request to url u, return the result
  26. import ubinascii,time,network,socket,json
  27. from machine import Pin,SoftSPI,RTC
  28. OUT=Pin.OUT
  29. #SECRET
  30. SRVR="http://some.example.com:81/"
  31. #/SECRET
  32. SSID="Chittychitty"
  33. PSWD="silverfang"
  34. SCHC=8
  35. lamps,schs=0,SCHC*[(0,0,-1)]
  36. rtc=RTC()
  37. brk=Pin(5,Pin.IN,Pin.PULL_UP)
  38. nCE=Pin(4,OUT,value=1)
  39. nDC=Pin(0,OUT)
  40. sST=Pin(2,OUT,value=0)
  41. spi=SoftSPI(baudrate=100000,sck=Pin(14),mosi=Pin(13),miso=Pin(12))
  42. sn='{:08x}'.format(int.from_bytes(machine.unique_id(),'little'))
  43. sta=network.WLAN(network.STA_IF)
  44. def slp(x):
  45.  time.sleep_ms(x)
  46. slp(500)
  47. def tsy(d):
  48.  y,m,d,_,h,i,s=d[0:7]
  49.  return 60*(60*(24*(365*y+y//4-y//100+y//400+30*m+((m+bool(m>8))//2)+(2 if bool(y%4)+bool(y%100)+bool(y%400)==2 else 1)*bool(m<3)+d-719560)+h)+i)+s
  50. def nTX(x):
  51.  x='{:>168}'.format(x);nCE.off();nDC.off();spi.write(b'!\x14\xB2 \x0C@\x80');nDC.on()
  52.  f=ubinascii.a2b_base64('IEwfTAhMRFQfNAgfYR5eTF5MHhBkDg4eEhJaZGBxDFUAYANfSBkqYEBRVUQQBAAYLnI5MQ43LiEqIkBQREpRIS4+Py4/Pz8uH3EIHx9fHi4/Lj8yYR8PHxsDOWADcSIQEPRFlP2mL3bBh2yig/HgZGLyEVWj9HTyk3WFjgYmMaoABRj6LpnlAIsDqyMCIQQZdYeVVfpNRR9VdQICilIiFbTyVYx0jQns+Y982YT4eXQRttFND/x8+tkfnY/BBxCE')
  53.  for i in range(0,126):
  54.   t,p=i//21%3*4,i+i//63*21
  55.   p,q=ord(x[p])%96,ord(x[p+21])%96
  56.   p,q,r,s=f[p],f[p+96],f[q],f[q+96]
  57.   y=p&31|r<<6&192,p>>5|q<<2&28|r<<1&192,q>>3|s<<3&192,0,p>>2&7|r<<4&240,q&7|r>>1&48|s<<6&192,q>>5&7|s<<1&240,0,p>>4&1|r<<2&124,q>>2&1|r>>3&12|s<<4&112,q>>7&1|s>>1&124,0
  58.   spi.write(bytes(y[t:t+4]))
  59.  nCE.on()
  60. def sDI(x):
  61.  sST.off();spi.write((~x).to_bytes(8,'big'));sST.on()
  62.  s='GS GoodSwicthes v-001SN:'+sn+10*' '+21*'='+'  01234567 89abcdef  '
  63.  for i in range(0,64):
  64.   s=s+(''if i%16 else str(i//16)+':')+(hex(i%16)[2:]if x&1<<i else'-')+(''if i%16-7 else':')+(''if i%16-15 else':'+str(i//16))
  65.  nTX(s)
  66. def htG(u):
  67.  print(u);_,_,h,f=u.split('/',3);h,p,_=(h+":80:").split(':',2);p=int(p);a=socket.getaddrinfo(h,p)[0][-1];s=socket.socket();s.connect(a);s.send(bytes("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n"%(f,h),'utf8'));d=bytes([]);w=0
  68.  while 1:
  69.   d=d+s.recv(1024)
  70.   if w==len(d)or len(d)>1024:
  71.    break
  72.   w=len(d)
  73.  while s.recv(64):
  74.   pass
  75.  s.close();_,d=str(d,'utf8').split("\r\n\r\n",1)
  76.  return d
  77.  
  78. old='-'
  79. while 1:
  80.  try:
  81.   if not brk.value():
  82.    break
  83.   if not sta.isconnected():
  84.    print("Conn");sta.active(True);sta.connect(SSID,PSWD);slp(10000)
  85.   else:
  86.    rtc.datetime(json.loads(htG(SRVR+"?now=now&sn="+sn)))
  87.    old=htG(SRVR+"?mode=alarm&sn="+sn+"&ack="+old)
  88.    new=json.loads(old)
  89.    lamps=int(new['lamps'])
  90.    for i in range(0,SCHC):
  91.     v=new['schedules'][i]
  92.     schs[i]=tsy(v),int(v[8]),int(v[9])
  93.    sDI(lamps)
  94.   n=tsy(rtc.datetime())
  95.   print("Scheds")
  96.   for i in range(0,SCHC):
  97.    v=schs[i]
  98.    if v[0]<n and v[2]>=0:
  99.     lamps=lamps&~(1<<v[1])|v[2]<<v[1]
  100.     htG(SRVR + "?sn=%s&schclr=%d&lamp=%d&val=%d"%(sn,i,v[1],v[2]))
  101.     sDI(lamps)
  102.   slp(1000)
  103.  except:
  104.   pass
RAW Paste Data