def close(self): for g in self.gpio_reset: gpio = g["gpio"] debug("Reset GPIO %d" % gpio) GPIO.setFunction(gpio, g["func"]) if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT: GPIO.digitalWrite(gpio, g["value"])
def GPIOSetup(): for g in GPIO_SETUP: gpio = g["gpio"] debug("Setup GPIO %d" % gpio) GPIO.setFunction(gpio, g["func"]) if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT: GPIO.output(gpio, g["value"])
def setup(self): for g in self.gpio_setup: gpio = g["gpio"] debug("Setup GPIO %d" % gpio) GPIO.setFunction(gpio, g["func"]) if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT: GPIO.digitalWrite(gpio, g["value"])
def GPIOReset(): for g in GPIO_RESET: gpio = g["gpio"] debug("Reset GPIO %d" % gpio) GPIO.setFunction(gpio, g["func"]) if g["value"] >= 0 and GPIO.getFunction(gpio) == GPIO.OUT: GPIO.output(gpio, g["value"])
def __setFunction__(self, channel, value): self.checkDigitalChannelExported(channel) self.checkPostingFunctionAllowed() GPIO.setFunction(channel, value)
def do_POST(self): if not self.checkAuthentication(): return relativePath = self.path.replace(self.server.context, "") if (relativePath.startswith("/")): relativePath = relativePath[1:]; if (relativePath.startswith("GPIO/")): (mode, s_gpio, operation, value) = relativePath.split("/") gpio = int(s_gpio) if (operation == "value"): if GPIO.getFunction(gpio) != GPIO.OUT: self.send_error(400, "Not Output") return; if (value == "0"): GPIO.output(gpio, GPIO.LOW) elif (value == "1"): GPIO.output(gpio, GPIO.HIGH) else: self.send_error(400, "Bad Value") return self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(value.encode()) elif (operation == "function"): value = value.lower() if value == "in": GPIO.setFunction(gpio, GPIO.IN) elif value == "out": GPIO.setFunction(gpio, GPIO.OUT) elif value == "pwm": GPIO.setFunction(gpio, GPIO.PWM) else: self.send_error(400, "Bad Function") return value = GPIO.getFunctionString(gpio) self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(value.encode()) elif (operation == "sequence"): if GPIO.getFunction(gpio) != GPIO.OUT: self.send_error(400, "Not Output") return; (period, sequence) = value.split(",") period = int(period) GPIO.outputSequence(gpio, period, sequence) self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(sequence[-1].encode()) elif (operation == "pwm"): if value == "enable": GPIO.enablePWM(gpio) elif value == "disable": GPIO.disablePWM(gpio) if GPIO.isPWMEnabled(gpio): result = "enabled" else: result = "disabled" self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(result.encode()) elif (operation == "pulse"): GPIO.pulse(gpio) self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write("OK".encode()) elif (operation == "pulseRatio"): ratio = float(value) GPIO.pulseRatio(gpio, ratio) self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(value.encode()) elif (operation == "pulseAngle"): angle = float(value) GPIO.pulseAngle(gpio, angle) self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(value.encode()) else: # operation unknown self.send_error(404, operation + " Not Found") return elif (relativePath.startswith("macros/")): (mode, fname, value) = relativePath.split("/") if (fname in self.server.callbacks): if ',' in value: args = value.split(',') else: args = [value] callback = self.server.callbacks[fname] self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(callback(*args).encode()) else: self.send_error(404, fname + " Not Found") return else: # path unknowns self.send_error(404, "Not Found")
def do_POST(self): if not self.checkAuthentication(): return relativePath = self.path.replace(self.server.context, "") if (relativePath.startswith("/")): relativePath = relativePath[1:]; if (relativePath.startswith("GPIO/")): (mode, s_gpio, operation, value) = relativePath.split("/") gpio = int(s_gpio) if (operation == "value"): if GPIO.getFunction(gpio) != GPIO.OUT: self.send_error(400, "Not Output") return; if (value == "0"): GPIO.output(gpio, GPIO.LOW) elif (value == "1"): GPIO.output(gpio, GPIO.HIGH) else: self.send_error(400, "Bad Value") return self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(value.encode()) elif (operation == "function"): value = value.lower() if value == "in": GPIO.setFunction(gpio, GPIO.IN) elif value == "out": GPIO.setFunction(gpio, GPIO.OUT) else: self.send_error(400, "Bad Function") return value = GPIO.getFunctionString(gpio) self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(value.encode()) elif (operation == "sequence"): if GPIO.getFunction(gpio) != GPIO.OUT: self.send_error(400, "Not Output") return; (period, sequence) = value.split(",") period = int(period) GPIO.outputSequence(gpio, period, sequence) self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(sequence[-1].encode()) else: # operation unknown self.send_error(404, operation + " Not Found") return elif (relativePath.startswith("macros/")): (mode, fname, value) = relativePath.split("/") if (fname in self.server.callbacks): callback = self.server.callbacks[fname] self.send_response(200) self.send_header("Content-type", "text/plain"); self.end_headers() self.wfile.write(callback(value).encode()) else: self.send_error(404, fname + " Not Found") return else: # path unknowns self.send_error(404, "Not Found")
#!/usr/bin/env python3 from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket import sys import _webiopi.GPIO import json PIN_L1 = 6 PIN_L2 = 13 PIN_R1 = 19 PIN_R2 = 26 g_mode = 0 g_percentage = 50 GPIO = _webiopi.GPIO GPIO.setFunction(PIN_L1, GPIO.PWM) GPIO.setFunction(PIN_L2, GPIO.PWM) GPIO.setFunction(PIN_R1, GPIO.PWM) GPIO.setFunction(PIN_R2, GPIO.PWM) def MotorDrive(iIn1Pin, iIn2Pin, percentage): if 100 < percentage: percentage = 100 if -100 > percentage: percentage = -100 if 10 > percentage and -10 < percentage: GPIO.pwmWrite(iIn1Pin, 0.0) GPIO.pwmWrite(iIn2Pin, 0.0) elif 0 < percentage: GPIO.pwmWrite(iIn1Pin, percentage * 0.01)
def do_POST(self, relativePath): if relativePath.startswith("GPIO/"): (mode, s_gpio, operation, value) = relativePath.split("/") gpio = int(s_gpio) if operation == "value": if value == "0": GPIO.output(gpio, GPIO.LOW) elif value == "1": GPIO.output(gpio, GPIO.HIGH) else: return (400, "Bad Value", M_PLAIN) return (200, value, M_PLAIN) elif operation == "function": value = value.lower() if value == "in": GPIO.setFunction(gpio, GPIO.IN) elif value == "out": GPIO.setFunction(gpio, GPIO.OUT) elif value == "pwm": GPIO.setFunction(gpio, GPIO.PWM) else: return (400, "Bad Function", M_PLAIN) value = GPIO.getFunctionString(gpio) return (200, value, M_PLAIN) elif operation == "sequence": (period, sequence) = value.split(",") period = int(period) GPIO.outputSequence(gpio, period, sequence) return (200, sequence[-1], M_PLAIN) elif operation == "pwm": if value == "enable": GPIO.enablePWM(gpio) elif value == "disable": GPIO.disablePWM(gpio) if GPIO.isPWMEnabled(gpio): result = "enabled" else: result = "disabled" return (200, result, M_PLAIN) elif operation == "pulse": GPIO.pulse(gpio) return (200, "OK", M_PLAIN) elif operation == "pulseRatio": ratio = float(value) GPIO.pulseRatio(gpio, ratio) return (200, value, M_PLAIN) elif operation == "pulseAngle": angle = float(value) GPIO.pulseAngle(gpio, angle) return (200, value, M_PLAIN) else: # operation unknown return (404, operation + " Not Found", M_PLAIN) elif relativePath.startswith("macros/"): (mode, fname, value) = relativePath.split("/") if fname in self.callbacks: callback = self.callbacks[fname] if "," in value: args = value.split(",") result = callback(*args) elif len(value) > 0: result = callback(value) else: result = callback() response = "" if result: response = "%s" % result return (200, response, M_PLAIN) else: return (404, fname + " Not Found", M_PLAIN) else: # path unknowns return (0, None, None)
import _webiopi.GPIO import time GPIO = _webiopi.GPIO # GPIO, status, time data = [[num, 0, 0.0] for num in [17, 27, 22]] for n in data: print(n[0], n[1], n[2]) GPIO.setFunction(n[0], GPIO.IN) while True: for n in data: status = GPIO.digitalRead(n[0]) if n[1] == 0 and status: n[1] = 1 n[2] = time.time() elif n[1] == 1 and not status: n[1] = 2 elif n[1] == 2 and 0.2 < (time.time() - n[2]): n[1] = 0 print("%d clicked" % n[0])
def do_POST(self): if not self.checkAuthentication(): return relativePath = self.path.replace(self.server.context, "") if (relativePath.startswith("/")): relativePath = relativePath[1:] if (relativePath.startswith("GPIO/")): (mode, s_gpio, operation, value) = relativePath.split("/") gpio = int(s_gpio) if (operation == "value"): if GPIO.getFunction(gpio) != GPIO.OUT: self.send_error(400, "Not Output") return if (value == "0"): GPIO.output(gpio, GPIO.LOW) elif (value == "1"): GPIO.output(gpio, GPIO.HIGH) else: self.send_error(400, "Bad Value") return self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(value.encode()) elif (operation == "function"): value = value.lower() if value == "in": GPIO.setFunction(gpio, GPIO.IN) elif value == "out": GPIO.setFunction(gpio, GPIO.OUT) elif value == "pwm": GPIO.setFunction(gpio, GPIO.PWM) else: self.send_error(400, "Bad Function") return value = GPIO.getFunctionString(gpio) self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(value.encode()) elif (operation == "sequence"): if GPIO.getFunction(gpio) != GPIO.OUT: self.send_error(400, "Not Output") return (period, sequence) = value.split(",") period = int(period) GPIO.outputSequence(gpio, period, sequence) self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(sequence[-1].encode()) elif (operation == "pwm"): if value == "enable": GPIO.enablePWM(gpio) elif value == "disable": GPIO.disablePWM(gpio) if GPIO.isPWMEnabled(gpio): result = "enabled" else: result = "disabled" self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(result.encode()) elif (operation == "pulse"): GPIO.pulse(gpio) self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write("OK".encode()) elif (operation == "pulseRatio"): ratio = float(value) GPIO.pulseRatio(gpio, ratio) self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(value.encode()) elif (operation == "pulseAngle"): angle = float(value) GPIO.pulseAngle(gpio, angle) self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(value.encode()) else: # operation unknown self.send_error(404, operation + " Not Found") return elif (relativePath.startswith("macros/")): (mode, fname, value) = relativePath.split("/") if (fname in self.server.callbacks): if ',' in value: args = value.split(',') else: args = [value] callback = self.server.callbacks[fname] self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write(callback(*args).encode()) else: self.send_error(404, fname + " Not Found") return else: # path unknowns self.send_error(404, "Not Found")
#!/usr/bin/env python3 from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket import sys import _webiopi.GPIO import json PIN_L1 = 6 PIN_L2 = 13 PIN_R1 = 19 PIN_R2 = 26 g_mode = 0 g_percentage = 50 GPIO = _webiopi.GPIO GPIO.setFunction(PIN_L1, GPIO.PWM) GPIO.setFunction(PIN_L2, GPIO.PWM) GPIO.setFunction(PIN_R1, GPIO.PWM) GPIO.setFunction(PIN_R2, GPIO.PWM) def MotorDrive(iIn1Pin, iIn2Pin, percentage): if 100 < percentage: percentage = 100 if -100 > percentage: percentage = -100 if 10 > percentage and -10 < percentage: GPIO.pwmWrite(iIn1Pin, 0.0) GPIO.pwmWrite(iIn2Pin, 0.0) elif 0 < percentage: GPIO.pwmWrite(iIn1Pin, percentage * 0.01) GPIO.pwmWrite(iIn2Pin, 0.0)
def do_POST(self, relativePath): if relativePath.startswith("EGG/"): (mode, s_gpio, operation, value) = relativePath.split("/") if s_gpio == "1": gpio = 7 elif s_gpio == "2": gpio = 8 elif s_gpio == "3": gpio = 9 elif s_gpio == "4": gpio = 10 elif s_gpio == "5": gpio = 11 elif s_gpio == "6": gpio = 22 elif s_gpio == "7": gpio = 23 elif s_gpio == "8": gpio = 24 else: gpio = 25 if operation == "value": if value == "off": GPIO.output(gpio, GPIO.LOW) elif value == "on": GPIO.output(gpio, GPIO.HIGH) else: return (400, "Bad Value", M_PLAIN) return (200, value, M_PLAIN) elif operation == "function": value = value.lower() if value == "in": GPIO.setFunction(gpio, GPIO.IN) elif value == "out": GPIO.setFunction(gpio, GPIO.OUT) elif value == "pwm": GPIO.setFunction(gpio, GPIO.PWM) else: return (400, "Bad Function", M_PLAIN) value = GPIO.getFunctionString(gpio) return (200, value, M_PLAIN) elif operation == "sequence": (period, sequence) = value.split(",") period = int(period) GPIO.outputSequence(gpio, period, sequence) return (200, sequence[-1], M_PLAIN) elif operation == "pwm": if value == "enable": GPIO.enablePWM(gpio) elif value == "disable": GPIO.disablePWM(gpio) if GPIO.isPWMEnabled(gpio): result = "enabled" else: result = "disabled" return (200, result, M_PLAIN) elif operation == "pulse": GPIO.pulse(gpio) return (200, "OK", M_PLAIN) elif operation == "pulseRatio": ratio = float(value) GPIO.pulseRatio(gpio, ratio) return (200, value, M_PLAIN) elif operation == "pulseAngle": angle = float(value) GPIO.pulseAngle(gpio, angle) return (200, value, M_PLAIN) else: # operation unknown return (404, operation + " Not Found", M_PLAIN) elif relativePath.startswith("macros/"): (mode, fname, value) = relativePath.split("/") if fname in self.callbacks: callback = self.callbacks[fname] if ',' in value: args = value.split(',') result = callback(*args) elif len(value) > 0: result = callback(value) else: result = callback() response = "" if result: response = "%s" % result return (200, response, M_PLAIN) else: return (404, fname + " Not Found", M_PLAIN) elif operation == "saca17": GPIO.setFunction(17, GPIO.OUT) elif operation == "saca171": GPIO.output(17,GPIO.HIGH) elif operation == "saca170": GPIO.output(17,GPIO.LOW) elif operation == "saca21": GPIO.setFunction(21, GPIO.OUT) elif operation == "saca211": GPIO.output(21,GPIO.HIGH) elif operation == "saca210": GPIO.output(21,GPIO.LOW) elif relativePath.startswith("EGGSOFF"): GPIO.output(7, GPIO.LOW) GPIO.output(8, GPIO.LOW) GPIO.output(9, GPIO.LOW) GPIO.output(10, GPIO.LOW) GPIO.output(11, GPIO.LOW) GPIO.output(22, GPIO.LOW) GPIO.output(23, GPIO.LOW) GPIO.output(24, GPIO.LOW) GPIO.output(25, GPIO.LOW) return (200, "ALL EGGS OFF", M_PLAIN) elif relativePath.startswith("EGGSON"): GPIO.output(7, GPIO.HIGH) GPIO.output(8, GPIO.HIGH) GPIO.output(9, GPIO.HIGH) GPIO.output(10, GPIO.HIGH) GPIO.output(11, GPIO.HIGH) GPIO.output(22, GPIO.HIGH) GPIO.output(23, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) GPIO.output(25, GPIO.HIGH) return (200, "ALL EGGS ON", M_PLAIN) else: # path unknowns return (0, None, None)
def do_POST(self, relativePath): if relativePath.startswith("GPIO/"): (mode, s_gpio, operation, value) = relativePath.split("/") gpio = int(s_gpio) if operation == "value": if value == "0": GPIO.output(gpio, GPIO.LOW) elif value == "1": GPIO.output(gpio, GPIO.HIGH) else: return (400, "Bad Value", M_PLAIN) return (200, value, M_PLAIN) elif operation == "function": value = value.lower() if value == "in": GPIO.setFunction(gpio, GPIO.IN) elif value == "out": GPIO.setFunction(gpio, GPIO.OUT) elif value == "pwm": GPIO.setFunction(gpio, GPIO.PWM) else: return (400, "Bad Function", M_PLAIN) value = GPIO.getFunctionString(gpio) return (200, value, M_PLAIN) elif operation == "sequence": (period, sequence) = value.split(",") period = int(period) GPIO.outputSequence(gpio, period, sequence) return (200, sequence[-1], M_PLAIN) elif operation == "pwm": if value == "enable": GPIO.enablePWM(gpio) elif value == "disable": GPIO.disablePWM(gpio) if GPIO.isPWMEnabled(gpio): result = "enabled" else: result = "disabled" return (200, result, M_PLAIN) elif operation == "pulse": GPIO.pulse(gpio) return (200, "OK", M_PLAIN) elif operation == "pulseRatio": ratio = float(value) GPIO.pulseRatio(gpio, ratio) return (200, value, M_PLAIN) elif operation == "pulseAngle": angle = float(value) GPIO.pulseAngle(gpio, angle) return (200, value, M_PLAIN) else: # operation unknown return (404, operation + " Not Found", M_PLAIN) elif relativePath.startswith("macros/"): (mode, fname, value) = relativePath.split("/") if fname in self.callbacks: callback = self.callbacks[fname] if ',' in value: args = value.split(',') result = callback(*args) elif len(value) > 0: result = callback(value) else: result = callback() response = "" if result: response = "%s" % result return (200, response, M_PLAIN) else: return (404, fname + " Not Found", M_PLAIN) else: # path unknowns return (0, None, None)
def do_POST(self, relativePath): if relativePath.startswith("EGG/"): (mode, s_gpio, operation, value) = relativePath.split("/") if s_gpio == "1": gpio = 7 elif s_gpio == "2": gpio = 8 elif s_gpio == "3": gpio = 9 elif s_gpio == "4": gpio = 10 elif s_gpio == "5": gpio = 11 elif s_gpio == "6": gpio = 22 elif s_gpio == "7": gpio = 23 elif s_gpio == "8": gpio = 24 else: gpio = 25 if operation == "value": if value == "off": GPIO.output(gpio, GPIO.LOW) elif value == "on": GPIO.output(gpio, GPIO.HIGH) else: return (400, "Bad Value", M_PLAIN) return (200, value, M_PLAIN) elif operation == "function": value = value.lower() if value == "in": GPIO.setFunction(gpio, GPIO.IN) elif value == "out": GPIO.setFunction(gpio, GPIO.OUT) elif value == "pwm": GPIO.setFunction(gpio, GPIO.PWM) else: return (400, "Bad Function", M_PLAIN) value = GPIO.getFunctionString(gpio) return (200, value, M_PLAIN) elif operation == "sequence": (period, sequence) = value.split(",") period = int(period) GPIO.outputSequence(gpio, period, sequence) return (200, sequence[-1], M_PLAIN) elif operation == "pwm": if value == "enable": GPIO.enablePWM(gpio) elif value == "disable": GPIO.disablePWM(gpio) if GPIO.isPWMEnabled(gpio): result = "enabled" else: result = "disabled" return (200, result, M_PLAIN) elif operation == "pulse": GPIO.pulse(gpio) return (200, "OK", M_PLAIN) elif operation == "pulseRatio": ratio = float(value) GPIO.pulseRatio(gpio, ratio) return (200, value, M_PLAIN) elif operation == "pulseAngle": angle = float(value) GPIO.pulseAngle(gpio, angle) return (200, value, M_PLAIN) else: # operation unknown return (404, operation + " Not Found", M_PLAIN) elif relativePath.startswith("macros/"): (mode, fname, value) = relativePath.split("/") if fname in self.callbacks: callback = self.callbacks[fname] if ',' in value: args = value.split(',') result = callback(*args) elif len(value) > 0: result = callback(value) else: result = callback() response = "" if result: response = "%s" % result return (200, response, M_PLAIN) else: return (404, fname + " Not Found", M_PLAIN) elif operation == "saca17": GPIO.setFunction(17, GPIO.OUT) elif operation == "saca171": GPIO.output(17, GPIO.HIGH) elif operation == "saca170": GPIO.output(17, GPIO.LOW) elif operation == "saca21": GPIO.setFunction(21, GPIO.OUT) elif operation == "saca211": GPIO.output(21, GPIO.HIGH) elif operation == "saca210": GPIO.output(21, GPIO.LOW) elif relativePath.startswith("EGGSOFF"): GPIO.output(7, GPIO.LOW) GPIO.output(8, GPIO.LOW) GPIO.output(9, GPIO.LOW) GPIO.output(10, GPIO.LOW) GPIO.output(11, GPIO.LOW) GPIO.output(22, GPIO.LOW) GPIO.output(23, GPIO.LOW) GPIO.output(24, GPIO.LOW) GPIO.output(25, GPIO.LOW) return (200, "ALL EGGS OFF", M_PLAIN) elif relativePath.startswith("EGGSON"): GPIO.output(7, GPIO.HIGH) GPIO.output(8, GPIO.HIGH) GPIO.output(9, GPIO.HIGH) GPIO.output(10, GPIO.HIGH) GPIO.output(11, GPIO.HIGH) GPIO.output(22, GPIO.HIGH) GPIO.output(23, GPIO.HIGH) GPIO.output(24, GPIO.HIGH) GPIO.output(25, GPIO.HIGH) return (200, "ALL EGGS ON", M_PLAIN) else: # path unknowns return (0, None, None)