Reputation: 271
I am developing a project using the pi pico w and I need to make get and post request to a website to report that the device is live and to send temperature data and to receive commands for now the only request I'm making is to reefdripper.ddnss.com/data which returns a random uuidv4 id. Every time I run the program my pico freezes after a random amount of requests sometimes its ten other times its 143
Also it seems to halt much quicker on average if I try to add loop1() and setup1() in the code to blink the onboard led.
I use the Arduino framework for the code
Here is my code:
#include <Arduino.h>
#include <WiFi.h>
#include "HTTPClient.h"
struct HTTPResponse
{
String response;
int code;
};
struct HTTPWIFI
{
private:
HTTPClient http;
HTTPResponse httpGETRequest(String serverName, String uri)
{
String payload = "{}";
http.begin(serverName+uri);
int httpResponseCode = http.GET();
payload = http.getString();
http.end();
return {payload, httpResponseCode};
}
HTTPResponse httpPOSTRequest(String serverName, String uri, String ContentType, String data)
{
String payload = "{}";
http.begin(serverName+uri);
http.addHeader("Content-Type", ContentType.c_str());
int code = http.POST(data.c_str());
payload = http.getString();
http.end();
return {payload, code};
}
public:
void Setup(String ssid, String passwd)
{
WiFi.begin(ssid.c_str(), passwd.c_str());
while(WiFi.status() != WL_CONNECTED) {
delay(750);
}
http.setReuse(true);
}
bool isConnectedtoWifi() { return (WiFi.status() == WL_CONNECTED); }
HTTPResponse PostRequest(String servername, String uri, String ContentType, String data)
{
return this->httpPOSTRequest(servername, uri, ContentType, data);
}
HTTPResponse GetRequest(String servername, String url)
{
return this->httpGETRequest(servername.c_str(), url.c_str());
}
};
String SSID = ""; // removed
String PASSWD = ""; // removed
String APIURL = "http://reefdripper.ddnss.org";
HTTPWIFI WifiHandler;
void setup() {
Serial.begin(115200);
WifiHandler.Setup(SSID, PASSWD);
}
void loop() {
if (WifiHandler.isConnectedtoWifi())
{
Serial.println(WifiHandler.GetRequest(APIURL, "/data").response);
delay(1000);
} else {
Serial.println("NOT connected");
}
}
I tried the debugger for errors but none were there. I tried to find memory leaks in the source code but no luck either. I searched the internet for help most were about the _thread module in micropython
I can't switch to micropython because of how slow it is a request in c++ takes 300ms while one in python takes 2.3 seconds
Help is greatly appreciated!
Upvotes: 0
Views: 699
Reputation: 271
I found a fix which is to use watchdog from the PICO SDK. With it, I can use both cores with no halting!
Here's the working code:
#include <Arduino.h>
#include <WiFi.h>
#include "HTTPClient.h"
struct HTTPResponse
{
String response;
int code;
};
struct HTTPWIFI
{
private:
HTTPClient http;
HTTPResponse httpGETRequest(String serverName, String uri)
{
String payload = "{}";
http.begin(serverName+uri);
int httpResponseCode = http.GET();
payload = http.getString();
http.end();
return {payload, httpResponseCode};
}
HTTPResponse httpPOSTRequest(String serverName, String uri, String ContentType, String data)
{
String payload = "{}";
http.begin(serverName+uri);
http.addHeader("Content-Type", ContentType.c_str());
int code = http.POST(data.c_str());
payload = http.getString();
http.end();
return {payload, code};
}
public:
void Setup(String ssid, String passwd)
{
WiFi.begin(ssid.c_str(), passwd.c_str());
while(WiFi.status() != WL_CONNECTED) {
delay(750);
}
http.setReuse(true);
}
bool isConnectedtoWifi() { return (WiFi.status() == WL_CONNECTED); }
HTTPResponse PostRequest(String servername, String uri, String ContentType, String data)
{
return this->httpPOSTRequest(servername, uri, ContentType, data);
}
HTTPResponse GetRequest(String servername, String url)
{
return this->httpGETRequest(servername.c_str(), url.c_str());
}
};
String SSID = "";
String PASSWD = "";
String APIURL = "http://reefdripper.ddnss.org";
HTTPWIFI WifiHandler;
void setup() {
if (watchdog_caused_reboot()) {
printf("Rebooted by Watchdog!\n");
} else {
printf("Clean boot\n");
}
watchdog_enable(2000, true);
Serial.begin(115200);
WifiHandler.Setup(SSID, PASSWD);
}
void loop() {
if (WifiHandler.isConnectedtoWifi())
{
Serial.println(WifiHandler.GetRequest(APIURL, "/data").response);
} else {
Serial.println("NOT connected");
}
}
void setup1()
{
pinMode(LED_BUILTIN, OUTPUT);
}
void loop1()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(75);
digitalWrite(LED_BUILTIN, LOW);
delay(75);
watchdog_update();
}
Upvotes: 1