2019-07-10 16:09:44 +00:00
|
|
|
#include <SPI.h>
|
|
|
|
#include <Ethernet.h>
|
|
|
|
#include <MySQL_Encrypt_Sha1.h>
|
|
|
|
#include <MySQL_Packet.h>
|
|
|
|
#include <MySQL_Cursor.h>
|
|
|
|
#include <MySQL_Connection.h>
|
|
|
|
#include <Dns.h>
|
2019-07-10 16:20:58 +00:00
|
|
|
#include "config.h"
|
2019-07-10 16:09:44 +00:00
|
|
|
|
|
|
|
typedef struct Input {
|
|
|
|
int id;
|
|
|
|
unsigned long timer;
|
|
|
|
};
|
|
|
|
|
|
|
|
char query[50];
|
|
|
|
unsigned long lastRefresh;
|
|
|
|
unsigned long lastDebug;
|
|
|
|
unsigned long lastRecord;
|
|
|
|
Input inputs[N_INPUTS];
|
|
|
|
EthernetClient ethClient;
|
|
|
|
MySQL_Connection db((Client *) ðClient);
|
|
|
|
|
|
|
|
void printTitle(String title) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
Serial.print('=');
|
|
|
|
Serial.println(" " + title);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void abortWithError(String error) {
|
|
|
|
showError(error);
|
2019-07-24 14:46:03 +00:00
|
|
|
digitalWrite(LED_PIN, HIGH);
|
2019-07-10 16:09:44 +00:00
|
|
|
while (true) delay(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int freeRam() {
|
|
|
|
extern int __heap_start, *__brkval;
|
|
|
|
int v;
|
|
|
|
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.begin(9600);
|
|
|
|
while (!Serial);
|
|
|
|
#endif
|
|
|
|
|
2019-07-24 14:46:03 +00:00
|
|
|
pinMode(LED_PIN, OUTPUT);
|
|
|
|
printTitle("Initializing");
|
|
|
|
|
2019-07-10 16:09:44 +00:00
|
|
|
if (Ethernet.begin(mac) == 0)
|
|
|
|
abortWithError("DHCP config failed.");
|
|
|
|
if (Ethernet.hardwareStatus() == EthernetNoHardware)
|
|
|
|
abortWithError("Eth shield not found.");
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2019-07-24 14:46:03 +00:00
|
|
|
Serial.print("IP: ");
|
|
|
|
Serial.println(Ethernet.localIP());
|
2019-07-10 16:09:44 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-24 14:46:03 +00:00
|
|
|
dbConnect();
|
2019-07-10 16:09:44 +00:00
|
|
|
|
|
|
|
unsigned long now = millis();
|
|
|
|
lastRefresh = now;
|
|
|
|
lastDebug = now;
|
|
|
|
lastRecord = now;
|
|
|
|
|
|
|
|
for (int i = 0; i < N_INPUTS; i++) {
|
|
|
|
Input* input = &inputs[i];
|
|
|
|
input->id = i + 1;
|
|
|
|
input->timer = 0;
|
|
|
|
pinMode(pins[i], INPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.println("RAM: " + String(freeRam()));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-07-24 14:46:03 +00:00
|
|
|
void showError(String error) {
|
|
|
|
digitalWrite(LED_PIN, HIGH);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.println(error);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-07-10 16:09:44 +00:00
|
|
|
boolean dbConnect() {
|
2019-07-24 14:46:03 +00:00
|
|
|
if (Ethernet.linkStatus() == LinkOFF) {
|
|
|
|
showError("Eth cable not connected.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (db.connected()) {
|
|
|
|
digitalWrite(LED_PIN, LOW);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-10 16:09:44 +00:00
|
|
|
DNSClient dns;
|
2019-07-24 14:46:03 +00:00
|
|
|
IPAddress dbServer;
|
2019-07-10 16:09:44 +00:00
|
|
|
dns.begin(Ethernet.dnsServerIP());
|
|
|
|
|
|
|
|
if (dns.getHostByName(DB_HOST, dbServer) != 1) {
|
|
|
|
showError("Cannot resolve DB.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2019-07-24 14:46:03 +00:00
|
|
|
Serial.print("Connecting to DB: ");
|
|
|
|
Serial.println(dbServer);
|
2019-07-10 16:09:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!db.connect(dbServer, DB_PORT, DB_USER, DB_PASS)) {
|
|
|
|
showError("Cannot connect to DB.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-24 14:46:03 +00:00
|
|
|
digitalWrite(LED_PIN, LOW);
|
2019-07-10 16:09:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void refreshTimers() {
|
|
|
|
unsigned long now = millis();
|
|
|
|
|
|
|
|
for (int i = 0; i < N_INPUTS; i++) {
|
|
|
|
Input* input = &inputs[i];
|
|
|
|
if (digitalRead(pins[i]) == HIGH)
|
|
|
|
input->timer += now - lastRefresh;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastRefresh = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
refreshTimers();
|
|
|
|
Ethernet.maintain();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (millis() - lastDebug > DEBUG_INTERVAL) {
|
|
|
|
printTitle("Status");
|
|
|
|
|
|
|
|
for (int i = 0; i < N_INPUTS; i++) {
|
|
|
|
Input* input = &inputs[i];
|
|
|
|
Serial.print("Input ");
|
|
|
|
|
|
|
|
String id = String(input->id);
|
|
|
|
for (int j = 2 - id.length(); j > 0; j--)
|
|
|
|
Serial.print(" ");
|
|
|
|
|
|
|
|
Serial.print(String(input->id) + ": ");
|
|
|
|
Serial.print(String(digitalRead(pins[i]) == HIGH));
|
|
|
|
Serial.println("/" + String(input->timer));
|
|
|
|
}
|
|
|
|
|
|
|
|
lastDebug = millis();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (millis() - lastRecord > RECORD_INTERVAL) {
|
|
|
|
printTitle("Recording to DB");
|
2019-07-24 14:46:03 +00:00
|
|
|
dbConnect();
|
2019-07-10 16:09:44 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < N_INPUTS; i++) {
|
|
|
|
refreshTimers();
|
|
|
|
Input* input = &inputs[i];
|
|
|
|
int timer = input->timer;
|
|
|
|
int seconds = timer / 1000;
|
|
|
|
input->timer = 0;
|
|
|
|
|
|
|
|
if (seconds > 0 && db.connected()) {
|
|
|
|
sprintf(query, SQL, input->id, seconds);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.println(query);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MySQL_Cursor *cur = new MySQL_Cursor(&db);
|
|
|
|
cur->execute(query);
|
|
|
|
delete cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastRecord = millis();
|
|
|
|
}
|
|
|
|
|
|
|
|
delay(DELAY);
|
|
|
|
}
|