Converting Analog Input

Hi, i am working on an air and sound pollution monitoring system using Cayenne and i need help converting the values from the sensors to ppm and db respectively.

which sensor are you using?

Am using MQ135 gas sensor and Microphone Sound Sensor

do you have a code to interface with arduino (without cayenne code)?

Yes, this is for the Gas sensor

#include <MQ135.h>

MQ135 gasSensor = MQ135(A1);
int ppm = gasSensor.getPPM();

void setup()
{
Serial.begin (9600);
}

void loop ()
{
float rzero = gasSensor.getRZero();
float ppm = gasSensor.getPPM();
Serial.println(ppm);

delay (1000);
}

Using this you are getting the correct sensor reading?
Which device along with your arduino are you using to connect to internet?

yes i am. I have connected the arduino to a raspberry for internet connection.

I get the correct readings but when i open my Cayenne dashboard, i dont get the same values on the widget

can you share the code used for this?

Here it is ;

#include <CayenneMQTTSerial.h>
#include <MQ135.h>
#define CAYENNE_PRINT Serial
#define VIRTUAL_CHANNEL 1
#define VIR_CHANNEL 2 
int soundValue;
int db;
float ppm;
char PPM;
char DB;
MQ135 gasSensor =  MQ135(1);



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

void setup()
{
  //Baud rate can be specified by calling Cayenne.begin(username, password, clientID, 9600);
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
}

void loop() {
 
  soundValue=analogRead (2);
  db=(soundValue+83.2073)/11.003;
  float rzero = gasSensor.getRZero();
  float ppm = gasSensor.getPPM();
  Cayenne.loop();
  
}

// 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(VIRTUAL_CHANNEL)
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
   
  Cayenne.virtualWrite(VIRTUAL_CHANNEL, analogRead(1));
  Cayenne.virtualWrite(VIRTUAL_CHANNEL, analogRead(ppm), "PPM" );
  
  // 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_OUT(VIR_CHANNEL)
{
   
  Cayenne.virtualWrite(VIR_CHANNEL, analogRead(db), "DB");
 
}
CAYENNE_IN(1)
{
  
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");

}

I have made the changes to your code. have a look at the data types Data types for Cayenne MQTT API

#include <CayenneMQTTSerial.h>
#include <MQ135.h>
#define CAYENNE_PRINT Serial
#define VIRTUAL_CHANNEL 1
#define VIR_CHANNEL 2
int soundValue;
int db;
float ppm;
char PPM;
char DB;
MQ135 gasSensor = MQ135(1);

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “4fb17570-521d-11e9-86b5-4fe3d2557533”;
char password = “70c88dc86d5140d9893866e251ab22d3396fe3c5”;
char clientID = “89b57c10-782b-11e9-beb3-736c9e4bf7d0”;

void setup()
{
//Baud rate can be specified by calling Cayenne.begin(username, password, clientID, 9600);
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
}

void loop() {
Cayenne.loop();
}

// 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()
{
soundValue=analogRead (2);
db=(soundValue+83.2073)/11.003;
float rzero = gasSensor.getRZero();
float ppm = gasSensor.getPPM();
Cayenne.virtualWrite(1, db, "analog_sensor", "null");
Cayenne.virtualWrite(2, rzero, "co2", "ppm" );
Cayenne.virtualWrite(3, ppm, "co2", "ppm" );
}

Thank you very much, i modified your a bit and it works perfectly.

One last thing, my sms alert doesnt work but the email alert does, is this because of my region?

Most likely. If you are outside the US you must include the country code.

Yes i already tried that but ill keep trying

which country are you from ?

Ghana

the contact number should be starting with +233

Ok

Hi Shramik, i dont know if you can help me with this but i fixed a 3.5 inch touchscreen to my raspberry pi to allow me use Cayenne website on it but when i go to the Cayenne website and log in, it tells me to download the Cayenne App for either IOS or Android.