Data from ESP8266 does not arrive on dashboard

Hi there,

@Alseb3 reported that he is having problems seeing data on his dashboard.

With kind regards,
Andreas.

Dear Sayed,

Your Beekeeper ID is "yqLBxx", but apparently no data has arrived for that yet. You can watch the output of

mosquitto_sub -h swarm.hiveeyes.org -t '#' -v

to verify data from your device is getting through.

Maybe, if it won’t work even after trying harder, you can share the code you are using on your ESP8266?

With kind regards,
Andreas.

Actually I don’t think it’s a code issue, it looks like some thing needs to be done on the dashboard.

The source code I am using.
#define WEIGHT                  true
#define SENSOR_BATTERY_LEVEL    false  // Falls Spannungsmessung ĂĽber Spannungsteiler erfolgen soll, wenn kein SIM800 Modul verwendet wird.
#define DEEPSLEEP_ENABLED       true   // Code ist aktuell nur auf TRUE ausgelegt, falls False, muĂź noch im main() ein Delay eingebaut werden.
#define SLEEP_TIMER             true   // Sleep-Dauer abhängig vom Ladezustand, Sleep_Timer noch nicht mit Wifi verifziert.
#define WUNDERGROUND            false  // Funktionert aktuell nur mit GSM_ENABLED false
#define SHT             true  // Temp&Humidity
#define WIFI_ACTIVE true

#if SENSOR_BATTERY_LEVEL
  int adc_level;
#endif

// Variablen für Spanungsmessung, werden unabhängig der Messmethode (Spannungsteiler o. SIM800 Modul benötigt)
int volt_level;
float voltage;
float temp=0;
float humid=0;
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>

#include <Wire.h>
#include "SparkFunHTU21D.h"

//Create an instance of the object
HTU21D myHumidity;

//Library for WifiManager
#include <ESP8266WebServer.h>

#if WIFI_ACTIVE
  #include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
#endif

#if SLEEP_TIMER

  #include <EEPROM.h>
  int power_save_mode;
  int address = 1;     // WillkĂĽrlich von mir festgelegt das im EEPROM an Adresse 1 der Wert fĂĽr den Ladezustand abgelegt wird.
  int voltbe4;         // Wert der vorherigen Messung 
  int voltbelow = 75;  // Schwellwert für die Aktivierung des PowerSave Modus (Verlängerung des SleepTimers auf 60 min)
  
#endif

int sleepTimeS = 900;  // 15-Minuten SleepTimer als Startwert

// Adafruit MQTT
// https://github.com/adafruit/Adafruit_MQTT_Library
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

// JSON serializer
// https://github.com/bblanchon/ArduinoJson
#include <ArduinoJson.h>

// ----
// MQTT
// ----

// Note that the "testdrive" channel as outlined within "MQTT_TOPIC" is not
// authenticated and can be used anonymously.
//
// To publish data to a personal data channel, please ask for appropriate
// credentials at https://community.hiveeyes.org/ or hello@hiveeyes.org.
//
// Documentation:
// https://community.hiveeyes.org/t/messdaten-an-die-hiveeyes-plattform-ubermitteln/1813
// https://community.hiveeyes.org/t/zugangsdaten-anfragen-und-account-erstellen/2193

// How often to retry connecting to the MQTT broker
#define MQTT_RETRY_COUNT    5

// How long to delay between MQTT (re)connection attempts (seconds)
#define MQTT_RETRY_DELAY    1.5f

// The address of the MQTT broker to connect to
#define MQTT_BROKER         "swarm.hiveeyes.org"
#define MQTT_PORT           1883

// A MQTT client ID, which should be unique across multiple devices for a user.
// Maybe use your MQTT_USERNAME and the date and time the sketch was compiled
// or just use an UUID (https://www.uuidtools.com/) or other random value.

// Hier eigenen Wert eintragen.
#define MQTT_CLIENT_ID      "yqLBxx"

// The credentials to authenticate with the MQTT broker.
// Eigene Login-Daten beim netten Team von Hiveeyes anfragen (s.o.)
#define MQTT_USERNAME       "alseb3@hotmail.com"
#define MQTT_PASSWORD       "{redacted}"

// The MQTT topic to transmit sensor readings to.
#define MQTT_TOPIC          "hiveeyes/testdrive/area-42/node-1/data.json"



#if WIFI_ACTIVE

  WiFiClient client;
  WiFiClient clientMQTT;

#endif


// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details
Adafruit_MQTT_Client mqtt(&clientMQTT, MQTT_BROKER, MQTT_PORT, MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD);

// Setup MQTT publishing handler
Adafruit_MQTT_Publish mqtt_publisher = Adafruit_MQTT_Publish(&mqtt, MQTT_TOPIC);

#if WEIGHT

  #include <HX711.h>
  #include <RunningMedian.h>  // http://playground.arduino.cc/Main/RunningMedian
  
  #define SCALE_DOUT_PIN_A 0 // DT
  #define SCALE_SCK_PIN_A 2 // SCK

  //#define SCALE_DOUT_PIN_B D2 // DT
  //#define SCALE_SCK_PIN_B D1 // SCK

  HX711 scale_A;
  //HX711 scale_B;

  long AktuellesGewicht_A = 0;
  //long AktuellesGewicht_B = 0;

  float Taragewicht_A = 186.5;  // Enter the value from the calibration here
  float Skalierung_A = 22.22;  // Enter the value from the calibration here
  
  //float Taragewicht_B = -238537;  // Hier ist der Wert aus der Kalibrierung einzutragen
  //float Skalierung_B = -42.614;  // Hier ist der Wert aus der Kalibrierung einzutragen

  long Gewicht_A = 999999;
  float GewichtEinzelmessung_A;

  //long Gewicht_B = 999999;
  //float GewichtEinzelmessung_B;

  // create RunningMedian object with 10 readings
  RunningMedian GewichtSamples_A = RunningMedian(10);
  //RunningMedian GewichtSamples_B = RunningMedian(10);

#endif

#if SENSOR_DS18B20

  // Temperatur Sensoren DS18B20 connectet to Pin D5
  #define DS18B20_PIN D5
  
  // 1-Wire library
  #include <OneWire.h>
  
  // DS18B20/DallasTemperature library
  #include <DallasTemperature.h>
  
  // For communicating with any 1-Wire device (not just DS18B20)
  OneWire oneWire(DS18B20_PIN);
  
  // Initialize DallasTemperature library with reference to 1-Wire object
  DallasTemperature sensors(&oneWire);
  
  // Device Adresses - dedected by oneWireSearch.ino or Multiple similar
  // Update device adress
  
  uint8_t Sensor1 [8] = { 0x28, 0xFF, 0xCF, 0xBD, 0xA4, 0x16, 0x04, 0x46 };  // Nr. 3 grĂĽne Schrift
  uint8_t Sensor2 [8] = { 0x28, 0xFF, 0x67, 0x65, 0x51, 0x16, 0x04, 0xEE }; // langes Kabel
  
  // Define Variable to hold temperature value for Sensor1
  float temps1;
  
  // Define Variable to hold temperature value for Sensor2
  float temps2;

#endif

#if WUNDERGROUND

  // Weatherundeground
  const char* weatherhost = "api.wunderground.com";
  
  // TextFinder Lib used for text extractation of XML Data
  #include <TextFinder.h>
  
  // Define variable as "char" as they will be extracted via finder.string of XML Data
  char currentTemp[8];
  char currentHumidity[8];
  
  // Weatherunderground Station - Alternative Station IMNCHEN1945
  const char*  WUG_Station = "IMANAM4";

#endif

bool mqtt_connect();
void transmit_readings();
void setup_weight();
void read_weight();
void setup_tempsensor();
void read_tempsensor();
void read_battery_level();
void power4sleep();
void read_weatherunderground();

void setup() {
  
Serial.begin(115200);
myHumidity.begin();
#if WIFI_ACTIVE

  Serial.println("Booting Wifi Mode ");

  // Start Wifi Manager to connect
  WiFiManager wifiManager;

  wifiManager.setConfigPortalTimeout(60);
  wifiManager.autoConnect("AutoConnectAP");

  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");

    Serial.println("ESP8266 in sleep for 1 min mode");
    ESP.deepSleep(60 * 1000000, WAKE_RF_DEFAULT);
    delay(100);

  }

  Serial.println(WiFi.localIP().toString());
  Serial.println(WiFi.SSID());

  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

#endif


setup_weight();

setup_tempsensor();

}

void loop() {
  humid = myHumidity.readHumidity();
  temp = myHumidity.readTemperature();
  if (!mqtt_connect()) {
    return;
  }

  read_weight();

  read_tempsensor();

  read_battery_level();

  read_weatherunderground();

  #ifdef SLEEP_TIMER
    power4sleep();
  #endif

  transmit_readings();

  delay(1000);

  #if DEEPSLEEP_ENABLED
      ESP.deepSleep(sleepTimeS * 1000000, WAKE_RF_DEFAULT);
      delay(100);
  #endif

  // This is currently still missing: What do we do if there is no DeepSleep, i.e. we stay in Main ()
  // delay (60 * 1000)

}

void setup_weight() {

  #if WEIGHT

    scale_A.begin(SCALE_DOUT_PIN_A, SCALE_SCK_PIN_A);
//    scale_B.begin(SCALE_DOUT_PIN_B, SCALE_SCK_PIN_B);

    // Waage A Setup
    scale_A.set_offset(Taragewicht_A);
    scale_A.set_scale(Skalierung_A);
  
    // Waage B Setup
//    scale_B.set_offset(Taragewicht_B);
//    scale_B.set_scale(Skalierung_B);
  #endif

}


void read_weight() {

  #if WEIGHT
  
    //clearRunningMedian Sample
    GewichtSamples_A.clear();
  //  GewichtSamples_B.clear();

    // read x times weight and take median
    // do this till running median sample is full
    while (GewichtSamples_A.getCount() < GewichtSamples_A.getSize()) {

      // wait between samples
      Serial.print(".");
      delay(180);

      // power HX711 / load cell
      scale_A.power_up();
//      scale_B.power_up();

      delay(2);  // wait for stabilizing

      // read raw data input of HX711
      GewichtEinzelmessung_A = scale_A.read();
//      GewichtEinzelmessung_B = scale_B.read();

      // switch off HX711 / load cell
      scale_A.power_down();
 //     scale_B.power_down();

      // calculate weight in kg
      Gewicht_A = (GewichtEinzelmessung_A - Taragewicht_A) / Skalierung_A;
//      Gewicht_B = (GewichtEinzelmessung_B - Taragewicht_B) / Skalierung_B;

      // use calculated kg values for median statistic
      GewichtSamples_A.add(Gewicht_A);
 //     GewichtSamples_B.add(Gewicht_B);

      AktuellesGewicht_A = GewichtSamples_A.getMedian();
 //     AktuellesGewicht_B = GewichtSamples_B.getMedian();

      Serial.print("Gewicht A: ");
      Serial.print(AktuellesGewicht_A);
      Serial.println(" g");
      
 //     Serial.print("Gewicht B: ");
 //     Serial.print(AktuellesGewicht_B);
 //     Serial.println(" g");
      
    } 
  #endif
}


bool mqtt_connect() {

  // If already connected, don't do anything and signal success
  if (mqtt.connected()) {
    return true;
  }

  Serial.println("Connecting to MQTT broker");

  // Reconnect loop
  uint8_t retries = MQTT_RETRY_COUNT;
  int8_t ret;
  while ((ret = mqtt.connect()) != 0) {

    Serial.println("Anzahl MQTT Connection Retries:");
    Serial.println(ret);
    Serial.println(String(mqtt.connectErrorString(ret)).c_str());

    retries--;
    if (retries == 0) {
      Serial.println("Giving up connecting to MQTT broker");
      
      // Falls keine MQTT Verbindung aufgebaut werden kann, hängen wir hier ewig in der Schleife
      // da solange die Funktion mqtt.connect aufgerufen wird
      // deshalb fangen wir von vorne an in dem wir den ESP 1min schlafen geht und die Schleife von vorne beginnt

      // Sleep fĂĽr 60 Sekunden.

          ESP.deepSleep(60 * 1000000, WAKE_RF_DEFAULT);
          delay(100);

      
      return false;
    }

    Serial.println("Retry MQTT Connection in x Seconds:");
    Serial.println(MQTT_RETRY_DELAY);

    // Wait some time before retrying
    delay(MQTT_RETRY_DELAY * 1000);
  }

  if (mqtt.connected()) {

    Serial.println("Successfully connected to MQTT broker");
    Serial.println(MQTT_BROKER);

    return true;
  }

  // Giving up on further connection attempts
  return false;
}


void setup_tempsensor() {

#if SENSOR_DS18B20

  Serial.println("Temperaturmessung mit dem DS18B20 ");

  // Start Dallas Temperature Library / Instanz
  sensors.begin();  // DS18B20 starten

  // Präzision auf 12 Bit
#ifndef TEMP_12_BIT
#define TEMP_12_BIT 0x7F // 12 bit
#endif
  sensors.setResolution(TEMP_12_BIT);

  //Anzahl Sensoren ausgeben
  Serial.print("Sensoren: ");
  Serial.println(sensors.getDeviceCount());

#endif

}


void read_tempsensor() {

#if SENSOR_DS18B20

  // Temperatursensor(en) auslesen
  sensors.requestTemperatures();

  // Read Temperature form each Sensor
  temps1 = sensors.getTempC(Sensor1);
  temps2 = sensors.getTempC(Sensor2);

#endif

}

void read_battery_level() {

#if SENSOR_BATTERY_LEVEL

  // Read the battery level from the ESP8266 analog input pin.
  // Analog read level is 10 bit 0-1023 (0V-1V).
  // Our 1M & 220K voltage divider takes the max
  // MY 1M & 3,3 K
  // LiPo value of 4.2V and drops it to 0.758V max.
  // This means our minimum analog read value should be 580 (3.14V)
  // and the maximum analog read value should be 774 (4.2V).

   // Read Value from A0 - max 3.3V, daher etwas andere Teiler
  // 1024 / 3,3  * 1,73 = 537 1,73V laut spannungsteiler bei 4,2 V mit 4,7k & 3,3k
  // 1024 / 3,3  * 1,3 = 400 1,3V lt. spannungsteiler bei 3,14 V mit 4,7k & 3,3 k

  
  
  adc_level = analogRead(A0);
  Serial.print("ADC_level: ");
  Serial.println(adc_level);

  // Map Value to % Level
  volt_level = map(adc_level, 400, 537, 0, 100);
  Serial.println(volt_level);

  // Calculate Voltage Value
  Serial.print("Voltage: ");
  voltage = adc_level * 4.2 / 537 ;
  Serial.println( voltage );
  
  // Give operating system / watchdog timer some breath
  yield();
#endif
}

void power4sleep() {

#if SLEEP_TIMER

  EEPROM.begin(512);

  //letzten Ladungsstand Wert aus EEprom auslesen
  voltbe4 = EEPROM.read(address);
  Serial.print("Read Value:");
  Serial.println(voltbe4);

  //aktuellen Ladungsstand Wert in EEprom ablegen
  EEPROM.write(address, volt_level );
  EEPROM.commit();

  if (volt_level < voltbelow && voltbe4 < voltbelow )
  {
    // If the charge value is below 75%, the sleep duration is extended to 60 min (= 3600 seconds)
    sleepTimeS = 3600;
    power_save_mode = 1;
  }
  else
  {
    // If the charge value is over 75%, the sleep duration is set to 15 min (= 900 seconds)
    sleepTimeS = 900;
    power_save_mode = 0;
  }

    Serial.print("Set Sleep Timer to:");
    Serial.println(sleepTimeS);
    Serial.print("Set Power Save to:");
    Serial.println(power_save_mode);
    
#endif

}

void read_weatherunderground() {

// Function provides current temperature and air humidity of a weather station in the area of ​​Weatherunderground
  // My station is IMANAM4 - if necessary, select a nearby station via the Weatherunderground website.

#if WUNDERGROUND

  if (client.connect(weatherhost, 80))
  {
    String wurl = "/weatherstation/WXCurrentObXML.asp?ID=IMANAM4" ;
    client.print(String("GET ") + wurl + " HTTP/1.1\r\n" + "Host: " + weatherhost + "\r\n" +  "Connection: close\r\n\r\n");

    while (!client.available()) {
      //  delay(200);
      delay(10000);
      Serial.print("client not availabel connect: ");
    }

    // Read all the lines of the reply from server and print them to Serial
    TextFinder finder(client);  // No idea why - but the text finder won't work without intancing

    //   while (client.available()) {
    String line = client.readStringUntil('\r');

    finder.getString("<temp_c>", "</temp_c>", currentTemp, 8) ;

    finder.getString("<relative_humidity>", "</relative_humidity>", currentHumidity, 8) ;

    Serial.println("closing connection");
  }

#endif

}

void transmit_readings() {

  // Build JSON object containing sensor readings
  // TODO: How many data points actually fit into this?
  StaticJsonBuffer<1024> jsonBuffer;


  // Create telemetry payload by manually mapping sensor readings to telemetry field names.
  // Note: For more advanced use cases, please have a look at the TerkinData C++ library
  //       https://hiveeyes.org/docs/arduino/TerkinData/README.html
  JsonObject& json_data = jsonBuffer.createObject();

  #if WUNDERGROUND
    json_data["WXD_Temp"]        = currentTemp;
    json_data["WXD_Humi"]        = currentHumidity;
  #endif

  #if SHT
    json_data["Temp"]           = temp;
    json_data["Humid"]           = humid;
  #endif

  #if WEIGHT
    json_data["Waage1"]          = AktuellesGewicht_A;
    //json_data["Waage2"]          = AktuellesGewicht_B;
  #endif


  #if SLEEP_TIMER
     json_data["power"]          = power_save_mode;
  #endif

  json_data["volt_level"]        = volt_level;
  json_data["voltage"]           = voltage;

  // Debugging
  json_data.printTo(Serial);


  // Serialize data
  int json_length = json_data.measureLength();
  char payload[json_length + 1];
  json_data.printTo(payload, sizeof(payload));

  // Publish data
  // TODO: Refactor to TerkinTelemetry
  if (mqtt_publisher.publish(payload)) {
    Serial.println("MQTT publish succeeded");
  } else {
    Serial.println("MQTT publish failed");
  }

}

Hi again,

It looks like you are using a modified version of @Stefan’s node-esp8266-generic.ino, right?

At this place:

you should be using something like

#define MQTT_TOPIC          "hiveeyes/yqLBxx/my-location-01/my-node-01/data.json"

Please replace my-location-01 and my-node-01 with anything that fits your needs.

Notwithstanding the above, "mosquitto_sub -h swarm.hiveeyes.org -t '#' -v" doesn’t show any data arriving on the MQTT topic "hiveeyes/testdrive/area-42/node-1" from you either, only one submission going to the subtopic message-jsonhive-teststand, which is apparently a Homie device.

With kind regards,
Andreas.

Hi once more,

did you also actually recognize the fork of the Adafruit_MQTT_Library library, where the MQTT buffer size has been increased?

