From b55cc425e5c7eec58e53918053d9e89830e123a8 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Fri, 9 Aug 2019 11:19:03 +0700 Subject: [PATCH] Update WaterishOS-minimal2.0.ino --- .../WaterishOS-minimal2.0.ino | 73 +++++++++++++------ 1 file changed, 51 insertions(+), 22 deletions(-) diff --git a/WaterishOS-minimal2.0/WaterishOS-minimal2.0.ino b/WaterishOS-minimal2.0/WaterishOS-minimal2.0.ino index 9a28041..2b50cef 100644 --- a/WaterishOS-minimal2.0/WaterishOS-minimal2.0.ino +++ b/WaterishOS-minimal2.0/WaterishOS-minimal2.0.ino @@ -1,15 +1,59 @@ #include #include #include +#include +#include Adafruit_MCP23017 mcp; -byte ledPin=13; +String MQTT_SERVER = ""; +String ssid = ""; +String password = ""; byte InteruptPinA=1; byte InteruptPinB=3; byte arduinoInterrupt=1; +long tslr=0; +WiFiClient espClient; +PubSubClient client(MQTT_SERVER, 1883, espClient); +PubSubClientTools mqtt(client); +ThreadController threadControl = ThreadController(); +Thread mqttupdater = Thread(); +Thread datacollector = Thread(); +boolean online=true; FlowMeter sensorA[6]= FlowMeter(1); FlowMeter sensorB[6]= FlowMeter(3); volatile boolean awakenByInterrupt = false; +void readA(){ + uint8_t pin=mcp.getLastInterruptPin(); + uint8_t val=mcp.getLastInterruptPinValue(); + for(int sid=0;sid<=5;sid++)sensorA[sid].count(); +} +void readB(){ + uint8_t pin=mcp.getLastInterruptPin(); + uint8_t val=mcp.getLastInterruptPinValue(); + for(int sid=8;sid<13;sid++)sensorB[sid-8].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); +} +void updatemqtt(){ + for() +} void setup(){ + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + int connectionattempt=0; + while(WiFi.status() != WL_CONNECTED) + { + //wait for it ... (Wait for Wifi Connection + connectionattempt++; + delay(500); + if(conbnectionattempt>=20){ + online=false; + break(); + } + } + attachInterrupt(digitalPinToInterrupt(1),readA,FALLING); + attachInterrupt(digitalPinToInterrupt(3),readB,FALLING); pinMode(1, FUNCTION_3); pinMode(3, FUNCTION_3); pinMode(1,INPUT); @@ -21,28 +65,13 @@ void setup(){ mcp.pinMode(i, INPUT); mcp.setupInterruptPin(i,RISING); } + datacollector.onRun(collectdata); + datacollector.setInterval(1000); + mqttupdater.onRun(updatemqtt); + mqttupdater.setInterval(1000); + threadControl.add(&datacollector); } - // We will setup a pin for flashing from the int routine - pinMode(ledPin, OUTPUT); -} -// The int handler will just signal that the int has happen -// we will do the work from the main loop. -void intCallBack(){ - awakenByInterrupt=true; -} - -void handleInterrupt(){ - - // Get more information from the MCP from the INT - uint8_t pin=mcp.getLastInterruptPin(); - uint8_t val=mcp.getLastInterruptPinValue(); - for(int sid=0;sid<=5;sid++)sensorA[sid].count(); - for(int sid=6;sid<11;sid++)sensorA[sid-6].count(); - cleanInterrupts(); - -// handy for interrupts triggered by buttons -// normally signal a few due to bouncing issues void loop(){ - attachInterrupt(arduinoInterrupt,intCallBack,RISING); + client.loop(); }