Sending all data to cayenne

Hi, can someone help me

I am making a project for my final study.
I want to ask if I can make all sensor data sent in the same time frame. in my case each of the data in the channel is sent one by one.

I have tried several programs such as Millis and OUT_DEFAULT but there are no significant changes.

coding example for OUT_DEFAULT
CAYENNE_OUT_DEFAULT(){
Cayenne.virtualWrite(proximityca, terima);
Cayenne.virtualWrite(proximityind, tolak);
Cayenne.virtualWrite(heater, suhu_lectrobin, TYPE_TEMPERATURE, UNIT_CELSIUS);
Cayenne.virtualWrite(volume, volumeharian);
Cayenne.virtualWrite(tinggi_sampah, var_tinggi, TYPE_PROXIMITY, UNIT_CENTIMETER);
Cayenne.virtualWrite(kapasitas_sampah, persen_kapasitas, TYPE_PROXIMITY, UNIT_PERCENT);
Cayenne.virtualWrite(silinderoff, var_persen_kompresi, TYPE_PROXIMITY, UNIT_PERCENT);
}

example for millis, I put it on the arduino loop function
if ( currentTime - previousTime_1 >= eventTime_piority1){
Cayenne.virtualWrite(proximityca, terima);
Cayenne.virtualWrite(proximityind, tolak);
Cayenne.virtualWrite(heater, suhu_lectrobin, TYPE_TEMPERATURE, UNIT_CELSIUS);
Cayenne.virtualWrite(volume, volumeharian);
Cayenne.virtualWrite(tinggi_sampah, var_tinggi, TYPE_PROXIMITY, UNIT_CENTIMETER);
Cayenne.virtualWrite(kapasitas_sampah, persen_kapasitas, TYPE_PROXIMITY, UNIT_PERCENT);
Cayenne.virtualWrite(silinderoff, var_persen_kompresi, TYPE_PROXIMITY, UNIT_PERCENT);
previousTime_1 = currentTime;
}

if it can be done, the device that I made can have a faster response, and it will be much better.
sorry for bad grammar, Regards

1000 ms = 1 sec. So looking at your serial monitor it is sending data in 5 seconds, so i guess there is some issue with your device or interent.
A typical 6 data publish is done under 1 sec. Have a look at my serial monitor

can i find out what method you are using so you can do that

i am using a nodemcu device with this code https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/tree/master/examples/Connections/ESP8266

ok I’ll try later. if there are still the same problems does the virtual channel sequence also affect?
for example in the example you gave, you created virtual channel 0-6. whereas in my project the sequence of virtual channels is random 1-2-3-7-10-12

it does not matter.
which device are you using?

I use Arduino Mega with the addition of the ESP 8266-01 WiFi module. I think this problem is caused by the way Arduino works executing the program in sequence. to overcome that I’ve tried using the millis function to make arduino work in multi-task, but in the serial monitor the way data transmission works is executed one by one in sequence

can you share the code.

of course, wait a minute I will turn on my laptop

Does the firmware version of esp8266 affect in this case, because I used the default esp8266 firmware when I bought it.

Blockquote
#include <Servo.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include “Ultrasonic.h”
#include “max6675.h”

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266Shield.h>

// WiFi network info.
char ssid = “xxi”;
char wifiPassword = “xxi”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = xxx
char password = xxx
char clientID = xxx

// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial1

ESP8266 wifi(&EspSerial);

LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
Ultrasonic ultrasonic(11,12);
Ultrasonic ultrasonic2 (40,42);
Servo myservo1;
Servo myservo2;
int sensorpin1 = 5; //ca
int sensorpin2 = 13; //ind
#define in1 2
#define in2 3
#define r1 46
#define r2 44
#define L1 38
#define L2 34
#define L3 36
bool roll = false;
bool temp = false;
float suhu_lectrobin;
int terima = 0;
int tolak = 0;
int GIu_Ultrasonic_Dist_CM=0;
int jarak_kompresi = 0;
int var_persen_kompresi;
float volumeharian;
int tinggi;
int var_tinggi;
int persen_kapasitas;
int persen_kompresi;
int pos1 = 92;
int pos2 = 92;
int buzzer = 4;
int soPin = 52;// SO=Serial Out
int csPin = 50;// CS = chip select CS pin
int sckPin = 48;// SCK = Serial Clock pin
MAX6675 lectrobin(sckPin, csPin, soPin);// create instance object of MAX6675

//Virtual Pin Channel Cayenne
#define proximityca 1
#define proximityind 2
#define kapasitas_sampah 7
#define heater 4
#define volume 5
#define tinggi_sampah 6
#define silinder_turun 14
#define silinder_naik 8
#define silinderoff 12
#define relay 10

