In case someone is interested in the code for mysensors mqtt gateway using the DS2482.h library from https://github.com/paeaetech/paeae. The temperature conversion code is from example. I am omitting the mysensors setup.
...
#include <DS2482.h>
#include <Ethernet.h>
#include <MySensors.h>
#include <Wire.h>
DS2482 ds( 0 ); //channels ds2482-800 is 0 to 7, DS2482-100 is just set 0
byte dsData[8]; //holding for onewire capture
byte dsAddr[8]; //1wire wire address and CRC
MyMessage msgAddr( 0, V_ID );
MyMessage msgTemp( 0, V_TEMP );
void setup() {
//Uses Wire library to communicate with DS2482 so be sure to call Wire.begin() before using the library.
Wire.begin();
ds.reset();
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo( SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER );
}
void loop() {
#ifdef MY_DEBUG
Serial.println( "DBG: loop start" );
#endif
ds.wireResetSearch();
ds.wireReset();
ds.wireSkip();
//need to send high power to bus?? Adding 5 volts to devices makes it work.
ds.wireWriteByte( 0x44 ); //convert temperature on all devices
delay( 1000 );
byte sensorId = 0;
while( ds.wireSearch( dsAddr ) ) {
#ifdef MY_DEBUG
Serial.print( sensorId );
Serial.print( " : " );
#endif
char sensorAddr[17]; //eg. 280838211713012D
for( int i = 0; i < 8; i++ ) {
sensorAddr[i*2] = ( dsAddr[i] >> 4 ) + 0x30;
if ( sensorAddr[i*2] > 0x39 ) {
sensorAddr[i*2] += 7;
}
sensorAddr[i*2+1] = ( dsAddr[i] & 0x0f ) + 0x30;
if ( sensorAddr[i*2+1] > 0x39 ) {
sensorAddr[i*2+1] += 7;
}
}
sensorAddr[16] = '\0'; //always zero terminate string
#ifdef MY_DEBUG
Serial.print( sensorAddr );
Serial.print( "\n" );
#endif
send( msgAddr.setSensor( sensorId ).set( sensorAddr ) );
#ifdef MY_DEBUG
Serial.print( sensorId );
Serial.print( " : " );
#endif
if ( ds.crc8( dsAddr, 7 ) != dsAddr[7] ) {
Serial.print( "CRC is not valid!" );
return;
}
/*
//test if device code DS18B20
if ( dsAddr[0]==0x28 ) {
//test if device code MAX31850
if ( dsAddr[0]==0x3B ) {
*/
//read temperature data.
ds.wireReset(); //ds.reset();
ds.selectChannel( 0 ); //necessary on -800
ds.wireSelect( dsAddr );
ds.wireWriteByte( 0xbe ); // Read Scratchpad command
//display hex values of scratchpad
for ( int i = 0; i < 9; i++ ) { // we need 9 bytes
dsData[i] = ds.wireReadByte();
#ifdef MY_DEBUG
if ( dsData[i] < 16 ) {
Serial.print( "0" );
}
Serial.print( dsData[i], HEX );
#endif
}
//convert to decimal temperature
int LowByte = dsData[0];
int HighByte = dsData[1];
int TReading = ( HighByte << 8 ) + LowByte;
int SignBit = TReading & 0x8000; // test most sig bit
if ( SignBit ) // negative
{
TReading = ( TReading ^ 0xffff ) + 1; // 2's comp
}
float Tc_100 = (double) TReading * 0.0625;
if ( SignBit ) { // If its negative
Tc_100 = 0 - Tc_100;
}
//print temp for each device
#ifdef MY_DEBUG
Serial.print( " : " );
Serial.println( Tc_100 );
#endif
send ( msgTemp.setSensor( sensorId ).set( Tc_100, 2 ) );
sensorId++;
}
#ifdef MY_DEBUG
Serial.println( "DBG: loop end" );
#endif
delay( 60000 );
}
I am of course happy for any feedback. Especially if I am doing anything stupid as I am no programmer :)