Store and share experiences about robocon, IoT, Embedded...
  • Home
  • Linux
  • Window
    • Latex
    • Matlab
  • Embedded programming
    • Jetson Tx1
    • Raspberry Pi
    • Raspberry Pico
  • Internet of things
    • HTTP+MySQL cho IoT
    • Websocket+Nodejs
    • ESP32
  • Electronics and robots
    • Robocon
    • Arduino
    • RISCV
    • FPGA
  • Network and devices
    • Basic CCNA
  • IoT Server
  • Q&A (Hỏi đáp)
  • About
No Result
View All Result
  • Home
  • Linux
  • Window
    • Latex
    • Matlab
  • Embedded programming
    • Jetson Tx1
    • Raspberry Pi
    • Raspberry Pico
  • Internet of things
    • HTTP+MySQL cho IoT
    • Websocket+Nodejs
    • ESP32
  • Electronics and robots
    • Robocon
    • Arduino
    • RISCV
    • FPGA
  • Network and devices
    • Basic CCNA
  • IoT Server
  • Q&A (Hỏi đáp)
  • About
No Result
View All Result
Store and share experiences about robocon, IoT, Embedded...
No Result
View All Result
Home Lập trình nhúng Raspberry Pico

How to connect Raspberry Pico W to a wireless network?

admin by admin
January 4, 2023
in HTTP+MySQL cho IoT, Raspberry Pico
0 0
0

This post shows you how to connect the Pico W to a wireless network. For displaying the connection status, an I2C OLED (4 pins) is used. Especially, I will show you the issues that may occur in practice when Pico W connects to a wireless network. Finally, some simple solutions to deal with the issues will be presented.  Let’s get started.

I. Connecting to an I2C OLED display

Firstly, we need to set up Pico W and the I2C OLED display. You simply connect the pins of Pico W and the pins of the OLED display as follows:

  • Pin GPIO0 (Pico W) -> Pin SDA (OLED)
  • Pin GPIO1 (Pico W) -> Pin SCL (OLED)
  • Vcc (Pico W) -> Vcc (OLED)
  • GND (Pico W) -> GND (OLED)

The following picture illustrates the connections on my break board. It is noted that my I2C OLED is the SSD1306 OLED display module.

Next, open your Thonny and create a python file, namely SSD1306.py ( its name depends on you). Next, you simply copy and paste the code below to check the Pico W and OLED work.

SSD OLED1306
Python
1
2
3
4
5
6
7
8
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
 
i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
 
oled.text("luuvachiase.net", 0, 0)
oled.show()

Firstly, the ssd1306 library needs to be imported, as shown in line 1 of the code above. Next, pin(0) and pin(1) of Pico W are defined to connect to pin (SDA) and pin(SCL) of SSD1306 OLED, respectively. Finally, we mount an object, namely “oled” to be used for the rest of our python code. For example, you only used line numbers 7 and 8 to display a text string.

Here are my results,

SSD1306 displays the contents received from Pico W through I2C protocol.

 

If you get the same, congratulations! You can go to the next step in this project.

II. Connecting to a wireless network

Connection status codes:

Connection status codes
C++
1
2
3
4
5
6
7
8
// Return value of cyw43_wifi_link_status
#define CYW43_LINK_DOWN (0)
#define CYW43_LINK_JOIN (1)
#define CYW43_LINK_NOIP (2)
#define CYW43_LINK_UP (3)
#define CYW43_LINK_FAIL (-1)
#define CYW43_LINK_NONET (-2)
#define CYW43_LINK_BADAUTH (-3)

This code is copied from the documentation of the RP2040 microcontroller.  However, they are defined in C++. We will use these codes to display the status of the wifi connection. For example:

Wifi connection status
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def switch(WL_code):
    if WL_code == 0:
        return "Link down"
    elif WL_code == 1:
        return "Link join"
    elif WL_code == 2:
        return "Link NoIP"
    elif WL_code == 3:
        return "Link up"
    elif WL_code == -1:
        return "Link fail"
    elif WL_code == -2:
        return "Link Nonet"
    elif WL_code == -3:
        return "Wrong P&S"

Next, create a python file, namely wireless.py, and save it on Raspberry Pico. Then, add the necessary libraries as follows:

wireless.py
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
import machine
import network
import secret
import time
 
from picozero import pico_temp_sensor
from time import sleep
from OLED1306 import *
 
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
 
stt = 0

In the code above, the libraries: machine, network, and time are the pre-build libraries. Regarding the secret, it is a separate file that contains the secret information like SSID and PASSWORD of the wireless network. You can create the file secret.py (save on Raspberry Pico) as follows:

secret.py
Python
1
2
SSID = "XXX-XXX"
PASSWORD = "XXX-XXX"

Next, we will define a function that will be used to connect to the wireless network.

wifi connection function
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def WL_connect():
    wlan.connect(secret.SSID, secret.PASSWORD)
    max_wait = 10    
    while max_wait >0:
        oled.fill(0)
        oled.text(secret.SSID, 0, 0)
        stt = wlan.status()
        oled.text(switch(stt), 0, 40)
        oled.text("connecting..."+str(max_wait), 0, 10)
        print("WL code:"+str(stt))
        oled.show()
        if stt <0 or stt >=3:
            break
        max_wait -=1
        time.sleep(1)
 
    return stt

In this function, we define a variable max_wait = 10 to check whether the connection is successful or failed. Simultaneously, this function prints the status code on the OLED display.
Finally, we complete the code as follows:

wireless.py
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import machine
import network
import secret
import time
 