//MultiTask System
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;
const long eventTime_piority1 = 1000; //in ms
const long eventTime_piority2 = 2000; //in ms
const long eventTime_piority3 = 3000; //in ms

void setup()
{
Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(115200);
delay(10);
Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(r1,OUTPUT);
pinMode(r2,OUTPUT);
pinMode(L1,OUTPUT);
pinMode(L2,OUTPUT);
pinMode(L3,OUTPUT);
pinMode(sensorpin1, INPUT);
pinMode(sensorpin2, INPUT);
pinMode (buzzer, OUTPUT);
myservo1.attach(8);
myservo2.attach(7);
lcd.begin (16,2);
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
}

void loop()
{
Cayenne.loop();
unsigned long currentTime = millis();
int sensorca = digitalRead(sensorpin1);
int sensorind = digitalRead(sensorpin2);
if (sensorca == 1 && sensorind == 1 && GIu_Ultrasonic_Dist_CM >= 14) {
lcd_terima();
terima = terima + 1 ;
servo_buka();
servo_buka2();
delay (3000);
servo_tutup();
servo_tutup2();
}

else if (sensorca == 1 && sensorind == 0) {
lcd_tolak();
tolak = tolak + 1;
delay (1000);
}
else if (sensorca == 0 && sensorind == 1 && GIu_Ultrasonic_Dist_CM > 14 && roll == false && temp == false) {
lcd_awal();
delay (100);
}

if ( currentTime - previousTime_3 >= eventTime_piority3){
// Membaca suhu heater
suhu_lectrobin = lectrobin.readCelsius();
Serial.print( "Suhu Heater = ");
Serial.print(suhu_lectrobin));
Serial.print( "C || ");

//Membaca sensor ultrasonik kompresi
jarak_kompresi = ultrasonic2.Ranging(CM);
persen_kompresi = (jarak_kompresi - 3) 4.34782609;
if ( persen_kompresi < 0 ){
var_persen_kompresi = 0;
}
else if ( persen_kompresi > 100){
var_persen_kompresi = 100;
}
else{
var_persen_kompresi = persen_kompresi;
}
Serial.print("Jarak Kompresi = ");
Serial.print(jarak_kompresi);
Serial.print(“cm || “);
Serial.println (”
**”);
previousTime_3 = currentTime;
}

