EmonTX sending bad data value from DHT22

Hi guys, I’m new here.
There is few years i bought an emontx V2 and a nanodeRF and they work well during few years.

Because i had changed my server ip i need upgrade the nanodeRF after few problems for finding ther good library, they work great.

But i want hadding a DHT22 on the emontx.

On the monitor when emontx is connected i can see the goods values temps1 and humidity1 between Power1 and Vrms
97O 4610 2220 3293
But when i receive on nanodeRF values are 0 for temps1 and 100 for humidity.
> [webClient]

DHCP status: 1
IP:  192.168.10.227
GW:  192.168.10.1
DNS: 8.8.8.8
Data sent: /api/post.json?apikey=YOURAPIKEY&node=1&json={rf_fail:1}
Data sent: /api/post.json?apikey=YOURAPIKEY&node=10&csv=0,0,100,3293
Time request sent
Data sent: /api/post.json?apikey=YOURAPIKEY&node=10&csv=4,0,100,3293
Data sent: /api/post.json?apikey=YOURAPIKEY&node=10&csv=3,0,100,3293

I don’t understand why Power and Vrms are well transmitted .

I think i need an outside view.

EmonTX V2 Sketch

    #define FILTERSETTLETIME 5000                                           //  Time (ms) to allow the filters to settle before sending data

const int CT1 = 1;
const int CT2 = 0;                                                      // Set to 0 to disable CT channel 2
const int CT3 = 0;


#define RF69_COMPAT 0 // SET TO 1 IF RFM69
#define RF_freq RF12_868MHZ                                                // Frequency of RF12B module can be RF12_433MHZ, RF12_868MHZ or RF12_915MHZ. You should use the one matching the module you have.
const int nodeID = 10;                                                  // emonTx RFM12B node ID
const int networkGroup = 210;                                           // emonTx RFM12B wireless network group - needs to be same as emonBase and emonGLCD

const int UNO = 1;                                                      // Set to 0 if your not using the UNO bootloader (i.e using Duemilanove) - All Atmega's shipped from OpenEnergyMonitor come with Arduino Uno bootloader
#include <avr/wdt.h>

#define RF69_COMPAT 0 // set to 1 to use RFM69CW
#include <JeeLib.h>   // make sure V12 (latest) is used if using RFM69CW
ISR(WDT_vect) { Sleepy::watchdogEvent(); }                              // Attached JeeLib sleep function to Atmega328 watchdog -enables MCU to be put into sleep mode inbetween readings to reduce power consumption

#include "EmonLib.h"
EnergyMonitor ct1,ct2,ct3;                                              // Create  instances for each CT channel

typedef struct { int power1, temp1, humidity1, battery; } PayloadTX;      // create structure - a neat way of packaging data for RF comms
PayloadTX emontx;

const int LEDpin = 9;                                                   // On-board emonTx LED

boolean settled = false;


#include "DHT.h"
DHT dht;


void setup()
{
  Serial.begin(9600);
  Serial.println("emonTX CT123DHT22 example");
  Serial.println("OpenEnergyMonitor.org");
  Serial.print("Node: ");
  Serial.print(nodeID);
  Serial.print(" Freq: ");
  if (RF_freq == RF12_433MHZ) Serial.print("433Mhz");
  if (RF_freq == RF12_868MHZ) Serial.print("868Mhz");
  if (RF_freq == RF12_915MHZ) Serial.print("915Mhz");
 Serial.print(" Network: ");
  Serial.println(networkGroup);

  if (CT1) ct1.currentTX(1, 111.1);                                     // Setup emonTX CT channel (ADC input, calibration)

  rf12_initialize(nodeID, RF_freq, networkGroup);                          // initialize RFM12B

  rf12_sleep(RF12_SLEEP);

  pinMode(LEDpin, OUTPUT);                                              // Setup indicator LED
  digitalWrite(LEDpin, HIGH);

  if (UNO) wdt_enable(WDTO_8S);                                         // Enable anti crash (restart) watchdog if UNO bootloader is selected. Watchdog does not work with duemilanove bootloader                                                             // Restarts emonTx if sketch hangs for more than 8s
  dht.setup(4); // data pin 4 for DHT22

}

