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

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


Random Gifts For Programmers
24/11/2024

Not really random. Not even pseudo random, more stuff that caught my attention and that I, for one, would like to be given. And, yes, if I'm not given them, I'd probably buy some for myself.



Highlights Of The Europe 2024 PostgreSQL Conference
22/11/2024

This year's premium conference for PostgreSQL took place in Athens, Greece between October 22-25. The nice Athenian weather and cultural aspect aside, the conference was a big hit too.


More News

espbook

 

Comments




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



Last Updated ( Saturday, 24 August 2024 )