Пример #1
0
def main():
    gc.collect()

    ledLight = Signal(Pin(2, Pin.OUT), invert=False)  # GPIO2 ( D2 )
    sleep(0.1)
    ledLight.on()
    dcMotorA = DCMotor(4, 5)  # GPIO4 ( D4 ), GPIO5 ( D5 )
    sleep(0.1)
    dcMotorA.stop()
    servoB = CServo(15)  # GPIO15 ( D15 )

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('', 80))
    sock.listen(5)
    while True:
        try:
            if gc.mem_free() < G_GC_THRESHOLD:
                gc.collect()
            conn, addr = sock.accept()
            conn.settimeout(3.0)
            print('Received HTTP GET connection request from %s' % str(addr))
            request = conn.recv(1024)
            conn.settimeout(None)
            request = str(request)
            print('GET Rquest Content = %s' % request)
            car_direction = request.find('/?car_direction')
            turn_left = request.find('/?turn_left')
            car_forward = request.find('/?car_forward')
            turn_right = request.find('/?turn_right')
            speed_up = request.find('/?speed_up')
            car_stop = request.find('/?car_stop')
            slow_down = request.find('/?slow_down')
            car_backward = request.find('/?car_backward')
            if car_direction == G_CMD_IDX:
                endIdx = request.find(' HTTP/')
                subReq = request[G_CMD_IDX:endIdx]
                startIdx = subReq.find('=') + 1
                virtualAngle = int(subReq[startIdx:])
                print('cmd: car_direction, virtualAngle =', virtualAngle)
                ledLight.on()
                servoB.carDirection(virtualAngle)
            elif turn_left == G_CMD_IDX:
                print('cmd: turn_left')
                ledLight.on()
                servoB.turnLeft()
            elif car_forward == G_CMD_IDX:
                print('cmd: car_forward')
                ledLight.on()
                dcMotorA.forward(G_FORWARD_SPEED)
            elif turn_right == G_CMD_IDX:
                print('cmd: turn_right')
                ledLight.on()
                servoB.turnRight()
            elif speed_up == G_CMD_IDX:
                print('cmd: speed_up')
                ledLight.on()
                dcMotorA.speedUp()
            elif car_stop == G_CMD_IDX:
                print('cmd: car_stop')
                ledLight.off()
                dcMotorA.stop()
            elif slow_down == G_CMD_IDX:
                print('cmd: slow_down')
                ledLight.on()
                dcMotorA.slowDown()
            elif car_backward == G_CMD_IDX:
                print('cmd: car_backward')
                ledLight.on()
                dcMotorA.backward(G_BACKWARD_SPEED)
            resHtml = getWebPage()
            conn.send('HTTP/1.1 200 OK\n')
            conn.send('Content-Type: text/html\n')
            conn.send('Connection: closed.\n\n')
            conn.sendall(resHtml)
            conn.close()
        except OSError as e:
            conn.close()
            print('Connection closed. OSError =', e)