void loop()
{



  if (CT1) {
    emontx.power1 = ct1.calcIrms(1480) * 240.0;                         //ct.calcIrms(number of wavelengths sample)*AC RMS voltage
    Serial.print(emontx.power1);
  }
  Serial.print(dht.getStatusString());
  int humidity1 = dht.getHumidity()*100;
  emontx.humidity1 = humidity1;
  int temp1 = dht.getTemperature()*100;
  emontx.temp1 = temp1;
    Serial.print("\t");
  Serial.print(emontx.humidity1);
  Serial.print("\t");
  Serial.print(emontx.temp1);
  Serial.print("\t");

  emontx.battery = ct1.readVcc();                                      //read emonTx battey (supply voltage)

  Serial.print(" "); Serial.print(emontx.battery);
  Serial.println(); delay(100);

  // because millis() returns to zero after 50 days !
  if (!settled && millis() > FILTERSETTLETIME) settled = true;

  if (settled)                                                            // send data only after filters have settled
  {
    send_rf_data();                                                       // *SEND RF DATA* - see emontx_lib
    digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW);      // flash LED
    emontx_sleep(5);                                                      // sleep or delay in seconds - see emontx_lib
  }
}

I cannot see a reason for a problem in your emonTx, but which sketch are you using in your NanodeRF?

Thank you for your answer.

I use the NanodeRF_multinode exemple, I want to modify this example to use it with a Domoticz server.
But here, for the moment I have to recover the good values. :smiley:

/*
  NanodeRF_multinode
*/

#define UNO       //anti crash wachdog reset only works with Uno (optiboot) bootloader, comment out the line if using delianuova
#define RF69_COMPAT  0                                                // Set to 1 if using RFM69CW or 0 is using RFM12B
#include <JeeLib.h>	     //https://github.com/jcw/jeelib
#include <avr/wdt.h>

#define MYNODE 1            
#define RF_freq RF12_868MHZ     // frequency
#define group 210            // network group

//---------------------------------------------------------------------
// The PacketBuffer class is used to generate the json string that is send via ethernet - JeeLabs
//---------------------------------------------------------------------
class PacketBuffer : public Print {
public:
PacketBuffer () : fill (0) {}
const char* buffer() { return buf; }
byte length() { return fill; }
void reset()
{ 
  memset(buf,NULL,sizeof(buf));
  fill = 0; 
}
virtual size_t write (uint8_t ch)
    { if (fill < sizeof buf) buf[fill++] = ch; }
byte fill;
char buf[150];
private:
};
PacketBuffer str;

//--------------------------------------------------------------------------
// Ethernet
//--------------------------------------------------------------------------
#include <EtherCard.h>		



// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x42,0x31,0x42,0x21,0x30,0x31 };

// 1) Set this to the domain name of your hosted emoncms - leave blank if posting to IP address 
const char website[] PROGMEM = "emoncms.org";

// or if your posting to a static IP server:
static byte hisip[] = { 192,168,10,50 };

// change to true if you would like the sketch to use hisip
boolean use_hisip = true;  

// 2) If your emoncms install is in a subdirectory add details here i.e "/emoncms3"
char basedir[] = "";

// 3) Set to your account write apikey 
char apikey[] = "YOURAPIKEY";

//IP address of remote sever, only needed when posting to a server that has not got a dns domain name (staticIP e.g local server) 
byte Ethernet::buffer[700];
static uint32_t timer;

const int redLED = 6;                     // NanodeRF RED indicator LED
//const int redLED = 17;  		  // Open Kontrol Gateway LED indicator
const int greenLED = 5;                   // NanodeRF GREEN indicator LED

int ethernet_error = 0;                   // Etherent (controller/DHCP) error flag
int rf_error = 0;                         // RF error flag - high when no data received 
int ethernet_requests = 0;                // count ethernet requests without reply                 

int dhcp_status = 0;
int dns_status = 0;

int data_ready=0;                         // Used to signal that emontx data is ready to be sent
unsigned long last_rf;                    // Used to check for regular emontx data - otherwise error

char line_buf[50];                        // Used to store line of http reply header

unsigned long time60s = -50000;


typedef struct { int power1, temp1, humidity1, battery; } PayloadTX;      // create structure - a neat way of packaging data for RF comms
PayloadTX emontx;

//**********************************************************************************************************************
// SETUP
//**********************************************************************************************************************
void setup () {
  
  //Nanode RF LED indictor  setup - green flashing means good - red on for a long time means bad! 
  //High means off since NanodeRF tri-state buffer inverts signal 
  pinMode(redLED, OUTPUT); digitalWrite(redLED,LOW);            
  pinMode(greenLED, OUTPUT); digitalWrite(greenLED,LOW);       
  delay(100); digitalWrite(redLED,HIGH);                          // turn off redLED
  
  Serial.begin(9600);
  Serial.println("\n[webClient]");

  //if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0) {	//for use with Open Kontrol Gateway 
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) {	//for use with NanodeRF
Serial.println( "Failed to access Ethernet controller");
ethernet_error = 1;  
  }

  dhcp_status = 0;
  dns_status = 0;
  ethernet_requests = 0;
  ethernet_error=0;
  rf_error=0;

  //For use with the modified JeeLib library to enable setting RFM12B SPI CS pin in the sketch. Download from: https://github.com/openenergymonitor/jeelib 
  // rf12_set_cs(9);  //Open Kontrol Gateway	
  // rf12_set_cs(10); //emonTx, emonGLCD, NanodeRF, JeeNode

  rf12_initialize(MYNODE, RF_freq,group);
  last_rf = millis()-40000;                                       // setting lastRF back 40s is useful as it forces the ethernet code to run straight away
   
  digitalWrite(greenLED,HIGH);                                    // Green LED off - indicate that setup has finished 
 
  #ifdef UNO
  wdt_enable(WDTO_8S); 
  #endif;
}

