LowPowerLab Forum

Hardware support => General topics => Topic started by: practicallyrational on July 09, 2016, 10:27:31 AM

Title: Having trouble with putting together a sensor network (RadioHead lib)
Post by: practicallyrational on July 09, 2016, 10:27:31 AM
Hey guys, I have been lurking around here for quite a while, trying to get familiar with how to program these moteinos with LoRa radios and rf69s to take temp readings from some ds18b20s, send them to a rasberry pi with either a mighty hat, or a loose 915mhz LoRa radio I have, store the readings in an sql database, display them to an html5/bootstrap gui, and send an email alert when temperatures exceed their bounds beyond a specified time frame. Pretty simple, but it's a big bite for me to chew with my experience.

I can write python programs, simple web crawlers and sql database scripts, regex, my html5 and bootstrap skills are very rudimentary, but they exist. I have been tinkering with this stuff for a while, but I haven't been able to get anywhere due to a variety of problems.

Most recently I haven't been able to even get the temp probes to read properly, they keep sending an 85°C and 185°F reading iirc. I have used two different moteinos, three sensors, two different example sketches, 10k resistors, 4.7k , and 1k, I have tried using different grounds and different voltage sources.. same read.

If I get that sorted out, I still haven't been able to combine the reliable datagram server program with a temp reading program in such a way that I can actually get the string or variable I want to be sent to actually send. I have managed to change what they send, but the proper syntax to pass the temp readings from one radio to another is eluding me. I'll see if I can't dig up a program mash up that I was working on...

// rf69_reliable_datagram_client.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, reliable messaging client
// with the RHReliableDatagram class, using the RH_RF69 driver to control a RF69 radio.
// It is designed to work with the other example rf69_reliable_datagram_server
// Tested on Moteino with RFM69 http://lowpowerlab.com/moteino/
// Tested on miniWireless with RFM69 www.anarduino.com/miniwireless
// Tested on Teensy 3.1 with RF69 on PJRC breakout board

#include <RHReliableDatagram.h>
#include <RH_RF69.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
#define ONE_WIRE_BUS 3
// Singleton instance of the radio driver
RH_RF69 driver;
//RH_RF69 driver(15, 16); // For RF69 on PJRC breakout board with Teensy 3.1
//RH_RF69 rf69(4, 2); // For MoteinoMEGA https://lowpowerlab.com/shop/moteinomega

// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, CLIENT_ADDRESS);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer = {  0x28, 0x54, 0x51, 0x04, 0x00, 0x00, 0x80, 0x36 };
DeviceAddress condenserThermometer = { 0x28, 0xE0, 0x64, 0x04, 0x00, 0x00, 0x80, 0xBB };;
char tempeRatures[50];
void setup()
{
  Serial.begin(9600);
  if (!manager.init())
    Serial.println("init failed");
  // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM

  sensors.begin();
  sensors.setResolution(insideThermometer, 9);
  sensors.setResolution(condenserThermometer, 9);

  char tempeRatures[50];
  sprintf(tempeRatures, "Refrigerator:%d Condenser:%d", sensors.getTempC(insideThermometer), sensors.getTempC(condenserThermometer));


  // If you are using a high power RF69, you *must* set a Tx power in the
  // range 14 to 20 like this:
  // driver.setTxPower(14);
}
void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempeRatures);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}
double temp;
uint8_t sendLen;
//char tempStr[30];

//char fuck = insideThermometer;
//char fucky = condenserThermometer;
//char fucky2 = tempeRatures;
// sprintf(tempeRatures, "Refrigerator:%d Condenser:%d", sensors.getTempC(insideThermometer),sensors.getTempC(condenserThermometer));


uint8_t data[] = {tempeRatures[30]};
// Dont put this on the stack:
uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];

void loop()
{
  sensors.requestTemperatures();
  sprintf(tempeRatures, "R:%d C:%d", sensors.getTempC(insideThermometer), sensors.getTempC(condenserThermometer));
  sendLen = strlen(tempeRatures);
  Serial.println("Sending to rf69_reliable_datagram_server");
  char tempeRatures[30];
  //Serial.print ("TEST");
  //Serial.print (fuck);
  //Serial.print (fucky);
  //Serial.print (fucky2);
  printTemperature(tempeRatures);
  printTemperature(condenserThermometer);
  printTemperature(insideThermometer);
  //Send a message to manager_server
  if (manager.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
  {
    //Now wait for a reply from the server
    uint8_t len = sizeof(buf);
    uint8_t from;
    if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
    {
      Serial.print("got reply from : 0x");
      Serial.print(from, HEX);
      Serial.print(": ");
      Serial.println((uint8_t *) tempeRatures, sendLen);
    }
    else
    {
      Serial.println("No reply, is rf69_reliable_datagram_server running?");
    }
  }
  else
    Serial.println("sendtoWait failed");
  delay(500);


}



As you can probably see, my utter lack of experience with C and C++ is pretty obvious. It's definitely not python.
(please excuse the variable names, I... have no real excuse, it was a joke aimed at a friend who was looking at the code for me. )
I have been looking at other projects that achieve what I am trying to do, but every time I try to get back to working on one problem with building the system, a new one seems to crop up with something that was working flawlessly.

Any advice or guidance would be greatly appreciated.
Title: Re: Having trouble with putting together a sensor network.
Post by: Felix on July 11, 2016, 03:24:12 PM
Thanks for your post,
I believe you're trying to use the RF69 RadioHead library (http://www.airspayce.com/mikem/arduino/RF69/) so unless someone else here has experience with that particular library I would recommend running this by the RadioHead creator.