Adding WiFi To The Pico 2
Written by Harry Fairhead   
Monday, 19 August 2024
Article Index
Adding WiFi To The Pico 2
Connecting the ESP8266 ESP-01
Attention!
Some Utility Functions
Connecting to WiFi
Sending Data
A Web Server
Complete Listing Of Web Server

Complete Listing Of Web Server

The following listing also includes all of the functions presented in the chapter even if they aren’t used.

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include <string.h>
#define Debug true
int initWiFi()
{
uart_init(uart1, 115200); gpio_set_function(4, GPIO_FUNC_UART); gpio_set_function(5, GPIO_FUNC_UART); uart_set_format(uart1, 8, 1, UART_PARITY_NONE); uart_set_translate_crlf(uart1, true); sleep_ms(100); return 0; }
int getBlock(uint8_t buf[], int len) { int count = 0; while (count < len - 1) { if (uart_is_readable_within_us(uart1, 10000)) { buf[count++] = uart_getc(uart1); if (Debug) uart_putc(uart0, buf[count - 1]); } else { break; } } buf[count] = 0; return count; }
int ATWiFi(uint8_t buf[], int len) {
uint8_t SendData[] = "AT\r\n"; uart_write_blocking(uart1, SendData, 4); return getBlock(buf, len); }
int getVersionWiFi(uint8_t buf[], int len) { uint8_t SendData[] = "AT+GMR\r\n"; uart_write_blocking(uart1, SendData, 8); return getBlock(buf, len); }
int resetWiFi(uint8_t buf[], int len) { uint8_t SendData[] = "AT+RST\r\n"; uart_write_blocking(uart1, SendData, 8); return getBlock(buf, len); }
int setUARTWiFi(uint8_t buf[], int len) {
uint8_t SendData[] =
"AT+UART_CUR=115200,8,1,0,0\r\n";
uart_write_blocking(uart1, SendData, 28); return getBlock(buf, len); }
int modeWiFi(uint8_t buf[], int len, int mode) {
uint8_t command[32]; int count = snprintf(command, 32,
"AT+CWMODE_CUR=%d\r\n", mode); uart_write_blocking(uart1, command, count); return getBlock(buf, len);
}
int getBlocks(uint8_t buf[], int len, int num,
char target[])
{ for (int i = 0; i < num; i++) { if (uart_is_readable_within_us(uart1,
1000 * 1000))
{ getBlock(buf, len); if (strstr(buf, target)) return i; } } return -1; }
int scanWiFi(uint8_t buf[], int len) {
uint8_t SendData[] = "AT+CWLAP\r\n"; uart_write_blocking(uart1, SendData, 18); return getBlocks(buf, len, 20, "OK"); }
int connectWiFi(uint8_t buf[], int len, char ssid[],
char pass[])
{ uint8_t command[128]; int count = snprintf(command, 128, "AT+CWJAP_CUR=\"%s\",\"%s\"\r\n", ssid, pass);
uart_write_blocking(uart1, command, count);
return getBlocks(buf, len, 20, "OK");
}
int getIPWiFi(uint8_t buf[], int len)
{
uint8_t SendData[] = "AT+CIFSR\r\n";
uart_write_blocking(uart1, SendData, 10);
return getBlocks(buf, len, 20, "OK");
}
int getWebPageWiFi(uint8_t buf[], int len,
char URL[], char page[])
{
uint8_t command[128]; int count = snprintf(command, 128, "AT+CIPSTART=\"TCP\",\"%s\",80\r\n", URL);
uart_write_blocking(uart1, command, count);
if (getBlocks(buf, len, 20, "OK") < 0)
return -1;
char http[150];
sprintf(http, "GET %s HTTP/1.1\r\n
Host:%s\r\n\r\n", page, URL);
count = snprintf(command, 128,
"AT+CIPSEND=%d\r\n", strlen(http));
uart_write_blocking(uart1, command, count);
if (getBlocks(buf, len, 20, ">") < 0)
return -1;
uart_write_blocking(uart1, http, strlen(http));
return getBlocks(buf, len, 20, "</html>");
}
int startServerWiFi(uint8_t buf[], int len)
{
char temp[256];
char id[10];
uart_write_blocking(uart1, "AT+CIPMUX=1\r\n", 13);
if (getBlocks(buf, len, 10, "OK") < 0)
return -1;
uart_write_blocking(uart1,
"AT+CIPSERVER=1,80\r\n", 19);
if (getBlocks(buf, len, 10, "OK") < 0)
return -1;
for (;;)
{
if (getBlocks(buf, len, 1, "+IPD") < 0)
continue;
char *b = strstr(buf, "+IPD");
b += 5;
strncpy(temp, b, sizeof(temp));
char *e = strstr(temp, ",");
int d = e - temp;
memset(id, '\0', sizeof(id));
strncpy(id, temp, d);
char data[] =
HTTP/1.0 200 OK\r\nServer: Pico\r\n
Content-type: text/html\r\n\r\n
<html><head><title>Temperature</title></head>
<body><p>
{\"humidity\":81%,\"airtemperature\":23.5C}
</p></body>
</html>\r\n";
uint8_t command[128];
int count = snprintf(command, 128,
"AT+CIPSEND=%s,%d\r\n", id, strlen(data));
uart_write_blocking(uart1, command, count);
if (getBlocks(buf, len, 10, ">") < 0)
return -1;

uart_write_blocking(uart1, data, strlen(data));
if (getBlocks(buf, len, 10, "OK") < 0)
return -1;
count = snprintf(command, 128,
"AT+CIPCLOSE=%s\r\n", id);
uart_write_blocking(uart1, command, count);
if (getBlocks(buf, len, 10, "OK") < 0)
return -1;
}
return 0;
}
int main()
{
stdio_init_all();
uint8_t buf[512];
initWiFi();
modeWiFi(buf, 512, 1);
connectWiFi(buf, 512, "SSID", "password");
getIPWiFi(buf, 512);
startServerWiFi(buf, 512);
sleep_ms(1000);
}

Summary

  • Adding WiFi to the Pico is fairly easy using the low-cost ESP8266 ESP-01, which connects via the serial port and makes use of AT style commands to control the device as if it was a WiFi modem.

  • In addition to an ESP8266, you also need a power supply capable of running it. The Pico seems able to do the job from its 3.3 output.

  • You can use AT commands to set the device into client mode and connect to a WiFi network.

  • While it is possible to use ad-hoc protocols, there are advantages in using TCP, HTTP and HTML so that other devices can work with the Pico.

  • The Pico can use client mode to download data from web servers.

  • It can also emulate a server to deliver data to any web browser or HTML-using client.

Related Articles

C Sockets - No Need For A Web Server!

 

Programming the Raspberry Pi Pico In C
Third Edition

By Harry Fairhead

PicoC3E360

Buy from Amazon.

Contents

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

Extra: Adding WiFi To The Pico 2

*** Extracts from Edition 2 not yet updated.

 

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


Google Introduces Gemini CLI Open-Source Agent
08/07/2025

Google is introducing Gemini CLI, an open-source AI agent that offers lightweight access to Gemini, Google's conversational chatbot that is based on Google's multimodal large language model  [ ... ]



Gemini On-Device - Generative AI For Robots
09/07/2025

In that same way Gemini can produce text, write poetry,  summarize an article, write code, and generate images, it can also generate robot actions with Gemini Robotics. Now, the new Gemini Roboti [ ... ]


More News

pico book

 

Comments




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



Last Updated ( Saturday, 24 August 2024 )