Advertisement
sohotcall

queue-request.py ESP8266, HC-SR05, UART Thermal Printer with Cutter, Micropython

Feb 20th, 2021
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. # URL http://  URL   /queue/request.php?print=true&category=1&json=json"
  2. # response ex. {"category":"Queue", "number":"001", "date":"2021-02-02 12:34:56"}
  3.  
  4. import network,socket,json
  5. from machine import Pin,time_pulse_us,UART
  6. from time import sleep,sleep_ms,sleep_us
  7. blk=Pin(13,Pin.OUT,value=1)
  8. buz=Pin(12,Pin.OUT,value=0)
  9. trg=Pin(14,Pin.OUT,value=0)
  10. ech=Pin(0,Pin.IN)
  11. prt=UART(1,9600,bits=8,parity=None,stop=1)
  12. sta=network.WLAN(network.STA_IF)
  13.  
  14. def dbg(s):
  15.  print(s)
  16. def htG(u):
  17.  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
  18.  while 1:
  19.   d=d+s.recv(1024)
  20.   if w==len(d)or len(d)>1024:
  21.    break
  22.   w=len(d)
  23.  while s.recv(64):
  24.   pass
  25.  s.close();_,d=str(d,'utf8').split("\r\n\r\n",1)
  26.  return d
  27.  
  28. cnt=0
  29. while 1:
  30.  try:
  31.   dbg("ite")
  32.   if not sta.isconnected():
  33.    dbg("Con")
  34.    blk.off();sleep(1);blk.on();
  35.    sta.active(True);sta.connect("   SSID   ","   PASSWORD     ");sleep(1)
  36.   trg.on();sleep_us(10);trg.off();mm=time_pulse_us(ech,1,30000)*100//582
  37.   if 10<mm<300:
  38.    cnt=cnt+1
  39.   else:
  40.    cnt=0
  41.   dbg("mm="+str(mm)+" cnt="+str(cnt))
  42.   if cnt==0:
  43.    blk.on();sleep_ms(250)
  44.   elif cnt in(1,3,4,5,7,8,9,11,12,13):
  45.    blk.off();sleep_ms(250)
  46.   elif cnt in(2,6,10):
  47.    blk.on();buz.on();sleep_ms(100);buz.off();blk.off();sleep_ms(150)
  48.   elif cnt==14:
  49.    blk.on();buz.on();sleep_ms(250);buz.off()
  50.    a=("  __     _   ___   ____  _ _   ___    __   ____  ___   ___  "+
  51.       " /  \  / | |_  ) |__ / | | | | __|  / /  |__  |( _ ) / _ \ "+
  52.       "| () |  | |  / /   |_ \ |_  _||__ \ / _ \  / / / _ \ \_  / "+
  53.       " \__/   |_| /___| |___/   |_| |___/ \___/  /_/  \___/  /_/  ")
  54.    obj=json.loads(htG("http://    URL    /queue/request.php?print=true&category=1&json=json"))
  55.    dbg("cat="+obj['category']+" no="+obj['number']+" date="+obj['date'])
  56.    prt.write("    COMPANY NAME     \r\nCategory: "+obj['category']+"\r\n")
  57.    x,y,z=[int(i)for i in obj['number']]
  58.    for i in range(0,4):
  59.     prt.write("   "+a[60*i+6*x:60*i+6*x+6]+a[60*i+6*y:60*i+6*y+6]+a[60*i+6*z:60*i+6*z+6]+"\r\n")
  60.    prt.write(obj['date']+"\r\n")
  61.    prt.write(b"\x0a\x0a\x1d\x56\x00\r\n")
  62.   else:
  63.    sleep_ms(250)
  64.  except KeyboardInterrupt:
  65.   break
  66.  except:
  67.   pass
  68.   sleep(250)
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement