Arduino code for sending DS18B20 temperature data via Feathe

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
CluelessRic
 
Posts: 16
Joined: Thu Feb 11, 2021 4:27 am

Arduino code for sending DS18B20 temperature data via Feathe

Post by CluelessRic »

My beginner's project involves sending DS18B20 temperature data via LoRa using a pair of Adafruit Feather M0 with RFM95 LoRa Radio - 900MHz boards. The sending board is battery powered with a solar charger, and receiving board will be connected to a LED matrix display.

I followed https://learn.adafruit.com/adafruit-fea ... m-9x-radio for example TX and RX Arduino code, and this tested successfully over the required range of 500m.

I now need to add the code to send and receive data from two DS18B20 sensors. I have not been able to find any user friendly adaptable code to date, and would appreciate any assistance.

Richard

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Arduino code for sending DS18B20 temperature data via Fe

Post by mikeysklar »

It is pretty much going to come down to using rf95.send().

If you have already looked at the Feather example folder you probably saw these:

https://github.com/adafruit/RadioHead/t ... ather9x_RX
https://github.com/adafruit/RadioHead/t ... ather9x_TX

A longer thread about the conversion issues.

https://forum.arduino.cc/t/help-with-lo ... g/527890/9

User avatar
CluelessRic
 
Posts: 16
Joined: Thu Feb 11, 2021 4:27 am

Re: Arduino code for sending DS18B20 temperature data via Fe

Post by CluelessRic »

Thanks Mike for your response.
My research had included the links you mention.
The first two do not help as I'm already sending and receiving simple text messages successfully.
I'm in the same situation as system was at the end of the longer unresolved thread; i.e. a I need some practical help.
Is there another forum that may be able to help with this issue?

User avatar
garyvdg
 
Posts: 91
Joined: Thu Jul 25, 2013 12:37 pm

Re: Arduino code for sending DS18B20 temperature data via Fe

Post by garyvdg »

Hello Ric,
Here is code I use for non-LoRa version of a Feather M0 RFM69 Packet Radio transmitter device.

It basically converts the decimal temperature (and voltage) values digit by digit to make up the character packet.

If this works for you, I can also send code for the receiver side to decode the packet string back to numerical values.

Code: Select all

#include <OneWire.h>
#include <DallasTemperature.h>
#include <RFM69.h>
//*********************************************************************************************
// *********** IMPORTANT SETTINGS - YOU MUST CHANGE/CONFIGURE TO FIT YOUR HARDWARE ************* 
//*********************************************************************************************
#define NETWORKID 100 // The same on all nodes that talk to each other 
#define NODEID 5    // The unique identifier of this node
#define RECEIVER 1  // The recipient of packets

#define FREQUENCY  RF69_915MHZ
#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HCW  true // set to 'true' if you are using an RFM69HCW module

// for Feather M0 Radio 
#define RFM69_CS 8
#define RFM69_IRQ 3
#define RFM69_IRQN 3
#define RFM69_RST 4
#define SERIAL_BAUD 115200

// Data wire is plugged into port 11 on the Feather M0
#define ONE_WIRE_BUS 11
#define TEMPERATURE_PRECISION 11
#define VBATPIN A7
#define LED 13 // onboard blinky


// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
DeviceAddress outsideThermometer;

int tens;
int units;
int decs;
double frac;
double frac1;
double frac2;
double x2;
double y;

double DTS;

double whole;

int16_t packettens = 0;
int16_t packetunits = 0;
int16_t packetdecs = 0;
int16_t Vtens = 0;
int16_t Vunits = 0;
int16_t Vdecs = 0;

char radiopacket[20] = "TXdata"; 

RFM69 radio = RFM69(RFM69_CS, RFM69_IRQ, IS_RFM69HCW, RFM69_IRQN);

#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL) // Required for Serial on Zero based boards
#define Serial SERIAL_PORT_USBVIRTUAL
#endif

