Problem in merging the two arduino code


#1

@kreggly
@iotsmarthomeajce
the am having the problem with my merging the two code.
working code 1.txt (1.2 KB)
working code 2.txt (805 Bytes)
my merging code.txt (1.8 KB)

when I merged the two code, nothing is displayed on dashboard. If I individually run the code, all value is displayed.

please analyze my code…and help me to correct it.


#2

You have to delete } for Cayenne.run ();

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

 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 delay(1000);
}

#3

ok…i removed “}”.
then also no value displayed.


#4

Can you try with this code?

#include <Adafruit_Sensor.h>
#include <CayenneSerial.h>
#include "DHT.h"  // library for dht11 sensor.
#define DHTPIN 4 // digital pin in which sensor connected.
#define DHTTYPE DHT11
#define TEMPERATURE_PIN V0
#define HUMIDITY_PIN V1
#define RAINING_PIN V2
#define WATER LEVEL_PIN V3
#define MOISTURE_PIN V4
#define VIRTUAL_PIN V7
#define echoPin 12 // Echo Pin
#define trigPin 13// Trigger Pin


int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

float temperature;
float humidity;
DHT dht(DHTPIN, DHTTYPE);

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


void setup()
{
    Cayenne.begin(token);
    dht.begin();
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
}

void loop()
{
  Cayenne.run();
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
 //Calculate the distance (in cm) based on the speed of sound.
  distance = duration/58.2;
  delay(2000);
}

CAYENNE_OUT(V0)
{
  float temperature = dht.readTemperature();
  
  Cayenne.virtualWrite(V0, temperature); //virtual pin
}

CAYENNE_OUT(V1)
{
  float humidity = dht.readHumidity();
  Cayenne.virtualWrite(V1, humidity); //virtual pin
}
CAYENNE_OUT(V2)
{
int b= analogRead(A0); //reads the sensor value
 Cayenne.virtualWrite(V2, b); //virtual pin
}
CAYENNE_OUT(V3)
{
int v= analogRead(A2); //reads the sensor value
 Cayenne.virtualWrite(V3, v); //virtual pin
}
CAYENNE_OUT(V4)
{
int m= analogRead(A1); //reads the sensor value
 Cayenne.virtualWrite(V4, m); //virtual pin
}
CAYENNE_OUT(V7)
{
  Cayenne.virtualWrite(V7, distance);
}

#5

yes sir…i tried the above mention code posted by you…same condition.nothing displays


#6

Can you delete and the again create the device. Also use the new char token that the new device has ?


#7

ok sir…wait


#8

i have deleted all the connected devices and re created with new token…same things happned


#9

Okey, give me some time to investigate.
Sorry!


#10

ok sir…take your valuable time…am waiting for you


#11

It is very strange. The code looks ok. It is just merging the two codes. Nothing special. I start to thing about playing with delay times? Can you try to put more seconds for delay and test it?


#12

yes sir…i have checked with different time delay as you says like 50, 1000, 5000 …nothing show…

what to do next??


#13

Can you try with this?
Also I want to ask you, when you make analogRead(), where do you declare A0, A1 and A2?

#include <Adafruit_Sensor.h>
#include <CayenneSerial.h>
#include "DHT.h"  // library for dht11 sensor.
#define DHTPIN 4 // digital pin in which sensor connected.
#define DHTTYPE DHT11
#define TEMPERATURE_PIN V0
#define HUMIDITY_PIN V1
#define RAINING_PIN V2
#define WATER LEVEL_PIN V3
#define MOISTURE_PIN V4
#define VIRTUAL_PIN V7
#define echoPin 12 // Echo Pin
#define trigPin 13// Trigger Pin


int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

float temperature;
float humidity;
DHT dht(DHTPIN, DHTTYPE);

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


void setup()
{
    Cayenne.begin(token);
    dht.begin();
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
}

void loop()
{
  Cayenne.run();
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
 //Calculate the distance (in cm) based on the speed of sound.
  distance = duration/58.2;
  delay(2000);
}

CAYENNE_OUT(TEMPERATURE_PIN)
{
  float temperature = dht.readTemperature();
  
  Cayenne.virtualWrite(TEMPERATURE_PIN, temperature); //virtual pin
  delay(2000);
}

