SG Electronic Systems was born as a support to the Open Hardware projects. Our experience allows us to support the Open Hardware team in the development of new electronic systems and in their distribution. We have a long experience in Fieldbus applications as: CAN BUS, RS-232, RS-422/485, Ethernet.
In this tutorial we explain how to program the Raspberry PICO W as a WIRELESS relay control.
The Relay board require a 5 V DC power supply (max allowed value is 5.1 V). Connect PICO W and Relay board as shown in the following picture.
The first step in Arduino sketch is the Wifi connection, SSID and Password are hard coded
char ssid[] = "your network SSID"; // your network SSID (name)
char pass[] = "your network password"; // your network password
In void setup there is the connection procedure
// 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();
}
printWifiStatus();
The Web Server on this tutorial is implemented with AsyncWebServer_RP2040W example. We define a server listener on port 80, the same used for browsing the web.
AsyncWebServer server(80);
After that, it is possible to define the web pages URL with the following command server.on. The idea is to prepare a function with parameters managements, the first is RELAY for relay number, the second is STATUS for on/off commanding.
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
{
handleRoot(request);
int paramsNr = request->params();
if (paramsNr == 2)
{
AsyncWebParameter* p1 = request->getParam(0);
AsyncWebParameter* p2 = request->getParam(1);
if ((p1->name() == "RELAY") && (p2->name() == "status"))
{
int rel_number = p1->value().toInt();
int rel_is_on = 0;
Serial.print("Relay ");
Serial.print(rel_number);
if (p2->value() == "on")
{
Serial.println(" On");
rel_is_on = REL_ON ;
}
else if (p2->value() == "off")
{
Serial.println(" Off");
rel_is_on = REL_OFF;
}
if ((rel_number>=1) && (rel_number <=8))
{
relay_status[rel_number-1] = rel_is_on;
if (rel_is_on == REL_ON) strcpy(rel_status_text[rel_number-1], "ON");
else if (rel_is_on == REL_OFF) strcpy(rel_status_text[rel_number-1], "OFF");
set_relay();
}
}
}
});
For exsemple, to switch on relay 1 the URL is http://boardip/?RELAY=1&status=on, instead to switch off is http://boardip/?RELAY=1&status=off
For erroneous URL we must define an handle for not find page
server.onNotFound(handleNotFound);
Now it is possible to start the server
server.begin();
The function
void handleRoot(AsyncWebServerRequest *request)
is used to generate the web page at realtime, snprintf is used to generate the HTML string sent by function request->send(200, "text/html", temp);
Web server testIt is possible to discover a connected device in your network with a NESCAN tool, or monitoring the PICO W serial console.
Put the IP on Firefox address bar, if you see the following page means that server is correctly configured and works fine.
Click on ON button to switch on relay 1, the relay 1 is switched on immediately, but the webpage is updated after 1 second. If you want to modify the update time, modify the following define.
#define STATUS_CHECK_INTERVAL 1000L
Comments