void setup(void) {
Serial.begin(SERIAL_BAUD);
delay(500);
// Serial.println("Feather RFM69HCW Transmitter");

// Hard Reset the RFM module 
pinMode(RFM69_RST, OUTPUT); 
digitalWrite(RFM69_RST, HIGH); 
delay(100); 
digitalWrite(RFM69_RST, LOW); 
delay(100);
// Initialize radio 
radio.initialize(FREQUENCY,NODEID,NETWORKID); 
if (IS_RFM69HCW) {
radio.setHighPower(); // Only for RFM69HCW & HW! 
}
radio.setPowerLevel(31); // power output ranges from 0 (5dBm) to 31 (20dBm)
radio.encrypt(ENCRYPTKEY);
pinMode(LED, OUTPUT);
pinMode(10, OUTPUT);

digitalWrite(LED, LOW);
digitalWrite(10, LOW);

// Serial.print("\nTransmitting at ");
// Serial.print(FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915); 
// Serial.println(" MHz");

  delay(100);
  sensors.begin();

  if (!sensors.getAddress(outsideThermometer, 0)) Serial.println("Unable to find address for Device 0"); 
  sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);      
        
  delay(10);


  
}  // End of Setup.

void loop(void) {
  digitalWrite(LED, HIGH);
  float LiBatReading = 0;
  double z;
  byte i;
  
  for (byte j = 0; j < 25; j++)
  {
    LiBatReading = LiBatReading + analogRead(VBATPIN);
    delay(10);
  }
  
  float measuredvbat = LiBatReading / 25;

  measuredvbat *= 2; // we divided by 2, so multiply back 
  measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage 
  measuredvbat /= 1024; // convert to voltage
  if(measuredvbat < 3.84)
{
  digitalWrite(10, HIGH);
  delay(50); 
  digitalWrite(10, LOW);
}
else
{
  digitalWrite(10, LOW);
}
  // Serial.print("VBat: " ); Serial.println(measuredvbat);
  z = round(measuredvbat * 100); 
  updateLEDvolt(z);
  digitalWrite(LED, LOW);
  delay(3000);

  // Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  // Serial.println("DONE");
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  // Serial.print("Temperature for the device 1 (index 0) is: ");
  // Serial.println(sensors.getTempCByIndex(0));  
  DTS = sensors.getTempCByIndex(0);

if(DTS >= 0)
{
  radiopacket[6] = '+';
}
else
{
  radiopacket[6] = '-';  
}

  z = abs(round(DTS * 10)); 
  updateLEDtemp(z);
   
  delay(3000);



if((DTS < 10) && (DTS > -10))
{
  radiopacket[7] = ' ';
  itoa(packetunits, radiopacket+8, 10); 
}

else
{
  itoa(packettens, radiopacket+7, 10); 
  itoa(packetunits, radiopacket+8, 10); 
}

radiopacket[9] = '.';
itoa(packetdecs, radiopacket+10, 10); 

radiopacket[11] = 'v';
radiopacket[12] = '0';

itoa(Vtens, radiopacket+13, 10); 
radiopacket[14] = '.';
itoa(Vunits, radiopacket+15, 10); 
itoa(Vdecs, radiopacket+16, 10); 

Serial.print("Sending "); 
Serial.println(radiopacket);

//target node Id, message as string or byte array, message length 
if (radio.sendWithRetry(RECEIVER, radiopacket, strlen(radiopacket))) 
  { 
  // Serial.println("OK"); 
  
  digitalWrite(LED, HIGH);
  delay(50); 
  digitalWrite(LED, LOW);
  
  }
  // delay(10);
radio.receiveDone(); //put radio in RX mode
Serial.flush(); //make sure all serial data is clocked out before sleeping the MCU 

}


void updateLEDvolt(double i)
{  
        decs = fmod(i, 10);
        Vdecs = decs;
        
        x2 = i/100;
        frac1 = modf(x2, &whole);
        tens = whole;
        Vtens = tens;
        
        x2 = (frac1 + 0.001) * 10;
        frac2 = modf(x2, &whole);
        units = whole;
        Vunits = units;
        
  delay(500);  

}


void updateLEDtemp(double i)
{  
        decs = fmod(i, 10);
        packetdecs = decs;
      
        x2 = i/100;
        frac1 = modf(x2, &whole);
        tens = whole;
        packettens = tens;
        
        x2 = (frac1 + 0.001) * 10;
        frac2 = modf(x2, &whole);
        units = whole;
        packetunits = units;
  
  delay(500);  

}

User avatar
CluelessRic
 
Posts: 16
Joined: Thu Feb 11, 2021 4:27 am

Re: Arduino code for sending DS18B20 temperature data via Fe

Post by CluelessRic »

Thanks for the suggested TX code Gary!
It will be a challenge for me to adapt this; I will let you know if I make progress.

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”