Flow Meter help with reading data


#19

Hello Everybody,
I use this code to measure flow rate. However, the widget is just standing at the last measured value. Any suggestions?

/*
Cayenne Serial USB Example
This sketch connects to the Cayenne server using an Arduino Serial USB connection
and runs the main communication loop.
The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Pins, data
should be sent to those pins using virtualWrites. Examples for sending and receiving
Virtual Pin data are under the Basics folder.
This requires the use of the Serial USB connection so you cannot use the Serial device for
printing messages. If you need to print you can use SoftwareSerial and connect another device
to read messages via the SoftwareSerial pins.
In order for this to work you must run the connection script on the machine the Arduino is connected to.
The scripts are located under the extras\scripts folder in the main library folder. This redirects the traffic
from the Arduino to the Cayenne server.
Steps:
1. Set the token variable to match the Arduino token from the Dashboard.
2. Compile and upload this sketch.
3. Launch the connection script as described below for Windows or Linux/OSX.
Windows:
  1. Open the Windows command line (cmd.exe)
  2. Navigate to the scripts folder by typing "cd [path]", e.g.  "cd C:\Users\[YourUserName]\Documents\Arduino\libraries\Cayenne\extras\scripts"
  3. Run the script by typing "cayenne-ser.bat -c COM4" (where COM4 is port with your Arduino) and hitting Enter
Linux and OSX:
    ./cayenne-ser.sh (may need to run with sudo)
    
You can specify port, baud rate, and server endpoint like this:
    ./cayenne-ser.sh -c <serial port> -b <baud rate> -s <server address> -p <server port>
    For instance :
      ./cayenne-ser.sh -c /dev/ttyACM0 -b 9600 -s arduino.mydevices.com -p 8442
    Run cayenne-ser.sh -h for more information
    Be sure to select the right serial port (there may be multiple).
ATTENTION!
  Do not use Serial to display any output in this sketch. It will interfere with the Serial
  USB connection. When uploading sketches the Arduino IDE may complain with "programmer is
  not responding" or "Access is denied." You will need to terminate the connection script
  before uploading new sketches since it blocks access to the Serial port. Also make sure 
  the Serial Monitor is disabled in the IDE since that can prevent the Arduino from 
  connecting to the Windows/Linux/OSX machine. If you use Visual Micro for Visual Studio make
  sure Automatic Debugging is disabled. Otherwise the Serial Monitor can interfere with the
  Serial connection.
*/

#include <CayenneSerial.h>

#include <OpenGarden.h>
#include <Wire.h>

#define VIRTUAL_LUM V5
#define VIRTUAL_IRR V6
#define VIRTUAL_TEMP V7

#define VIRTUAL_BAT V1
#define VIRTUAL_NODE_TEMP V2
#define VIRTUAL_NODE_HUM V3
#define VIRTUAL_NODE_LUM V4
#define VIRTUAL_LITERS V8
/////////
#define FLOWSENSORPIN 11

// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
  uint8_t x = digitalRead(FLOWSENSORPIN);
  
  if (x == lastflowpinstate) {
    lastflowratetimer++;
    return; // nothing changed!
  }
  
  if (x == HIGH) {
    //low to high transition!
    pulses++;
  }
  lastflowpinstate = x;
  flowrate = 1000.0;
  flowrate /= lastflowratetimer;  // in hertz
  lastflowratetimer = 0;
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
  }
}






//////////////////
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "8vaedkt5m6";

void setup()
{
  //Baud rate can be specified by calling Cayenne.begin(token, 9600);
  Cayenne.begin(token);
  OpenGarden.initSensors();    //Initialize sensors power 
  OpenGarden.sensorPowerON();  //Turns on the sensor power supply
  OpenGarden.initIrrigation(1); //Initialize irrigation number 1 
  OpenGarden.irrigationOFF(1);
  delay(1000);
  OpenGarden.initRF();
  OpenGarden.initRTC();
  
  
  ////////////////
  

   
   pinMode(FLOWSENSORPIN, INPUT);
   digitalWrite(FLOWSENSORPIN, HIGH);
   lastflowpinstate = digitalRead(FLOWSENSORPIN);
   useInterrupt(true);

}

void loop()
{
  
  Cayenne.run();
 
}
CAYENNE_OUT(VIRTUAL_LITERS)
{
  float liters = pulses;
  liters /= 7.5;
  liters /= 60.0;
  
  Cayenne.virtualWrite(VIRTUAL_LITERS, liters);
  delay(1000);
}

CAYENNE_OUT(VIRTUAL_TEMP)
{
  float soilTemperature0 = OpenGarden.readSoilTemperature();
  
  //Serial.print(soilTemperature0);
  Cayenne.virtualWrite(VIRTUAL_TEMP, soilTemperature0);
}

