|
|
|
@ -1,4 +1,24 @@
|
|
|
|
|
// #define USE_INTERRUPT
|
|
|
|
|
#include <ESPMegaPRO.h>
|
|
|
|
|
#include <ETH.h>
|
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
|
#include <PubSubClientTools.h>
|
|
|
|
|
#include <Thread.h>
|
|
|
|
|
#include <StaticThreadController.h>
|
|
|
|
|
|
|
|
|
|
// Network Connectivity
|
|
|
|
|
#define HOSTNAME "espmega-pro-r3"
|
|
|
|
|
const IPAddress IP(192, 168, 0, 210);
|
|
|
|
|
const IPAddress SUBNET(255, 255, 255, 0);
|
|
|
|
|
const IPAddress GATEWAY(192, 168, 0, 1);
|
|
|
|
|
const IPAddress DNS(10, 192, 1, 1);
|
|
|
|
|
const IPAddress MQTT_SERVER(192, 168, 0, 26);
|
|
|
|
|
const int MQTT_PORT = 1883;
|
|
|
|
|
// #define MQTT_USE_AUTH
|
|
|
|
|
#ifdef MQTT_USE_AUTH
|
|
|
|
|
const char MQTT_USERNAME[] = "username";
|
|
|
|
|
const char MQTT_PASSWORD[] = "password";
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Inputs
|
|
|
|
|
const int DEBOUNCE_TIME_MS = 50;
|
|
|
|
@ -14,19 +34,87 @@ int pwm_values[16];
|
|
|
|
|
// Forward declaration
|
|
|
|
|
void virtual_interrupt_loop();
|
|
|
|
|
void virtual_interrupt_callback(int pin, int state);
|
|
|
|
|
void network_begin();
|
|
|
|
|
void mqtt_connect();
|
|
|
|
|
void mqtt_subscribe();
|
|
|
|
|
void thread_initialization();
|
|
|
|
|
void mqtt_callback(String topic, String message);
|
|
|
|
|
|
|
|
|
|
WiFiClient eth;
|
|
|
|
|
PubSubClient mqtt_client(MQTT_SERVER, 1883, eth);
|
|
|
|
|
PubSubClientTools mqtt(mqtt_client);
|
|
|
|
|
|
|
|
|
|
Thread mqtt_reconnector = Thread();
|
|
|
|
|
StaticThreadController<1> thread_controller(&mqtt_reconnector);
|
|
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
|
{
|
|
|
|
|
ESPMega_begin();
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
Serial.println("ESPMega R3 Initializing . . .");
|
|
|
|
|
Serial.println("Initializing Network . . .");
|
|
|
|
|
network_begin();
|
|
|
|
|
Serial.println("Initializing MQTT . . .");
|
|
|
|
|
mqtt_connect();
|
|
|
|
|
Serial.println("Initializing Threads . . .");
|
|
|
|
|
thread_initialization();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
|
{
|
|
|
|
|
virtual_interrupt_loop();
|
|
|
|
|
ESPMega_loop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void network_begin()
|
|
|
|
|
{
|
|
|
|
|
ETH.setHostname(HOSTNAME);
|
|
|
|
|
ETH.config(IP, GATEWAY, SUBNET, DNS);
|
|
|
|
|
ETH.begin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mqtt_connect()
|
|
|
|
|
{
|
|
|
|
|
if (!mqtt_client.connected())
|
|
|
|
|
{
|
|
|
|
|
Serial.print("MQTT not connected, connecting . . .\n");
|
|
|
|
|
#ifdef MQTT_USE_AUTH
|
|
|
|
|
mqtt_client.connect(HOSTNAME, MQTT_USERNAME, MQTT_PASSWORD);
|
|
|
|
|
#else
|
|
|
|
|
mqtt_client.connect(HOSTNAME);
|
|
|
|
|
#endif
|
|
|
|
|
if (mqtt_client.connected())
|
|
|
|
|
{
|
|
|
|
|
mqtt_subscribe();
|
|
|
|
|
Serial.print("MQTT connected\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Serial.print("MQTT not connected, continuing in standalone mode\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mqtt_subscribe()
|
|
|
|
|
{
|
|
|
|
|
mqtt.subscribe("/espmega", mqtt_callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void thread_initialization()
|
|
|
|
|
{
|
|
|
|
|
Serial.print("Initializing MQTT Thread\n");
|
|
|
|
|
mqtt_reconnector.onRun(mqtt_connect);
|
|
|
|
|
mqtt_reconnector.setInterval(30000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void mqtt_callback(String topic, String message)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void virtual_interrupt_callback(int pin, int state)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void virtual_interrupt_loop()
|
|
|
|
|