Dear Lewishollow, Thanks for your guidance and I tried to alter the RFM node sketch to include the EMON functions using EmonLib.h library, but I am wrong some where and getting error message while compiling. I give below your RFM code that I modified to get just Vrms and Irms with no success. please help me and correct my code as I am stuck and could not proceed. Thanks with great regards, Ragoth.
// RFM69 node sketch
//
// This node talks to the MQTT-Gateway and will:
// - send sensor data periodically and on-demand
// - receive commands from the gateway to control actuators
// - receive commands from the gateway to change settings
//
// Several nodes can operate within a single network; each having a unique node ID.
// On startup the node operates with default values, set on compilation.
//
// Hardware used is a 3.3 Volt 8MHz arduino Pro; this is easier to interface to RFM69
//
//
// Current defined devices are:
//
// 0 uptime: read uptime node in minutes
// 1 node: read/set transmission interval in seconds, 0 means no periodic transmission
// 2 RSSI: read radio signal strength
// 3 Version: read version node software
// 4 voltage: read battery level
// 5 ACK: read/set acknowledge message after a 'set' request
// 6 toggle: read/set toggle function on button press
// 7 timer: read/set activation timer after button press in seconds, 0 means no timer
// 9 retry: read number of retransmissions needed in radiolink
//
// 16 actuator: read/set LED or relay output
// 40 Button: tx only: message sent when button pressed
// 48 temperature: read temperature
// 49 humidity: read humidity
// 64 light: read light level
// 65 flame: read flame sensor
// 66 gas: read gas sensor
// 90 error: tx only: error message if no wireless connection (generated by gateway)
// 92 error: tx only: device not supported
// 99 wakeup: tx only: first message sent on node startup
//
//
// 0 - 15 Node system devices
// 16 - 31 Binary output (LED, relay)
// 32 - 49 Integer output (pwm, dimmer)
// 40 - 47 Binary input (button, switch, PIR-sensor)
// 48 - 63 Real input (temperature, humidity)
// 64 - 72 Integer input (light intensity)
//
// The button can be set to:
// - generate a message on each press (limited to one per 10 seconds) and/or
// - toggle the output ACT1 (local node function) or
// - activate the output for a fixed time period (local node function)
//
// A debug mode is included which outputs messages on the serial output
//
// RFM69 Library by Felix Rusu - felix@lowpowerlab.com
// Get the RFM69 library at:
github.com/LowPowerLab///
// version 1.7 by Computourist@gmail.com december 2014
// version 2.0 increased payload size; implemented node uptime; standard device type convention; error handling .
// version 2.1 removed device 8; changed handling of device 40; compatible with gateway V2.2 ; march 2015
// version 2.1.1 lewishollow - dannyoleson@gmail.com - Changed to OO format. Adding/removing known devices is simple.
// Low power mode for batter nodes.
// version 2.1.2 lewishollow - dannyoleson@gmail.com - added device 9 for computourist's gateway 2.3+ compatibility.
// Fixed analog output to support dimmers. Fixed error in argument order for AnalogSensorData devices.
// WARNING - if you added your own Analog Sensor devices, fix your instantiations to match the new arg order!
#include "RfmAioNodeLibrary.h"
#include <RFM69.h>
#include <RFM69_ATC.h>
#include <SPI.h>
#include <DHT.h>
#include "EmonLib.h" // Include Emon Library
#include <LowPower.h> //https://github.com/rocketscream/Low-Power/archive/master.zip
// Wireless settings Match frequency to the hardware version of the radio
#define FREQUENCY RF69_433MHZ
//#define FREQUENCY RF69_868MHZ
//#define FREQUENCY RF69_915MHZ
#define IS_RFM69HW // uncomment only for RFM69HW!
#define ONESECOND 1000
//
// CONFIGURATION PARAMETERS
//
#define NODEID 3 // unique node ID within the closed network
#define NETWORKID 100 // network ID of the network
#define ENCRYPTKEY "abcdefghijklmnop" // 16-char encryption key; same as on Gateway!
#define DEBUG // uncomment for debugging
//#define LOWPOWERNODE // uncomment for battery-powered node
//
// ENABLE OR DISABLE DEVICES HERE
//
//#define PHOTOSENSORENABLED //A0_64
#define REEDSWITCHENABLED //D3-42
//#define BUTTONENABLED //D8_40
//#define ACTUATORENABLED //D9_16
//#define ACT1ENABLED // D9_19
//#define ACT2ENABLED // D7_17
//#define PIRENABLED //D8_41
//#define PIR1ENABLED //D7_41
//#define PIR2ENABLED //D8_43
//#define FLAMESENSORENABLED //A1_65
//#define GASSENSORENABLED //A2_66
//#define DHTSENSORENABLED //D4_48&49
#define EMONSENSORENABLED //D4_48&49
//#define RAINSENSORENABLED //A0_67
//#define DIMMERENABLED //D6_33
// TO ADD DEVICES, DECLARE THEM BELOW ALONG WITH NECESSARY DATA AS INDICATED BY THE CLASS
// USING A DEFINE FOR EACH DEVICE BEING ENABLED IS WHAT ALLOWS THIS CODE TO WORK ON A DEVICE
// WITH ANY SET OF SENSORS
// DO NOT MODIFY THIS SET OF SYSTEM DEVICES
NodeSystemDataClass *uptimeData;
NodeSystemDataClass *txIntervalData;
NodeSystemDataClass *rssiData;
NodeSystemDataClass *versionData;
NodeSystemDataClass *voltageData;
NodeSystemDataClass *ackData;
NodeSystemDataClass *toggleData;
NodeSystemDataClass *timerData;
NodeSystemDataClass *retryData;
ErrorDataClass *connectionError;
ErrorDataClass *unknownDevice;
WakeupSignalClass *wakeUp;
//END SYSTEM DEVICES
// Binary input settings
#ifdef BUTTONENABLED
#define BTN 8 // Button pin
#define BTNDEVICEID 40
DigitalInputDataClass *buttonData;
#endif //BUTTONENABLED
#ifdef REEDSWITCHENABLED
#define REEDSWITCHPIN 3
#define REEDSWITCHDEVICEID 42
DigitalInputDataClass *reedSwitchData;
#endif //REEDSWITCHENABLED
// Binary output settings
#ifdef ACTUATORENABLED
#define ACT1 9 // Actuator pin (LED or relay)
#define ACTUATORDEVICEID 16
DigitalOutputDataClass *actuatorData;
#endif //ACTUATORENABLED
#ifdef ACT1ENABLED
#define ACT1PIN 9 // Actuator pin (LED or relay)
#define ACT1DEVICEID 19
DigitalOutputDataClass *actuator1Data;
#endif
#ifdef ACT2ENABLED
#define ACT2PIN 7 // Actuator pin (LED or relay)
#define ACT2DEVICEID 17
DigitalOutputDataClass *actuator2Data;
#endif
#ifdef PIRENABLED
#define PIRPIN 8
#define PIRDEVICEID 41
DigitalInputDataClass *pirData;
#endif
#ifdef PIR1ENABLED
#define PIR1PIN 7
#define PIR1DEVICEID 41
DigitalInputDataClass *pir1Data;
#endif
#ifdef PIR2ENABLED
#define PIR2PIN 8
#define PIR2DEVICEID 43
DigitalInputDataClass *pir2Data;
#endif
// Analog sensor settings
#ifdef PHOTOSENSORENABLED
#define LIGHTPIN A0
#define LIGHTSENSORDEVICEID 64
AnalogSensorDataClass *lightSensorData;
#endif //PHOTOSENSORENABLED
#ifdef FLAMESENSORENABLED
#define FLAMEPIN A1
#define FLAMESENSORDEVICEID 65
AnalogSensorDataClass *flameSensorData;
#endif //FLAMESENSORENABLED
#ifdef GASSENSORENABLED
#define GASPIN A2
#define GASSENSORDEVICEID 66
AnalogSensorDataClass *gasSensorData;
#endif //GASSENSORENABLED
#ifdef RAINSENSORENABLED
#define RAINPIN A0
#define RAINSENSORDEVICEID 67
AnalogSensorDataClass *rainSensorData;
#endif //RAINSENSORENABLED
// DHT sensor setting
#ifdef DHTSENSORENABLED
#define DHTPIN 4 // DHT data connection
#define DHTTEMPDEVICEID 48
#define DHTHUMIDITYDEVICEID 49
#define DHTTYPE DHT11 // type of sensor
// EMON sensor setting
#ifdef EMONSENSORENABLED
#define EMONPIN 2 // DHT data connection
#define EMONPIN 3
#define EMONVOLTDEVICEID 50
#define EMONAMPDEVICEID 51
//#define EMON // type of sensor
DHT dht(DHTPIN, DHTTYPE, 3); // initialise temp/humidity sensor for 3.3 Volt arduino
//DHT dht(DHTPIN, DHTTYPE); // 5 volt power
RealInputDataClass *dhtTempSensorData;
RealInputDataClass *dhtHumSensorData;
#endif //DHTSENSORENABLED
EnergyMonitor emon1; // initialise temp/humidity sensor for 3.3 Volt arduino
RealInputDataClass *emonVoltSensorData;
RealInputDataClass *emonAmpSensorData;
#endif //EMONSENSORENABLED
#ifdef DIMMERENABLED
#define DIM1PIN 6
#define DIMDEVICEID 33
AnalogOutputDataClass *dimmerData;
#endif
//
// END DEVICE DECLARATION
//if using buttons, you can change behavior here
long timeInterval = 20 * ONESECOND; // timer interval in ms
bool toggleOnButton = true; // toggle output on button press
// DO NOT MODIFY THE REST OF THIS HEADER UNTIL THE SETUP FUNCTION
bool setAck = false; // send ACK message on 'SET' request
bool promiscuousMode = false; // only listen to nodes within the closed network
bool isLowPowerNode;
long thisCycleActualMillis;
#ifdef LOWPOWERNODE
long numWakes = 0;
bool firstLoop = true;
int wakeUpPinState;
#endif
Message mes;
RFM69_ATC radio;
// adding components in setup() will add to this array and increment the count
ComponentDataClass *connectedComponents[100];
int connectedComponentsCount = 0;
//
//===================== SETUP ========================================
//
void setup()
{
//sprintf(versionString, "AIO v%f", VERSIONNUM);
// DO NOT MODIFY THE BELOW CODE UNTIL INDICATED
//
// instantiate node system devices
uptimeData = new NodeSystemDataClass(UPTIMEDEVICEID, NULL, &getUptime);
txIntervalData = new NodeSystemDataClass(TXINTERVALDEVICEID, NULL, &getTxInterval);
rssiData = new NodeSystemDataClass(RSSIDEVICEID, NULL, &getSignalStrength);
versionData = new NodeSystemDataClass(VERSIONDEVICEID, NULL, &getVersion);
voltageData = new NodeSystemDataClass(VOLTAGEDEVICEID, NULL, &getVoltage);
ackData = new NodeSystemDataClass(ACKDEVICEID, NULL, &getAck);
//externed in componentdata.h for all device types to see
isLowPowerNode = false;
#ifdef LOWPOWERNODE
pinMode(REEDSWITCHPIN, INPUT);
isLowPowerNode = true;
wakeUpPinState = digitalRead(REEDSWITCHPIN);
#endif //LOWPOWERNODE
// instantiate error devices
connectionError = new ErrorDataClass(WIRELESSCONNECTIONERROR, NULL);
unknownDevice = new ErrorDataClass(UNSUPPORTEDDEVICE, NULL);
// instantiate our one wakeup device - DO NOT MODIFY
wakeUp = new WakeupSignalClass(WAKEUPNODE, NULL);
wakeUp->setShouldSend(true); //always send wakeup on first loop
toggleData = new NodeSystemDataClass(TOGGLEDEVICEID, NULL, &getToggleState);
timerData = new NodeSystemDataClass(TIMERDEVICEID, NULL, &getTimerInterval);
retryData = new NodeSystemDataClass(RETRYDEVICED, NULL, &getTxRetryCount);
//
// MODIFY BELOW THIS POINT
// instantiate binary output devices - DO NOT MODIFY
#ifdef ACTUATORENABLED
actuatorData = new DigitalOutputDataClass(ACTUATORDEVICEID, ACT1);
#endif //ACTUATORENABLED
#ifdef ACT1ENABLED
actuator1Data = new DigitalOutputDataClass(ACT1DEVICEID, ACT1PIN);
#endif
#ifdef ACT2ENABLED
actuator2Data = new DigitalOutputDataClass(ACT2DEVICEID, ACT2PIN);
#endif
#ifdef PIRENABLED
pirData = new DigitalInputDataClass(PIRDEVICEID, PIRPIN, ONESECOND);
#endif
#ifdef PIR1ENABLED
pir1Data = new DigitalInputDataClass(PIR1DEVICEID, PIR1PIN, ONESECOND);
#endif
#ifdef PIR2ENABLED
pir2Data = new DigitalInputDataClass(PIR2DEVICEID, PIR2PIN, ONESECOND);
#endif
// instantiate binary input devices - DO NOT MODIFY
#ifdef BUTTONENABLED
int buttonDelay = 2 * ONESECOND;
buttonData = new DigitalInputDataClass(BTNDEVICEID, BTN, buttonDelay);
#endif //BUTTONENABLED
#ifdef REEDSWITCHENABLED
reedSwitchData = new DigitalInputDataClass(REEDSWITCHDEVICEID, REEDSWITCHPIN, 10);
reedSwitchData->periodicSendEnabled = true;
#endif //REEDSWITCHENABLED
// instantiate real data sensors - callbacks are required
#ifdef DHTSENSORENABLED
dht.begin();
bool useFarenheit = true;
bool forceReading = false;
long overrideTxInterval = 30 * ONESECOND;
dhtTempSensorData = new RealInputDataClass(DHTTEMPDEVICEID, DHTPIN, overrideTxInterval, &getDhtTemperatureFarenheit);
dhtHumSensorData = new RealInputDataClass(DHTHUMIDITYDEVICEID, DHTPIN, overrideTxInterval, &getDhtHumidity);
#endif //DHTSENSORENABLED
#ifdef EMONSENSORENABLED
// emon1.calcVI(20,2000);
//dht.begin();
//bool useFarenheit = true;
bool forceReading = false;
long overrideTxInterval = 30 * ONESECOND;
emonVoltSensorData = new RealInputDataClass(EMONVOLTDEVICEID, EMONPIN 2, overrideTxInterval, &getEmonVolt);
emonAmpSensorData = new RealInputDataClass(EMONAMPDEVICEID, EMONPIN 3, overrideTxInterval, &getEmonAmp);
#endif //EMONSENSORENABLED
// instantiate analog sensors - no callback required
#ifdef PHOTOSENSORENABLED
int lightDeltaThreshold = 50;
lightSensorData = new AnalogSensorDataClass(LIGHTSENSORDEVICEID, LIGHTPIN, 2 * ONESECOND, lightDeltaThreshold);
lightSensorData->periodicSendEnabled = true;
#endif //PHOTOSENSORENABLED
#ifdef FLAMESENSORENABLED
int flameDeltaThreshold = 20;
flameSensorData = new AnalogSensorDataClass(FLAMESENSORDEVICEID, FLAMEPIN, 2 * ONESECOND, flameDeltaThreshold);
flameSensorData->periodicSendEnabled = true;
#endif //FLAMESENSORENABLED
#ifdef GASSENSORENABLED
int gasDeltaThreshold = 70;
gasSensorData = new AnalogSensorDataClass(GASSENSORDEVICEID, GASPIN, 5 * ONESECOND, gasDeltaThreshold);
gasSensorData->periodicSendEnabled = true;
#endif //GASSENSORENABLED
#ifdef RAINSENSORENABLED
int rainSenorThreshold = 20;
rainSensorData = new AnalogSensorDataClass(RAINSENSORDEVICEID, RAINPIN, 2 * ONESECOND, rainSenorThreshold);
#endif //RAINSENSORENABLED
#ifdef DIMMERENABLED
dimmerData = new AnalogOutputDataClass(DIMDEVICEID, DIM1PIN, 2.55);
#endif
#ifdef DEBUG
Serial.begin(SERIAL_BAUD);
#endif
radio.initialize(FREQUENCY, NODEID, NETWORKID); // initialise radio
#ifdef IS_RFM69HW
radio.setHighPower(); // only for RFM69HW!
#endif
radio.encrypt(ENCRYPTKEY); // set radio encryption
radio.promiscuous(promiscuousMode); // only listen to closed network
radio.enableAutoPower(-90);
#ifdef DEBUG
Serial.print("Node Software Version ");
Serial.println(VERSION);
Serial.print("\nTransmitting at ");
Serial.print(FREQUENCY == RF69_433MHZ ? 433 : FREQUENCY == RF69_868MHZ ? 868 : 915);
Serial.println(" Mhz...");
#endif
} // end setup
//
//
//==================== MAIN ========================================
//
void loop()
{
#ifdef LOWPOWERNODE
// how many times 8s an uninterrupted sleep should occur
// increase this number to lower node power usage
// at the cost of getting updates less frequently
const int sleepMultiplier = 45;
attachInterrupt(1, wakeUpHandler, !wakeUpPinState);
radio.sleep();
// sleep 8s * number of user-defined sleep-cycles
for (int loop = 0; loop < sleepMultiplier && digitalRead(REEDSWITCHPIN) == wakeUpPinState && !firstLoop; loop++)
{
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
wakeUpPinState = digitalRead(REEDSWITCHPIN);
// set all connected components to send starting after system devices since status can't be requested
for (int component = 0; component < connectedComponentsCount; component++)
{
if (connectedComponents[component]->deviceId >= 16 && connectedComponents[component]->deviceId < 90)
{
connectedComponents[component]->setShouldSend(true);
}
}
//set the system devices for manual send
rssiData->setShouldSend(true);
voltageData->setShouldSend(true);
detachInterrupt(1);
thisCycleActualMillis = millis();
numWakes++;
firstLoop = false;
#else
// RECEIVE radio input
if (receiveData())
{
parseCmd(); // receive and parse any radio input
}
#endif
// find buttons and handle their timers appropriately
for (int componentNum = 0; componentNum < connectedComponentsCount; componentNum++)
{
if (ISDIGITALINPUTSENSOR(componentNum))
{
DigitalInputDataClass *thisDigitalInput = (DigitalInputDataClass *)connectedComponents[componentNum];
if (thisDigitalInput->getIsButton())
{
thisDigitalInput->handleInput();
thisDigitalInput->handleTimer();
}
}
}
// SEND RADIO PACKETS
sendMsg();
} // end loop
//
//
//===================== FUNCTIONS ==========================================
//
//======== RECEIVEDATA : receive data from gateway over radio
//
bool receiveData()
{
bool validPacket = false;
if (radio.receiveDone()) // check for received packets
{
validPacket = radio.DATALEN == sizeof(mes);
if (validPacket)
{
mes = *(Message*)radio.DATA;
validPacket = true; // YES, we have a packet !
}
#ifdef DEBUG
if (!validPacket)
{
Serial.println("invalid message structure..");
}
else
{
Serial.print(mes.devID);
Serial.print(", ");
Serial.print(mes.cmd);
Serial.print(", ");
Serial.print(mes.intVal);
Serial.print(", ");
Serial.print(mes.fltVal);
Serial.print(", RSSI= ");
Serial.println(radio.RSSI);
Serial.print("Node: ");
Serial.println(mes.nodeID);
}
#endif
}
if (radio.ACKRequested())
{
radio.sendACK(); // respond to any ACK request
}
return validPacket; // return code indicates packet received
} // end recieveData
//
//
//============== PARSECMD: analyse messages and execute commands received from gateway
//
void parseCmd()
{ // parse messages received from the gateway
bool deviceFound = false;
for (int connectedComponentNumber = 0; connectedComponentNumber < connectedComponentsCount; connectedComponentNumber++)
{
ComponentDataClass *thisDevice = connectedComponents[connectedComponentNumber];
thisDevice->setShouldSend(false);
if (mes.devID == thisDevice->deviceId)
{
deviceFound = true;
if (mes.cmd == READ)
{
thisDevice->setShouldSend(true);
}
if (ISNODESYSTEMDEVICE(mes.devID))
{
if (mes.devID == TXINTERVALDEVICEID)
{
if (mes.cmd == WRITE) // cmd == 0 means write a value
{
thisDevice->setShouldSend(setAck);
// change interval to radio packet value - min value is 10
for (int deviceNum = 0; deviceNum < connectedComponentsCount; deviceNum++)
{
mes.intVal < 10 ? connectedComponents[deviceNum]->txInterval = 10
: connectedComponents[deviceNum]->txInterval = mes.intVal;
}
#ifdef DEBUG
Serial.print("Setting interval to ");
Serial.print(mes.intVal);
Serial.println(" seconds");
#endif
}
}
else if (mes.devID == ACKDEVICEID)
{
if (mes.cmd == WRITE)
{
// intVal will indicate whether or not to acknowledge
// for safety, ensure it's 1 or 0
setAck = !(!(mes.intVal));
thisDevice->setShouldSend(setAck); // acknowledge message ?
}
}
else if (mes.devID == TOGGLEDEVICEID)
{
if (mes.cmd == WRITE)
{
toggleOnButton = !(!(mes.intVal));
if (setAck)
{
thisDevice->setShouldSend(true); // acknowledge message ?
}
}
}
else if (mes.devID == TIMERDEVICEID)
{
if (mes.cmd == WRITE)
{
timeInterval = mes.intVal; // change interval
if (timeInterval < 5 && timeInterval != 0)
{
timeInterval = 5;
}
thisDevice->setShouldSend(setAck); // acknowledge message ?
} // cmd == 1 means read a value
}
}
else if (ISDIGITALOUTPUTDEVICE(mes.devID))
{
if (mes.cmd == 0) // cmd == 0 means write
{
if (mes.intVal == 0 || mes.intVal == 1)
{
DigitalOutputDataClass *digitalOutput;
for (int componentNum = 0; componentNum < connectedComponentsCount; componentNum++)
{
if (connectedComponents[componentNum]->deviceId == mes.devID)
{
digitalOutput = (DigitalOutputDataClass *)connectedComponents[componentNum];
}
}
digitalOutput->setState(mes.intVal);
digitalOutput->setShouldSend(setAck); // acknowledge message ?
}
}
}
else if (ISANALOGOUTPUTDEVICE(mes.devID))
{
if (mes.cmd == 0) // cmd == 0 means write
{
AnalogOutputDataClass *analogOutput;
for (int componentNum = 0; componentNum < connectedComponentsCount; componentNum++)
{
if (connectedComponents[componentNum]->deviceId == mes.devID)
{
analogOutput = (AnalogOutputDataClass *)connectedComponents[componentNum];
}
}
analogOutput->setState(mes.intVal);
analogOutput->setShouldSend(setAck); // acknowledge message ?
}
}
}
}
if (!deviceFound)
{
unknownDevice->setShouldSend(true);
}
} // end parseCmd
//
//
//====================== SENDMSG: sends messages that are flagged for transmission
//
void sendMsg()
{
mes.nodeID = NODEID;
mes.intVal = 0;
mes.fltVal = 0;
mes.cmd = 0; // '0' means no action needed in gateway
if (wakeUp->getShouldSend())
{
mes.devID = wakeUp->deviceId;
txRadio();
wakeUp->setShouldSend(false);
}
for (int componentNumber = 0; componentNumber < connectedComponentsCount; componentNumber++)
{
if (ISNODESYSTEMDEVICE(connectedComponents[componentNumber]->deviceId))
{
NodeSystemDataClass *nodeDevice = (NodeSystemDataClass *)connectedComponents[componentNumber];
if (nodeDevice->getShouldSend())
{
mes.devID = nodeDevice->deviceId;
// node devices can be floats or ints - setting both to catch all
mes.fltVal = nodeDevice->getValueToSend();
mes.intVal = (int)nodeDevice->getValueToSend();
txRadio();
nodeDevice->setShouldSend(false);
}
}
if (ISDIGITALOUTPUTDEVICE(connectedComponents[componentNumber]->deviceId))
{
DigitalOutputDataClass *digitalOutputDevice = (DigitalOutputDataClass *)connectedComponents[componentNumber];
if (digitalOutputDevice->getShouldSend())
{
mes.devID = digitalOutputDevice->deviceId;
mes.intVal = digitalOutputDevice->getValueToSend();
txRadio();
digitalOutputDevice->setShouldSend(false);
}
}
if (ISANALOGOUTPUTDEVICE(connectedComponents[componentNumber]->deviceId))
{
AnalogOutputDataClass *analogOutputDevice = (AnalogOutputDataClass *)connectedComponents[componentNumber];
if (analogOutputDevice->getShouldSend())
{
mes.devID = analogOutputDevice->deviceId;
mes.intVal = analogOutputDevice->getValueToSend();
txRadio();
analogOutputDevice->setShouldSend(false);
}
}
if (ISDIGITALINPUTSENSOR(connectedComponents[componentNumber]->deviceId))
{
DigitalInputDataClass *digitalInputDevice = (DigitalInputDataClass *)connectedComponents[componentNumber];
if (digitalInputDevice->getShouldSend())
{
mes.devID = digitalInputDevice->deviceId;
mes.intVal = digitalInputDevice->getValueToSend();
txRadio();
digitalInputDevice->setShouldSend(false);
}
}
if (ISREALINPUTSENSOR(connectedComponents[componentNumber]->deviceId))
{
RealInputDataClass *realInputDevice = (RealInputDataClass *)connectedComponents[componentNumber];
if (realInputDevice->getShouldSend())
{
mes.devID = realInputDevice->deviceId;
mes.fltVal = realInputDevice->getValueToSend();
txRadio();
realInputDevice->setShouldSend(false);
}
}
if (ISANALOGSENSOR(connectedComponents[componentNumber]->deviceId))
{
AnalogSensorDataClass *analogSensorDevice = (AnalogSensorDataClass *)connectedComponents[componentNumber];
if (analogSensorDevice->getShouldSend())
{
mes.devID = analogSensorDevice->deviceId;
mes.intVal = analogSensorDevice->getValueToSend();
txRadio();
analogSensorDevice->setShouldSend(false);
}
}
if (ISERRORCODE(connectedComponents[componentNumber]->deviceId))
{
if (connectedComponents[componentNumber]->getShouldSend())
{
mes.intVal = mes.devID;
mes.devID = connectedComponents[componentNumber]->deviceId;
txRadio();
connectedComponents[componentNumber]->setShouldSend(false);
}
}
if (ISWAKEUPCODE(connectedComponents[componentNumber]->deviceId))
{
if (connectedComponents[componentNumber]->getShouldSend())
{
mes.devID = connectedComponents[componentNumber]->deviceId;
txRadio();
connectedComponents[componentNumber]->setShouldSend(false);
}
}
}
}
//
//
//======================= TXRADIO
//
void txRadio() // Transmits the 'mes'-struct to the gateway
{
bool retx = true;
int i = 0;
const int maxRetries = 6;
while (retx && i < maxRetries)
{
if (radio.sendWithRetry(GATEWAYID, (const void*)(&mes), sizeof(mes), 5)) {
retx = false;
#ifdef DEBUG
Serial.print(" message ");
Serial.print(mes.devID);
Serial.println(" sent...");
#endif
}
else delay(500);
i++;
}
numTxAttempts = i; // store number of retransmissions needed
#ifdef DEBUG
if (retx) Serial.println("No connection...");
#endif
} // end txRadio
void populateMessagePayloadWithVersion()
{
int i;
for (i = 0; i < sizeof(VERSION); i++) {
mes.payLoad
= VERSION;
}
mes.payLoad = '\0'; // software version in payload string
}
//
//
//============== functions to use for callbacks to pass to the device classes
//
//
float getUptime()
{
return millis() / 60000;
}
float getTxInterval()
{
int returnValue = -1;
if (connectedComponentsCount > 0)
{
returnValue = connectedComponents[0]->txInterval;
}
return returnValue;
}
float getSignalStrength()
{
return radio.RSSI;
}
float getVersion()
{
populateMessagePayloadWithVersion();
return 0.0;
}
float getVoltage()
{
long voltage; // Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA, ADSC));
voltage = ADCL;
voltage |= ADCH << 8;
voltage = 1126400L / voltage; // Back-calculate in mV
return float(voltage / 1000.0);
}
float getAck()
{
return setAck * 1.0;
}
float getToggleState()
{
return (float)toggleOnButton;
}
float getTimerInterval()
{
return (float)timeInterval;
}
float getTxRetryCount()
{
return (float)numTxAttempts;
}
#ifdef LOWPOWERNODE
void wakeUpHandler()
{
// Just a handler for the pin interrupt.
}
#endif //LOWPOWERNODE
#ifdef DHTSENSORENABLED
float getDhtTemperatureFarenheit()
{
return dht.readTemperature(true);
}
float getDhtHumidity()
{
return dht.readHumidity();
}
#endif //DHTSENSORENABLED
#ifdef EMONSENSORENABLED
float getsupplyVoltage()
{
return emon1.Vrms();
}
float getIrms()
{
return emon1.Irms();
}
#endif //EMONSENSORENABLED