Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #SRVR: Lamp nodeJS server URL
- #SSID: Hotspot SSID
- #PSWD: Hotspot password
- #SCHC: Schedule data count (must be same as set in nodeJS server)
- #lamps: 64 bit unsigned, one bit for control each lamp (0=Off, 1=On)
- #schs: array of schedule data [(timestamp, lamp_index, state)]
- # array length is SCHC
- # timestamp using formula in tsy() function
- # lamp_index is index of lamp to control (0 to 63)
- # state: -1=clear/do nothing, 0=turn off, 1=turn on, 2=toggle on/off
- # after the schedule task is executed, the state will be changed to -1
- #brk (pin GPIO5): pull low to break the program loop, exiting it (entering REPL maybe)
- #nCE (pin GPIO4): connect to Nokia display pin CS/CE (chip select/chip enable)
- #nDC (pin GPIO0): connect to Nokia display pin DC (data/command)
- #sST (pin GPIO2): connect to 74HC595 Serial register ST_CP pin
- #pin GPIO14 (SPI SCK): connect to Nokia display pin CLK and 74HC595 Serial register SH_CP pin
- #pin GPIO13 (SPI MOSI): connect to Nokia display pin DIN and74HC595 Serial register DS pin
- #pin GPIO12 (SPI MISO):
- #sn: Serial number, 8 char hexadecimal string
- #tsy: convert tuple (year, month, date, 0, hour, minute, second, 0) to timestamp
- # this is simple timestamp, every day is 24 * 60 * 60 seconds
- #nTX(x): display 162 char string to Nokia display in 8 row and 21 col
- #sDI(x): send inverted 64 bit unsigned x to 74HC595 Serial register (inverted because relay pins are active low)
- # display information about lamp states on Nokia display
- #htG(u): http get request to url u, return the result
- import ubinascii,time,network,socket,json
- from machine import Pin,SoftSPI,RTC
- OUT=Pin.OUT
- #SECRET
- SRVR="http://some.example.com:81/"
- #/SECRET
- SSID="Chittychitty"
- PSWD="silverfang"
- SCHC=8
- lamps,schs=0,SCHC*[(0,0,-1)]
- rtc=RTC()
- brk=Pin(5,Pin.IN,Pin.PULL_UP)
- nCE=Pin(4,OUT,value=1)
- nDC=Pin(0,OUT)
- sST=Pin(2,OUT,value=0)
- spi=SoftSPI(baudrate=100000,sck=Pin(14),mosi=Pin(13),miso=Pin(12))
- sn='{:08x}'.format(int.from_bytes(machine.unique_id(),'little'))
- sta=network.WLAN(network.STA_IF)
- def slp(x):
- time.sleep_ms(x)
- slp(500)
- def tsy(d):
- y,m,d,_,h,i,s=d[0:7]
- 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
- def nTX(x):
- x='{:>168}'.format(x);nCE.off();nDC.off();spi.write(b'!\x14\xB2 \x0C@\x80');nDC.on()
- f=ubinascii.a2b_base64('IEwfTAhMRFQfNAgfYR5eTF5MHhBkDg4eEhJaZGBxDFUAYANfSBkqYEBRVUQQBAAYLnI5MQ43LiEqIkBQREpRIS4+Py4/Pz8uH3EIHx9fHi4/Lj8yYR8PHxsDOWADcSIQEPRFlP2mL3bBh2yig/HgZGLyEVWj9HTyk3WFjgYmMaoABRj6LpnlAIsDqyMCIQQZdYeVVfpNRR9VdQICilIiFbTyVYx0jQns+Y982YT4eXQRttFND/x8+tkfnY/BBxCE')
- for i in range(0,126):
- t,p=i//21%3*4,i+i//63*21
- p,q=ord(x[p])%96,ord(x[p+21])%96
- p,q,r,s=f[p],f[p+96],f[q],f[q+96]
- 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
- spi.write(bytes(y[t:t+4]))
- nCE.on()
- def sDI(x):
- sST.off();spi.write((~x).to_bytes(8,'big'));sST.on()
- s='GS GoodSwicthes v-001SN:'+sn+10*' '+21*'='+' 01234567 89abcdef '
- for i in range(0,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))
- nTX(s)
- def htG(u):
- 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
- while 1:
- d=d+s.recv(1024)
- if w==len(d)or len(d)>1024:
- break
- w=len(d)
- while s.recv(64):
- pass
- s.close();_,d=str(d,'utf8').split("\r\n\r\n",1)
- return d
- old='-'
- while 1:
- try:
- if not brk.value():
- break
- if not sta.isconnected():
- print("Conn");sta.active(True);sta.connect(SSID,PSWD);slp(10000)
- else:
- rtc.datetime(json.loads(htG(SRVR+"?now=now&sn="+sn)))
- old=htG(SRVR+"?mode=alarm&sn="+sn+"&ack="+old)
- new=json.loads(old)
- lamps=int(new['lamps'])
- for i in range(0,SCHC):
- v=new['schedules'][i]
- schs[i]=tsy(v),int(v[8]),int(v[9])
- sDI(lamps)
- n=tsy(rtc.datetime())
- print("Scheds")
- for i in range(0,SCHC):
- v=schs[i]
- if v[0]<n and v[2]>=0:
- lamps=lamps&~(1<<v[1])|v[2]<<v[1]
- htG(SRVR + "?sn=%s&schclr=%d&lamp=%d&val=%d"%(sn,i,v[1],v[2]))
- sDI(lamps)
- slp(1000)
- except:
- pass
Add Comment
Please, Sign In to add comment