Essentially, this line of the PlatformIO configuration used for building this sketch pulls in the commit https://github.com/daq-tools/Adafruit_MQTT_Library/commit/6d796024, adjusting MAXBUFFERSIZE more appropriately, see also Inbetriebnahme der DatenĂĽbertragung per MQTT und TinyGSM-Bibliothek ĂĽber SIM800-Modul auf ESP8266 - #23 by Stefan.

With kind regards,
Andreas.

yes i use that sketch with minor modification, where i can use this " "mosquitto_sub -h swarm.hiveeyes.org -t '#' -v""
actually i am not expert in programing only hobbies!
thanks very much for support,
i had change for trail #define MQTT_TOPIC “hiveeyes/yqLBxx/my-location-01/my-node-01/data.json”
and run the sketch

You can use it on any system’s terminal where you can install the Mosquitto MQTT broker and its client tools. For example:

# Install on Debian Linux
apt-get install mosquitto-clients

# Install on macOS/Homebrew
brew install mosquitto

However, you can also watch the stream of transmissions at https://swarm.hiveeyes.org/transmissions without the need to install anything on your workstation.

Maybe also, just to be more safe than sorry, modify this setting to “#define SLEEP_TIMER false”.

i had disable this,
how i can setup the dashboard to receive the sent data from ESP8266,

Dear Sayed,

have you been able to confirm that data transmissions from your device are arriving on the MQTT bus, for example through https://swarm.hiveeyes.org/transmissions?

Otherwise, there will be no data to display on the dashboard.

With kind regards,
Andreas.

1 Like

just i confirm there is issue with my gateway ip is not correct,
also i had noticed that i have issue with scale initialization,
i will use another library which was working for me on normal sketch,

further more it success after i used another library for Hx711, now looks it is transmit the data to Wtee,
now how to setup dashboard and display the data?

1 Like

Indeed, congratulations.

hiveeyes/yqLBxx/my-location-01/my-node-01/data.json {"WXD_Temp":"","WXD_Humi":"","Temp":21.2651,"Humid":66.53265,"Weight":0,"volt_level":0,"voltage":0}

Corresponding dashboards should have been created automatically. Here we go:

You can adjust them to your own needs and use “Save as…” to save them under a different name. Enjoy!

Please note that both values now have been saved as strings into the database and can not be augmented to float values again, see also Data acquisition for specific measurement fields gets rejected.

You will have to change to another channel (my-location-01/my-node-01) in order to make that happen. Please assure those items have values in them.

There is no thing to worry about because you are just in trial & error mode right now. Your system will stabilize and then, before actually deploying it, you might want to change to a stable channel name.

Hi,

I have another question about the data flow.

Currently, I am running 4 separate ESP8266 with scales, they suppose to transmit data at approximately the same time. All nodes are running the same sketch. Only a single board is different and holds the SHT sensor.

My dashboard is at: Alseb3 Hive - Grafana (hiveeyes.org)

When I connect to the board over serial, I occasionally can observe failures to transmit data. Is there any issue that could block the transmission to the server?

With kind regards,
Sayed.

Dear Sayed,

Congratulations that you managed to put them together and make them submit data to the Hiveeyes Backend. Everything looks fine now, no?

Hm, it is hard to tell from the distance what the reason might be for this. I am pretty confident that the server backend currently does not have any issues. Data from other users is constantly acquired successfully.

Maybe you can share your code by uploading it somewhere appropriate?

With kind regards,
Andreas.

thanks,
the issue that it could be using same credential to login to Server
i am not able to join all together in same board as limitation of gpio

Best regards,

Sayed Baqer Mohammed Alseba

the sketch as following
but i noticed one note there // A MQTT client ID, which should be unique across multiple devices for a user.
and the error code in serial MQTT publish failed
the cause of the issue was Client ID, by changing it with numbering it is able to connect successfully at same time

2 Likes

Great to hear you figured this out. Thanks for letting us know!