#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Average.h>
#include <EasyScheduler.h>
Schedular Task1;
Schedular Task2;
// NRF24 RADIO MANAGEMENT
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int meteodata[4]; // 4 element array holding Meteodata readings
int signal;
float temp1 = 0; // variable to store the value coming from the sensor
float temp1K = 0;
float temp1F = 0;
float press1 = 0;
float hum1 = 0; // variable to store the value coming from the sensor
float addry1 = 0;
float adwet1 = 0;
float vp1 = 0;
float mr1 = 0;
float heat1 = 0;
float ah1 = 0;
float dp1 = 0;
float vpe1 = 0;
float ad1 = 0;
float he1 = 0;
float batPercentage;
float batVoltage;
char temp1f[10];
char hum1f[10];
char batpercentf[10];
char batVoltagef[10];
char vp1f[8];
char mr1f[8];
char heat1f[8];
char ah1f[8];
char dp1f[8];
char vpe1f[8];
char ad1f[8];
char he1f[8];
// CONSTANTS FOR SATURATED VAPOR PRESSURE CALCULATION
float a0 = 6.107799961;
float a1 = 0.4436518521;
float a2 = 0.01428945805;
float a3 = 0.0002650648471;
float a4 = 0.000003031240396;
float a5 = 0.00000002034080948;
float a6 = 0.00000000006136820929;
// CONSTANTS FOR HEAT INDEX CALCULATION
float c1 = -42.379;
float c2 = 2.04901523;
float c3 = 10.14333127;
float c4 = -0.22475541;
float c5 = -0.00683783;
float c6 = -0.05481717;
float c7 = 0.00122874;
float c8 = 0.00085282;
float c9 = -0.00000199;
unsigned long currentTime;
unsigned long loopTime;
unsigned int counter=0;
unsigned long number_sum=0;
unsigned long number_avg=0;
// Window Creation strings (without initial escape sequence)
char header[] = {"w,1,0,0,100,3,1,"}; // no title
char window2[] = {"w,2,0,3,50,32,1,Indoor air data"};
char window3[] = {"w,3,50,3,50,32,1,Outdoor air data"};
char window4[] = {"w,4,0,35,100,14,1,Terminal Input"};
char footer[] = {"w,5,0,49,100,1,0,"}; // no border, no title
// Line colour strings (without initial escape sequence)
char header_colour[] = {"l,0,2,333,001"}; // White text on purple lines 0-2
char window3_colour[] = {"l,3,32,222,000"}; // white text on blue lines 35-48
char window4_colour[] = {"l,35,48,222,001"}; // white text on blue lines 35-48
char footer_colour[] = {"l,49,49,000,111"}; // black text on grey line 49
void vga_command(char *command_str) {
// Function to easily send command to VGA board
Serial.print("^["); // send escape sequence
Serial.println(command_str); // send Command string
// Most commands don't take very long, but need a small delay to complete
// The Reboot cammand needs 2 seconds
if(command_str[0]=='r') delay(2000); // Wait 2 seconds for reboot
if((command_str[0]=='w') || (command_str[0]=='p')) delay(20);
// Small delay for window commands
delay(2); // Other commands need a tiny delay
}
void vga_inverse(char *text_str) {
// Function to write inverse characters to VGA board
// Makes bit 7 of each character a 1
unsigned int i;
for(i=0;text_str[i] != '\0';i++) {
text_str[i]=text_str[i] | 0x80;
}
Serial.print(text_str);
}
void setup() {
Task1.start();
Task2.start();
//NRF24 RADIO INIT
radio.begin();
radio.setPALevel( RF24_PA_MAX );
radio.setDataRate( RF24_250KBPS);
radio.setCRCLength( RF24_CRC_8 ) ;
radio.openReadingPipe(1,pipe);
radio.startListening();
// initialize
// vga_command("b,115200");
// vga_command("r"); // reboot VGA board
Serial.begin(115200); // Set baud rate for serialVGA board
// vga_command("r"); // reboot VGA board
vga_command(header); // Create header
// Serial.print(" ");
Serial.print(" ATMOSPERIC MONITOR ");
vga_command(window2); // Create Window2
vga_command(window3); // Create Window3
vga_command(window4); // Create Window4
vga_command("c,4,1"); // Turn text cursor ON in window 4
vga_command(footer); // Create footer
// Serial.print(" Hobbytronics Ltd www.hobbytronics.co.uk");
// Serial.print(" serialVGA demo");
vga_command(header_colour); // header window colour
// vga_command(window3_colour); // window 3 colour
vga_command(window4_colour); // window 4 colour
// vga_command(footer_colour); // footer window colour
currentTime = millis();
loopTime = currentTime;
vga_command("f,3"); // Set Window3 as focus
vga_command("p,0,1");
Serial.print("Temperature"); // print data
vga_command("p,30,1");
Serial.print("C"); // print data
vga_command("p,0,2");
Serial.print("Relative humidity"); // print data
vga_command("p,30,2");
Serial.print("%"); // print data
vga_command("p,0,3");
Serial.print("Absolute humidity");
vga_command("p,30,3");
Serial.print("g/m3"); // print data
vga_command("p,0,4");
Serial.print("Dew point"); // print data
vga_command("p,30,4");
Serial.print("C"); // print data
vga_command("p,0,5");
Serial.print("Saturated vapor press."); // print data
vga_command("p,30,5");
Serial.print("hPa"); // print data
vga_command("p,0,6");
Serial.print("Water vapor press.");
vga_command("p,30,6");
Serial.print("hPa"); // print data
vga_command("p,0,7");
Serial.print("Humidex");
vga_command("p,30,7");
Serial.print("C"); // print data
// vga_command("p,0,8");
// Serial.print("Enthalphy");
// vga_command("p,30,8");
// Serial.print("kJ/kg"); // print data
vga_command("p,0,9");
Serial.print("Battery voltage");
vga_command("p,30,9");
Serial.print("mV");
vga_command("p,0,10");
Serial.print("Battery status");
vga_command("p,30,10");
Serial.print("%");
}
void loop() {
Task1.check(funct1,1);
Task2.check(funct2,1000);
}
void funct1() {
// CALCULATIONS
temp1K = temp1 + 273.15; // °C to K conversion
temp1F = temp1 / 5 * 9 + 32.000;
vp1 = a0 + temp1 * (a1 + temp1 * ( a2 + temp1 * (a3 + temp1 * ( a4 + temp1 * (a5 + a6 * temp1)))));
vpe1 = (hum1 * vp1) / 100.00; // Water vapor pressure
mr1 = 621.9907 * vpe1 / (press1 - vpe1); // Mixing ratio
ah1 = 2.16679 * vpe1 * 100 / temp1K;
dp1 = (-430.22 + 237.70 * log(vpe1)) / (-log(vpe1) + 19.08) ; // Dew point
addry1 = (((press1 * 100) - (vpe1 * 100)) / (287.04597 * (temp1K))); // Dry air fraction density
adwet1 = (vpe1 * 100) / (461.495 * (temp1K)); // Water fraction density
ad1 = addry1 + adwet1; // Actual air density
he1 = temp1 * (1.01 + 0.00189 * mr1) + 2.500 * mr1;
heat1 = ((c1 + c2 * temp1F+ c3 * hum1 + c4 * temp1F * hum1 + c5 * square(temp1F) + c6 * square(hum1) + c7 * square(temp1F) * hum1 + c8 * temp1F * square(hum1) + c9 * square(temp1F) * square(hum1))-32) / 9 * 5;
dtostrf(temp1,5, 2, temp1f);
dtostrf(hum1,5, 2, hum1f);
dtostrf(vp1,7, 2, vp1f);
dtostrf(heat1,7, 2, heat1f);
dtostrf(ah1,7, 2, ah1f);
dtostrf(dp1,7, 2, dp1f);
dtostrf(vpe1,7, 2, vpe1f);
dtostrf(ad1,7, 4, ad1f);
dtostrf(he1,7, 3, he1f);
dtostrf(batPercentage, 9, 2, batpercentf);
dtostrf(batVoltage, 9, 3, batVoltagef);
currentTime = millis();
if(currentTime >= (loopTime + 300)){
// send every half second
loopTime = currentTime; // Updates loopTime
/*
vga_command("f,2"); // Set Window2 as focus
Serial.print("This is data - "); // print data
Serial.println(counter, DEC);
*/
vga_command("f,3"); // Set Window3 as focus
vga_command("p,24,1"); // Set Window3 text position to 0,0
Serial.print(temp1f);
vga_command("p,24,2"); // Set Window3 text position to 0,0
Serial.print(hum1f);
vga_command("p,22,3"); // Set Window3 text position to 0,0
Serial.print(ah1f);
vga_command("p,22,4"); // Set Window3 text position to 0,0
Serial.print(dp1f);
vga_command("p,22,5"); // Set Window3 text position to 0,0
Serial.print(vp1f);
vga_command("p,22,6"); // Set Window3 text position to 0,0
Serial.print(vpe1f);
vga_command("p,22,7"); // Set Window3 text position to 0,0
Serial.print(heat1f);
// vga_command("p,22,8"); // Set Window3 text position to 0,0
// Serial.print(he1f);
vga_command("p,20,9"); // Set Window3 text position to 0,0
Serial.print(batVoltagef);
vga_command("p,20,10"); // Set Window3 text position to 0,0
Serial.print(batpercentf);
/*
Serial.print("Temperature "); // print data
Serial.print(temp1f);
Serial.print(" C");
vga_command("p,0,2");
Serial.print("Relative humidity "); // print data
Serial.print(hum1f);
Serial.print(" %");
vga_command("p,0,3");
Serial.print("Absolute humidity "); // print data
Serial.print(ah1f);
Serial.print(" g/m3");
vga_command("p,0,4");
Serial.print("Dew point "); // print data
Serial.print(dp1f);
Serial.print(" C");
// Serial.print("Actual air density "); // print data
// Serial.print(ad1f);
// Serial.println(" kg/m3");
vga_command("p,0,5");
Serial.print("Saturated vapor press."); // print data
Serial.print(vp1f);
Serial.print(" hPa");
vga_command("p,0,6");
Serial.print("Watet vapor press. "); // print data
Serial.print(vpe1f);
Serial.print(" hPa");
vga_command("p,0,7");
Serial.print("Humidex "); // print data
Serial.print(heat1f);
Serial.print(" C");
// vga_command("p,0,8");
// Serial.print("Enthalphy "); // print data
// Serial.print(he1f);
// Serial.println(" kJ/kg");
vga_command("p,0,9");
Serial.print("Battery voltage "); // print data
Serial.print(batVoltagef);
Serial.print(" mV");
vga_command("p,0,10");
Serial.print("Battery charge "); // print data
Serial.print(batpercentf);
Serial.print(" %");
*/
}
/*
if (Serial.available() > 0) {
// Data received into Arduino board, echo'd out to Window 4 (command window)
int inByte = Serial.read();
vga_command("f,4"); // Set Window4 as focus
Serial.write(inByte);
}
*/
}
void funct2() {
{
if ( radio.available() )
{
// Read the data payload until we've received everything
bool done = false;
while (!done)
{
// Fetch the data payload
done = radio.read( meteodata, sizeof(meteodata) );
signal = 1;
}
}
else
{
//meteodata[0];
//meteodata[1];
// Serial.println("No radio available");
}
}//--(end main
temp1 = meteodata[0] / 100.00;
hum1 = meteodata[1] / 100.00;
batPercentage = meteodata[2] / 100.00;
batVoltage = meteodata[3] / 1000.00;
}
Comments
Please log in or sign up to comment.