Raspberry Pi Pico łączy się ze światem przez ESP 01

Źródło: https://github.com/neeraj95575/ESP-01-connect-to-raspberry-pi-pico

Łączenie

Schemat połączeń Raspberry Pi Pico z Esp01
Raspberry Pi PicoESP-01
Pin2 / GP1TX
Pin1 / GP0RX
PIN36 / 3,3VGPIO16 / EN
PIN36 / 3,3VVCC
PIN38/28/23/18/13/8/3GND

KOD

from machine import Pin, I2C, UART
import time
import utime
import credentials
import random

lotto = [1, 16, 19, 30, 35, 41]

value1 = 0
value2 = 0
value3 = 0
    
WiFi_SSID= credentials.WIFI_SSID              # Wifi_SSID
WiFi_password = credentials.WIFI_PASSWORD     # WiFi Password
TCP_ServerIP = "184.106.153.149"   # Thingspeak IP address
Port = '80'                        # Thingspeak port
API_KEY = credentials.TS_API_KEY_WRITE        # API write Key

uart = UART(0, 115200)           # Default Baud rate

def sendAT(cmd,ack,timeout=2000):
    uart.write(cmd+'\r\n')
    t = time.ticks_ms()
    while (time.ticks_ms() - t) < timeout:
        s=uart.read()
        if(s != None):
            s=s.decode("utf-8")
            print(s)
            if(s.find(ack) >= 0):
                return True
    return False


def send_data(v1, v2, v3):    
    data="GET /update?key="+API_KEY+"&field1=%s&field2=%s&field3=%s"%(v1, v2, v3)+"\r\n";
    final=len(data)
    reading=0
    sendAT("AT+CIPSTART=\"TCP\",\""+TCP_ServerIP+"\","+Port,"OK",5000)
    sendAT("AT+CIPSEND="+str(final)+"\r\n","OK")
    time.sleep(0.5)
    uart.write(data)
    print(data)
    sendAT('AT+CIPCLOSE'+'\r\n',"OK")
    
 
sendAT("AT","OK")
sendAT("AT+CWMODE=1","OK")
sendAT("AT+CWJAP=\""+WiFi_SSID+"\",\""+WiFi_password+"\"","OK",20000)
sendAT("AT+CIFSR","OK")

while True:
        value1 += 1 
        value2 = random.randint(15, 35)
        value3 = random.choice(lotto)
        send_data(value1, value2, value3)
        print(f"field1 = {value1}, field2 = {value2}, field3 = {value3}")
        time.sleep(10)#delay of 10 second
        


LINKI

Dodaj komentarz