Hi guys,
I made a battery powered sensor with MLX90614 to check my furnace's temp. It needs Adafruit's MLX90614 library. I'm using RFM69 radios, so please make sure you chose the right one.
/**
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2015 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
*******************************
*
* REVISION HISTORY
* Version 1.0 - Henrik Ekblad
*
* DESCRIPTION
* Pressure sensor example using BMP085 module
* http://www.mysensors.org/build/pressure
*
*/
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
//#define MY_RADIO_NRF24
#define MY_RADIO_RFM69
#include <SPI.h>
#include <MySensors.h>
#include <Wire.h>
//MLX90614 library from Adafruit
#include <Adafruit_MLX90614.h>
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
#define MY_PARENT_NODE_ID 0
#define TEMP_CHILD 0
#define TEMP2_CHILD 1
#define BAT_CHILD 2
int BATTERY_SENSE_PIN = A0; // select the input pin for the battery sense point
int oldBatteryPcnt = 0;
// Sleep time between reads (in ms). Do not change this value as the forecast algorithm needs a sample every minute.
const unsigned long SLEEP_TIME = 60000;
float lastTemp=-1;
float lastTemp2=-1;
float lastBat=-1;
boolean metric;
MyMessage tempMsg(TEMP_CHILD, V_TEMP);
MyMessage temp2Msg(TEMP2_CHILD, V_TEMP);
MyMessage batMsg(BAT_CHILD, V_VOLTAGE);
void setup() {
// metric = getConfig().isMetric;
mlx.begin();
// use the 1.1 V internal reference
// pinMode(5, OUTPUT);
#if defined(__AVR_ATmega2560__)
analogReference(INTERNAL1V1);
#else
analogReference(INTERNAL);
#endif
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("MLX90614", "1.0");
// Register sensors to gw (they will be created as child devices)
present(TEMP_CHILD, S_TEMP);
present(TEMP2_CHILD, S_TEMP);
present(BAT_CHILD, S_MULTIMETER);
}
// Loop
void loop() {
Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempC());
Serial.print("*C\tObject = "); Serial.print(mlx.readObjectTempC()); Serial.println("*C");
int sensorValue = analogRead(BATTERY_SENSE_PIN);
int batteryPcnt = sensorValue / 10.23;
float batteryV = sensorValue * 0.003363075;
#ifdef MY_DEBUG
Serial.print("Battery Voltage: ");
Serial.print(batteryV);
Serial.println(" V");
Serial.print("Battery percent: ");
Serial.print(batteryPcnt);
Serial.println(" %");
#endif
if (oldBatteryPcnt != batteryPcnt) {
// Power up radio after sleep
sendBatteryLevel(batteryPcnt);
oldBatteryPcnt = batteryPcnt;
}
send(tempMsg.set(mlx.readAmbientTempC(), 2));
lastTemp = mlx.readAmbientTempC();
send(temp2Msg.set(mlx.readObjectTempC(), 2));
lastTemp2 = mlx.readObjectTempC();
send(batMsg.set(batteryV, 2));
lastBat = batteryV;
sleep(SLEEP_TIME);
}