#include <OneWire.h>
#include <DallasTemperature.h>
#include <stdio.h>
const uint8_t ID = 1;// Identification number for the unit.
// Data wire is plugged into digital pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
// Calculate checksum of the messsage
uint16_t Fletcher16( uint8_t *data, int count )
{
uint16_t sum1 = 0;
uint16_t sum2 = 0;
int index;
for ( index = 0; index < count; ++index )
{
sum1 = (sum1 + data[index]) % 255;
sum2 = (sum2 + sum1) % 255;
}
return (sum2 << 8) | sum1;
}
// Function where to initialize things.
void setup() {
sensors.begin(); // Start up the dallas library
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
uint8_t incomingByte = Serial.read();
if (incomingByte - 48 == ID){
// put your main code here, to run repeatedly:
int hum = analogRead(A0);
float temp = sensors.getTempCByIndex(0);
char buffer1[256], buffer2[256];
sprintf(buffer1, "$%d,%d,%d", ID, hum, (int)(temp*10));
uint16_t sum = Fletcher16(buffer1, strlen(buffer1));
sprintf(buffer2,"%s*%02X\n", buffer1, sum);
Serial.print(buffer2);
}
}
delay(10);
}