Cubecell DS18B20 Lorawan

Hi All,

I would like to use a Cubecell dev board to measure temperature using a DS18B20 and send it to TTN via LoRaWan alongside a current battery voltage reading before going into deep sleep for a period of time.

I thought a good starting point for me as a rookie would be to look at the LoRaWan sensor examples provided in Arduino IDE.

The problem is, I’m having difficulty understanding how to construct the payload. Does anybody know where I can find some documentation so that I can understand exactly what is going on in this code? Its not clear to me what a “puc” is.

 * \brief   Prepares the payload of the frame
 */
HDC1080 hdc1080;
static void prepareTxFrame( uint8_t port )
{
	/*appData size is LORAWAN_APP_DATA_MAX_SIZE which is defined in "commissioning.h".
	*appDataSize max value is LORAWAN_APP_DATA_MAX_SIZE.
	*if enabled AT, don't modify LORAWAN_APP_DATA_MAX_SIZE, it may cause system hanging or failure.
	*if disabled AT, LORAWAN_APP_DATA_MAX_SIZE can be modified, the max value is reference to lorawan region and SF.
	*for example, if use REGION_CN470, 
	*the max value for different DR can be found in MaxPayloadOfDatarateCN470 refer to DataratesCN470 and BandwidthsCN470 in "RegionCN470.h".
	*/
    pinMode(Vext,OUTPUT);
    digitalWrite(Vext,LOW);
    hdc1080.begin(0x40);
    float temperature = (float)(hdc1080.readTemperature());
    float humidity = (float)(hdc1080.readHumidity());
    hdc1080.end();
    digitalWrite(Vext,HIGH);
    uint16_t batteryVoltage = getBatteryVoltage();
    unsigned char *puc;

    puc = (unsigned char *)(&temperature);
    appDataSize = 10;
    appData[0] = puc[0];
    appData[1] = puc[1];
    appData[2] = puc[2];
    appData[3] = puc[3];

    puc = (unsigned char *)(&humidity);
    appData[4] = puc[0];
    appData[5] = puc[1];
    appData[6] = puc[2];
    appData[7] = puc[3];

    appData[8] = (uint8_t)(batteryVoltage>>8);
    appData[9] = (uint8_t)batteryVoltage;

 
}

Given that the DS18B20 is one of the most common sensors that noobs play with, would it be a good idea to provide a commented example for people like me to learn from?

Thanks,

Dan

puc is only a variable name for the unsigned char the values are stored in.
this variable contains 4 bytes in an array.

you can decode that array in ttn with:

function bytesToInt(by) {
  f = by[0] | by[1]<<8 | by[2]<<16 | by[3]<<24;
  return f;
} 

function bytesToFloat(by) {
  var bits = by[3]<<24 | by[2]<<16 | by[1]<<8 | by[0];
  var sign = (bits>>>31 === 0) ? 1.0 : -1.0;
  var e = bits>>>23 & 0xff;
  var m = (e === 0) ? (bits & 0x7fffff)<<1 : (bits & 0x7fffff) | 0x800000;
  var f = sign * m * Math.pow(2, e - 150);
  return f;
} 

you can than decode with (i = index in array of bytes that are transmitted)

value = bytesToFloat(bytes.slice(i,i+=4));

Sorry for the delay in replying. Thanks for this, really helpful I think. Will set some time aside to see if I can modify the other sensor examples to work with a ds18b20 sometime over the next few days.

No doubt I’ll have some more questions I’m afraid!