ESP8266 (Cayenne) - Problem with data of sensor


I have connected ultrasonic sensor with ESP8266+Cayenne, but i have problem with data of sensor.

In my dashboard dont change data of sensor. When i connect ESP8266 in power, in dashboard is showed the same value of sensor(51%) and it dont change.

Can you help me how to solve this problem ?

                                                           THIS IS THE CODE

#include <CayenneMQTTESP8266.h>
#include “Ultrasonic.h”

Ultrasonic ultrasonic(14,12);

char ssid = “";
char wifiPassword[] = "
char username = “";
char Password[] = "
char clientID = “*****************”;
int GIu_Ultrasonic_Dist_CM=0;

//=====================Basic Setup ============================

void setup(){
Cayenne.begin(username, Password, clientID, ssid, wifiPassword); // Setup cayenne server for MQTT protocol

void loop()

  Cayenne.virtualWrite(V7, GIu_Ultrasonic_Dist_CM);             

  Serial.println(" cm" );


Do not send data from main loop, Have a look at this topic Sending MQTT messages within rate limits
Are you using app to view the data?

Yes, i am using app to view the data.

Can you send me the right code, please?

Something is going wrong with my code.

Waiting you to help me.


Delele the existing widget and try this code:

I have used this code but it’s not working.

In serial monitor is shown nothing and the widget is not created.

Do you know why is happening this ?

are you having the serial monitor set to 9600 baud rate. Try pressing the RST button on the esp with the serial monitor open.

I have changed the Serial.begin from 9600 to 115200.

In serial monitor the shown the distance 51 cm. This value doesnt change, anf the widger is not created again.

in the code the serial monitor is set to 9600, so why are using 115200?

I have changed the serial.bgin in the code from 9600 to 115200.

Do you know why the code is not working?


I am using this code to measure the distance and it is working well:

const int trigPin = 5; // pin no. 5 is D1 ESP8266
const int echoPin = 4; // pin no. 5 is D2 ESP8266
// defines variables
long duration;
int dist;
void setup()

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication @9600
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
dist= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");

Can you tell me how to connect with Cayenne, please ?

Waiting for your response.

try this code:

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "wifiPassword";

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


// Defines pins numbers for the HC-SR04 connections.
#define TRIGGER_PIN  5
#define ECHO_PIN 4

// Defines variables for storing the calculated values.
long duration;
int distance;

void setup() {
  pinMode(ECHO_PIN, INPUT);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

void loop() {

// This function is called at intervals to send HC-SR04 sensor data to Cayenne.
  digitalWrite(TRIGGER_PIN, LOW); // Clears the trigger pin

  digitalWrite(TRIGGER_PIN, HIGH); // Sets the trigger pin on HIGH state for 10 micro seconds
  digitalWrite(TRIGGER_PIN, LOW);

  duration = pulseIn(ECHO_PIN, HIGH); // Reads the echo pin, returns the sound wave travel time in microseconds

  distance = duration * 0.034 / 2; // Calculating the distance.

  // Send the distance value to Cayenne on proximity widget in centimeter.
  Cayenne.virtualWrite(DISTANCE_VIRTUAL_CHANNEL, distance, "prox", "cm");

Thanks, it’s wokring. But the data is changing very rarely.

have a look at this topic Sending MQTT messages within rate limits