CAYENNE_OUT(VIRTUAL_LUM)
{
  int luminosity0 = OpenGarden.readLuminosity();
  
  //Serial.print(soilTemperature0);
  Cayenne.virtualWrite(VIRTUAL_LUM, luminosity0);
}

CAYENNE_IN(VIRTUAL_IRR)
{
 
  // read the integer value which should be 0 or 1
  int enabled = getValue.asInt();
  if (enabled == 1) {
    // turns on the motor at the specified speed
    OpenGarden.irrigationON(1);
  } else {
    // turn motor off
    OpenGarden.irrigationOFF(1);
  }
}

CAYENNE_OUT(VIRTUAL_BAT)
{
 OpenGarden.receiveFromNode(); //Receive data from node
  delay(1000); 
  Payload node1Packet = OpenGarden.getNodeData(node1);
  
  float battery1 = node1Packet.supplyV;
  float battery2 = (battery1 / 1000);
  //Serial.print(battery1);
  Cayenne.virtualWrite(VIRTUAL_BAT, battery2);
  OpenGarden.receiveFromNode(); //Receive data from node
}

CAYENNE_OUT(VIRTUAL_NODE_TEMP)
{
 OpenGarden.receiveFromNode(); //Receive data from node
  delay(1000); 
  Payload node1Packet = OpenGarden.getNodeData(node1);
  
  float airTemperature1 = node1Packet.temperature;
  //Serial.print(airTemperature1);
  Cayenne.virtualWrite(VIRTUAL_NODE_TEMP, airTemperature1);
  OpenGarden.receiveFromNode(); //Receive data from node
  
}
CAYENNE_OUT(VIRTUAL_NODE_HUM)
{
 OpenGarden.receiveFromNode(); //Receive data from node
  delay(1000); 
  Payload node1Packet = OpenGarden.getNodeData(node1);
  
 float airHumidity1 = node1Packet.humidity;
  //Serial.print(airTemperature1);
  Cayenne.virtualWrite(VIRTUAL_NODE_HUM, airHumidity1);
  OpenGarden.receiveFromNode(); //Receive data from node
}

CAYENNE_OUT(VIRTUAL_NODE_LUM)
{
 OpenGarden.receiveFromNode(); //Receive data from node
  delay(1000); 
  Payload node1Packet = OpenGarden.getNodeData(node1);
  
 int luminosity1 = node1Packet.light;
  //Serial.print(node1Packet);
  Cayenne.virtualWrite(VIRTUAL_NODE_LUM, luminosity1);
  OpenGarden.receiveFromNode(); //Receive data from node
}


#20

Hello. I have a doubt. How do I get the amount in liters? to know how many liters of water spent in a month. thank you.


#21

@aniederauer there is code above given by @ognqn.chikov that calculates liters but it seems he had a problem with it. give it a try and let us know.


#22

Hello. I’ve been testing the code above. I got some good answers. but I can not modify it to have as one of the final answers the total liters spent.

also due to some “noise” by some mistake of assembly, is summing daily liters. I’m going to work on this too.


#23

hello, i have the same problem just like you. i am able to see the output in the serial monitor but not on cayenne. can you please help?


#24

@syazwin.snr you need to swift to cayenne MQTT. Converting Cayenne Arduino LIbrary sketches to Cayenne MQTT Arduino

in your code there is no command which sends value to cayenne.


#25

can you help me with the command?


#26

have you shifted to MQTT?


#27
#define CAYENNE_PRINT Serial
#define Cayenne_Alcohol V1
#include <CayenneMQTTEthernet.h>
const int VIRTUAL_PIN = 0;
const int VIRTUAL_PIN = 1;
const int VIRTUAL_PIN = 2;
char username[] = "";
char password[] = "";
char clientID[] = "";


int mq3_analogPin = A0; // connected to the output pin of MQ3
byte red=4, green=5, yellow=6; // pins for leds
// remember 100 to 1000 ohm resistor in series with each LED

float alcohol = 0;


void newFunctionName (int VIRTUAL_PIN)
{Cayenne.virtualWrite(Cayenne_Alcohol,alcohol,"voltage", "V");}


void setup() 
{
  Serial.begin(9600);
  Cayenne.begin ("username", "password", "clientID");
  pinMode(red,OUTPUT);
  pinMode(yellow,OUTPUT);
  pinMode(green,OUTPUT);
  Serial.print("Alcohol value:");
  Serial.begin(9600); // open serial at 9600 bps
}
void loop()

