const int pin = 2; //pin for pulse input
const int SETP = 1; //(1/SETP)=K factor (user set on flowmeter)
const int numReadings = 10; // the number of readings to be averaged
const int t = 10; // the amount of time in between seiral print
volatile unsigned long count = 0; //interupt service routine variable
unsigned long pulse = 0;
unsigned long lastRead = 0;
unsigned long interval = 1000;//one second
int readings[numReadings]; // the readings taken from the flowmeter
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the running average
int GpM; // variable for solving Gsllons per Minute
int a=0; //variable for sending message every so often
int test = 21;
int i;
int n =10;
boolean joinAccept = false;
String inputString = ""; // a String to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// initialize serial communications at 57600 bps:
Serial.begin(57600); // initialize LoRa module communications
// reserve 200 bytes for the inputString:
inputString.reserve(200);
// setting up the LM-210 LoRa module control pins:
pinMode(3, OUTPUT); // module P1 pin
pinMode(4, OUTPUT); // module P2 pin
// P1=0 and P2=0 : module active mode (Mode 1)
digitalWrite(3, LOW); // module P1 pin
digitalWrite(4, LOW); // module P2 pin
pinMode(pin, INPUT_PULLUP); //set pulse pin as input
//digital pin interupt (triggered everytime pin goes from low to high)
attachInterrupt(0, isrCount, RISING);
//intialize all the readings to 0:
for (int thisReading =0; thisReading<numReadings; thisReading++)
{
readings[thisReading] = 0;
}
Serial.println (255, HEX); // wake-up LM-130 from sleep mode
for (i = 0; i < n; i++)
{serialEvent();
Serial.println (i);
if (joinAccept == 1) i=n;
delay (1000);
}
Serial.print ("joinAccept = ");
Serial.println (joinAccept);
if (joinAccept == 0) { loraconfig(); }
if (joinAccept) { Serial.println ("no lora config needed");}
}
void loop() {
if (millis() - lastRead >= interval) //read interrupt count every second
{
lastRead += interval;
// disable interrupts,make copy of count,reenable interrupts
noInterrupts();
pulse = count;
count = 0;
interrupts();
int pulse3 =pulse/3;
Serial.print("AAT2 Tx=2,uncnf,");
Serial.println(pulse3,HEX);
}
GpM = pulse * 60 * SETP; //calculates gallons per minute
for (i = 0; i < n; i++)
{
serialEvent();
//Serial.println (i);
delay (300);
}
fr_average();
// Serial.print("AAT2 Tx=2,uncnf,");
//Serial.println(test,HEX);
// delay(10000);
return;
}
void fr_average()
{
// subtract the last reading:
total = total - readings[readIndex];
//read from the sensor:
readings[readIndex] = GpM;
// add the reading to the total:
total = total + readings[readIndex];
//advance to the next postion in the array:
readIndex = readIndex + 1;
// if we are at the end of the array...
if (readIndex >= numReadings) {
//...wrap around to the beginning/start the readings over:
readIndex = 0;
}
//calculate the average:
average = total / numReadings;
// send it to the computer every minute
if (a >= t)
{
byte payload[2];
payload[0] = highByte(average);
payload[1] = lowByte(average);
Serial.print("AAT2 Tx=2,uncnf,"); // LoRaWAN port 2, unconfirmed,
if (payload[0]<16) //add leading zero
{
Serial.print(0, HEX);
}
Serial.print( payload[0] ,HEX);
Serial.println( payload[1] ,HEX);
//Serial.println( "GpM");
delay(1000); // delay in between readings for stability
a=0;
}
else {a++;}
}
void isrCount() //Interupt Service Routine
{
count++; //increases pulse count every time the interupt occurs (which is every time the pulse goes from low to high)
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
SerialParse ();
}
}
}
void loraconfig()
// this initializes the LM-110 LoRa module to TTN LoRaWAN OTAA
{
//Serial.println("AAT1 Reset"); delay (5000); //assume successful join to TTN network
// set join mode = OTAA, ADR=0, DutyCycle=on)
Serial.println("AAT2 JoinMode=1"); delay (50);
Serial.println("AAT2 ADR=0"); delay (50);
Serial.println("AAT2 DutyCycle=1"); delay (50);
//Set 8 channel TX frequencies for TTN USA
Serial.println("AAT2 Tx_Channel=0,903900000,30,1,0"); delay (50);
Serial.println("AAT2 Tx_Channel=1,904100000,30,1,0"); delay (50);
Serial.println("AAT2 Tx_Channel=2,904300000,30,1,0"); delay (50);
Serial.println("AAT2 Tx_Channel=3,904500000,30,1,0"); delay (50);
Serial.println("AAT2 Tx_Channel=4,904700000,30,1,0"); delay (50);
Serial.println("AAT2 Tx_Channel=5,904900000,30,1,0"); delay (50);
Serial.println("AAT2 Tx_Channel=6,905100000,30,1,0"); delay (50);
Serial.println("AAT2 Tx_Channel=7,905300000,30,1,0"); delay (50);
// set AppEui & AppKey
Serial.println("AAT2 AppEui=Not_for_you"); delay (50); //TTN Application ID: Not_for_you
Serial.println("AAT2 AppKey=Also_not_for_you"); delay (50); //Device ID: Also_not_for_you
Serial.println("AAT1 Save"); delay (5000);
// save and reset;
Serial.println("AAT1 Reset"); delay (1000); //assume successful join to TTN network
}
void SerialParse () {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
if(inputString.indexOf("JOIN_NOT_ACCEPT") >= 0) {Serial.println("JOIN_NOT_ACCEPT action");signal_Led(1);};
if(inputString.indexOf("JOIN_ACCEPT") >= 0) {Serial.println("JOIN_ACCEPT action"); joinAccept = true; signal_Led(2);};
if(inputString.indexOf("Tx_ok") >= 0) {Serial.println("Tx_ok action"); signal_Led(3); i=10;};
if(inputString.indexOf("Tx_no_free_ch") >= 0) {Serial.println("Tx_no_free_ch action"); signal_Led(4);};
if(inputString.indexOf("Tx_not_joined") >= 0) {Serial.println("Tx_not_joined action"); signal_Led(5);};
if(inputString.indexOf("Tx_noACK") >= 0) {Serial.println("Tx_noACK action"); signal_Led(6);};
//INPUT DATA WAS PARSED, SO WE GET BACK TO INITIAL STATE.
inputString = "";
stringComplete = false;
}
}
void signal_Led(int n) {
for (i = 0; i < n; i++)
{
digitalWrite(13, HIGH);
delay (500);
digitalWrite(13, LOW); // LED off;
delay (500);
}
}