Problem reading moisture sensor value and sending it with RF69


I started working with a YL-38 moisture sensor with Arduino (Whisper node) board and my goal is to make a simple script that will read the moisture level + Arduino battery voltage and send them via RF69 radio module onwards to another arduino node. I have this simple example code where I am printing the measured values (moisture and voltage) every 8 seconds to the serial monitor before sending the values. The printed values should be something like 9250 for the moisture and about 3200 for the battery voltage but I am constantly getting this to the serial monitor:

Setting up the warehouse leakage detection node: 
Putting Radio and SPI Flash to Sleep
Sending moisture reading: 9250
Sendin⸮ٽ⸮х⸮⸮⸮ɕ⸮⸮⸮⸮⸮遚⸮⸮⸮jR⸮Sending moisture reading: 10230
Sendiٽ⸮х⸮⸮⸮ɕ⸮⸮⸮⸮⸮遚⸮ŠjR⸮Sending moisture reading: 10230
Sendin⸮ٽ⸮х⸮⸮⸮ɕ⸮⸮⸮⸮⸮遚⸮⸮⸮jR⸮Sending moisture reading: 10230

The moisture reading gets this 10230 value and does not change even if i apply some moisture to the sensor and the battery voltage is just some weird characters. Could someone point me to the right direction with my code? Thanks.


#include <T2WhisperNode.h>
#include <LowPower.h>
#include <RH_RF69.h>
// Pin-out
#define DATA_PIN A0
#define PWD_PIN A1

// SPI Flash
T2Flash myFlash;
// RFM69 Radio
#define RADIO_FREQUENCY 433.0
#define RADIO_TX_POWER 13
#define RADIO_ENCRYPTION_KEY { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }
RH_RF69 myRadio;
// T2 Message
T2Message myMsg;
//Change nodeAddr according to source node
#define nodeAddr 0x96
#define baseAddr 0x0B
// integer to hold WaterLeakage level 
int MoistureLevel = 0;
// Initialization
void setup()
  Serial.println(F("Setting up the warehouse leakage detection node: "));  
  Serial.println(F("Putting Radio and SPI Flash to Sleep"));
  // Radio - Initialize the radio and put it to sleep to save energy
  uint8_t myRadioEncryptionKey[] = RADIO_ENCRYPTION_KEY;
  // Flash - We're not using, so just power it down to save energy

  // Setup pins
  digitalWrite(PWD_PIN, LOW);
  digitalWrite(DATA_PIN, LOW);
  pinMode(PWD_PIN, OUTPUT);
  pinMode(DATA_PIN, INPUT);

  // Setup the Blue LED pin
  digitalWrite(T2_WPN_LED_1, LOW); // Set LED to Off
  pinMode(T2_WPN_LED_1, OUTPUT);   // Set LED pint to OUTPUT
  // Blink the blue led once at the boot
  digitalWrite(T2_WPN_LED_1, HIGH);
  digitalWrite(T2_WPN_LED_1, LOW);
// Main loop count
uint8_t loopCount = 1;
// Main loop
void loop()

  if(loopCount == 1)

    // Do some work!
    loopCount = 0;


  // Using Low-Power library to put the MCU to Sleep
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);


void sendMoistureVolt()
  // Turn the Sensor ON
  digitalWrite(PWD_PIN, HIGH);
  digitalWrite(DATA_PIN, HIGH);

  //Wait a bit for the sensor to wake up

  // Read moisture
  int m = analogRead(DATA_PIN);
  int moisture = m * 10;

  //Turn the Sensor OFF

  // Define integer for voltage
  int voltage = 0;

  // Read from battery
  voltage = T2Utils::readVoltage(T2_WPN_VBAT_VOLTAGE, T2_WPN_VBAT_CONTROL);

  Serial.print("Sending moisture reading: ");
  Serial.print("Sending voltage reading: ");
  digitalWrite(PWD_PIN, LOW);
  digitalWrite(DATA_PIN, LOW);
  // Prepare Message and send it
  myMsg.cmd = 0x03; // Return Data
  myMsg.idx = 0x05; // HID and Sensors
  myMsg.sdx = 0x0a; // Temperature, humidity and voltage[0] = highByte(moisture); // highbyte moisture[1] = lowByte(moisture); // lowbyte moisture[2] = highByte(voltage); // highbyte voltage[3] = lowByte(voltage); // lowbyte voltage
  myMsg.len = 4; // Update length


void sendMessage()
  uint8_t radioBufLen = 0;

  // Prepare the Message headers
  myMsg.src = nodeAddr;
  myMsg.dst = baseAddr;

  // Encode Message and get the full length
  myMsg.getSerializedMessage(radioBuf, &radioBufLen);

  // Send it
  myRadio.send(radioBuf, radioBufLen);

  //Turn the Sensor OFF

That is NOT what that code does.

  // Define integer for voltage
  int voltage = 0;

Really? I'd never have figured out what that code was doing, without your useless comment.

Does your code for reading the sensors work if you stop falling asleep all the time? Does it work if you don't try to use the radio?

In other words, what functionality are you introducing that interferes with reading the moisture level and/or battery voltage?

By the way, why are you setting the digital mode of a pin when you are using the pin as an analog pin?

Some comments dont correlate with the current behaviour of the code because I forgot to fix these. I have managed to get the code working when I dont prepare the message and send it (line 119-127). The message preparing does not seem to work right. The digital mode should be deleted there, I will fix that because the sensor is an analog device.