{
  // give ample warmup time for readings to stabilize
  Cayenne.loop();

  int mq3_value = analogRead(mq3_analogPin);
  float voltage = mq3_value* (5.0/1023.0);
  Serial.print("Alcohol value:");
  Serial.println(mq3_value);
  Serial.print("Voltage(V)");
  Serial.println(voltage); 
  if (mq3_value<400)
  {
    digitalWrite(green,HIGH);
    delay(1000);
    digitalWrite(green,LOW);
  }
  else if(mq3_value>900)
  {
    digitalWrite(red,HIGH);
    
    delay(1000);
    digitalWrite(red,LOW);
  }
  else
  {
    digitalWrite(yellow,HIGH);
    delay(1000);
    digitalWrite(yellow,LOW);
  }
  delay(100); //Just here to slow down the output.
}

#28

can you please check my coding?


#29

what’s the error here?


#30

in your code the “alcohol” value is always zero. rest i have changed.

#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>
char username[] = "";
char password[] = "";
char clientID[] = "";


int mq3_analogPin = A0; // connected to the output pin of MQ3
byte red=4, green=5, yellow=6; // pins for leds
// remember 100 to 1000 ohm resistor in series with each LED

float alcohol = 0;
unsigned long lastMillis = 0;

void setup() 
{
  Serial.begin(9600);
  Cayenne.begin ("username", "password", "clientID");
  pinMode(red,OUTPUT);
  pinMode(yellow,OUTPUT);
  pinMode(green,OUTPUT);
  Serial.print("Alcohol value:");
  Serial.begin(9600); // open serial at 9600 bps
}
void loop()

{
  // give ample warmup time for readings to stabilize
  Cayenne.loop();
  if (millis() - lastMillis > 10000) {
    lastMillis = millis();
    Cayenne.virtualWrite(0, alcohol,"voltage", "V");
  }
  int mq3_value = analogRead(mq3_analogPin);
  float voltage = mq3_value* (5.0/1023.0);
  Serial.print("Alcohol value:");
  Serial.println(mq3_value);
  Serial.print("Voltage(V)");
  Serial.println(voltage); 
  if (mq3_value<400)
  {
    digitalWrite(green,HIGH);
    delay(1000);
    digitalWrite(green,LOW);
  }
  else if(mq3_value>900)
  {
    digitalWrite(red,HIGH);
    
    delay(1000);
    digitalWrite(red,LOW);
  }
  else
  {
    digitalWrite(yellow,HIGH);
    delay(1000);
    digitalWrite(yellow,LOW);
  }
  delay(100); //Just here to slow down the output.
}

#31

thankyou so much sir! but stated here the MQTT connection failed, error 5. can i know how to slove this?


#32

check your MQTT credential.


#33

i’ve create the new one account to have the new set of credential. but it still doesnt work. can you please help me?


#34

what error you are getting.


#35

MQTT connect failed, error 5


#36

post the code you are using.


#37

#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = “xxxx”;
char password[] = “xxxx”;
char clientID[] = “xxxx”;

int mq3_analogPin = A0; // connected to the output pin of MQ3
byte red=4, green=5, yellow=6; // pins for leds
// remember 100 to 1000 ohm resistor in series with each LED

float alcohol = 0;
unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
Cayenne.begin(“username”, “password”, “clientID”);
pinMode(red,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(green,OUTPUT);
Serial.print(“Alcohol value:”);
Serial.begin(9600); // open serial at 9600 bps
}

void loop() {
Cayenne.loop();
if (millis() - lastMillis > 10000) {
lastMillis = millis();
Cayenne.virtualWrite(0, alcohol,“voltage”, “V”);
}
int mq3_value = analogRead(mq3_analogPin);
float voltage = mq3_value* (5.0/1023.0);
Serial.print(“Alcohol value:”);
Serial.println(mq3_value);
Serial.print(“Voltage(V)”);
Serial.println(voltage);
if (mq3_value<400)
{
digitalWrite(green,HIGH);
delay(1000);
digitalWrite(green,LOW);
}
else if(mq3_value>900)
{
digitalWrite(red,HIGH);

delay(1000);
digitalWrite(red,LOW);

}
else
{
digitalWrite(yellow,HIGH);
delay(1000);
digitalWrite(yellow,LOW);
}
delay(100); //Just here to slow down the output.
}
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());
// Some examples of other functions you can use to send data.
Cayenne.celsiusWrite(1, 22.0);
Cayenne.luxWrite(2, 700);
Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“Channel %u, value %s”, request.channel, getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
}


#38

before going further, add a new device and add your MQTT credential into this simple code.

// This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

unsigned long lastMillis = 0;

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
}

void loop() {
	Cayenne.loop();

	//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
	if(millis() - lastMillis > 10000) {
		lastMillis = millis();
		//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
		Cayenne.virtualWrite(0, lastMillis);
		//Some examples of other functions you can use to send data.
		//Cayenne.celsiusWrite(1, 22.0);
		//Cayenne.luxWrite(2, 700);
		//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
	}
}