diff --git a/WaterishOS-core2.0/WaterishOS-core2.0.ino b/WaterishOS-core2.0/WaterishOS-core2.0.ino index 475f816..ba81637 100644 --- a/WaterishOS-core2.0/WaterishOS-core2.0.ino +++ b/WaterishOS-core2.0/WaterishOS-core2.0.ino @@ -54,9 +54,12 @@ void writelcd(String line1, String line2){ void updatelcd() { writelcd("Waterish OS S[A]",String((int)sensorA[0].getCurrentFlowrate())+" "+String((int)sensorA[1].getCurrentFlowrate())+" "+String((int)sensorA[2].getCurrentFlowrate())+" "+String((int)sensorA[3].getCurrentFlowrate())+" "+String((int)sensorA[4].getCurrentFlowrate())+" "+String((int)sensorA[5].getCurrentFlowrate())); - delay(5000); + delay(1000); writelcd("Waterish OS S[B]",String((int)sensorB[0].getCurrentFlowrate())+" "+String((int)sensorB[1].getCurrentFlowrate())+" "+String((int)sensorB[2].getCurrentFlowrate())+" "+String((int)sensorB[3].getCurrentFlowrate())+" "+String((int)sensorB[4].getCurrentFlowrate())+" "+String((int)sensorB[5].getCurrentFlowrate())); + delay(1000); + writelcd("Waterish OS INTN",String((int)sensor1.getCurrentFlowrate())+" "+String((int)sensor2.getCurrentFlowrate())); } + void ICACHE_RAM_ATTR readA() { uint8_t pin = mcp.getLastInterruptPin(); uint8_t val = mcp.getLastInterruptPinValue(); @@ -72,20 +75,26 @@ void ICACHE_RAM_ATTR readB() { for (int counter=0; counter <= 15; counter++)mcp.digitalRead(counter); } void ICACHE_RAM_ATTR read1() { - + sensor1.count(); } void ICACHE_RAM_ATTR read2() { - + sensor2.count(); } 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); + sensor1.tick(1000); + sensor2.tick(1000); } void updatemqtt() { for (int counter=0; counter <= 5; counter++)mqtt.publish("/waterishos/node" + nodename + "/flowrateA/" + String(counter+1), String(sensorA[counter].getCurrentFlowrate())); for (int counter=0; counter <= 5; counter++)mqtt.publish("/waterishos/node" + nodename + "/volumeA/" + String(counter+1), String(sensorA[counter].getTotalVolume())); for (int counter=0; counter <= 5; counter++)mqtt.publish("/waterishos/node" + nodename + "/flowrateB/" + String(counter+1), String(sensorB[counter].getCurrentFlowrate())); for (int counter=0; counter <= 5; counter++)mqtt.publish("/waterishos/node" + nodename + "/volumeB/" + String(counter+1), String(sensorB[counter].getTotalVolume())); + 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())); } void setup() { lcd.begin(); @@ -146,7 +155,7 @@ void setup() { if(online)mqttupdater.onRun(updatemqtt); if(online)mqttupdater.setInterval(1000); lcdmanager.onRun(updatelcd); - lcdmanager.setInterval(5000); + lcdmanager.setInterval(1000); threadControl.add(&datacollector); threadControl.add(&lcdmanager); if(online)threadControl.add(&mqttupdater); diff --git a/WaterishOS-mcp2.0/WaterishOS-core2.0.ino b/WaterishOS-mcp2.0/WaterishOS-core2.0.ino new file mode 100644 index 0000000..ba81637 --- /dev/null +++ b/WaterishOS-mcp2.0/WaterishOS-core2.0.ino @@ -0,0 +1,169 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "private.h" +#include +#include +#include +#include +#include +Adafruit_MCP23017 mcp; +long tslr = 0; +WiFiClient espClient; +PubSubClient client("siwatsystem.com", 1883, espClient); +PubSubClientTools mqtt(client); +ThreadController threadControl = ThreadController(); +Thread mqttupdater = Thread(); +Thread datacollector = Thread(); +Thread lcdmanager = Thread(); +boolean online = true; +boolean sensorstate[12]; +boolean lastsensorstate[12]; +boolean firstrun = true; +FlowMeter sensorA[6] = FlowMeter(14); +FlowMeter sensorB[6] = FlowMeter(3); +FlowMeter sensor1 = FlowMeter(12); +FlowMeter sensor2 = FlowMeter(13); +volatile boolean awakenByInterrupt = false; +LiquidCrystal_I2C lcd(0x3F, 16, 2); +int menu; +void writelcd(String line1, String line2){ + lcd.clear(); + lcd.print(line1); + lcd.setCursor(0,1); + lcd.print(line2); +} +void updatelcd() +{ + writelcd("Waterish OS S[A]",String((int)sensorA[0].getCurrentFlowrate())+" "+String((int)sensorA[1].getCurrentFlowrate())+" "+String((int)sensorA[2].getCurrentFlowrate())+" "+String((int)sensorA[3].getCurrentFlowrate())+" "+String((int)sensorA[4].getCurrentFlowrate())+" "+String((int)sensorA[5].getCurrentFlowrate())); + delay(1000); + writelcd("Waterish OS S[B]",String((int)sensorB[0].getCurrentFlowrate())+" "+String((int)sensorB[1].getCurrentFlowrate())+" "+String((int)sensorB[2].getCurrentFlowrate())+" "+String((int)sensorB[3].getCurrentFlowrate())+" "+String((int)sensorB[4].getCurrentFlowrate())+" "+String((int)sensorB[5].getCurrentFlowrate())); + delay(1000); + writelcd("Waterish OS INTN",String((int)sensor1.getCurrentFlowrate())+" "+String((int)sensor2.getCurrentFlowrate())); +} + +void ICACHE_RAM_ATTR readA() { + uint8_t pin = mcp.getLastInterruptPin(); + uint8_t val = mcp.getLastInterruptPinValue(); + sensorA[pin].count(); + mqtt.publish("/waterishos/debug", "Aterupt"); + for (int counter=0; counter <= 15; counter++)mcp.digitalRead(counter); + Serial.print("A interupted"+String((int)digitalRead(14))); +} +void ICACHE_RAM_ATTR readB() { + uint8_t pin = mcp.getLastInterruptPin(); + uint8_t val = mcp.getLastInterruptPinValue(); + for (int sid = 8; sid < 13; sid++)sensorB[sid - 8].count(); + for (int counter=0; counter <= 15; counter++)mcp.digitalRead(counter); +} +void ICACHE_RAM_ATTR read1() { + sensor1.count(); +} +void ICACHE_RAM_ATTR read2() { + sensor2.count(); +} +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); + sensor1.tick(1000); + sensor2.tick(1000); +} +void updatemqtt() { + for (int counter=0; counter <= 5; counter++)mqtt.publish("/waterishos/node" + nodename + "/flowrateA/" + String(counter+1), String(sensorA[counter].getCurrentFlowrate())); + for (int counter=0; counter <= 5; counter++)mqtt.publish("/waterishos/node" + nodename + "/volumeA/" + String(counter+1), String(sensorA[counter].getTotalVolume())); + for (int counter=0; counter <= 5; counter++)mqtt.publish("/waterishos/node" + nodename + "/flowrateB/" + String(counter+1), String(sensorB[counter].getCurrentFlowrate())); + for (int counter=0; counter <= 5; counter++)mqtt.publish("/waterishos/node" + nodename + "/volumeB/" + String(counter+1), String(sensorB[counter].getTotalVolume())); + 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())); +} +void setup() { + lcd.begin(); + writelcd(" Siwat INC (tm) "," Waterish OS"); + delay(1000); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + int connectionattempt = 0; + while (WiFi.status() != WL_CONNECTED && online) + { + //wait for it ... (Wait for Wifi Connection) + writelcd("WiFi Connecting"," Attempt "+String(connectionattempt)); + connectionattempt++; + delay(500); + if (connectionattempt >= 60) { + writelcd(" Cannot Connect"," Going Offline!"); + online = false; + } + } + String wifiname(ssid); + if(online)writelcd(" WiFi Connected",wifiname); + delay(3000); + writelcd("Boot Sequence P3"," Loading Kernel"); + Serial.begin(115200); + pinMode(14, INPUT_PULLUP); + pinMode(12, INPUT); + pinMode(13, INPUT); + attachInterrupt(digitalPinToInterrupt(14), readA, RISING); + attachInterrupt(digitalPinToInterrupt(1), readB, RISING); + attachInterrupt(digitalPinToInterrupt(12), read1, RISING); + attachInterrupt(digitalPinToInterrupt(13), read2, RISING); + delay(1000); + writelcd("Boot Sequence P3","Waking Processor"); + mcp.begin(); + mcp.setupInterrupts(true, false, LOW); + for (int i = 0; i <= 15; i++) + { + mcp.pinMode(i, INPUT); + mcp.pullUp(i, LOW); + mcp.setupInterruptPin(i, RISING); + } + delay(1000); + writelcd("Boot Sequence P3"," Success!"); + delay(2000); + writelcd("Waterish OS a3.9","Reading Sensors"); + delay(1000); + writelcd(" Telemetry Node","siwatsystem.com"); + if (client.connect("waterishos",telemetryuser,telemetrykey)) { + writelcd(" Telemetry Node","Connected"); + delay(1000); + } else { + writelcd(" Telemetry Node"," Failed Offline"); + online=false; + delay(3000); + } + datacollector.onRun(collectdata); + datacollector.setInterval(1000); + if(online)mqttupdater.onRun(updatemqtt); + if(online)mqttupdater.setInterval(1000); + lcdmanager.onRun(updatelcd); + lcdmanager.setInterval(1000); + threadControl.add(&datacollector); + threadControl.add(&lcdmanager); + if(online)threadControl.add(&mqttupdater); + if(online)updatemqtt(); + +} + +void loop() { + if(online)client.loop(); + threadControl.run(); +} diff --git a/WaterishOS-mcp2.0/config.h b/WaterishOS-mcp2.0/config.h new file mode 100644 index 0000000..656018b --- /dev/null +++ b/WaterishOS-mcp2.0/config.h @@ -0,0 +1,11 @@ +const char ssid[] = ""; +const char password[] = ""; +const char* telemetryhost = ""; +const char* telemetrykey = ""; +const int telemetryport = 1883; +const char* telemetryuser = ""; +const byte InteruptPinA=1; +const byte InteruptPinB=3; +const byte arduinoInterrupt=1; +const byte buttonPin = 0; +const String nodename="devkit"; diff --git a/WaterishOS-mcp2.0/lcdcontrol.h b/WaterishOS-mcp2.0/lcdcontrol.h new file mode 100644 index 0000000..ca16735 --- /dev/null +++ b/WaterishOS-mcp2.0/lcdcontrol.h @@ -0,0 +1,15 @@ +class disp { + public: + LiquidCrystal_I2C lcd(0x27, 16, 2); + int menu; + void setmenu(int mnp){ + this->menu = mnp; + } + void drawmenu() + { + switch(this->menu) + case: '1': + this->lcd.print(" Waterish OS OK "); + + } +};