Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
v1.13.1 to use new WiFi101_Generic library
Browse files Browse the repository at this point in the history
### Release v1.13.1

1. Using new [`WiFi101_Generic library`](https://github.com/khoih-prog/WiFi101_Generic) for sending larger data
2. Add astyle using `allman` style. Restyle the library
3. Update `Packages' Patches`
  • Loading branch information
khoih-prog authored Nov 24, 2022
1 parent 5187833 commit d113915
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 154 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/****************************************************************************************************************************
RP2040W-AdvancedWebServer.ino
For RP2040W with CYW43439 WiFi.
Based on and modified from Gil Maimon's ArduinoWebsockets library https://github.com/gilmaimon/ArduinoWebsockets
to support STM32F/L/H/G/WB/MP1, nRF52, SAMD21/SAMD51, RP2040 boards besides ESP8266 and ESP32
The library provides simple and easy interface for websockets (Client and Server).
Built by Khoi Hoang https://github.com/khoih-prog/Websockets2_Generic
Licensed under MIT license
*****************************************************************************************************************************/
Expand Down Expand Up @@ -51,9 +51,9 @@ void handleRoot()

if (minFreeHeap > curFreeHeap)
minFreeHeap = curFreeHeap;

digitalWrite(LED_BUILTIN, LED_ON);

int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
Expand Down Expand Up @@ -85,7 +85,7 @@ body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Col
void handleNotFound()
{
digitalWrite(LED_BUILTIN, LED_ON);

String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
Expand All @@ -109,14 +109,14 @@ void handleNotFound()
#define TEMP_STR_LEN 80

String out;

void drawGraph()
{
{
char temp[TEMP_STR_LEN];
static uint32_t previousStrLen = ORIGINAL_STR_LEN;

digitalWrite(LED_BUILTIN, LED_ON);

if (out.length() == 0)
{
LOGWARN1(F("String Len = 0, extend to"), ORIGINAL_STR_LEN);
Expand All @@ -126,32 +126,33 @@ void drawGraph()
out = F( "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n" \
"<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"3\" stroke=\"rgb(0, 0, 0)\" />\n" \
"<g stroke=\"blue\">\n");

int y = rand() % 130;

for (int x = 15; x < 300; x += 15)
{
int y2 = rand() % 130;

memset(temp, 0, TEMP_STR_LEN);

//sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2);
snprintf(temp, TEMP_STR_LEN - 1, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 15, 140 - y2);
snprintf(temp, TEMP_STR_LEN - 1, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y,
x + 15, 140 - y2);

if (out.length() > previousStrLen - TEMP_STR_LEN)
{
{
LOGERROR3(F("String Len > "), previousStrLen, F(", extend to"), previousStrLen + TEMP_STR_LEN * 2);

// Add twice to be safe
previousStrLen += TEMP_STR_LEN * 2;
previousStrLen += TEMP_STR_LEN * 2;

out.reserve(previousStrLen);
}

out += temp;
y = y2;
}

out += F("</g>\n</svg>\n");

server.send(200, "image/svg+xml", out);
Expand All @@ -175,11 +176,13 @@ void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LED_OFF);

Serial.begin(115200);

while (!Serial && millis() < 5000);

Serial.print("\nStarting RP2040W-AdvancedWebServer on "); Serial.println(BOARD_NAME);
Serial.print("\nStarting RP2040W-AdvancedWebServer on ");
Serial.println(BOARD_NAME);
Serial.println(WEBSOCKETS2_GENERIC_VERSION);

///////////////////////////////////
Expand All @@ -188,22 +191,23 @@ void setup()
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");

// don't continue
while (true);
}

Serial.print(F("Connecting to SSID: "));
Serial.println(ssid);

status = WiFi.begin(ssid, pass);

delay(1000);

// attempt to connect to WiFi network
while ( status != WL_CONNECTED)
{
delay(500);

// Connect to WPA/WPA2 network
status = WiFi.status();
}
Expand Down Expand Up @@ -248,18 +252,18 @@ void checkClient()
if (client.available())
{
WebsocketsMessage msg = client.readNonBlocking();

// log
Serial.print("Got Message: ");
Serial.println(msg.data());

// return echo
client.send("Echo: " + msg.data());

// close the connection
client.close();
}

checkClient_timeout = millis() + CLIENT_CHECK_INTERVAL;
}
}
Expand Down
38 changes: 38 additions & 0 deletions examples/Generic/RP2040W/RP2040W-AdvancedWebServer/defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/****************************************************************************************************************************
defines.h
For RP2040W with CYW43439 WiFi.
Based on and modified from Gil Maimon's ArduinoWebsockets library https://github.com/gilmaimon/ArduinoWebsockets
to support STM32F/L/H/G/WB/MP1, nRF52, SAMD21/SAMD51, RP2040 boards besides ESP8266 and ESP32
The library provides simple and easy interface for websockets (Client and Server).
Built by Khoi Hoang https://github.com/khoih-prog/Websockets2_Generic
Licensed under MIT license
*****************************************************************************************************************************/

#ifndef defines_h
#define defines_h

#if ( defined(ARDUINO_RASPBERRY_PI_PICO_W) )
#if defined(WEBSOCKETS_USE_RP2040W)
#undef WEBSOCKETS_USE_RP2040W
#endif
#define WEBSOCKETS_USE_RP2040W true
#define USE_RP2040W_WIFI true
#define USE_WIFI_NINA false
#else
#error This code is intended to run only on the RP2040W boards ! Please check your Tools->Board setting.
#endif

#include <WiFi.h>

#define DEBUG_WEBSOCKETS_PORT Serial
// Debug Level from 0 to 4
#define _WEBSOCKETS_LOGLEVEL_ 3

char ssid[] = "your_ssid"; // your network SSID (name)
char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP), length must be 8+

#endif //defines_h
62 changes: 32 additions & 30 deletions examples/Generic/RP2040W/RP2040W-Client/RP2040W-Client.ino
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
/****************************************************************************************************************************
RP2040W-Client.ino
For RP2040W with CYW43439 WiFi.
Based on and modified from Gil Maimon's ArduinoWebsockets library https://github.com/gilmaimon/ArduinoWebsockets
to support STM32F/L/H/G/WB/MP1, nRF52, SAMD21/SAMD51, RP2040 boards besides ESP8266 and ESP32
The library provides simple and easy interface for websockets (Client and Server).
Built by Khoi Hoang https://github.com/khoih-prog/Websockets2_Generic
Licensed under MIT license
*****************************************************************************************************************************/
/****************************************************************************************************************************
RP2040W Websockets Client
RP2040W Websockets Client
This sketch:
This sketch:
1. Connects to a WiFi network
2. Connects to a Websockets server
3. Sends the websockets server a message ("Hello to Server from ......")
4. Prints all incoming messages while the connection is open
Hardware:
Hardware:
For this sketch you only need an RP2040W board.
Originally Created : 15/02/2019
Original Author : By Gil Maimon
Original Repository : https://github.com/gilmaimon/ArduinoWebsockets
Originally Created : 15/02/2019
Original Author : By Gil Maimon
Original Repository : https://github.com/gilmaimon/ArduinoWebsockets
*****************************************************************************************************************************/

Expand All @@ -46,23 +46,23 @@ using namespace websockets2_generic;

WebsocketsClient client;

void onEventsCallback(WebsocketsEvent event, String data)
void onEventsCallback(WebsocketsEvent event, String data)
{
(void) data;
if (event == WebsocketsEvent::ConnectionOpened)

if (event == WebsocketsEvent::ConnectionOpened)
{
Serial.println("Connnection Opened");
}
else if (event == WebsocketsEvent::ConnectionClosed)
}
else if (event == WebsocketsEvent::ConnectionClosed)
{
Serial.println("Connnection Closed");
}
else if (event == WebsocketsEvent::GotPing)
}
else if (event == WebsocketsEvent::GotPing)
{
Serial.println("Got a Ping!");
}
else if (event == WebsocketsEvent::GotPong)
}
else if (event == WebsocketsEvent::GotPong)
{
Serial.println("Got a Pong!");
}
Expand All @@ -83,33 +83,35 @@ void printWifiStatus()
void setup()
{
Serial.begin(115200);

while (!Serial && millis() < 5000);

Serial.println("\nStarting RP2040W-Client on " + String(BOARD_NAME));
Serial.println(WEBSOCKETS2_GENERIC_VERSION);

///////////////////////////////////

// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");

// don't continue
while (true);
}

