2019-08-21 14:19:57 +00:00
|
|
|
#include <ArduinoJson.h>
|
2019-08-10 12:18:20 +00:00
|
|
|
#include <LiquidCrystal_I2C.h>
|
2019-08-10 11:25:07 +00:00
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include <MqttWildcard.h>
|
|
|
|
#include <PubSubClientTools.h>
|
|
|
|
#include <BearSSLHelpers.h>
|
|
|
|
#include <CertStoreBearSSL.h>
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <ESP8266WiFiAP.h>
|
|
|
|
#include <ESP8266WiFiGeneric.h>
|
|
|
|
#include <ESP8266WiFiMulti.h>
|
|
|
|
#include <ESP8266WiFiScan.h>
|
|
|
|
#include <ESP8266WiFiSTA.h>
|
|
|
|
#include <ESP8266WiFiType.h>
|
|
|
|
#include <WiFiClient.h>
|
|
|
|
#include <WiFiClientSecure.h>
|
|
|
|
#include <WiFiClientSecureAxTLS.h>
|
|
|
|
#include <WiFiClientSecureBearSSL.h>
|
|
|
|
#include <WiFiServer.h>
|
|
|
|
#include <WiFiServerSecureAxTLS.h>
|
|
|
|
#include <WiFiServerSecureBearSSL.h>
|
|
|
|
#include <WiFiUdp.h>
|
2019-08-12 09:41:32 +00:00
|
|
|
#include "private.h"
|
2019-08-10 11:25:07 +00:00
|
|
|
#include <FlowMeter.h>
|
|
|
|
#include <Wire.h>
|
|
|
|
#include <Adafruit_MCP23017.h>
|
|
|
|
#include <Thread.h>
|
|
|
|
#include <ThreadController.h>
|
2019-09-04 14:07:36 +00:00
|
|
|
#include <AceButton.h>
|
2019-08-10 11:25:07 +00:00
|
|
|
Adafruit_MCP23017 mcp;
|
2019-09-04 16:03:00 +00:00
|
|
|
using namespace ace_button;
|
2019-08-10 13:11:36 +00:00
|
|
|
long tslr = 0;
|
2019-08-10 11:25:07 +00:00
|
|
|
WiFiClient espClient;
|
2019-09-04 16:28:36 +00:00
|
|
|
boolean allowbuttonint = true;
|
2019-08-10 11:25:07 +00:00
|
|
|
PubSubClient client("siwatsystem.com", 1883, espClient);
|
|
|
|
PubSubClientTools mqtt(client);
|
|
|
|
ThreadController threadControl = ThreadController();
|
|
|
|
Thread mqttupdater = Thread();
|
|
|
|
Thread datacollector = Thread();
|
2019-08-12 09:33:05 +00:00
|
|
|
Thread lcdmanager = Thread();
|
2019-08-10 13:11:36 +00:00
|
|
|
boolean online = true;
|
2019-08-12 13:29:58 +00:00
|
|
|
boolean sensorstate[12];
|
|
|
|
boolean lastsensorstate[12];
|
|
|
|
boolean firstrun = true;
|
2019-08-12 14:22:05 +00:00
|
|
|
FlowMeter sensor1 = FlowMeter(12);
|
|
|
|
FlowMeter sensor2 = FlowMeter(13);
|
2019-09-04 16:28:36 +00:00
|
|
|
boolean devcon = false;
|
|
|
|
int devconmenu = 0;
|
2019-08-10 11:25:07 +00:00
|
|
|
volatile boolean awakenByInterrupt = false;
|
2019-08-12 08:56:39 +00:00
|
|
|
LiquidCrystal_I2C lcd(0x3F, 16, 2);
|
2019-08-10 13:11:36 +00:00
|
|
|
int menu;
|
2019-09-04 16:28:36 +00:00
|
|
|
void writelcd(String line1, String line2) {
|
|
|
|
if (!devcon) {
|
|
|
|
lcd.clear();
|
|
|
|
lcd.print(line1);
|
|
|
|
lcd.setCursor(0, 1);
|
|
|
|
lcd.print(line2);
|
|
|
|
Serial.print("Writing to LCD: " + line1 + " " + line2 + "\n");
|
2019-09-04 14:07:36 +00:00
|
|
|
}
|
2019-08-12 08:56:39 +00:00
|
|
|
}
|
2019-08-12 09:33:05 +00:00
|
|
|
void updatelcd()
|
2019-08-10 13:11:36 +00:00
|
|
|
{
|
2019-09-04 16:28:36 +00:00
|
|
|
if (!devcon)writelcd("1: " + String((int)sensor1.getCurrentFlowrate()) + "L/h " + String(sensor1.getTotalVolume()) + "L", "2:" + String((int)sensor2.getCurrentFlowrate()) + "L/h " + String(sensor2.getTotalVolume()) + "L");
|
2019-08-12 14:22:05 +00:00
|
|
|
}
|
|
|
|
void ICACHE_RAM_ATTR read1() {
|
2019-08-12 14:33:52 +00:00
|
|
|
sensor1.count();
|
2019-08-12 14:22:05 +00:00
|
|
|
}
|
|
|
|
void ICACHE_RAM_ATTR read2() {
|
2019-08-12 14:33:52 +00:00
|
|
|
sensor2.count();
|
2019-08-10 11:25:07 +00:00
|
|
|
}
|
2019-08-10 13:11:36 +00:00
|
|
|
void collectdata() {
|
2019-08-12 14:33:52 +00:00
|
|
|
sensor1.tick(1000);
|
|
|
|
sensor2.tick(1000);
|
2019-08-10 11:25:07 +00:00
|
|
|
}
|
2019-08-10 13:11:36 +00:00
|
|
|
void updatemqtt() {
|
2019-08-12 14:33:52 +00:00
|
|
|
mqtt.publish("/waterishos/node" + nodename + "/flowrate1", String(sensor1.getCurrentFlowrate()));
|
|
|
|
mqtt.publish("/waterishos/node" + nodename + "/volume1", String(sensor1.getTotalVolume()));
|
|
|
|
mqtt.publish("/waterishos/node" + nodename + "/flowrate2", String(sensor2.getCurrentFlowrate()));
|
|
|
|
mqtt.publish("/waterishos/node" + nodename + "/volume2", String(sensor2.getTotalVolume()));
|
2019-08-10 13:11:36 +00:00
|
|
|
}
|
2019-08-21 14:14:48 +00:00
|
|
|
// Copyright Siwat INC(tm) 2019
|
|
|
|
// Created by Siwat Sirichai
|
|
|
|
// ESPCommander EXTENDED v1.2
|
|
|
|
// For Legal Distribution of this code, the comment must not be removed!
|
|
|
|
void espcommander(String topic, String rawcommand) {
|
2019-09-04 16:28:36 +00:00
|
|
|
StaticJsonDocument<200> doc;
|
|
|
|
DeserializationError error = deserializeJson(doc, rawcommand);
|
|
|
|
if (error) {
|
|
|
|
Serial.print(F("deserializeJson() failed: "));
|
|
|
|
Serial.println(error.c_str());
|
2019-08-21 14:14:48 +00:00
|
|
|
return;
|
2019-09-04 16:28:36 +00:00
|
|
|
}
|
|
|
|
const char* command = doc["command"];
|
|
|
|
const char* value = doc["value"];
|
|
|
|
}
|
2019-08-21 14:14:48 +00:00
|
|
|
//END OF ESPCOMMANDER CODE
|
2019-08-10 13:11:36 +00:00
|
|
|
void setup() {
|
2019-09-04 13:44:33 +00:00
|
|
|
Serial.begin(115200);
|
2019-08-12 08:56:39 +00:00
|
|
|
lcd.begin();
|
2019-09-04 16:28:36 +00:00
|
|
|
writelcd(" Siwat INC (tm) ", " Waterish OS");
|
2019-08-12 08:56:39 +00:00
|
|
|
delay(1000);
|
2019-08-10 11:25:07 +00:00
|
|
|
WiFi.mode(WIFI_STA);
|
2019-09-04 15:57:11 +00:00
|
|
|
WiFi.disconnect(true);
|
2019-09-04 16:28:11 +00:00
|
|
|
WiFi.begin(ssid, password);
|
2019-08-10 13:11:36 +00:00
|
|
|
int connectionattempt = 0;
|
2019-08-12 09:09:36 +00:00
|
|
|
while (WiFi.status() != WL_CONNECTED && online)
|
2019-08-10 11:25:07 +00:00
|
|
|
{
|
|
|
|
//wait for it ... (Wait for Wifi Connection)
|
2019-09-04 16:28:36 +00:00
|
|
|
writelcd("WiFi Connecting", " Attempt " + String(connectionattempt));
|
2019-08-10 11:25:07 +00:00
|
|
|
connectionattempt++;
|
|
|
|
delay(500);
|
2019-08-12 09:44:27 +00:00
|
|
|
if (connectionattempt >= 60) {
|
2019-09-04 16:28:36 +00:00
|
|
|
writelcd(" Cannot Connect", " Going Offline!");
|
2019-08-10 13:11:36 +00:00
|
|
|
online = false;
|
2019-08-10 11:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-12 09:54:22 +00:00
|
|
|
String wifiname(ssid);
|
2019-09-04 16:28:36 +00:00
|
|
|
if (online)writelcd(" WiFi Connected", wifiname);
|
2019-08-12 09:54:22 +00:00
|
|
|
delay(3000);
|
2019-09-04 16:28:36 +00:00
|
|
|
writelcd("Boot Sequence P3", " Loading Kernel");
|
2019-08-12 14:22:05 +00:00
|
|
|
pinMode(14, INPUT_PULLUP);
|
|
|
|
pinMode(12, INPUT);
|
|
|
|
pinMode(13, INPUT);
|
2019-09-04 14:07:36 +00:00
|
|
|
pinMode(0, INPUT_PULLUP);
|
2019-08-12 14:22:05 +00:00
|
|
|
attachInterrupt(digitalPinToInterrupt(12), read1, RISING);
|
|
|
|
attachInterrupt(digitalPinToInterrupt(13), read2, RISING);
|
2019-08-12 09:09:36 +00:00
|
|
|
delay(1000);
|
2019-09-04 16:28:36 +00:00
|
|
|
writelcd("Boot Sequence P3", "Waking Processor");
|
2019-08-12 14:43:37 +00:00
|
|
|
delay(1000);
|
2019-09-04 16:28:36 +00:00
|
|
|
writelcd(" SETTINGS.H", "co-processor:OFF");
|
2019-08-12 14:43:37 +00:00
|
|
|
delay(3000);
|
2019-08-10 11:25:07 +00:00
|
|
|
mcp.begin();
|
2019-08-10 13:11:36 +00:00
|
|
|
for (int i = 0; i <= 15; i++)
|
2019-08-10 11:25:07 +00:00
|
|
|
{
|
|
|
|
mcp.pinMode(i, INPUT);
|
2019-08-12 13:54:10 +00:00
|
|
|
mcp.pullUp(i, LOW);
|
2019-08-10 13:11:36 +00:00
|
|
|
mcp.setupInterruptPin(i, RISING);
|
|
|
|
}
|
2019-08-12 09:09:36 +00:00
|
|
|
delay(1000);
|
2019-09-04 16:28:36 +00:00
|
|
|
writelcd("Boot Sequence P3", " Success!");
|
2019-08-12 09:15:03 +00:00
|
|
|
delay(2000);
|
2019-09-04 16:28:36 +00:00
|
|
|
writelcd("Waterish OS a3.9", "Reading Sensors");
|
2019-08-12 09:33:05 +00:00
|
|
|
delay(1000);
|
2019-09-04 16:28:36 +00:00
|
|
|
writelcd(" Telemetry Node", "siwatsystem.com");
|
|
|
|
if (client.connect("waterishos", telemetryuser, telemetrykey)) {
|
|
|
|
writelcd(" Telemetry Node", "Connected");
|
2019-08-21 14:01:13 +00:00
|
|
|
mqtt.subscribe("/waterishos/node" + nodename + "command", espcommander);
|
2019-08-12 09:54:22 +00:00
|
|
|
delay(1000);
|
|
|
|
} else {
|
2019-09-04 16:28:36 +00:00
|
|
|
writelcd(" Telemetry Node", " Failed Offline");
|
|
|
|
online = false;
|
2019-08-12 09:54:22 +00:00
|
|
|
delay(3000);
|
|
|
|
}
|
2019-08-10 11:25:07 +00:00
|
|
|
datacollector.onRun(collectdata);
|
|
|
|
datacollector.setInterval(1000);
|
2019-09-04 16:28:36 +00:00
|
|
|
if (online)mqttupdater.onRun(updatemqtt);
|
|
|
|
if (online)mqttupdater.setInterval(1000);
|
2019-08-12 09:33:05 +00:00
|
|
|
lcdmanager.onRun(updatelcd);
|
2019-08-12 14:43:37 +00:00
|
|
|
lcdmanager.setInterval(250);
|
2019-08-10 11:25:07 +00:00
|
|
|
threadControl.add(&datacollector);
|
2019-08-12 09:33:05 +00:00
|
|
|
threadControl.add(&lcdmanager);
|
2019-09-04 16:28:36 +00:00
|
|
|
if (online)threadControl.add(&mqttupdater);
|
|
|
|
if (online)updatemqtt();
|
|
|
|
|
2019-08-10 13:11:36 +00:00
|
|
|
}
|
2019-08-10 11:25:07 +00:00
|
|
|
|
2019-08-10 13:11:36 +00:00
|
|
|
void loop() {
|
2019-09-04 16:28:36 +00:00
|
|
|
if (online)client.loop();
|
|
|
|
if (!devcon)threadControl.run();
|
2019-09-04 14:22:18 +00:00
|
|
|
}
|
2019-09-04 16:28:36 +00:00
|
|
|
void drawcon() {
|
|
|
|
if (devconmenu == 1) {
|
|
|
|
writelcd("connectionchck.c", "Check Connection");
|
2019-09-04 15:57:11 +00:00
|
|
|
}
|
2019-09-04 14:07:36 +00:00
|
|
|
}
|
2019-09-04 16:28:36 +00:00
|
|
|
void startdevcon() {
|
|
|
|
allowbuttonint = false;
|
|
|
|
devcon = true; //no going back, Delete Waterish OS and Install Siwat INC(R) DEVCON OS
|
|
|
|
writelcd(" SIWAT INC(R) ", "DEVCON OS V3.2.4");
|
|
|
|
delay(1000);
|
|
|
|
writelcd("DEVCON TOOLKITS", "3 Extension Load");
|
|
|
|
delay(1000);
|
|
|
|
writelcd("Initia Extension", "connectionchck.c");
|
|
|
|
delay(2000);
|
2019-09-04 16:32:33 +00:00
|
|
|
writelcd("Initia Extension", "hall_pid_tuner.c");
|
2019-09-04 16:28:36 +00:00
|
|
|
delay(2000);
|
|
|
|
writelcd("Initia Extension", "credit_display.c");
|
|
|
|
delay(2000);
|
|
|
|
writelcd("DEVCON TOOLKITS", "set WTOS at 0x3D");
|
|
|
|
delay(1000);
|
|
|
|
allowbuttonint = true;
|
|
|
|
devconmenu = 1;
|
|
|
|
drawcon();
|
2019-08-10 11:25:07 +00:00
|
|
|
}
|