#include <SX1272.h>
#include <SPI.h>
#include <DHT11.h>
#define uint unsigned int
#define ulong unsigned long
#define PIN_ANEMOMETER 2
#define PIN_RAINGAUGE 3
#define PIN_VANE 5
#define MSECS_CALC_WIND_SPEED 1000
#define MSECS_CALC_WIND_DIR 1000
#define MSECS_CALC_RAIN_FALL 1000
#define NUMDIRS 8
int err;
float temp, hum, vol, val;
char x, iSpeed;
int pin=4;
DHT11 dht11(pin);
int e;
char mensaje [60];
char temperatura[6];
char humedad [6];
char direccion [2];
char velocidad[6];
char lluvia [6];
ulong adc[NUMDIRS] = {26, 45, 77, 118, 161, 196, 220, 256};
char *strVals[NUMDIRS] = {"W","NW","N","SW","NE","S","SE","E"};
byte dirOffset=0;
volatile int numRevsAnemometer = 0; // Incremented in the interrupt
volatile int numDropsRainGauge = 0; // Incremented in the interrupt
ulong nextCalcSpeed; // When we next calc the wind speed
ulong nextCalcDir; // When we next calc the direction
ulong nextCalcRain; // When we next calc the rain drop
ulong time; // Millis() at each start of loop().
//=======================================================
// Interrupt handler for anemometer. Called each time the reed
// switch triggers (one revolution).
//=======================================================
void countAnemometer() {
numRevsAnemometer++;
}
//=======================================================
// Interrupt handler for rain gauge. Called each time the reed
// switch triggers (one drop).
//=======================================================
void countRainGauge() {
numDropsRainGauge++;
}
//=======================================================
// Direccion del viento
void calcWindDir() {
int PIN_DIRECCION=5;
int val;
byte x, reading;
val = analogRead(PIN_DIRECCION);
val >>=2; // Shift to 255 range
reading = val;
// Look the reading up in directions table. Find the first value
// that's >= to what we got.
for (x=0; x<NUMDIRS; x++) {
if (adc[x] >= reading)
break;
}
//Serial.println(reading, DEC);
x = (x + dirOffset) % 8; // Adjust for orientation
Serial.print(" Dir: ");
Serial.println(strVals[x]);
}
//=======================================================
// Velocidad del viento
void calcWindSpeed() {
int x, iSpeed;
// This will produce kph * 10
// (didn't calc right when done as one statement)
long speed = 24011;
speed *= numRevsAnemometer;
speed /= MSECS_CALC_WIND_SPEED;
iSpeed = speed; // Need this for formatting below
Serial.print("Wind speed: ");
x = iSpeed / 10;
Serial.print(speed);
Serial.print('.');
x = iSpeed % 10;
Serial.print(x);
Serial.println();
numRevsAnemometer = 0; // Reset counter
}
//=======================================================
// Lluvia
void calcRainFall() {
int x, iVol;
// This will produce mm * 10000
// (didn't calc right when done as one statement)
long vol = 2794; // 0.2794 mm
vol *= numDropsRainGauge;
vol /= MSECS_CALC_RAIN_FALL;
iVol = vol; // Need this for formatting below
Serial.print("Rain fall: ");
x = iVol / 10000;
Serial.print(x);
Serial.print('.');
x = iVol % 10000;
Serial.print(x);
Serial.println();
numDropsRainGauge = 0; // Reset counter
}
//===========================================
//Sensor temperatura y humedad
void temperaturahumedad()
{
if((err=dht11.read(hum, temp))==0)
{
Serial.print("Temperatura: ");
Serial.print(temp);
Serial.print(" Humedad: ");
Serial.print(hum);
Serial.println();
}
else
{
Serial.println();
Serial.print("Error Num :");
Serial.print(err);
Serial.println();
}
}
//=======================================================
// Inicio
void setup() {
Serial.begin(38400);
pinMode(PIN_ANEMOMETER, INPUT);
digitalWrite(PIN_ANEMOMETER, HIGH);
digitalWrite(PIN_RAINGAUGE, HIGH);
attachInterrupt(0, countAnemometer, FALLING);
attachInterrupt(1, countRainGauge, FALLING);
nextCalcRain = millis() + MSECS_CALC_RAIN_FALL;
nextCalcSpeed = millis() + MSECS_CALC_WIND_SPEED;
nextCalcDir = millis() + MSECS_CALC_WIND_DIR;
// Modulo a ON
sx1272.ON();
// Modo de transmisión
e = sx1272.setMode(1);
Serial.println(F("Configurado modo: Estado "));
Serial.println(e, DEC);
// Seleccionamos frecuencia del canal
e = sx1272.setChannel(CH_13_868);
Serial.println(F("Configurado canal: Estado "));
Serial.println(e, DEC);
// Seleccionamos nivel de potencia
e = sx1272.setPower('M');
Serial.println(F("Configurada potencia: Estado "));
Serial.println(e, DEC);
// Seleccionamos dirección del nodo
e = sx1272.setNodeAddress(2);
Serial.println(F("Configurada dirección del nodo: Estado "));
Serial.println(e, DEC);
// Imprimimos mensaje
Serial.println(F("SX1272 configurado satisfactoriamente"));
}
//=======================================================
// Bucle principal
void loop() {
time = millis();
temperaturahumedad();
if (time >= nextCalcSpeed) {
calcWindSpeed();
nextCalcSpeed = time + MSECS_CALC_WIND_SPEED;
}
if (time >= nextCalcDir) {
calcWindDir();
nextCalcDir = time + MSECS_CALC_WIND_DIR;
}
if (time >= nextCalcRain) {
calcRainFall();
nextCalcRain = time + MSECS_CALC_RAIN_FALL;
dht11.read(hum, temp);//Leemos valor temperatura y humedad
e = sx1272.sendPacketTimeout(3, mensaje);//Selección dirección y mensaje a enviar
dtostrf (temp, 3, 2, temperatura);//Pasamos valor flotante a string
dtostrf (hum, 3, 2, humedad);//Pasamos valor flotante a string
dtostrf (val, 3, 2, direccion);//Pasamos valor flotante a string
//dtostrf (y, 3, 2, velocidad);//Pasamos valor flotante a string
//dtostrf (x, 2, lluvia);//Pasamos valor flotante a string
//Enviamos mensaje por canal 13 al dispositivo 3
sprintf(mensaje,"temperatura: %s , humedad: %s , velocidad: %i ,direccion %s , lluvia %s \r",temperatura, humedad,velocidad, direccion, lluvia);
}
}