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>
|
|
|
|
#include "config.h"
|
|
|
|
#include <FlowMeter.h>
|
|
|
|
#include <Wire.h>
|
|
|
|
#include <Adafruit_MCP23017.h>
|
|
|
|
#include <Thread.h>
|
|
|
|
#include <ThreadController.h>
|
|
|
|
Adafruit_MCP23017 mcp;
|
2019-08-10 13:11:36 +00:00
|
|
|
long tslr = 0;
|
2019-08-10 11:25:07 +00:00
|
|
|
WiFiClient espClient;
|
|
|
|
PubSubClient client("siwatsystem.com", 1883, espClient);
|
|
|
|
PubSubClientTools mqtt(client);
|
|
|
|
ThreadController threadControl = ThreadController();
|
|
|
|
Thread mqttupdater = Thread();
|
|
|
|
Thread datacollector = Thread();
|
2019-08-10 13:11:36 +00:00
|
|
|
boolean online = true;
|
|
|
|
FlowMeter sensorA[6] = FlowMeter(1);
|
|
|
|
FlowMeter sensorB[6] = FlowMeter(3);
|
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-08-12 08:56:39 +00:00
|
|
|
void writelcd(String line1, String line2){
|
|
|
|
lcd.clear();
|
|
|
|
lcd.print(line1);
|
|
|
|
lcd.setCursor(0,1);
|
|
|
|
lcd.print(line2);
|
|
|
|
}
|
2019-08-10 13:11:36 +00:00
|
|
|
void drawmenu()
|
|
|
|
{
|
2019-08-10 13:11:49 +00:00
|
|
|
switch (menu)
|
2019-08-10 13:14:38 +00:00
|
|
|
{
|
2019-08-10 13:12:54 +00:00
|
|
|
case '1':
|
2019-08-10 13:14:38 +00:00
|
|
|
lcd.print(" Waterish OS OK ");
|
2019-08-12 08:56:39 +00:00
|
|
|
|
2019-08-10 13:14:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-08-10 13:11:36 +00:00
|
|
|
}
|
|
|
|
void readA() {
|
|
|
|
uint8_t pin = mcp.getLastInterruptPin();
|
|
|
|
uint8_t val = mcp.getLastInterruptPinValue();
|
|
|
|
for (int sid = 0; sid <= 5; sid++)sensorA[sid].count();
|
2019-08-10 11:25:07 +00:00
|
|
|
}
|
2019-08-10 13:11:36 +00:00
|
|
|
void readB() {
|
|
|
|
uint8_t pin = mcp.getLastInterruptPin();
|
|
|
|
uint8_t val = mcp.getLastInterruptPinValue();
|
|
|
|
for (int sid = 8; sid < 13; sid++)sensorB[sid - 8].count();
|
2019-08-10 11:25:07 +00:00
|
|
|
}
|
2019-08-10 13:11:36 +00:00
|
|
|
void collectdata() {
|
|
|
|
for (int sid = 0; sid <= 5; sid++)sensorA[sid].tick(1000);
|
|
|
|
for (int sid = 8; sid < 13; sid++)sensorB[sid - 8].tick(1000);
|
2019-08-10 11:25:07 +00:00
|
|
|
}
|
2019-08-10 13:11:36 +00:00
|
|
|
void updatemqtt() {
|
|
|
|
for (int counter; counter <= 6; counter++)mqtt.publish("/waterishos/node" + nodename + "/flowrateA/" + counter, String(sensorA[counter].getCurrentFlowrate()));
|
|
|
|
for (int counter; counter <= 6; counter++)mqtt.publish("/waterishos/node" + nodename + "/volumeA/" + counter, String(sensorA[counter].getTotalVolume()));
|
|
|
|
for (int counter; counter <= 6; counter++)mqtt.publish("/waterishos/node" + nodename + "/flowrateB/" + counter, String(sensorB[counter].getCurrentFlowrate()));
|
|
|
|
for (int counter; counter <= 6; counter++)mqtt.publish("/waterishos/node" + nodename + "/volumeB/" + counter, String(sensorB[counter].getTotalVolume()));
|
|
|
|
}
|
|
|
|
void setup() {
|
2019-08-12 08:56:39 +00:00
|
|
|
lcd.begin();
|
|
|
|
writelcd(" Siwat INC (tm) "," Waterish OS");
|
|
|
|
delay(1000);
|
2019-08-10 11:25:07 +00:00
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
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-08-12 09:09:36 +00:00
|
|
|
writelcd("WiFi Connecting"," Attempt "+String(connectionattempt));
|
2019-08-10 11:25:07 +00:00
|
|
|
connectionattempt++;
|
|
|
|
delay(500);
|
2019-08-10 13:11:36 +00:00
|
|
|
if (connectionattempt >= 20) {
|
2019-08-12 09:09:36 +00:00
|
|
|
writelcd(" Cannot Connect"," Going Offline!");
|
|
|
|
delay(3000);
|
2019-08-10 13:11:36 +00:00
|
|
|
online = false;
|
2019-08-10 11:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-12 09:09:36 +00:00
|
|
|
writelcd("Boot Sequence P3"," Loading Kernel");
|
2019-08-10 13:11:36 +00:00
|
|
|
attachInterrupt(digitalPinToInterrupt(1), readA, FALLING);
|
|
|
|
attachInterrupt(digitalPinToInterrupt(3), readB, FALLING);
|
2019-08-10 11:53:01 +00:00
|
|
|
pinMode(1, FUNCTION_3);
|
|
|
|
pinMode(3, FUNCTION_3);
|
2019-08-10 13:11:36 +00:00
|
|
|
pinMode(1, INPUT);
|
|
|
|
pinMode(3, INPUT);
|
2019-08-12 09:09:36 +00:00
|
|
|
delay(1000);
|
|
|
|
writelcd("Boot Sequence P3","Waking Processor");
|
2019-08-10 11:25:07 +00:00
|
|
|
mcp.begin();
|
2019-08-10 13:11:36 +00:00
|
|
|
mcp.setupInterrupts(true, false, LOW);
|
|
|
|
for (int i = 0; i <= 15; i++)
|
2019-08-10 11:25:07 +00:00
|
|
|
{
|
|
|
|
mcp.pinMode(i, INPUT);
|
2019-08-10 13:11:36 +00:00
|
|
|
mcp.setupInterruptPin(i, RISING);
|
|
|
|
}
|
2019-08-12 09:09:36 +00:00
|
|
|
delay(1000);
|
|
|
|
writelcd("Boot Sequence P3"," Success!");
|
2019-08-10 11:25:07 +00:00
|
|
|
datacollector.onRun(collectdata);
|
|
|
|
datacollector.setInterval(1000);
|
2019-08-12 09:09:36 +00:00
|
|
|
if(online)mqttupdater.onRun(updatemqtt);
|
|
|
|
if(online)mqttupdater.setInterval(1000);
|
2019-08-10 11:25:07 +00:00
|
|
|
threadControl.add(&datacollector);
|
2019-08-12 09:09:36 +00:00
|
|
|
if(online)threadControl.add(&mqttupdater);
|
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-08-12 09:09:36 +00:00
|
|
|
if(online)client.loop();
|
2019-08-10 13:11:36 +00:00
|
|
|
threadControl.run();
|
2019-08-10 11:25:07 +00:00
|
|
|
}
|