Update WaterishOS-minimal2.0.ino

This commit is contained in:
Siwat Sirichai 2019-08-09 11:19:03 +07:00
parent 45d63cbdd3
commit b55cc425e5
1 changed files with 51 additions and 22 deletions

View File

@ -1,15 +1,59 @@
#include <FlowMeter.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Thread.h>
#include <ThreadController.h>
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);
}
}
// We will setup a pin for flashing from the int routine
pinMode(ledPin, OUTPUT);
datacollector.onRun(collectdata);
datacollector.setInterval(1000);
mqttupdater.onRun(updatemqtt);
mqttupdater.setInterval(1000);
threadControl.add(&datacollector);
}
// 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();
}