Files
examples/arduino-example/src/main.cpp

200 lines
5.6 KiB
C++
Raw Normal View History

/**
*
* Copyright (c) 2020-2025 IRext Opensource Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2025-10-30 20:58:20 +08:00
#include <Arduino.h>
#include <WiFiS3.h>
2026-01-01 20:34:23 +08:00
#include <ArduinoGraphics.h>
#include <Arduino_LED_Matrix.h>
#include "configure.h"
2026-01-01 20:34:23 +08:00
#include "remote.h"
2026-01-01 20:34:23 +08:00
#define WIFI_SERVER_PORT (8000)
// global variable definitions
constexpr char ssid[] = SECRET_SSID;
constexpr char pass[] = SECRET_PASS;
2025-10-30 20:58:20 +08:00
// commands and events
auto *aHello = "a_hello";
auto *eHello = "e_hello";
auto *aBin = "a_bin";
auto *eBin = "e_bin";
auto *aControl = "a_control";
auto *eControl = "e_control";
auto *aError = "a_error";
auto *eError = "e_error";
2025-10-30 20:58:20 +08:00
int status = WL_IDLE_STATUS;
unsigned long lastStatusCheck = 0;
boolean wifiStatusPrinted = false;
ArduinoLEDMatrix matrix;
WiFiServer server(WIFI_SERVER_PORT);
WiFiClient client;
boolean clientConnected = false;
void drawIp(const char *ipAddr) {
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textScrollSpeed(100);
matrix.textFont(Font_5x7);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(ipAddr);
matrix.endText(SCROLL_LEFT);
matrix.endDraw();
}
2025-10-30 20:58:20 +08:00
void printWiFiStatus() {
unsigned long currentMillis = millis();
if (currentMillis - lastStatusCheck >= 10000 && !wifiStatusPrinted) {
IPAddress ip = WiFi.localIP();
if (0 == strcmp(ip.toString().c_str(), "0.0.0.0")) {
lastStatusCheck = currentMillis;
return;
}
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
Serial.print("IP address: ");
Serial.println(ip);
2026-01-01 20:34:23 +08:00
const long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI): ");
Serial.print(rssi);
Serial.println(" dBm");
drawIp(ip.toString().c_str());
lastStatusCheck = currentMillis;
wifiStatusPrinted = true;
}
2025-10-30 20:58:20 +08:00
}
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(100);
}
matrix.begin();
matrix.beginDraw();
matrix.stroke(0xFFFFFFFF);
matrix.textScrollSpeed(100);
constexpr char text[] = "IRext Example";
matrix.textFont(Font_4x6);
matrix.beginText(0, 1, 0xFFFFFF);
matrix.println(text);
matrix.endText(SCROLL_LEFT);
matrix.endDraw();
Serial.println("Wi-Fi started in station mode");
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
2025-10-30 20:58:20 +08:00
status = WiFi.begin(ssid, pass);
2025-10-30 20:58:20 +08:00
if (status == WL_CONNECTED) {
Serial.println("\nConnection Successful!");
server.begin();
}
else {
Serial.print("\nConnection Failed! Status: ");
Serial.println(status);
2025-10-30 20:58:20 +08:00
}
}
2025-10-30 20:58:20 +08:00
void onConnected(WiFiClient *client) {
client->flush();
Serial.println("Client connected");
client->println(eHello);
}
void onDisconnected(WiFiClient *client) {
remoteClose();
client->flush();
client->stop();
Serial.println("Client disconnected");
}
void onError(WiFiClient *client) {
client->flush();
client->stop();
}
void onCommand(const String *command, WiFiClient *client) {
if (command->startsWith(aHello)) {
Serial.println("Received hello command");
client->println(eBin);
client->flush();
} else if (command->startsWith(aBin)) {
Serial.println("Received bin command");
if (remoteOpen(command->c_str()) > 0) {
client->println(eControl);
} else {
Serial.println("Failed to parse bin command");
client->println(eError);
}
} else if (command->startsWith(aControl)) {
Serial.println("Received control command");
remoteControl(command->c_str());
} else if (command->startsWith(aError)) {
Serial.println("Received error command");
onError(client);
}
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Connection lost. Reconnecting...");
status = WiFi.begin(ssid, pass);
if (status == WL_CONNECTED) {
Serial.println("Reconnected!");
}
} else {
printWiFiStatus();
client = server.available();
if (client.connected()) {
if (false == clientConnected) {
onConnected(&client);
}
clientConnected = true;
if (client.available()) {
const String received = client.readStringUntil('\n');
Serial.println(received);
onCommand(&received, &client);
}
} else {
if (clientConnected) {
onDisconnected(&client);
}
clientConnected = false;
}
}
delay(10);
}