PlatformIO Community

ESP32 no datas send to rasp-pi database

Hi there,
to keep it clearer i created a new thread.
My code can be compiled but for some reasons my raspberry can’t receive the temp and humid.

I use an ESP32 NodeMCU with a raspberry pi (influxdb, mqtt & grafana).

With help from this community (manuelbl) i changed my code from Arduino to PlatformIO. I added some void’s and *void mqttPublish(const char topic, float payload);

This is the code:

....

void setup() {
Serial.begin(115200);
while (! Serial);

if (!bme.begin(MY_BME280_ADDRESS)) {
Serial.println("Could not find a valid BME280 sensor, check wiring or BME-280 address!");
while (1);
}

// Use force mode so that the sensor returns to sleep mode when the measurement is finished
bme.setSampling(Adafruit_BME280::MODE_FORCED,
              Adafruit_BME280::SAMPLING_X1, // temperature
              Adafruit_BME280::SAMPLING_NONE, // pressure
              Adafruit_BME280::SAMPLING_X1, // humidity
              Adafruit_BME280::FILTER_OFF);

void setupWifi();
mqttClient.setServer(MQTT_SERVER, 1883);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

}

void mqttPublish(const char *topic, float payload);

void loop() {
if (!mqttClient.connected()) {
void mqttReconnect();
}

mqttClient.loop();

long now = millis();
if (now - lastMsgTime > MQTT_PUBLISH_DELAY) {
lastMsgTime = now;

// Reading BME280 sensor data
bme.takeForcedMeasurement(); // has no effect in normal mode
humidity = bme.readHumidity();
temperature = bme.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
  Serial.println(temperature);
  Serial.println(humidity);
  Serial.println("BME280 reading issues");
  return;
}

// Publishing sensor data
mqttPublish(MQTT_TOPIC_TEMPERATURE, temperature);
mqttPublish(MQTT_TOPIC_HUMIDITY, humidity);
delay(100);
esp_deep_sleep_start();
}


}

void setupWifi() {
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

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

void mqttReconnect() {
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");

// Attempt to connect
if (mqttClient.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD, 
MQTT_TOPIC_STATE, 1, true, "disconnected", false)) {
  Serial.println("connected");

  // Once connected, publish an announcement...
  mqttClient.publish(MQTT_TOPIC_STATE, "connected", true);
} else {
  Serial.print("failed, rc=");
  Serial.print(mqttClient.state());
  Serial.println(" try again in 5 seconds");
  delay(5000);
 }
 }
 }

void mqttPublish(const char *topic, float payload) {
Serial.print(topic);
Serial.print(": ");
Serial.println(payload);

mqttClient.publish(topic, String(payload).c_str(), true);
}

After i uploaded it to my ESP32 i got the following messages into the monitor:
Code used and edited with Platformio:

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:8740
load:0x40080400,len:5800
entry 0x4008069c
home/bme280/temperature: 20.92
home/bme280/humidity: 56.32

Nothing to see of my serial.print

This is my ‘old’ code that works with Arduino:

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8
Connecting to Unifi
.
WiFi connected
IP address: 10.0.1.66
Attempting MQTT connection...connected
home/bme280/temperature: 21.14
home/bme280/humidity: 55.48

I got following tip: Arduino file compile errors

I need help please, i changed only voids but it doenst work i have no idea why.

Here’s your bug:

This line is just a declaration. It doesn’t do anything:

void setupWifi();

Therefore, setupWifi() is never called.

What you want instead is to first declare setupWifi()(beforesetup()`) and then to call it like so:

setupWifi();

The same bug applies to mqttReconnect().

I recommend to put all forward declarations in one place before all functions, i.e. at about line 40.

1 Like

Ok i changed it, but i got some errors:

....
void setupWifi();
void mqttReconnect();
void mqttPublish(const char *topic, float payload);

void setup() {
Serial.begin(115200);
while (! Serial);

if (!bme.begin(MY_BME280_ADDRESS)) {
Serial.println("Could not find a valid BME280 sensor, check wiring or BME-280 address!");
while (1);
}

// Use force mode so that the sensor returns to sleep mode when the measurement is finished
bme.setSampling(Adafruit_BME280::MODE_FORCED,
              Adafruit_BME280::SAMPLING_X1, // temperature
              Adafruit_BME280::SAMPLING_NONE, // pressure
              Adafruit_BME280::SAMPLING_X1, // humidity
              Adafruit_BME280::FILTER_OFF);

setupWifi();
mqttClient.setServer(MQTT_SERVER, 1883);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

}

void loop() {
if (!mqttClient.connected()) {
void mqttReconnect();
}

mqttClient.loop();

long now = millis();
if (now - lastMsgTime > MQTT_PUBLISH_DELAY) {
lastMsgTime = now;

  // Reading BME280 sensor data
  bme.takeForcedMeasurement(); // has no effect in normal mode
  humidity = bme.readHumidity();
  temperature = bme.readTemperature();
  if (isnan(humidity) || isnan(temperature)) {
  Serial.println(temperature);
  Serial.println(humidity);
  Serial.println("BME280 reading issues");
  return;
  }

// Publishing sensor data
mqttPublish(MQTT_TOPIC_TEMPERATURE, temperature);
mqttPublish(MQTT_TOPIC_HUMIDITY, humidity);
delay(100);
esp_deep_sleep_start();
 }


}

setupWifi() {
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

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

mqttReconnect() {
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");

// Attempt to connect
if (mqttClient.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD, 
MQTT_TOPIC_STATE, 1, true, "disconnected", false)) {
  Serial.println("connected");

  // Once connected, publish an announcement...
  mqttClient.publish(MQTT_TOPIC_STATE, "connected", true);
  } else {
  Serial.print("failed, rc=");
  Serial.print(mqttClient.state());
  Serial.println(" try again in 5 seconds");
  delay(5000);
 }
 }
}

void mqttPublish(const char *topic, float payload) {
Serial.print(topic);
Serial.print(": ");
Serial.println(payload);

mqttClient.publish(topic, String(payload).c_str(), true);
}

ERRORS:

Functions that differ only by the return type can not be overloaded 43,6
Functions that differ only by the return type can not be overloaded. 44.6
The explicit type is missing (“int” is assumed) .101,1
The explicit type is missing (“int” is assumed) .118,1

Now the return types are missing in the function defintions. It should be:

void setupWifi() {
   ...
}

Same for mqttReconnect(). So overall, it should look the same as mqttPublish():

// declaration of function, including return and parameter types,
// semicolon at the end, located outside of any function and before first use
void mqttPublish(const char *topic, float payload); 

...

void loop() {
  ...
  // call of function, inside of other function, without parameter types,
  // semicolon at the end
  mqttPublish(MQTT_TOPIC_TEMPERATURE, temperature);
  ...
}

// defintion of function, no semicolon but with a pair of braces,
// located outside of other functions
void mqttPublish(const char *topic, float payload) {
  ...
}
2 Likes

I have one more strange behavior:

I’ve changed the sleeptime from 300 sec. to 1200 sec (20 mins). but 1200 is not accepted i still got every 5 mins my values.

#define uS_TO_S_FACTOR 1000000 // micro seconds for sleep mode
#define TIME_TO_SLEEP 300      //

With my Arduino file 1200 works perfect.

Any idea?

Full code please, it’s hard to tell with partial code… But it seems like you’re using a different macro for the send rate?

This is the full code.


#define MQTT_PUBLISH_DELAY 5000
#define MQTT_CLIENT_ID “esp32bme280”

#define uS_TO_S_FACTOR 1000000 // micro seconds for sleep mode
#define TIME_TO_SLEEP 300      // seconds for sleep mode, the esp32 sends data every 300 
seconds and goes to sleep mode, you can change this value as your mind


setupWifi();
mqttClient.setServer(MQTT_SERVER, 1883);
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
}

 void loop()
{
if (!mqttClient.connected())
{
mqttReconnect();
}

mqttClient.loop();

long now = millis();
 if (now - lastMsgTime > MQTT_PUBLISH_DELAY)
{
lastMsgTime = now;


// Publishing sensor data
mqttPublish(MQTT_TOPIC_TEMPERATURE, temperature);
mqttPublish(MQTT_TOPIC_HUMIDITY, humidity);
delay(100);
esp_deep_sleep_start();
}
}

So you’ve got a double mechanism, one time the if (now - lastMsgTime > MQTT_PUBLISH_DELAY) and then a deeplsleep after the transmission? Usage of only one or the other makes sense here, since after deepsleep the chip is reset and you start at the setup().

But that set aside, the deepsleep code should kick in correctly. Can you Serial.println("Deep sleep seconds: " + String(TIME_TO_SLEEP)) to make sure you’re running the updated version?

Ok, i removed the publish_delay and i’ll test it.

After removing:
long now = millis();
if (now - lastMsgTime > MQTT_PUBLISH_DELAY)
{
lastMsgTime = now;

and #define delay i got a instant freeze into the loop, i changed it back and its ok.