waterish_os_rev3_public/WaterishOS-minimal2.0/WaterishOS-minimal2.0.ino

105 lines
3.2 KiB
Arduino
Raw Normal View History

2019-08-09 04:27:48 +00:00
#include <PubSubClient.h>
#include <MqttWildcard.h>
#include <PubSubClientTools.h>
2019-08-09 04:21:23 +00:00
#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 <WiFiServerSecure.h>
#include <WiFiServerSecureAxTLS.h>
#include <WiFiServerSecureBearSSL.h>
#include <WiFiUdp.h>
2019-08-09 02:17:05 +00:00
#include <FlowMeter.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
2019-08-09 04:19:03 +00:00
#include <Thread.h>
#include <ThreadController.h>
2019-08-09 02:17:05 +00:00
Adafruit_MCP23017 mcp;
2019-08-09 04:19:03 +00:00
String ssid = "";
String password = "";
2019-08-09 02:17:05 +00:00
byte InteruptPinA=1;
byte InteruptPinB=3;
byte arduinoInterrupt=1;
2019-08-09 04:19:03 +00:00
long tslr=0;
WiFiClient espClient;
2019-08-09 04:31:07 +00:00
String nodename="devkit";
2019-08-09 04:36:27 +00:00
PubSubClient client("siwatsystem.com", 1883, espClient);
2019-08-09 04:19:03 +00:00
PubSubClientTools mqtt(client);
ThreadController threadControl = ThreadController();
Thread mqttupdater = Thread();
Thread datacollector = Thread();
boolean online=true;
2019-08-09 02:24:17 +00:00
FlowMeter sensorA[6]= FlowMeter(1);
FlowMeter sensorB[6]= FlowMeter(3);
2019-08-09 02:17:05 +00:00
volatile boolean awakenByInterrupt = false;
2019-08-09 04:19:03 +00:00
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(){
2019-08-09 04:20:55 +00:00
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()));}
2019-08-09 02:17:05 +00:00
void setup(){
2019-08-09 04:19:03 +00:00
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);
2019-08-09 04:32:19 +00:00
if(connectionattempt>=20){
2019-08-09 04:19:03 +00:00
online=false;
2019-08-09 04:28:24 +00:00
break;
2019-08-09 04:19:03 +00:00
}
}
attachInterrupt(digitalPinToInterrupt(1),readA,FALLING);
attachInterrupt(digitalPinToInterrupt(3),readB,FALLING);
2019-08-09 02:17:05 +00:00
pinMode(1, FUNCTION_3);
pinMode(3, FUNCTION_3);
2019-08-09 02:31:53 +00:00
pinMode(1,INPUT);
pinMode(3,INPUT);
2019-08-09 03:21:21 +00:00
mcp.begin();
2019-08-09 02:17:05 +00:00
mcp.setupInterrupts(true,false,LOW);
2019-08-09 03:21:21 +00:00
for(int i=0;i<=15;i++)
{
mcp.pinMode(i, INPUT);
mcp.setupInterruptPin(i,RISING);
}
2019-08-09 04:19:03 +00:00
datacollector.onRun(collectdata);
datacollector.setInterval(1000);
mqttupdater.onRun(updatemqtt);
mqttupdater.setInterval(1000);
threadControl.add(&datacollector);
2019-08-09 04:36:27 +00:00
threadControl.add(&mqttupdater);
2019-08-09 03:21:21 +00:00
}
2019-08-09 02:00:25 +00:00
2019-08-09 02:17:05 +00:00
void loop(){
2019-08-09 04:19:03 +00:00
client.loop();
2019-08-09 02:00:25 +00:00
}