//**********************************************************************************************************************
// LOOP
//**********************************************************************************************************************
void loop () {
  
  #ifdef UNO
  wdt_reset();
  #endif

  dhcp_dns();   // handle dhcp and dns setup - see dhcp_dns tab
  
  // Display error states on status LED
  if (ethernet_error==1 || rf_error==1 || ethernet_requests > 0) digitalWrite(redLED,LOW);
else digitalWrite(redLED,HIGH);

  //-----------------------------------------------------------------------------------------------------------------
  // 1) On RF recieve
  //-----------------------------------------------------------------------------------------------------------------
  if (rf12_recvDone()){      
  if (rf12_crc == 0 && (rf12_hdr & RF12_HDR_CTL) == 0)
  {
    int node_id = (rf12_hdr & 0x1F);
    byte n = rf12_len;
     
    str.reset();
    str.print(basedir); str.print("/api/post.json?");
    str.print("apikey="); str.print(apikey);
    str.print("&node=");  str.print(node_id);
    str.print("&csv=");
    for (byte i=0; i<n; i+=2)
    {
      int num = ((unsigned char)rf12_data[i+1] << 8 | (unsigned char)rf12_data[i]);
      if (i) str.print(",");
      str.print(num);
    }

    str.print("\0");  //  End of json string
    data_ready = 1; 
    last_rf = millis(); 
    rf_error=0;
  }
  }


  //-----------------------------------------------------------------------------------------------------------------
  // 2) If no data is recieved from rf12 module the server is updated every 30s with RFfail = 1 indicator for debugging
  //-----------------------------------------------------------------------------------------------------------------
  if ((millis()-last_rf)>30000)
  {
last_rf = millis();                                                 // reset lastRF timer
str.reset();                                                        // reset json string
str.print(basedir); str.print("/api/post.json?");
str.print("apikey="); str.print(apikey);
str.print("&node=");  str.print(MYNODE);                            // Add this line where MYNODE is the node no of the NanodeRF this sketch is uploaded to
str.print("&json={rf_fail:1}\0");                                   // No RF received in 30 seconds so send failure 
data_ready = 1;                                                     // Ok, data is ready
rf_error=1;
  }

  //-----------------------------------------------------------------------------------------------------------------
  // 3) Send data via ethernet
  //-----------------------------------------------------------------------------------------------------------------
  ether.packetLoop(ether.packetReceive());
  
  if (data_ready) {

Serial.print("Data sent: "); Serial.println(str.buf); // print to serial json string

// Example of posting to emoncms.org http://emoncms.org 
// To point to your account just enter your WRITE APIKEY 
ethernet_requests ++;
ether.browseUrl(PSTR("") ,str.buf, website, my_callback);
data_ready =0;
  }
  
  if (ethernet_requests > 10) delay(10000); // Reset the nanode if more than 10 request attempts have been tried without a reply

  if ((millis()-time60s)>60000)
  {
time60s = millis();                                                 // reset lastRF timer
str.reset();
str.print(basedir); str.print("/time/local.json?"); str.print("apikey="); str.print(apikey);
Serial.println("Time request sent");
ether.browseUrl(PSTR("") ,str.buf, website, my_callback);
  }
}
//**********************************************************************************************************************

//-----------------------------------------------------------------------------------
// Ethernet callback
// recieve reply and decode
//-----------------------------------------------------------------------------------
static void my_callback (byte status, word off, word len) {
  int lsize =   get_reply_data(off);
  
  if (strcmp(line_buf,"ok")==0)
  {
Serial.println("OK recieved"); ethernet_requests = 0; ethernet_error = 0;
  }
  else if(line_buf[0]=='t')
  {
Serial.print("Time: ");
Serial.println(line_buf);

char tmp[] = {line_buf[1],line_buf[2],0};
byte hour = atoi(tmp);
tmp[0] = line_buf[4]; tmp[1] = line_buf[5];
byte minute = atoi(tmp);
tmp[0] = line_buf[7]; tmp[1] = line_buf[8];
byte second = atoi(tmp);

if (hour>0 || minute>0 || second>0) 
{  
  char data[] = {'t',hour,minute,second};
  int i = 0; while (!rf12_canSend() && i<10) {rf12_recvDone(); i++;}
  rf12_sendStart(0, data, sizeof data);
  rf12_sendWait(0);
}
  }
}