Serial.print(F("Connecting to SSID: "));
Serial.println(ssid);

status = WiFi.begin(ssid, pass);

delay(1000);

// attempt to connect to WiFi network
while ( status != WL_CONNECTED)
{
delay(500);

// Connect to WPA/WPA2 network
status = WiFi.status();
}
Expand All @@ -122,35 +124,35 @@ void setup()
Serial.println(websockets_server_host);

// run callback when messages are received
client.onMessage([&](WebsocketsMessage message)
client.onMessage([&](WebsocketsMessage message)
{
Serial.print("Got Message: ");
Serial.println(message.data());
});

// run callback when events are occuring
client.onEvent(onEventsCallback);

// try to connect to Websockets server
bool connected = client.connect(websockets_server_host, websockets_server_port, "/");
if (connected)

if (connected)
{
Serial.println("Connected!");

String WS_msg = String("Hello to Server from ") + BOARD_NAME;
client.send(WS_msg);
}
else
}
else
{
Serial.println("Not Connected!");
}
}

void loop()
void loop()
{
// let the websockets client check for incoming messages
if (client.available())
if (client.available())
{
client.poll();
}
Expand Down
4 changes: 2 additions & 2 deletions examples/Generic/RP2040W/RP2040W-Client/defines.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/****************************************************************************************************************************
defines.h
For RP2040W with CYW43439 WiFi.
Based on and modified from Gil Maimon's ArduinoWebsockets library https://github.com/gilmaimon/ArduinoWebsockets
to support STM32F/L/H/G/WB/MP1, nRF52, SAMD21/SAMD51, RP2040 boards besides ESP8266 and ESP32
The library provides simple and easy interface for websockets (Client and Server).
Built by Khoi Hoang https://github.com/khoih-prog/Websockets2_Generic
Licensed under MIT license
*****************************************************************************************************************************/
Expand Down
Loading

0 comments on commit d113915

Please sign in to comment.