ESP8266 : "new client connected" flag exist?


#1

HI,
i’m using ESP-01 with Arduino IDE
i’m on Cayenne WEB & ANDROID dashboard

My projects (ESP8266) send the data of my sensors every 5 minutes to Cayenne. If it restart, he also sends the last measurements.
Is there a function in the Arduino Cayenne library for ESP8266 (Mqtt) that tells me if a client is connected? Ex: I open the app on my smartphone, or i open my Dashboard on my pc.
I could then send the status of the sensors live, and avoid waiting for the 5 minutes of refresh.

thanks
Christian


#2

yes, check this link out. If your using MQTT you don’t need to edit the library, it already is setup for it. Just call those functions and you can set a LED or something to indicate if its online or not. Not sure if this is exactly what you looking for but its what I use.


#3

ok @vapor83 i used it

to know if my device is good connected to “mqqt” cayenne, that is always true because the connection working good all time (ex: when i “power on” my ESP or reset).

BUT : the thing i want is to know if someone open the dashboard on the web or on smartphone , at this time, my ESP device detect it, and send all data to cayenne immediately (without waiting 5 minutes).

Why ? Because (for the moment, i hope resolved in future) when i open Cayenne Dashboard it said “server error” or “blanc” widget without data. I’m waiting few seconds (because i send refresh data all 30sec) and all data appear !

+Because we are on 21 century and i don’t want to wait 30s :smile:


#4

ahh I see. I don’t think thats possible currently for data refresh on smartphone/web connection. I would add this to the Ideas/Suggestions category and maybe they will add it in the future.

i agree that would be handy.


#5

Hey @samarchri

What you could do, is add a “REFRESH” button on the dashboard.
If pressed, it could call a functionRefresh(); that manually sends the values to the dashboard.
It should work fine!

BTW, it´s a great idea, since I´m facing the same problem of not wanting to wait 1 minute.
So I´ll try to code the function and share it as soon as I have some spare time.

Cheers!
Marc


#6

@spacefolder good idea


#7

Tried it & it works fine!

The code is based upon the default Cayenne ESP8266 sketch, which sends the “seconds elapsed” since boot, every 30 seconds to channel 0.

YOU MUST create a button on the Dashboard and assign it to Channel 1.
[Add new… > Device/Widget > Custom Widgets > Button]

  1. Enter a name for the button (REFRESH in this case)
  2. Select Device
  3. Data type: Digital Actuator
  4. Channel number: 1
  5. Choose an icon
  6. Add Widget to the dashboard!

This function will “force” a refresh when the button is pressed.
It will send the updated millis value to the Dashboard immediately, without having to wait 30 seconds till next update.

image

Hope it helps.
Cheers!
Marc


 //#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.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[] = "";

unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
  Cayenne.loop();

  //Publish data every 30 seconds (30000 milliseconds).
  if (millis() - lastMillis > 30000) {
    lastMillis = millis();
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    Cayenne.virtualWrite(0, (lastMillis / 1000));
  }
}

// !! YOU MUST create a button on the Dashboard ad assign it to Channel 1.
// [Add new... > Device/Widget > Custom Widgets > Button]
// 1) Enter a name for the button (REFRESH in this case)
// 2) Select Device
// 3) Data type: Digital Actuator
// 4) Channel number: 1
// 5) Choose an icon
// 6) Add Widget to the dashboard!

// This function will "force" a refresh when the button is pressed.
// It will send the updated millis value to the Dashboard immediately,
// without having to wait 30 seconds till next update!

CAYENNE_IN(1) {
  Cayenne.virtualWrite(0, (millis() / 1000));
}

#8

good job Marc :ok_hand:
Perhaps , in futur, device will be able to do that automatically with only 1 function like :

CAYENNE_CLIENT_CONNECTED()
{
  CAYENNE_LOG("A Client is connected !");
  Serial.println("Someone Open Cayenne Dashboard and connected to this device.");
  sendDataToCayenne(); //go to function : send all data immediately
}

@rsiegel if you listen to me :wink: