Can't read Sensor's Data

Hi everyone. I’m writing a program to send data from DHT11 Sensor to a meshnetwork,. I’m using “painlessMesh” library. Here is my code

#include <painlessMesh.h>

#include "DHT.h"
#define DHTPIN 4     // what digital pin the DHT sensor is connected to
#define DHTTYPE DHT11   //there are multiple kinds of DHT sensors
DHT dht(DHTPIN, DHTTYPE);
  // MESH Details
#define   MESH_PREFIX     "NGHI" //name for your MESH
#define   MESH_PASSWORD   "NGHI1234" //password for your MESH
#define   MESH_PORT       5555 //default port
  //Number for this node
int nodeNumber = 2;
  //String to send to other nodes with sensor readings
Scheduler userScheduler; // to control your personal task
painlessMesh  mesh;
 void sendMessage() ; 
Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
void sendMessage()
{
  // Serializing in JSON Format
  DynamicJsonDocument doc(1024);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  doc["TEMP"] = t;
  doc["HUM"] = h;
  String msg ;
  serializeJson(doc, msg);
  mesh.sendBroadcast( msg );
  Serial.println("from node2");
  Serial.println(msg);
  taskSendMessage.setInterval((TASK_SECOND * 10));
}
// Needed for painless library
void receivedCallback( uint32_t from, String &msg ) {
  String json;
  DynamicJsonDocument doc(1024);
  json = msg.c_str();
  DeserializationError error = deserializeJson(doc, json);
  if (error)
  {
    Serial.print("deserializeJson() failed: ");
    Serial.println(error.c_str());
  }
}
void newConnectionCallback(uint32_t nodeId) {
  Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}
void changedConnectionCallback() {
  Serial.printf("Changed connections\n");
}
void nodeTimeAdjustedCallback(int32_t offset) {
  Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
}
void setup() {
  Serial.begin(115200);
  mesh.setDebugMsgTypes( ERROR | STARTUP );  // set before init() so that you can see startup messages
  mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT );
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
  userScheduler.addTask( taskSendMessage );
  taskSendMessage.enable();
}
void loop() {
  mesh.update();
}

But when i compile this program, it can’ show the temperature and humidity data from DHT11

I tested my DHT11 with another simple program and there is no error with hardware of this sensor
What should i do to solve this problem ? Thank you verymuch

I'm not familiar with this library painlessmesh Name seems to be not true

I would lookup democodes for painlessmesh

you have just defined a send-message function but you never call this function

best regards Stefan

hi,

you miss dht.begin(); inside setup

/ls

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.