Skip to content

Instantly share code, notes, and snippets.

@maxpromer
Created January 31, 2024 13:54
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 maxpromer/3a87e403804f26258b0053075172aa2d to your computer and use it in GitHub Desktop.
Save maxpromer/3a87e403804f26258b0053075172aa2d to your computer and use it in GitHub Desktop.
ATD3.5-S3 x NETPIE
#include <Arduino.h>
#include <lvgl.h>
#include <ATD3.5-S3.h>
#include "gui/ui.h"
#include <WiFi.h>
#include <PubSubClient.h>
#define MQTT_SERVER "mqtt.netpie.io"
#define MQTT_PORT 1883
#define MQTT_CLIENT_ID "58dd3ff2-33e7-4203-8a83-528b65076fd9"
#define MQTT_USERNAME "u7EfgLTA9DtFGiq13warSB29FWMVreg7"
#define MQTT_PASSWORD "RCjyKPCEEFwtFPG6v562PkmsxApmJdo9"
const char * ssid = "CYD1";
const char * password = "12345678";
int light_pin[] = { 1, 2, 42, 41 };
WiFiClient espClient;
PubSubClient client(espClient);
void on_light_button_click(lv_event_t * e) {
lv_obj_t * target = lv_event_get_target(e);
int i = (int) lv_event_get_user_data(e);
bool isOn = lv_obj_has_state(target, LV_STATE_CHECKED);
digitalWrite(light_pin[i], isOn ? LOW : HIGH);
char topic[30];
sprintf(topic, "@msg/light%d", i + 1);
client.publish(topic, isOn ? "1" : "0");
}
void lightUpdate(lv_obj_t * obj, int pin, int value) {
digitalWrite(pin, value == 1 ? LOW : HIGH);
if (value) {
lv_obj_add_state(obj, LV_STATE_CHECKED);
} else {
lv_obj_clear_state(obj, LV_STATE_CHECKED);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
int value = 0;
if (length > 0) {
value = payload[0] == '1' ? 1 : 0;
}
if (strcmp(topic, "@msg/light1") == 0) {
lightUpdate(ui_light1, light_pin[0], value);
} else if (strcmp(topic, "@msg/light2") == 0) {
lightUpdate(ui_light2, light_pin[1], value);
} else if (strcmp(topic, "@msg/light3") == 0) {
lightUpdate(ui_light3, light_pin[2], value);
} else if (strcmp(topic, "@msg/light4") == 0) {
lightUpdate(ui_light4, light_pin[3], value);
}
}
void setup() {
Serial.begin(115200);
// Setup peripherals
Display.begin(0); // rotation number 0
Touch.begin();
Sound.begin();
// Card.begin(); // uncomment if you want to Read/Write/Play/Load file in MicroSD Card
for (int pin : light_pin) {
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
}
// Map peripheral to LVGL
Display.useLVGL(); // Map display to LVGL
Touch.useLVGL(); // Map touch screen to LVGL
Sound.useLVGL(); // Map speaker to LVGL
// Card.useLVGL(); // Map MicroSD Card to LVGL File System
// Add load your UI function
ui_init();
// Add event handle
lv_obj_add_event_cb(ui_light1, on_light_button_click, LV_EVENT_VALUE_CHANGED, (void *) 0);
lv_obj_add_event_cb(ui_light2, on_light_button_click, LV_EVENT_VALUE_CHANGED, (void *) 1);
lv_obj_add_event_cb(ui_light3, on_light_button_click, LV_EVENT_VALUE_CHANGED, (void *) 2);
lv_obj_add_event_cb(ui_light4, on_light_button_click, LV_EVENT_VALUE_CHANGED, (void *) 3);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(MQTT_SERVER, MQTT_PORT);
client.setCallback(callback);
if (client.connect(MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD)) {
client.subscribe("@msg/#");
}
}
void loop() {
Display.loop(); // Keep GUI work
client.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment