Raspberry Pi Pico W/MicroPython act as socket client, connect to HC-25 WiFi Module.

It's my MicroPython exercise run on Raspberry Pi Pico W, to act as WiFi station/socket client connect to socket server on HC-25 WiFi Module.



Remark:
In my trial, it's unstable to connect to HC-25 when I stop Raspberry Pi Pico W/MicroPython and run again. May be need power OFF-ON, some delay, or handle something, I have no any idea right now!

mpyPicoW_SocketClient.py
"""
Raspberry Pi Pico W/MicroPython exercise:
A simple socket client connect to HC-25

ref:
Raspberry Pi Pico W official book:
Connecting to the Internet with Raspberry Pi Pico W
"""
import sys
import os
import time
import network
import socket

ssid = 'myHC-25'
password = 'password'
serverAddr = "192.168.4.1"
serverPort = 8080

# To know WLAN status
# It's helpflul in learning and debugging
def understandWLAN(wwlan):
    print("------------------------------")
    print("Information to understand wlan")
    print(wwlan)
    #print(dir(wwlan))
    print("active(): ", wwlan.active())
    print("isconnected(): ", wwlan.isconnected())
    s = wwlan.status()
    print("status() =", s)
    if s == network.STAT_IDLE:
        print("STAT_IDLE – no connection and no activity")
    elif s == network.STAT_CONNECTING:
        print("STAT_CONNECTING – connecting in progress")
    elif s == network.STAT_WRONG_PASSWORD:
        print("STAT_WRONG_PASSWORD – failed due to incorrect password")
    elif s == network.STAT_NO_AP_FOUND:
        print("STAT_NO_AP_FOUND – failed because no access point replied")
    elif s == network.STAT_CONNECT_FAIL:
        print("STAT_CONNECT_FAIL – failed due to other problems")
    elif s == network.STAT_GOT_IP:
        print("STAT_GOT_IP – connection successful")
    else:
        print("- unknown status!!!")
    
    print("------------------------------")

print("=========================================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("=========================================================")
print()
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
understandWLAN(wlan)

wlan.connect(ssid, password)

while not wlan.isconnected():
    print("Waiting to connect:", end='*\r')
    time.sleep(0.5)
    print("Waiting to connect:", end='-\r')
    time.sleep(0.5)

print("wlan connected")
understandWLAN(wlan)

print(wlan.ifconfig())
print("my IP:", wlan.ifconfig()[0])

ai = socket.getaddrinfo(serverAddr, serverPort)
addr = ai[0][-1]

s = socket.socket()

try:
    s.connect(addr)
    s.send(b"\nHello from Pico W at " + wlan.ifconfig()[0])
    while True:
        rx = s.recv(512)
        print(rx)
except:
    print("Somethong wrong")
    understandWLAN(wlan)
    s.close()
    wlan.disconnect()
    
    while True:
        pass

mpyPicoW_SocketClient_ST7735.py
"""
Raspberry Pi Pico W/MicroPython exercise:
A simple socket client connect to HC-25
display on 0.96" 80x160 (RGB) IPS with ST7735S SPI Driver

ref:
Raspberry Pi Pico W official book:
Connecting to the Internet with Raspberry Pi Pico W
"""
import sys
import os
import time
import network
import socket
from myST7735 import TFT
from sysfont import sysfont
from machine import SPI

ssid = 'myHC-25'
password = 'password'
serverAddr = "192.168.4.1"
serverPort = 8080

# To know WLAN status
# It's helpflul in learning and debugging
def understandWLAN(wwlan):
    print("------------------------------")
    print("Information to understand wlan")
    print(wwlan)
    #print(dir(wwlan))
    print("active(): ", wwlan.active())
    print("isconnected(): ", wwlan.isconnected())
    s = wwlan.status()
    print("status() =", s)
    if s == network.STAT_IDLE:
        print("STAT_IDLE – no connection and no activity")
    elif s == network.STAT_CONNECTING:
        print("STAT_CONNECTING – connecting in progress")
    elif s == network.STAT_WRONG_PASSWORD:
        print("STAT_WRONG_PASSWORD – failed due to incorrect password")
    elif s == network.STAT_NO_AP_FOUND:
        print("STAT_NO_AP_FOUND – failed because no access point replied")
    elif s == network.STAT_CONNECT_FAIL:
        print("STAT_CONNECT_FAIL – failed due to other problems")
    elif s == network.STAT_GOT_IP:
        print("STAT_GOT_IP – connection successful")
    else:
        print("- unknown status!!!")
    
    print("------------------------------")

print("=========================================================")
print(sys.implementation[0], os.uname()[3],
      "\nrun on", os.uname()[4])
print("=========================================================")

#=== prepare ST7735 TFT
#spi = SPI(2, baudrate=20000000, polarity=0, phase=0, sck=Pin(14), mosi=Pin(13), miso=Pin(12))
spi = SPI(1, baudrate=20000000, polarity=0, phase=0)
pin_DC = 14
pin_RESET = 13
pin_CS = 15
#tft=TFT(spi,16,17,18)
tft=TFT(spi, pin_DC, pin_RESET, pin_CS)
tft.initr()
tft.rgb(False)
tft.invertcolor(True)
#===
tft.rotation(1)
tft.setStart(1, 26)
tft.fill(TFT.BLACK)

tft.text((10, 10),
         "Raspberry Pi Pico W TCP Client",
         TFT.BLUE,
         sysfont, 2,
         nowrap=False)
time.sleep(1)

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
understandWLAN(wlan)

wlan.connect(ssid, password)

while not wlan.isconnected():
    print("Waiting to connect:", end='*\r')
    time.sleep(0.5)
    print("Waiting to connect:", end='-\r')
    time.sleep(0.5)

print()
print("wlan connected")
understandWLAN(wlan)

print(wlan.ifconfig())
print("my IP:", wlan.ifconfig()[0])

ai = socket.getaddrinfo(serverAddr, serverPort)
addr = ai[0][-1]

s = socket.socket()

try:
    s.connect(addr)
    print("socket connected:", s)
    s.send(b"\nHello from Pico W at " + wlan.ifconfig()[0])
    while True:
        rx = s.recv(512)
        print(rx)
        
        tft.fill(TFT.BLACK)
        tft.text((10, 10),
                 rx.decode(),
                 TFT.WHITE,
                 sysfont, 2,
                 nowrap=False)
        
except:
    print("Somethong wrong")
    
    tft.fill(TFT.BLACK)
    tft.text((10, 10),
             "Somethong wrong",
             TFT.RED,
             sysfont, 2,
             nowrap=False)

    s.close()
    wlan.disconnect()
    
    while True:
        pass
For the ST7735 part, refer to last exercise: Raspberry Pi Pico W/MicroPython x 0.96" 80x160 ST7735 SPI IPS.


Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library.