The Pico/W In C: A Better Connect
Written by Harry Fairhead   
Monday, 23 January 2023
Article Index
The Pico/W In C: A Better Connect
The cyw43_arch Functions
A Better Connect

A Better Connect

The setup function given in the previous section is good while you are debugging, but it would be good to have visual feedback using the onboard LED. To do this we need to use the asynchronous version of the connection function and we need to check the status to modify the speed of flashing of the LED:

int setup(uint32_t country, const char *ssid,
const char *pass,
uint32_t auth)
{
if (cyw43_arch_init_with_country(country))
{
return 1;
}
cyw43_arch_enable_sta_mode();
if (cyw43_arch_wifi_connect_async(ssid, pass, auth))
{
return 2;
}
int flashrate = 1000;
int status = CYW43_LINK_UP + 1;
while (status >= 0 && status != CYW43_LINK_UP)
{
int new_status = cyw43_tcpip_link_status(
&cyw43_state,
CYW43_ITF_STA);
if (new_status != status)
{
status = new_status;
flashrate = flashrate / (status + 1);
printf("connect status: %d %d\n",
status, flashrate);
}
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
sleep_ms(flashrate);
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
sleep_ms(flashrate);
}
if (status < 0)
{
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
}
else
{
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
}
return status;
}

If you use this extended setup function the onboard LED starts to flash slowly and speeds up with each stage of connection. If the function ends without an error, a negative status, the LED is left on, otherwise it is turned off. You can modify the function to give more information by using different flash rates.

If you are puzzled by the way the status is handled the reason for the initial int status = CYW43_LINK_UP + 1 is that the status isn’t updated until the first change of status and so you have to initialize it to a value that cannot be returned as a valid status code. After this you can detect changes in the status and react accordingly.

In chapter but not in this extract

  • LwIP NETIF
  • A Web Client
  • A Web Server
  • A Custom Web Site
  • A Dynamic Web Page – Server Side Includes
  • Listing
  • Where Next

 

Summary

  • The Pico W’s new WiFi hardware is connected via an SPI bus and this makes use of several GPIO lines that are used in the Pico for different things. In particular, GP25 is no longer used to control the internal LED. The internal LED is now controlled by a GPIO line on the WiFi chip.

  • The new WiFi software has two components – a low-level driver and the LwIP library which provides higher-level networking making use of the driver.

  • The WiFi system works in two modes, polling or background mode.

  • Connecting to a WiFi access point is easy, but providing feedback on the current state to the user is more difficult.

  • Creating a web client can be done using low-level TCP functions, but it is simpler to use the supplied HTTP client app.

  • Similarly a web server can be built using TCP functions, but the supplied HTTP server is easier to use.

  • To use the server you need to build and apply the htmlgen/makefsdata to convert the web site into a single C file which can be included in your project.

  • To create a dynamic web site you need to include SSI (Server Side Include) tags and a suitable SSI handling function.

  • The driver and the LwIP library provide a huge range of poorly documented possibilities.

 

Programming the Raspberry Pi Pico In C

By Harry Fairhead

picoC2E360

Buy from Amazon.

Contents

  • Preface
  • Chapter 1 The Raspberry Pi Pico – Before We Begin
  • Chapter 2 Getting Started
  • Chapter 3 Getting Started With The GPIO
  • Chapter 4 Simple Output
  • Chapter 5 Some Electronics
  • Chapter 6 Simple Input
        Extract:   GPIO Input ***NEW!
  • Chapter 7 Advanced Input – Events and Interrupts
  • Chapter 8 Pulse Width Modulation
        Extract: Basic PWM
  • Chapter 9 Controlling Motors And Servos
  • Chapter 10 Getting Started With The SPI Bus
  • Chapter 11 A-To-D and The SPI Bus
  • Chapter 12 Using The I2C Bus
  • Chapter 13 Using The PIO
        Extract: A 1-Wire PIO Program  
  • Chapter 14 The DHT22 Sensor Implementing A Custom Protocol
  • Chapter 15 The 1‑Wire Bus And The DS1820
  • Chapter 16 The Serial Port
  • Chapter 17 Using the Pico W
       Extract: Simple Web Client
       Extract:A Better Connect
  • Chapter 18 The Pico/W In C: Direct To Hardware 

Extra: Adding WiFi To The Pico 2

<ASIN:1871962803>

<ASIN:187196279X>

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Raspberry Pi CM5 - Expensive And Undocumented
27/11/2024

So the unexpected has happened - the Compute Module 5 has been launched. But it simply emphasises some problems with adopting the Pi as an IoT device.



DuckDB And Hydra Partner To Get DuckDB Into PostgreSQL
11/11/2024

The offspring of that partnership is pg_duckdb, an extension that embeds the DuckDB engine into the PostgreSQL database, allowing it to handle analytical workloads.


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Monday, 23 January 2023 )