I try geeting data also with PayloadTX but same results
emontx = (PayloadTX) rf12_data;
Serial.print(emontx.temp1);

I cannot see anything wrong there either. The only difference in your emonTx sketch to what we normally do, is you multiply temperature and humidity by 100, we multiply by 10. But both should be less than the biggest integer, 32767, even with 100% humidity and 100 °C.

I shall convert your emonTx 2 sketch to run on an emonTH V1 (which I have, which has a DHT22 fitted) and I shall load your NanodeRF sketch into a NanodeRF (I have one of those too), and see what I get. But that won’t be for a couple of days, because there are other things that I must do first.

Looking again a little more carefully at your sketch, it appears that you’re not using the same DHT library that we use. (e.g, the method to initialise is DHT dht(DHTPIN, DHTTYPE);, whereas you have DHT dht( ) and a separate method to set the pin. That shouldn’t affect this problem, but it means you can’t use our sketches directly without translating them.
The library we use is this one: GitHub - adafruit/DHT-sensor-library: Arduino library for DHT11, DHT22, etc Temperature & Humidity Sensors

Thank you so much.
It work great with this lib, but i don’t understand why. :smiley:
For me an int value is an int, if in the monitor values are good, i don’t understand why after transmitting they become bad.

Nor do I. There is something very peculiar happening. I still have not had time to look in detail and get my old NanodeRF out.

If you ‘hard code’ fixed values into your emonTx sketch, e.g. after the line

 emontx.temp1 = temp1;

you write

emontx.humidity1 = 45.67;
 emontx.temp1 = 23.45;

you should see at the Nanode
xxxx [power] 2345 4567 3293

(was 97O a typo copying the values? - I expected 970)

I’ve got your emonTx sketch running on an emonTx V2, the only change is it doesn’t have a DHT22, so I have hard-coded test values:

emontx.temp1 = 1234;
emontx.humidity1 = 5678;

as the post immediately above.
It genuinely transmits the correct data, verified by my emonGLCD “debugger” sketch which displays the string it receives as byte values.
I have modified your Nanode sketch to remove all the commands to send to Ethernet because that’s not relevant, I’ve put these print statements in:

  //-----------------------------------------------------------------------------------------------------------------
  // 1) On RF receive
  //-----------------------------------------------------------------------------------------------------------------
  if (rf12_recvDone())
  {      
      if (rf12_crc == 0 && (rf12_hdr & RF12_HDR_CTL) == 0)
      {
        int node_id = (rf12_hdr & 0x1F);
        byte n = rf12_len;
         
        str.reset();
        str.print(basedir); str.print("/api/post.json?");
        str.print("apikey="); str.print(apikey);
        str.print("&node=");  str.print(node_id);
        str.print("&csv=");
Serial.print("Node: ");Serial.print(node_id);Serial.print(" ");
        for (byte i=0; i<n; i+=2)
        {
          int num = ((unsigned char)rf12_data[i+1] << 8 | (unsigned char)rf12_data[i]);
          if (i) str.print(",");
            str.print(num);
Serial.print(num); Serial.print(", ");            
        }
        str.print("\0");  //  End of json string
        data_ready = 1; 

Serial.println((char *)str.buffer());

        last_rf = millis(); 
        rf_error=0;
      }
  }

and it accurately prints the test values:

[webClient]
Node: 10 18, 1234, 5678, 3342, /api/post.json?apikey=YOURAPIKEY&node=10&csv=18,1234,5678,3342
Node: 10 23, 1234, 5678, 3342, /api/post.json?apikey=YOURAPIKEY&node=10&csv=23,1234,5678,3342
Node: 10 22, 1234, 5678, 3342, /api/post.json?apikey=YOURAPIKEY&node=10&csv=22,1234,5678,3342
Node: 10 15, 1234, 5678, 3342, /api/post.json?apikey=YOURAPIKEY&node=10&csv=15,1234,5678,3342

I cannot explain why it does not work correctly for you. The values must be getting overwritten somewhere, but I can see nothing to indicate where that might be. I don’t believe it could be in the Ethernet library or functions, because all that has been well proven. But of course I could be wrong.
All I can suggest is you put “Print” statements as close as possible to the data coming out of JeeLib, and verify it there.

How much free memory do you have when it compiles? You could reduce the size of the Ethernet buffer as I don’t think you’re using all 700 bytes.