Author Topic: Feather M0 RFM95 / Sending Temperature Data  (Read 1953 times)

cat408tn

  • NewMember
  • *
  • Posts: 1
Feather M0 RFM95 / Sending Temperature Data
« on: April 04, 2018, 11:34:12 PM »
I'm trying to collect sensor data using the TMP36GZ temperature sensor from Adafruit and send it to another Feather M0. The code I've used so far is from RadioHead and I've changed it a little to better suit my needs. I'm collecting the data just fine, but I can't seem to have the data send correctly. The package on the server side always just shows the same large number (not the correct temperature) when I pull up the serial monitor. For the transmitter, I display the temperature data collected and it's correct, but when I send it to the receiver it changes it somehow. I'm very new to this programming language for Arduino and I could just be missing something simple. Any help would be greatly appreciated. I'm trying to keep this as simple as possible. Here is my code for the transmitter and receiver:

Transmitter:

// rf95_client.pde
// -*- mode: C++ -*-

#include <SPI.h>
#include <RH_RF95.h>

RH_RF95 rf95(8, 3); // Adafruit Feather M0 with RFM95

byte sendLen;
char buffer[50];

void setup()
{

  Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");
  else Serial.println("init OK - 915 MHz");

}

void loop()
{

  int readingT = analogRead(1);

  float voltage = readingT * 5.0;
  voltage /= 1024.0;

  float temperatureC = (voltage - 0.5) * 37.0;
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

   Serial.print(voltage); Serial.println(" V");
  Serial.print(temperatureC); Serial.println(" degrees C");
  Serial.print(temperatureF); Serial.println(" degrees F");

  //send temp
  sprintf(buffer, "%f", temperatureF);
  sendLen = strlen(buffer);
  rf95.send((uint8_t *) buffer, sendLen);

  delay(1000);
}



Receiver:

// rf95_server.pde
// -*- mode: C++ -*-

#include <SPI.h>
#include <RH_RF95.h>

RH_RF95 rf95(8, 3);

int led = 9;

char buffer[50];

void setup()
{

  pinMode(led, OUTPUT);     
  Serial.begin(9600);
  if (!rf95.init())
    Serial.println("init failed");
  else Serial.println("init OK - 915 MHz"); 
 
}

void loop()
{
 
  if (rf95.available())
  {
    // Should be a message for us now   
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (rf95.recv(buf, &len))
    {
      Serial.println("Recieved:");

  digitalWrite(led, HIGH);
      Serial.print("Temperature: ");
      Serial.print((char*)buf); Serial.println(" degrees F"); Serial.println(" ");   
    }
    else
    {
      Serial.println("recv failed");
    }
  }
}

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: Feather M0 RFM95 / Sending Temperature Data
« Reply #1 on: April 05, 2018, 12:40:13 AM »
I haven't used the M0 Feather but some (most) Arduino implementations don't support "%f" format in sprintf unless a special compiler option is set.  I suggest that you print the result from
Code: [Select]
  //send temp
  sprintf(buffer, "%f", temperatureF);
 
at your transmitter before sending to make sure that sprintf is producing what you expect.

See arduino dtostrf() function if sprintf isn't working as you expect.

Tom

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Feather M0 RFM95 / Sending Temperature Data
« Reply #2 on: April 05, 2018, 07:35:39 AM »
@cat408tn,

Arduiniana's Pstring is a great library for doing easy string manipulation: http://arduiniana.org/libraries/pstring/

PS: Please enclose your code blocks using the [ CODE ] tag (select and click the # icon).