if ( currentTime - previousTime_2 >= eventTime_piority2){
//Membaca sensor ultrasonik kapasitas
GIu_Ultrasonic_Dist_CM = ultrasonic.Ranging(CM);
tinggi = 33 - GIu_Ultrasonic_Dist_CM ;
if (tinggi < 0){
var_tinggi = 0;
}
else{
var_tinggi = tinggi;
}
persen_kapasitas = var_tinggi 5;
volumeharian = ((var_tinggi * 15
15*3.14)/1000);
Serial.print("Tinggi = ");
Serial.print(var_tinggi);
Serial.print("cm || " );
Serial.print("Kapasitas Penampungan = “);
Serial.print(persen_kapasitas);
Serial.print(”% || " );

previousTime_2 = currentTime;
}

if ( currentTime - previousTime_1 >= eventTime_piority1){
Cayenne.virtualWrite(proximityca, terima);
Cayenne.virtualWrite(proximityind, tolak);
Cayenne.virtualWrite(heater, suhu_lectrobin, TYPE_TEMPERATURE, UNIT_CELSIUS);
Cayenne.virtualWrite(volume, volumeharian);
Cayenne.virtualWrite(tinggi_sampah, var_tinggi, TYPE_PROXIMITY, UNIT_CENTIMETER);
Cayenne.virtualWrite(kapasitas_sampah, persen_kapasitas, TYPE_PROXIMITY, UNIT_PERCENT);
Cayenne.virtualWrite(silinderoff, var_persen_kompresi, TYPE_PROXIMITY, UNIT_PERCENT);
previousTime_1 = currentTime;
}

//Alert ketika kapasitas penuh
if ( GIu_Ultrasonic_Dist_CM <= 14 && roll==false && temp== false ){ //&& counter_buzzer < 5
lcd_penuh();
bunyi_buzzer();
}

//alert kompresi
kompresi();

//konfigurasi relay 2
if (temp == true || roll == true){
digitalWrite(r2, HIGH);
}
else if (temp == false && roll == false){
digitalWrite(r2, LOW);
}
}

//fungsi custom LCD
void lcd_awal (){
lcd.clear();
lcd.setCursor (4,0);
lcd.print(“Welcome”);
lcd.setCursor (3,1);
lcd.print(“Lectro BIN”);
digitalWrite(L1, HIGH);
digitalWrite(L2, LOW);
digitalWrite(L3, LOW);
}

void lcd_terima(){
lcd.clear();
lcd.setCursor (4,0);
lcd.print(“Welcome”);
lcd.setCursor (3,1);
lcd.print(“Terimakasih”);
}

void lcd_tolak(){
lcd.clear();
lcd.setCursor (4,0);
lcd.print(“Welcome”);
lcd.setCursor (1,1);
lcd.print(“Sampah Ditolak”);
}

void lcd_penuh(){
lcd.clear();
lcd.setCursor (3,0);
lcd.print(“Lectro BIN”);
lcd.setCursor (1,1);
lcd.print(“Kapasitas penuh”);
digitalWrite(L2, HIGH);
digitalWrite(L1, LOW);
digitalWrite(L3, LOW);
}

void lcd_kompresi (){
lcd.clear();
lcd.setCursor (3,0);
lcd.print(“Lectro BIN”);
lcd.setCursor (1,1);
lcd.print(“Proses Kompresi”);
digitalWrite(L3, HIGH);
digitalWrite(L2, LOW);
digitalWrite(L1, LOW);
}

//fugnsi aktivasi buzzer
void bunyi_buzzer (){
for ( int i=0; i < 6; i++){
tone(buzzer, 1000); // Send 1KHz sound signal…
delay(500); // …for 1 sec
noTone(buzzer); // Stop sound…
delay(500); // …for 1sec
}
}

//fungsi aktivasi batas kompresi
void kompresi(){
if ( jarak_kompresi > 25 && roll == false ){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}

//fungsi aktivasi motor servo
void servo_buka (){
for (pos1 = 90; pos1 >= 1; pos1 -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo1.write(pos1);
//myservo2.write(pos1);// tell servo to go to position in variable ‘pos’
// waits 15ms for the servo to reach the position
}
}

void servo_tutup (){
for (pos1 = 0; pos1 <= 92; pos1 += 1) { // goes from 180 degrees to 0 degrees
myservo1.write(pos1);
//myservo2.write(pos1);// tell servo to go to position in variable ‘pos’
// waits 15ms for the servo to reach the position
}
}

void servo_buka2 (){
for (pos2 = 90; pos2 <= 179; pos2 += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
// myservo1.write(pos1);
myservo2.write(pos2);// tell servo to go to position in variable ‘pos’
// waits 15ms for the servo to reach the position
}
}

void servo_tutup2 (){
for (pos2 = 180; pos2 >= 92; pos2 -= 1) { // goes from 180 degrees to 0 degrees
//myservo1.write(pos1);
myservo2.write(pos2);// tell servo to go to position in variable ‘pos’
// waits 15ms for the servo to reach the position
}
}

CAYENNE_IN(silinder_turun)
{
// get value sent from dashboard
int currentValue_turun = getValue.asInt(); // 0 to 1

// assuming you wire your relay as normally open
if (currentValue_turun == 1) {
lcd_kompresi ();
roll=true;
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
else {
roll = false;
}
}
CAYENNE_IN(silinder_naik)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

// assuming you wire your relay as normally open
if (currentValue == 1) {
lcd_kompresi ();
temp=true;
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
else {
temp=false;
}
}

CAYENNE_IN(relay)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

// assuming you wire your relay as normally open
if (currentValue == 1) {
digitalWrite(r1, HIGH);
}
else {
digitalWrite(r1, LOW);
}
}

change this to 10 sec or more.

then what about this? do I have to change the time

Blockquote
const long eventTime_piority3 = 3000; //in ms

sorry, change const long eventTime_piority1 = 1000; //in ms so that data is published at 10 second interval or more.

ok I’ll try the next option, but can you explain the effect? because according to my logic if the data is updated frequently isn’t that much better?

in your case where eventTime_piority1 is 1 sec, whereas the time taken is suppose 2 sec then while the first loop is sending the set of data the next time interval currentTime - previousTime is already true.

previously I asked if this problem was affected by the firmware version of esp8266, because I used the default firmware

The ESP-01 software version should be 1.0.0 or later

it still doesn’t work, you can see the my serial monitor in this video
https://drive.google.com/file/d/1Mq0EHMO48SPTkzJOscPyvXqLzHJ6Ei5z/view?usp=sharing

okay. i also see the lag with arduino mega and esp8266. I guess that related the serial communication between the arduino and esp.