รายละเอียดคือใช้ raspberry pi รับ logic input 0/1 จาก arduino แล้วให้รายงานออกผ่านทาง web http เพื่อไม่ให้เปลือง resource และเรียบง่าย จึงไม่ติดตั้ง apache หรือ nginx แต่ใช้ BaseHTTPServer pyhton มารันแทน จากนั้น device ตัวอื่นก็สามารถรับข้อมูลผ่านทาง web http ได้แล้ว
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | root@raspberrypi:/var/www/html# cat app.py #!/usr/bin/env python #Ref: https://gist.github.com/bradmontgomery/2219997 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer import SocketServer import time import RPi.GPIO as GPIO #sec = ["s1","s2","s3","s4","s5","s6","s7","s8","s9","s10","s11","s12","s13","s14","s15","s16","s17"] pins = ["18","23","4","17","27","22","10","9","11","0","5","6","13","19","26","14","15"] GPIO.setmode(GPIO.BCM) for i in range(len(pins)): GPIO.setup(int(pins[i]),GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #for j in range(len(pins)): # if GPIO.input(int(pins[j])) == True: # pin = pins[j] class S(BaseHTTPRequestHandler): def _set_headers(self): self.send_response(200) self.send_header('Content-type', 'text/html') self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Access-Control-Expose-Headers', 'access-control-allow-origin') self.end_headers() def do_GET(self): self._set_headers() #pin = "empty" ck = 0 if self.path == "/bcm" : for j in range(len(pins)): if GPIO.input(int(pins[j])) == True: #pin = pins[j] self.wfile.write(pins[j]) self.wfile.write(" ") ck = 1 if ck == 0 : self.wfile.write("empty") #self.wfile.write(pin) else: d = open("index.html","r") data = d.read() self.wfile.write(data) d.close() def do_HEAD(self): self._set_headers() def do_POST(self): # Doesn't do anything with posted data self._set_headers() self.wfile.write("<html><body><h1>POST!</h1></body></html>") def run(server_class=HTTPServer, handler_class=S, port=8080): server_address = ('', port) httpd = server_class(server_address, handler_class) print 'Starting httpd...' httpd.serve_forever() if __name__ == "__main__": from sys import argv if len(argv) == 2: run(port=int(argv[1])) else: run() |