Flow Meter help with reading data

@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.

1 Like

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.

1 Like

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?

@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.

can you help me with the command?

have you shifted to MQTT?

#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.
}

can you please check my coding?

what’s the error here?

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.
}

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

check your MQTT credential.

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

what error you are getting.

MQTT connect failed, error 5

post the code you are using.

#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”);
}

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);
	}
}

sorry, im just a beginner. but what do you mean by “add new device” :sweat_smile:

have a look at this: Adding a New Device using MQTT
follow step 3 and add MQTT credential into the code i gave above.