from picozero import pico_temp_sensor
from time import sleep
from OLED1306 import *
 
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
 
stt = 0
 
def WL_connect():
    wlan.connect(secret.SSID, secret.PASSWORD)
    max_wait = 10
    while max_wait >0:
        stt = wlan.status()
        oled.fill(0)
        oled.text(secret.SSID, 0, 0)
        oled.text(switch(stt), 0, 40)
        oled.text("connecting..."+str(max_wait), 0, 10)
        oled.show()
        print(str(stt))
        if stt <0 or stt >=3:
            break
        max_wait -=1
        time.sleep(1)
    return stt
 
# handle connection error
def switch(WL_code):
    if WL_code == 0:
        return "Link down"
    elif WL_code == 1:
        return "Link join"
    elif WL_code == 2:
        return "Link NoIP"
    elif WL_code == 3:
        return "Link up"
    elif WL_code == -1:
        return "Link fail"
    elif WL_code == -2:
        return "NoIP"
    elif WL_code == -3:
        return "Wrong P&S"
 
oled.fill(0)
while True:
    if(WL_connect()==3):
        break

We place the WL_connect() function in a while loop. Therefore, it will retry to connect to the wireless network until it achieves a successful connection (code = 3).

These are two videos that illustrate two scenarios. The first one is a successful connection. In the second case, we first unplug the access point (AP) to observe the behaviors of Pico W via the status displayed on the OLED. Then, we plug the AP into the power supply and achieve a successful connection.

      • Case 1:

    • Case 2:

The access point is unplugged in the second video before Pico W starts. Then, at the 50th second, the access point was plugged. As expected, Pico W connected to the access point successfully, Link up!.

These results have demonstrated that the WL_connect() function works well. In the next post, we will use this function to fix the issue that occurred in the case of uploading data from Pico W to the internet.

Số lượt xem: 34
Tags: picopico Wraspberry piwireless network

Related Posts

Lập trình Raspberry Pi Pico [Bài 2]. Nạp firmware, chạy chương trình “Hello Pico”

Lập trình Raspberry Pi Pico [Bài 2]. Nạp firmware, chạy chương trình “Hello Pico”

March 1, 2021
797
Lập trình Raspberry Pi Pico [Bài 1]: Pico có gì đặc biệt?

Lập trình Raspberry Pi Pico [Bài 1]: Pico có gì đặc biệt?

March 1, 2021
541
IoT Webserver- ESP8266/ESP32 gửi dữ liệu lên Cloud, hiển thị ra trình duyệt web với MySQL và PHP

Giám sát nhiệt độ sử dụng cảm biến DS18b20 và ESP8266 hiển thị bằng biểu đồ với Highcharts và MySQL

April 18, 2020
3.6k

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Bài viết đọc nhiều

IoT Webserver- ESP8266/ESP32 gửi dữ liệu lên Cloud, hiển thị ra trình duyệt web với MySQL và PHP

IoT Webserver- ESP8266/ESP32 gửi dữ liệu lên Cloud, hiển thị ra trình duyệt web với MySQL và PHP

January 30, 2023
16.9k
Điều khiển ESP8266/ESP32 từ xa qua internet, không cần mở Port modem

Điều khiển ESP8266/ESP32 từ xa qua internet, không cần mở Port modem

November 7, 2019
10.1k
ESP32-CAMERA: Cài đặt môi trường Arduino IDE và nạp chương trình

ESP32-CAMERA: Cài đặt môi trường Arduino IDE và nạp chương trình

December 27, 2019
6.9k
IoT webserver- Gửi thông báo bằng email từ ESP8266 không dùng IFTTT

IoT webserver- Gửi thông báo bằng email từ ESP8266 không dùng IFTTT

November 19, 2020
5.5k
Store and share experiences about robocon, IoT, Embedded…

Lưu và chia sẻ những gì đã đọc, đã làm, đã nghiên cứu về vi điều khiển, hệ thống nhúng, internet of things, kiến trúc máy tính và hệ điều hành.

Liên hệ với quản trị viên

Chủ đề

  • Arduino
  • CCNA cơ bản
  • Cisco
  • Điện tử- Robot
  • ESP32
  • FPGA
  • HTTP+MySQL cho IoT
  • IoT Server
  • Jetson Tx1
  • Lập trình nhúng
  • Latex
  • Linux
  • Mạng và thiết bị mạng
  • Raspberry Pi
  • Raspberry Pico
  • RISCV
  • Robocon
  • Web of things
  • Window
  • WordPress

Quản trị trang

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

© 2019- 2023 luuvachiase.net - Phát triển và quản trị bởi Đỗ Ngọc Tuấn và Nguyễn Văn Tuấn ***Vui lòng ghi rõ nguồn khi trích dẫn bài viết từ Website này. DMCA.com Protection Status

No Result
View All Result
  • Trang chủ
  • Linux
  • Window
  • Lập trình nhúng
    • Jetson Tx1
    • Raspberry Pi
  • Web of things
    • HTTP+MySQL cho IoT
    • Websocket+Nodejs
  • Điện tử- Robot
    • Robocon
    • Arduino
    • RISCV
    • FPGA
  • Mạng và thiết bị mạng
    • CCNA cơ bản
  • IoT Server
  • Giới thiệu
  • Q&A (Hỏi đáp)

© 2019- 2023 luuvachiase.net - Phát triển và quản trị bởi Đỗ Ngọc Tuấn và Nguyễn Văn Tuấn ***Vui lòng ghi rõ nguồn khi trích dẫn bài viết từ Website này. DMCA.com Protection Status

Login to your account below

Forgotten Password?

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In