complete iot structure

This commit is contained in:
Siwat Sirichai 2023-12-28 14:52:52 +07:00
parent da277e1e4a
commit 0dfb066a74
10 changed files with 259 additions and 74 deletions

View file

@ -3,36 +3,35 @@
// Instantiate ESPMega
ESPMegaPRO espmega = ESPMegaPRO();
void mqtt_callback(char *topic, char *payload)
{
Serial.print("MQTT Callback: ");
Serial.print(topic);
Serial.print(" ");
Serial.println(payload);
}
void setup()
{
// Initialize ESPMega
espmega.begin();
Serial.println("ESPMega initialized");
espmega.enableIotModule();
espmega.iot.connectToEthernet();
IPAddress ip(192, 168, 0, 11);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
espmega.iot.setETHStaticIp(ip, gateway, subnet);
espmega.iot.setBaseTopic("/testmegaoop");
espmega.iot.connectToMqtt("espmega", "192.168.0.26", 1883);
espmega.iot.publishRelative("test", "test");
espmega.iot.subscribeRelative("test");
espmega.iot.registerMqttCallback(mqtt_callback);
Serial.println("ESPMega IoT initialized");
}
void loop()
{
// Read all the inputs and print them
for (int i = 0; i < 16; i++)
{
Serial.print("Input ");
Serial.print(i);
Serial.print(": ");
Serial.print(espmega.inputs.digitalRead(i));
Serial.println();
}
// Turn on all the outputs
for (int i = 0; i < 16; i++)
{
espmega.outputs.digitalWrite(i, true);
}
// Wait 1 second
delay(1000);
// Turn off all the outputs
for (int i = 0; i < 16; i++)
{
espmega.outputs.digitalWrite(i, false);
}
// Wait 1 second
delay(1000);
// Call ESPMega loop
espmega.loop();
}