17/11/2022, 22:21
(Questo messaggio è stato modificato l'ultima volta il: 17/11/2022, 22:22 da max95.)
Ciao a tutti,
volevo provare ad associare un modulino ESP01 (del quale ammetto non conoscere un granché...) alla mia schedina pico. Ho realizzato le connessioni come riportate alla fine del post. In rete ho trovato il seguente codice il quale però non mi porta da nessuna parte... il modulo l'ho collegato sulla UART0 (usando i gpio tx 16 e tx 17).
Dove sto sbagliando?
questo l'output che ottengo su seriale:
ps. ho volutamente oscurato il nome della rete e la pass..
volevo provare ad associare un modulino ESP01 (del quale ammetto non conoscere un granché...) alla mia schedina pico. Ho realizzato le connessioni come riportate alla fine del post. In rete ho trovato il seguente codice il quale però non mi porta da nessuna parte... il modulo l'ho collegato sulla UART0 (usando i gpio tx 16 e tx 17).
Dove sto sbagliando?
Codice:
import uos
from machine import Pin, UART
import utime
recv_buf="" # receive buffer global variable
print()
print("Machine: \t" + uos.uname()[4])
print("MicroPython: \t" + uos.uname()[3])
uart0 = machine.UART(0, baudrate=115200, tx=Pin(16), rx=Pin(17))
print(uart0)
def Rx_ESP_Data():
recv=bytes()
while uart0.any()>0:
recv+=uart0.read(1)
res=recv.decode('utf-8')
return res
def Connect_WiFi(cmd, uart=uart0, timeout=3000):
print("CMD: " + cmd)
uart.write(cmd)
utime.sleep(7.0)
Wait_ESP_Rsp(uart, timeout)
print()
def Send_AT_Cmd(cmd, uart=uart0, timeout=3000):
print("CMD: " + cmd)
uart.write(cmd)
Wait_ESP_Rsp(uart, timeout)
print()
def Wait_ESP_Rsp(uart=uart0, timeout=3000):
prvMills = utime.ticks_ms()
resp = b""
while (utime.ticks_ms()-prvMills)<timeout:
if uart.any():
resp = b"".join([resp, uart.read(1)])
print("resp:")
try:
print(resp.decode())
except UnicodeError:
print(resp)
Send_AT_Cmd('AT\r\n') #Test AT startup
Send_AT_Cmd('AT+GMR\r\n') #Check version information
Send_AT_Cmd('AT+CIPSERVER=0\r\n') #Check version information
Send_AT_Cmd('AT+RST\r\n') #Check version information
Send_AT_Cmd('AT+RESTORE\r\n') #Restore Factory Default Settings
Send_AT_Cmd('AT+CWMODE?\r\n') #Query the WiFi mode
Send_AT_Cmd('AT+CWMODE=1\r\n') #Set the WiFi mode = Station mode
Send_AT_Cmd('AT+CWMODE?\r\n') #Query the WiFi mode again
Send_AT_Cmd('AT+CWLAP\r\n', timeout=10000) #List available APs
Connect_WiFi('AT+CWJAP="+++++","++++"\r\n', timeout=5000) #Connect to AP using SSID & Password
Send_AT_Cmd('AT+CIFSR\r\n') #Obtain the Local IP Address
utime.sleep(3.0)
print ('Starting connection to ESP8266...')
while True:
print("ciao")
res =""
res=Rx_ESP_Data()
utime.sleep(2.0)
if '+IPD' in res: # if the buffer contains IPD(a connection), then respond with HTML handshake
id_index = res.find('+IPD')
print("resp:")
print(res)
connection_id = res[id_index+5]
print("connectionId:" + connection_id)
print ('! Incoming connection - sending webpage')
uart0.write('AT+CIPSEND='+connection_id+',200'+'\r\n') #Send a HTTP response then a webpage as bytes the 108 is the amount of bytes you are sending, change this if you change the data sent below
utime.sleep(1.0)
uart0.write('HTTP/1.1 200 OK'+'\r\n')
uart0.write('Content-Type: text/html'+'\r\n')
uart0.write('Connection: close'+'\r\n')
uart0.write(''+'\r\n')
uart0.write('<!DOCTYPE HTML>'+'\r\n')
uart0.write('<html>'+'\r\n')
uart0.write('<body><center><h1>Raspberry Pi Pico-ESP-01-ESP8266- Web Server</h1></center>'+'\r\n')
uart0.write('<center><h2>Interfacing of ESP8266 WiFi Module with Raspberry Pi Pico</h2></center>'+'\r\n')
uart0.write('</body></html>'+'\r\n')
utime.sleep(4.0)
Send_AT_Cmd('AT+CIPCLOSE='+ connection_id+'\r\n') # once file sent, close connection
utime.sleep(2.0)
recv_buf="" #reset buffer
print ('Waiting For connection...')
questo l'output che ottengo su seriale:
Codice:
Machine: Raspberry Pi Pico with RP2040
MicroPython: v1.19.1 on 2022-06-18 (GNU 11.2.0 MinSizeRel)
UART(0, baudrate=115200, bits=8, parity=None, stop=1, tx=16, rx=17, txbuf=256, rxbuf=256, timeout=0, timeout_char=1, invert=None)
CMD: AT
resp:
b'\xfe'
CMD: AT+GMR
resp:
CMD: AT+CIPSERVER=0
resp:
CMD: AT+RST
resp:
CMD: AT+RESTORE
resp:
CMD: AT+CWMODE?
resp:
CMD: AT+CWMODE=1
resp:
CMD: AT+CWMODE?
resp:
CMD: AT+CWLAP
resp:
CMD: AT+CWJAP="++++","++++"
resp:
CMD: AT+CIFSR
resp:
Starting connection to ESP8266...
ciao
ps. ho volutamente oscurato il nome della rete e la pass..