2023年11月5日日曜日

picow-radicon,HC05 with microbit,flask rasdicon code,

## raspberry pi pico w wifi code :: # is softap, hard code  is station mode

from machine import Pin,PWM

import network

import socket

import utime

# LED = machine.Pin("LED",machine.Pin.OUT) #builtin led

IN1 = PWM(Pin(14))

IN2 = PWM(Pin(15))

IN1.freq(100)

IN2.freq(100)

IN3 = PWM(Pin(16))

IN4 = PWM(Pin(17))

IN3.freq(100)

IN4.freq(100)

#IN1 = Pin(14,Pin.OUT)

#IN2 = Pin(15,Pin.OUT)

#IN3 = Pin(16,Pin.OUT)

#IN4 = Pin(17,Pin.OUT)

max_duty = 65025

# from metaele labo site


def stop():

    IN1.duty_u16(0)

    IN2.duty_u16(0)

    IN3.duty_u16(0)

    IN4.duty_u16(0)

    utime.sleep(0.5)

    #IN1.value(0)

    #IN2.value(0)

    #IN3.value(0)

    #IN4.value(0)

    #utime.sleep(0.5)


def front():

    IN1.duty_u16(int(max_duty*0.5))

    IN2.duty_u16(0)

    IN3.duty_u16(int(max_duty*0.5))

    IN4.duty_u16(0)

    #IN1.value(1)

    #IN2.value(0)

    #IN3.value(1)

    #IN4.value(0)  

    utime.sleep(0.5)

    stop()

   

def back():

    IN1.duty_u16(0)

    IN2.duty_u16(int(max_duty*0.5))

    IN3.duty_u16(0)

    IN4.duty_u16(int(max_duty*0.5))

    #IN1.value(0)

    #IN2.value(1)

    #IN3.value(0)

    #IN4.value(1)

    utime.sleep(0.5)

    stop()

    

def left():

    IN1.duty_u16(int(max_duty*0.5))

    IN2.duty_u16(0)

    IN3.duty_u16(0)

    IN4.duty_u16(int(max_duty*0.5))

    #IN1.value(1)

    #IN2.value(0)

    #IN3.value(0)

    #IN4.value(1)

    utime.sleep(0.5)

    stop()


def right():

    IN1.duty_u16(0)

    IN2.duty_u16(int(max_duty*0.5))

    IN3.duty_u16(int(max_duty*0.5))

    IN4.duty_u16(0)

    #IN1.value(0)

    #IN2.value(1)

    #IN3.value(1)

    #IN4.value(0)

    utime.sleep(0.5)

    stop()

    

#ssid = 'RPI_PICO_AP'                  

#password = '12345678' 

ssid = '184F32CF8BF3-2G'                  

password = '2215085363556' 

wlan = network.WLAN(network.STA_IF)

wlan.active(True)

wlan.connect(ssid, password)

while wlan.isconnected() == False:

    print('Connecting to Wi-Fi router')

    utime.sleep(1)

wlan_status = wlan.ifconfig()

print('Connected!')

print(f'IP Address: {wlan_status[0]}')

print(f'Netmask: {wlan_status[1]}')

print(f'Default Gateway: {wlan_status[2]}')

print(f'Name Server: {wlan_status[3]}')

#ap = network.WLAN(network.AP_IF)

#ap.config(essid=ssid, password=password)

#ap.active(True)            #activating

#while ap.active() == False:

#  pass

#print('Connection is successful')

#print(ap.ifconfig())

# print('listening on', ap.ifconfig()[0])  # wlan change to ap fof softap mode

addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()

s.bind(addr)

s.listen(1)

#html

def home_page():

    html = """<html>

    <title>Tank Control</title>

    <center>

    <h>Tank Control</h1>

    <p>

    <a href="/front"><input type="button" value="front"></a>

    <a href="/back"><input type="button" value="back"></a>

    </p>

    <p>

    <a href="/right"><input type="button" value="right"></a>

    <a href="/left"><input type="button" value="left"></a>

    </p>

    </center>

    <html>"""

    return html


while True:

    cl,addr = s.accept()   

    print("req from = ")

    print(addr)

    request = cl.recv(1024)

    request = str(request)

    if request.find("/front") ==6:

        front()

    if request.find("/back") ==6:

        back()

    if request.find("/right") ==6:

        right()

    if request.find("/left") ==6:

        left()

    response = home_page()

    cl.send(response)

    cl.close()

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Arduinoを使いBluetooth通信をやってみる!HC-05/HC-06 Bluetoothモジュールの使い方! | ぶらり@web走り書き (burariweb.info) 総論

micro:bitとつなぐにはswitch scienceのi2cロジック変換を用いてTX,RXをクロス接続

5Vと3Vの接続は不要だった HC05のGNDはmicrobitのGNDに直付けする

serial.redirect(
SerialPin.P0,
SerialPin.P1,
BaudRate.BaudRate9600
)
basic.forever(function () {
    basic.showString(serial.readUntil(serial.delimiters(Delimiters.NewLine)))
    basic.pause(200)
})

ーーーーーーーーーーーーーー templates/main.html ------------------------------ 

<html><head><title>Current Status</title></head>

<body>

  <h1>Device Listing and Status</h1>

{% for pin in pins %}

 {% if pins[pin].name == "for-back": %}

  <p>The {{ pins[pin].name }}</br></br>

      (<a href="/{{pin}}/for">forward</a>) 

      (<a href="/{{pin}}/back">backward</a>)</br>

  </p>

 {% elif pins[pin].name == "left-right": %}

  <p>The {{ pins[pin].name }}</br></br>

      (<a href="/{{pin}}/left">left</a>) 

      (<a href="/{{pin}}/right">right</a>)</br>

  </p>

 {% endif %}

{% endfor %}

{% if message %}

  <h2>{{message}}</h2>

{% endif %}

</body></html>

------------------------- weblamp-radicon.py --------------------------------

だいぶ論理が冗長なので将来変更しようと思う


from gpiozero import LED, Motor

from time import sleep



from flask import Flask, render_template, request

app = Flask(__name__)


motor = Motor(forward=19, backward=26,pwm=True)

motor2 = Motor(forward=12, backward=16,pwm=True)

pins = {

    23 : {'name' : 'for-back'},

    25 : {'name' : 'left-right'}

    }


led23 = LED(23)

led23.off()

led25 = LED(25)

led25.off()


@app.route("/")

def main():

    templateData = {

        'pins' : pins

        }

    return render_template('main.html', **templateData)


@app.route("/<changePin>/<action>")

def action(changePin, action):

    changePin = int(changePin)

    deviceName = pins[changePin]['name']

    if action == "for":

        if changePin == 23:

            led23.on()

            motor.forward(0.3)

            motor2.forward(0.3)

            sleep(2)

            motor.stop()

            motor2.stop()

        message = "Turned " + deviceName + " to for."


    if action == "back":

        if changePin == 23:

            led23.off()

            motor.backward(0.3)

            motor.backward(0.3)

            sleep(2)

            motor.stop()

            motor2.stop()

        message = "Turned " + deviceName + " to back."

        

    if action == "left":

        if changePin == 25:

            led25.on()

            motor.forward(0.3)

            motor2.backward(0.3)

            sleep(2)

            motor.stop()

            motor2.stop()

        message = "Turned " + deviceName + " to left."


    

    if action == "right":

        if changePin == 25:

            led25.off()

            motor.backward(0.3)

            motor2.forward(0.3)

            sleep(2)

            motor.stop()

            motor2.stop()

        message = "Turned " + deviceName + " to left."



    templateData = {

        'message' : message,

        'pins' : pins

    }


    return render_template('main.html', **templateData)


if __name__ == "__main__":

    app.run(host='0.0.0.0', port=80, debug=True)


0 件のコメント:

コメントを投稿