Skip to content

Instantly share code, notes, and snippets.

@JA1TYE
Created October 30, 2022 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JA1TYE/46dce4f07fe762931e44f40e7067c48d to your computer and use it in GitHub Desktop.
Save JA1TYE/46dce4f07fe762931e44f40e7067c48d to your computer and use it in GitHub Desktop.
Core2 Commander example for WiFi-Enabled HDMI Switcher
#include <M5Core2.h>
#include <WiFi.h>
ButtonColors colorNormal = { BLACK, WHITE, 0x8410};
ButtonColors colorPressed = { WHITE, BLACK, 0x8410};
Button HDMIIn1 = Button(0, 0, 160, 120, false, "IN 1", colorPressed,colorNormal,MC_DATUM,0,0,10);
Button HDMIIn2 = Button(160, 0, 160, 120, false, "IN 2",colorPressed, colorNormal,MC_DATUM,0,0,10);
//WiFi Settings
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
//Server Setting
const char* host = "YOUR_HDMI_SWITCHER_ADDR";
QueueHandle_t xQueueMB;
void sendRequest(String url){
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
}
void selectHDMI(uint8_t in){
if(in == 0){
sendRequest("/sw/0");
}
else{
sendRequest("/sw/1");
}
}
void WiFiTask(void* pvParameters)
{
uint8_t data;
int ret;
while(1){
ret = xQueueReceive( xQueueMB, &data, portMAX_DELAY );
if(data <= 1){
selectHDMI(data);
}
Serial.println(data);
delay(1);
}
}
void HDMI1Handler(Event& e){
uint8_t data = 0;
xQueueOverwrite(xQueueMB, &data);
}
void HDMI2Handler(Event& e){
uint8_t data = 1;
xQueueOverwrite(xQueueMB, &data);
}
void setup() {
M5.begin();
xQueueMB = xQueueCreate(1, sizeof(uint8_t));
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
HDMIIn1.addHandler(HDMI1Handler, E_TOUCH);
HDMIIn2.addHandler(HDMI2Handler, E_TOUCH);
M5.Buttons.draw();
//Start wifi task
xTaskCreateUniversal(
WiFiTask,
"WiFiTask",
8192,
NULL,
1,
NULL,
APP_CPU_NUM
);
}
void loop() {
M5.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment