89 lines
3.0 KiB
C++
Executable File
89 lines
3.0 KiB
C++
Executable File
#include <Narcoleptic.h>
|
|
#include <DHT.h>
|
|
#include <MANCHESTER.h>
|
|
|
|
#define BLINK_MODE true
|
|
|
|
#define NODE_ID 1 // On 8bits so 0..255
|
|
|
|
#define MESSAGE_SIZE 6 // Number of bytes to send
|
|
#define SEND_MESSAGE_DELAY 5000 // Delay in ms between each value's extraction
|
|
#define SEND_433_COUNT 4 // How many times the message has to be send
|
|
#define SEND_433_PAUSE 160 // 16 multiple
|
|
|
|
// Define connectors used for the node
|
|
#define TX_PIN 7
|
|
#define LED_PIN 13
|
|
#define DHT11_PIN 2
|
|
#define DHTTYPE DHT11 // DHT 11
|
|
|
|
DHT dht(DHT11_PIN, DHTTYPE);
|
|
|
|
// Array of bytes to will make the message
|
|
// In this node : 2 bytes for voltage, 2 bytes for
|
|
uint8_t msgData[MESSAGE_SIZE] = {0, 0, 0, 0, 0, 0};
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
// Read current supply voltage
|
|
//--------------------------------------------------------------------------------------------------
|
|
long readVcc() {
|
|
bitClear(PRR, PRADC); ADCSRA |= bit(ADEN); // Enable the ADC
|
|
long result;
|
|
// Read 1.1V reference against Vcc
|
|
#if defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
|
|
ADMUX = _BV(MUX5) | _BV(MUX0); // For ATtiny84
|
|
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
|
|
ADMUX = _BV(MUX3) | _BV(MUX2);
|
|
#else
|
|
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // For ATmega328
|
|
#endif
|
|
delay(2); // Wait for Vref to settle
|
|
ADCSRA |= _BV(ADSC); // Convert
|
|
while (bit_is_set(ADCSRA,ADSC));
|
|
result = ADCL;
|
|
result |= ADCH<<8;
|
|
result = 1126400L / result; // Back-calculate Vcc in mV
|
|
ADCSRA &= ~ bit(ADEN); bitSet(PRR, PRADC); // Disable the ADC to save power
|
|
return result; // Vcc in millivolts
|
|
}
|
|
|
|
void setup() {
|
|
pinMode(LED_PIN, OUTPUT);
|
|
if (BLINK_MODE) digitalWrite(LED_PIN, LOW);
|
|
//MANCHESTER.setupTransmit(TX_PIN, MAN_1200);
|
|
MANCHESTER.SetTxPin(0);
|
|
msgData[0] = NODE_ID;
|
|
// Wait 1s to allow DHT11 to initialize
|
|
Narcoleptic.delay(1000);
|
|
}
|
|
|
|
void loop() {
|
|
// Read Vcc value
|
|
long currentVcc = readVcc();
|
|
uint16_t uint16_currentVcc = (uint16_t)currentVcc;
|
|
// Save millivolts in two bytes to keep high precision. Will be decoded by the gateway
|
|
uint8_t byteData[2] = {uint16_currentVcc >> 8, uint16_currentVcc & 0xFF};
|
|
msgData[2] = byteData[0];
|
|
msgData[3] = byteData[1];
|
|
|
|
// Read data from DHT11 sensor
|
|
int chk = DHT.read11(DHT11_PIN);
|
|
// DHT11 values can be put in a byte value due to the low precision
|
|
msgData[4] = (uint8_t)DHT.humidity;
|
|
msgData[5] = (uint8_t)DHT.temperature;
|
|
|
|
// Send message SEND_433_COUNT times with a delay of SEND_433_PAUSE ms for each
|
|
for (int i=0; i<SEND_433_COUNT; i++) {
|
|
msgData[1] = i;
|
|
if (BLINK_MODE) digitalWrite(LED_PIN, HIGH);
|
|
//MANCHESTER.transmitArray(MESSAGE_SIZE, msgData);
|
|
MANCHESTER.Transmit(msgData);
|
|
|
|
if (BLINK_MODE) digitalWrite(LED_PIN, LOW);
|
|
// Wait between each send
|
|
Narcoleptic.delay(SEND_433_PAUSE);
|
|
}
|
|
// Wait before getting new sensor value
|
|
Narcoleptic.delay(SEND_MESSAGE_DELAY);
|
|
}
|