CAYENNE_OUT(HUMIDITY_PIN)
{
  float humidity = dht.readHumidity();
  Cayenne.virtualWrite(HUMIDITY_PIN, humidity); //virtual pin
  delay(2000);
}
CAYENNE_OUT(RAINING_PIN)
{
int b= analogRead(A0); //reads the sensor value
 Cayenne.virtualWrite(RAINING_PIN, b); //virtual pin
 delay(2000);
}
CAYENNE_OUT(WATER LEVEL_PIN)
{
int v= analogRead(A2); //reads the sensor value
 Cayenne.virtualWrite(WATER LEVEL_PIN, v); //virtual pin
 delay(2000);
}
CAYENNE_OUT(MOISTURE_PIN)
{
int m= analogRead(A1); //reads the sensor value
 Cayenne.virtualWrite(MOISTURE_PIN, m); //virtual pin
 delay(2000);
}
CAYENNE_OUT(VIRTUAL_PIN)
{
  Cayenne.virtualWrite(VIRTUAL_PIN, distance);
  delay(2000);
}

#14

This code also not giving any value.
about declaration of analog pin
if i declare the pin as #define pinMode(A0, INPUT);
#define pinMode(A1, INPUT);
#define pinMode(A2, INPUT);
THEN also nothing came

without declaration also the same thing happened


#15

From the screenshot that you send earlier it is visible that the device is connected to the platform. It is so strange that the values are not sending. This code is basically the same as the previous, despite that I use old school method for declaring PIN.

#include <Adafruit_Sensor.h>
#include <CayenneSerial.h>
#include "DHT.h"  // library for dht11 sensor.
#define DHTPIN 4 // digital pin in which sensor connected.
#define DHTTYPE DHT11
#define TEMPERATURE_PIN V0
#define HUMIDITY_PIN V1
#define RAINING_PIN V2
#define WATER LEVEL_PIN V3
#define MOISTURE_PIN V4
#define VIRTUAL_PIN V7
#define echoPin 12 // Echo Pin
#define trigPin 13// Trigger Pin

int rain = A0;
int water_level = A2;
int moisture = A1;
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

float temperature;
float humidity;
DHT dht(DHTPIN, DHTTYPE);

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


void setup()
{
    Cayenne.begin(token);
    dht.begin();
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(rain,OUTPUT);
    pinMode(water_level,OUTPUT);   
}

void loop()
{
  Cayenne.run();
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
 //Calculate the distance (in cm) based on the speed of sound.
  distance = duration/58.2;
  delay(2000);
}

CAYENNE_OUT(TEMPERATURE_PIN)
{
  float temperature = dht.readTemperature();
  delay(3000);
  Cayenne.virtualWrite(TEMPERATURE_PIN, temperature); //virtual pin
  delay(2000);
}

CAYENNE_OUT(HUMIDITY_PIN)
{
  float humidity = dht.readHumidity();
  Cayenne.virtualWrite(HUMIDITY_PIN, humidity); //virtual pin
  delay(2000);
}
CAYENNE_OUT(RAINING_PIN)
{
int b= analogRead(rain); //reads the sensor value
delay(3000);
 Cayenne.virtualWrite(RAINING_PIN, b); //virtual pin
 delay(2000);
}
CAYENNE_OUT(WATER LEVEL_PIN)
{
int v= analogRead(water_level); //reads the sensor value
 delay(3000);
 Cayenne.virtualWrite(WATER LEVEL_PIN, v); //virtual pin
 delay(2000);
}
CAYENNE_OUT(MOISTURE_PIN)
{
int m= analogRead(moisture); //reads the sensor value
delay(3000);
 Cayenne.virtualWrite(MOISTURE_PIN, m); //virtual pin
 delay(2000);
}
CAYENNE_OUT(VIRTUAL_PIN)
{
  Cayenne.virtualWrite(VIRTUAL_PIN, distance);
  delay(2000);
}

#16

yep.i know that.

what to do now sir??


#17

Did you try the last code?
Can you also Serial.print() every value, just to check if the values are properly reading?


#18

a
i am trying now.i will get back soon with result.


#19

I tried the above mention code by you …I lead the same result…nothing prints on the dashboard.

if I try my first working code individually…it works fine.I got all the reading
if I try my second working code i.e. ultrasonic sensor code, individually, then also working fine.

then problems occurs only which it combines, no data received by dashboard


#20

Yes, I understand. What about in the merging code to try to print all the measurements to the console. I just want to be sure that everything is measured and to start from somewhere to solve the problem :slight_smile: