Problem in sending data to Cayenne using Uno with ESP8266-01

Hi guys, I am new to Cayenne and Arduino. Currently, I want to do a project which is heartbeat monitoring using IOT platform so that I can know the patient’s heart rate anytime.
So I began with the step by connecting the uno to Cayenne. It seems ok but after a while, it will go offline.
I’ve tried using both softwareserial and espserial and got the same thing.
So I think since it takes a while to go offline, I can try and send some data to it within that time, just to see if it works or not. Unfortunately, no data able to send to Cayanne.

Below is the code. I am very new to this and I’ve browsed a few, but I not quite understand, I saw their code in uploading the data to Cayenne almost the same as mine? But mine can’t work.

Hope you guys can enlighten me.
Thanks in advance.


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

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

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

#include <SoftwareSerial.h>
SoftwareSerial EspSerial (2,3); //rx,tx

#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
const int PulseWire = 0;

PulseSensorPlayground pulseSensor;

ESP8266 wifi(&EspSerial);

void setup()
{
Serial.begin(9600);
delay(10);

// Set ESP8266 baud rate

EspSerial.begin(9600);
delay(10);

Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);

pulseSensor.analogInput(PulseWire);

}

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

CAYENNE_OUT_DEFAULT()
{
int myBPM = pulseSensor.getBeatsPerMinute();
if (pulseSensor.sawStartOfBeat())
{
Cayenne.virtualWrite(V1,myBPM);

}

}

CAYENNE_IN_DEFAULT()
{
CAYENNE_LOG(“Channel %u, value %s”, request.channel, getValue.asString());

}

this code is quite unstable for arduino uno and is particular meant for arduino mega, because of extra serial pins. Though you can open the serial monitor and see what is the error that is printed out. Also, does restarting the arduino, makes it connect again?

Hi, thanks for the reply.
Yes, it will connect again after restarting the arduino.
Yea the instruction for using the code did mention it might be unstable if the softwareserial is used.


this is what printed on the serial monitor.
but i did tried use espserial which i plug to the only one tx rx pin on the uno and didnt open serial monitor during the process. but the result still the same, for both softwareserial and espserial.
they both connect the arduino to cayenne but after a while will go offline.

And i am not sure whether my code is correct or not for the heartbeat tracker, because cant see a value uploaded to cayenne.

the serial monitor output that you have shared, those it output only those 4 lines and stop?

Hi yes, sometimes, it output those 4 and stop,
sometimes, it output those 4 and continue show “esp not responding” for several times, and show thosse 4 again, and continue “esp not responding” and the looping.

as it suggest the esp is not responding. we can experiment something. can you try changing the baud rate of esp. use this code to set the baud rate:

// Basic serial communication with ESP8266
// Uses serial monitor for communication with ESP8266
//
//  Pins
//  Arduino pin 2 (RX) to ESP8266 TX
//  Arduino pin 3 to voltage divider then to ESP8266 RX
//  Connect GND from the Arduiono to GND on the ESP8266
//  Pull ESP8266 CH_PD HIGH
//
// When a command is entered in to the serial monitor on the computer 
// the Arduino will relay it to the ESP8266
//
 //AT+CIOBAUD=9600
//AT+IPR=9600

//#include <SoftwareSerial.h>
//SoftwareSerial ESPserial(6, 7); // RX | TX
 
void setup() 
{
    Serial.begin(9600);     // communication with the host computer
    //while (!Serial)   { ; }
 
    // Start the software serial for communication with the ESP8266
    Serial1.begin(9600);  
 
    Serial.println("");
    Serial.println("Remember to to set Both NL & CR in the serial monitor.");
    Serial.println("Ready");
    Serial.println("");    
}
 
void loop() 
{
    // listen for communication from the ESP8266 and then write it to the serial monitor
    if ( Serial1.available() )   {  Serial.write( Serial1.read() );  }
 
    // listen for user input and send it to the ESP8266
    if ( Serial.available() )       {  Serial1.write( Serial.read() );  }
}

once upload, open the serial monitor and type AT+CIOBAUD=115200.

then upload the cayenne code with baud change to 115200.

Hi, thanks again for the reply.
It seems not working using that code. To change to baud 115200, I refer to this:
https://elexfocus.com/esp8266-interfacing-with-arduino-uno/

After I change to 115200, and also change the code of the cayenne code, the results seems different.

Here is the things in serial monitor:

10:15:41.201 → [1529] ESP is not responding
10:15:43.740 → [4040] ESP is not responding
10:15:46.242 → [6550] ESP is not responding
10:15:49.090 → [9392] Failed to enable MUX
10:15:52.117 → [12421] Connected to WiFi
10:15:52.117 → [12422] Connecting to mqtt.mydevices.com:1883
10:15:53.545 → [13870] Connected
10:15:54.486 → [14795] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key
10:15:59.493 → [19807] Publish: topic 6, channel 65534, value ATmega328P, subkey , key
10:16:04.494 → [24819] Publish: topic 7, channel 65534, value 16000000, subkey , key
10:16:09.502 → [29833] Publish: topic 5, channel 65534, value 1.3.0, subkey , key
10:16:14.548 → [34845] Publish: topic 8, channel 65534, value ESP8266Shield, subkey , key
10:16:14.583 → [34906] Publish: topic 1, channel 1, value 0, subkey , key
10:16:24.556 → [44880] Connection ok
10:16:29.599 → [49910] Publish: topic 1, channel 1, value 0, subkey , key
10:16:34.606 → [54904] Connection ok
10:16:44.628 → [64934] Publish: topic 1, channel 1, value 0, subkey , key
10:16:49.969 → [70287] Connection ok
10:17:09.513 → [89838] Disconnected
10:17:09.513 → [89838] Connecting to mqtt.mydevices.com:1883
10:17:11.528 → [91839] Connected
10:17:12.640 → [92965] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key
10:17:17.676 → [97977] Publish: topic 6, channel 65534, value ATmega328P, subkey , key
10:17:22.663 → [102989] Publish: topic 7, channel 65534, value 16000000, subkey , key
10:17:27.675 → [108002] Publish: topic 5, channel 65534, value 1.3.0, subkey , key
10:17:32.711 → [113014] Publish: topic 8, channel 65534, value ESP8266Shield, subkey , key
10:17:32.746 → [113045] Publish: topic 1, channel 1, value 0, subkey , key
10:17:33.999 → [114292] Connection ok
10:17:44.002 → [124320] Connection ok
10:17:47.743 → [128055] Publish: topic 1, channel 1, value 0, subkey , key
10:17:57.796 → [138091] Connection ok
10:18:02.736 → [143062] Publish: topic 1, channel 1, value 0, subkey , key
10:18:07.813 → [148118] Connection ok
10:18:17.769 → [158070] Publish: topic 1, channel 1, value 0, subkey , key
10:18:18.919 → [159223] Connection ok
10:18:28.903 → [169224] Connection ok
10:18:32.763 → [173078] Publish: topic 1, channel 1, value 0, subkey , key
10:18:38.923 → [179238] Connection ok
10:18:47.812 → [188092] Publish: topic 1, channel 1, value 0, subkey , key
10:18:52.791 → [193114] Connection ok
10:19:02.789 → [203114] Publish: topic 1, channel 1, value 0, subkey , key
10:19:07.823 → [208136] Connection ok
10:19:23.099 → [223398] Disconnected
10:19:23.099 → [223398] Connecting to mqtt.mydevices.com:1883
10:19:25.169 → [225478] Connected
10:19:26.415 → [226718] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key
10:19:31.411 → [231731] Publish: topic 6, channel 65534, value ATmega328P, subkey , key
10:19:36.438 → [236743] Publish: topic 7, channel 65534, value 16000000, subkey , key
10:19:41.436 → [241756] Publish: topic 5, channel 65534, value 1.3.0, subkey , key
10:19:46.468 → [246768] Publish: topic 8, channel 65534, value ESP8266Shield, subkey , key
10:19:46.503 → [246799] Publish: topic 1, channel 1, value 0, subkey , key
10:19:47.545 → [247835] Connection ok
10:19:57.560 → [257865] Connection ok
10:20:01.516 → [261810] Publish: topic 1, channel 1, value 0, subkey , key
10:20:07.556 → [267872] Connection ok
10:20:16.521 → [276816] Publish: topic 1, channel 1, value 0, subkey , key
10:20:21.910 → [282218] Connection ok
10:20:41.464 → [301771] Disconnected
10:20:41.464 → [301771] Connecting to mqtt.mydevices.com:1883
10:20:43.045 → [303353] Connected
10:20:44.076 → [304388] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key
10:20:49.110 → [309400] Publish: topic 6, channel 65534, value ATmega328P, subkey , key
10:20:54.114 → [314412] Publish: topic 7, channel 65534, value 16000000, subkey , key
10:20:59.117 → [319425] Publish: topic 5, channel 65534, value 1.3.0, subkey , key
10:21:04.134 → [324438] Publish: topic 8, channel 65534, value ESP8266Shield, subkey , key
10:21:04.166 → [324469] Publish: topic 1, channel 1, value 0, subkey , key
10:21:05.122 → [325412] Connection ok
10:21:15.131 → [335440] Connection ok
10:21:19.186 → [339476] Publish: topic 1, channel 1, value 0, subkey , key
10:21:25.130 → [345444] Connection ok
10:21:34.181 → [354480] Publish: topic 1, channel 1, value 0, subkey , key

But on the cayenne dashboard, it still goes offline after a while, and get connected again, and offline…

still no data uploaded to cayanne. or maybe something with the the widget I set… hmm

you are publishing the heartbeat sensor data to channel, but the value is 0. Check first without cayenne code if you are getting reading from the sensor.

there are disconnect issue with your device. Try a bit lower baud rate.

Hi, thanks again.
Yea the sensor works fine using other code, it shows quite accurate heartbeat.

I’ve tried to lower the baud rate, the results still the same.:sweat_smile:

Hi, i have tried using arduino mega, the result is similar to the uno :joy::joy::joy::sweat_smile::sweat_smile::sweat_smile::sweat_smile:

获取 Outlook for Android

can you share the code without cayenne code that gives you the correct sensor reading.
also did you use voltage divider and are you using serial1 with arduino mega.

Hi there, sorry for the late reply.
Yes I did use the voltage divider.

The code without cayanne code is :

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   

//  Variables
const int PulseWire = 0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13;          // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore.
                               // Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
                               // Otherwise leave the default "550" value. 
                               
PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"


void setup() {   

  Serial.begin(9600);          // For Serial Monitor

  // Configure the PulseSensor object, by assigning our variables to it. 
  pulseSensor.analogInput(PulseWire);   
  pulseSensor.blinkOnPulse(LED13);       //auto-magically blink Arduino's LED with heartbeat.
  pulseSensor.setThreshold(Threshold);   

  // Double-check the "pulseSensor" object was created and "began" seeing a signal. 
   if (pulseSensor.begin()) {
    Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.  
  }
}



void loop() {

 int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
                                               // "myBPM" hold this BPM value now. 

if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened". 
 Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
 Serial.print("BPM: ");                        // Print phrase "BPM: " 
 Serial.println(myBPM);                        // Print the value inside of myBPM. 
}

  delay(20);                    // considered best practice in a simple sketch.

}

And yes, I used serial1 for arduino mega. Right now I return the mega to the owner and back to my uno.

And I do it again with a little bit of alteration on the code, this is the code right after the Cayenne authentication info:

//#define EspSerial Serial
#include <SoftwareSerial.h>
SoftwareSerial EspSerial (2,3); //rx,tx

#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
const int PulseWire = 0;


PulseSensorPlayground pulseSensor;

ESP8266 wifi(&EspSerial);

void setup()
{
  Serial.begin(9600);
  delay(10);

    // Set ESP8266 baud rate
  EspSerial.begin(9600);
  delay(10);


  Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);

  pulseSensor.analogInput(PulseWire);


}

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(1)
{
  int myBPM = pulseSensor.getBeatsPerMinute();
  if (pulseSensor.sawStartOfBeat())
  {
    Cayenne.virtualWrite(1,myBPM);    
  }
  

  
  delay (10);

}

And here is the result:

21:10:32.709 -> [5685] Connected to WiFi
21:10:32.709 -> [5685] Connecting to mqtt.mydevices.com:1883
21:10:34.798 -> [7799] Connected
21:10:36.119 -> [9090] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key 
21:10:41.128 -> [14126] Publish: topic 6, channel 65534, value ATmega328P, subkey , key 
21:10:46.175 -> [19162] Publish: topic 7, channel 65534, value 16000000, subkey , key 
21:10:51.218 -> [24196] Publish: topic 5, channel 65534, value 1.3.0, subkey , key 
21:10:56.231 -> [29227] Publish: topic 8, channel 65534, value ESP8266Shield, subkey , key 
21:11:06.320 -> [39298] Connection ok
21:11:16.329 -> [49328] Connection ok
21:11:26.371 -> [59357] Connection ok

While the sirial monitor printing connection ok, the cayanne dashboard will sometime go offline also.

can you try changing Cayenne.loop(); to Cayenne.loop(10);

Hi, thanks again.
The result is the same…

//#define EspSerial Serial
#include <SoftwareSerial.h>
SoftwareSerial EspSerial (2,3); //rx,tx

#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
const int PulseWire = 0;


PulseSensorPlayground pulseSensor;

ESP8266 wifi(&EspSerial);

void setup()
{
  Serial.begin(9600);
  delay(10);

    // Set ESP8266 baud rate
  EspSerial.begin(9600);
  delay(10);


  Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);

  pulseSensor.analogInput(PulseWire);


}
int myBPM;

void loop()
{
  Cayenne.loop(10);
 myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
                                               // "myBPM" hold this BPM value now. 

if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened". 
 Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
 Serial.print("BPM: ");                        // Print phrase "BPM: " 
 Serial.println(myBPM);                        // Print the value inside of myBPM. 
}

  delay(20); 
  
}


CAYENNE_OUT(1)
{
    Cayenne.virtualWrite(1,myBPM);    
}

try this code, to get the BPM reading on cayenne.

if you are using arduino uno, it will happen.

Hi, thanks again.

Here is what printed on serial monitor:

21:36:54.193 -> [88155] Connected to WiFi
21:36:54.193 -> [88156] Connecting to mqtt.mydevices.com:1883
21:36:57.466 -> [91432] Connected
21:37:00.140 -> [94116] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key 
21:37:05.180 -> [99154] Publish: topic 6, channel 65534, value ATmega328P, subkey , key 
21:37:10.221 -> [104192] Publish: topic 7, channel 65534, value 16000000, subkey , key 
21:37:15.265 -> [109227] Publish: topic 5, channel 65534, value 1.3.0, subkey , key 
21:37:20.306 -> [114259] Publish: topic 8, channel 65534, value ESP8266Shield, subkey , key 
21:37:20.375 -> [114353] Publish: topic 1, channel 1, value 0, subkey , key 
21:37:30.468 -> [124444] Connection ok
21:37:35.440 -> [129399] Publish: topic 1, channel 1, value 0, subkey , key 
21:37:45.531 -> [139489] Connection ok
21:37:50.468 -> [144445] Publish: topic 1, channel 1, value 0, subkey , key 
21:38:00.551 -> [154536] Connection ok
21:38:05.488 -> [159451] Publish: topic 1, channel 1, value 0, subkey , key 
21:38:15.580 -> [169542] Connection ok
21:38:20.520 -> [174498] Publish: topic 1, channel 1, value 0, subkey , key 
21:38:30.605 -> [184588] Connection ok
21:38:35.581 -> [189544] Publish: topic 1, channel 1, value 0, subkey , key 
21:38:45.676 -> [199635] Connection ok
21:38:50.634 -> [204591] Publish: topic 1, channel 1, value 0, subkey , key 

still value 0…:rofl::rofl:

i dont think there is any reason to laugh, if you can solve on your own then please go ahead.

try this code:

//#define EspSerial Serial
#include <SoftwareSerial.h>
SoftwareSerial EspSerial (2,3); //rx,tx

#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
const int PulseWire = 0;


PulseSensorPlayground pulseSensor;

ESP8266 wifi(&EspSerial);

void setup()
{
  Serial.begin(9600);
  delay(10);

    // Set ESP8266 baud rate
  EspSerial.begin(9600);
  delay(10);


  Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);

  pulseSensor.analogInput(PulseWire);


}
int myBPM;

void loop()
{
  //Cayenne.loop(10);
 myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
                                               // "myBPM" hold this BPM value now. 
 Serial.println(myBPM);
if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened". 
 Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
 Serial.print("BPM: ");                        // Print phrase "BPM: " 
 Serial.println(myBPM);                        // Print the value inside of myBPM. 
}

  delay(20); 
  
}


CAYENNE_OUT(1)
{
    Cayenne.virtualWrite(1,myBPM);    
}

Hi, sorry for the late reply.
And sorry for causing the misunderstanding, the emoji actually mean I was kinda fed up.

Thanks for the suggesting code, and this is the result I get

17:37:57.148 -> [59497] Connected to WiFi
17:37:57.148 -> [59497] Connecting to mqtt.mydevices.com:1883
17:37:59.477 -> [61864] Connected
17:38:00.905 -> [63264] Publish: topic 4, channel 65534, value Arduino Uno, subkey , key 
17:38:05.948 -> [68302] Publish: topic 6, channel 65534, value ATmega328P, subkey , key 
17:38:10.992 -> [73339] Publish: topic 7, channel 65534, value 16000000, subkey , key 
17:38:16.004 -> [78374] Publish: topic 5, channel 65534, value 1.3.0, subkey , key 
17:38:21.054 -> [83406] Publish: topic 8, channel 65534, value ESP8266Shield, subkey , key 
17:38:21.124 -> 0
17:38:21.124 -> 0
17:38:21.124 -> 0
17:38:21.124 -> 0

and all the way zeros

/*  Getting_BPM_to_Monitor prints the BPM to the Serial Monitor, using the least lines of code and PulseSensor Library.
    Tutorial Webpage: https://pulsesensor.com/pages/getting-advanced

  --------Use This Sketch To------------------------------------------
  1) Displays user's live and changing BPM, Beats Per Minute, in Arduino's native Serial Monitor.
  2) Print: "♥  A HeartBeat Happened !" when a beat is detected, live.
  2) Learn about using a PulseSensor Library "Object".
  4) Blinks LED on PIN 13 with user's Heartbeat.
  --------------------------------------------------------------------*/

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>

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";

//  Variables
const int PulseWire = 0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13;          // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore.
// Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
// Otherwise leave the default "550" value.

PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"

#define EspSerial Serial1

ESP8266 wifi(&EspSerial);


void setup() {

  Serial.begin(9600);          // For Serial Monitor

  // Configure the PulseSensor object, by assigning our variables to it.
  pulseSensor.analogInput(PulseWire);
  pulseSensor.blinkOnPulse(LED13);       //auto-magically blink Arduino's LED with heartbeat.
  pulseSensor.setThreshold(Threshold);

  // Double-check the "pulseSensor" object was created and "began" seeing a signal.
  if (pulseSensor.begin()) {
    Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.
  }
  EspSerial.begin(115200);
  delay(10);

  Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);

}



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

  int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
  // "myBPM" hold this BPM value now.
  Serial.println(myBPM);                        // Print the value inside of myBPM.

  if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened".
    Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
    Serial.print("BPM: ");                        // Print phrase "BPM: "
    Serial.println(myBPM);                        // Print the value inside of myBPM.
  }

  delay(20);                    // considered best practice in a simple sketch.

}

Hi, thanks again, appreciate that you are very patient in helping me…
I have tried the code, but the result is the esp not responding, i notice that you set the espserial.begin to 115200 and i changed the baud rate of the esp8266-01 to 115200 as well… its not responding.