#include <SoftwareSerial.h>
#include <dht.h>
#include <Adafruit_BME280.h>
#include <Wire.h>
// DHT22 variables
dht DHT;
#define DHT22_PIN 3
float temp_DHT_1;
float RH_DHT_1;
// BME280 variables
Adafruit_BME280 bme;
float temp_BME;
float RH_BME;
float altimeter;
// Renkeer Anemometer variables
const byte anemometer[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B};
byte values[9];
SoftwareSerial mod(4, 5);
#define txEnable_mod 6
float Wind_Speed_kph;
float windspeed_kts;
int heading;
// Serial communication
#define txEnable 2
int address = 302;
char data[64] = {0};
int y100;
int y200;
int packet1 = 201;
int packet2 = 202;
int packet3 = 203;
int packet4 = 204;
int packet5 = 205;
int packet6 = 206;
int comRelease = 207;
float tempAvg;
float RHAvg;
// Misc global variables
int w;
int x;
void setup() {
Serial.begin(19200);
mod.begin(4800);
pinMode(txEnable_mod, OUTPUT);
delay(1000);
bme.begin(0x77);
pinMode(txEnable, OUTPUT);
w = 1;
x = 0;
}
void loop() {
// temperature, humidity, and barometric pressure
while(w == 1) {
int chk = DHT.read22(DHT22_PIN);
temp_DHT_1 = (DHT.temperature) * 9/5 + 32;
RH_DHT_1 = DHT.humidity;
temp_BME = bme.readTemperature() * 9/5 + 32;
RH_BME = bme.readHumidity();
altimeter = (bme.readPressure() / 3386.39);
tempAvg = ((temp_DHT_1 + temp_BME) / 2.0);
RHAvg = ((RH_DHT_1 + RH_BME) / 2.0);
delay(20);
x = 0;
w = 2;
}
// wind speed and heading from Renkeer anemometer
while(w == 2) {
// Transmit the request to the sensor
digitalWrite(txEnable_mod, HIGH);
delay(10);
mod.write(anemometer, sizeof(anemometer));
digitalWrite(txEnable_mod, LOW);
delay(10);
// Wait until we have the expected number of bytes or timeout
unsigned long startTime = millis();
while (mod.available() < 9 && millis() - startTime < 2000) {
delay(1);
}
if (mod.available() >= 9) {
// Read the response
byte index = 0;
while (mod.available() && index < 9) {
values[index] = mod.read();
index++;
}
// Parse the Wind Speed value
int Wind_Speed_Int = int(values[3] << 8 | values[4]);
float Wind_Speed_m_s = Wind_Speed_Int / 100.0;
Wind_Speed_kph = Wind_Speed_m_s * 3.6; // Conversion to km/h
windspeed_kts = Wind_Speed_kph / 1.852;
// Parse the Wind Direction value
heading = int(values[5] << 8 | values[6]);
}
delay(20);
w = 3;
}
// serial communication
while(w == 3) {
byte n = Serial.available();
if (n != 0) {
byte m = Serial.readBytesUntil('\n', data, 64);
data[m] = '\0'; //null-byte
y100 = atoi(strtok(data, ","));
y200 = atoi(strtok(NULL, ","));
if(y100 == address) {
if(y200 == 0) {
}
if(y200 == packet1) {
delay(200);
digitalWrite(txEnable, HIGH);
Serial.print(address);
Serial.print(",");
Serial.print(packet1);
Serial.print(",");
Serial.print(tempAvg);
Serial.print(",");
Serial.print(RHAvg);
Serial.print(",");
Serial.println(0);
digitalWrite(txEnable, LOW);
delay(50);
y200 = 0;
}
if(y200 == packet2) {
delay(200);
digitalWrite(txEnable, HIGH);
Serial.print(address);
Serial.print(",");
Serial.print(packet2);
Serial.print(",");
Serial.print(0);
Serial.print(",");
Serial.print(0);
Serial.print(",");
Serial.println(0);
digitalWrite(txEnable, LOW);
delay(50);
y200 = 0;
}
if(y200 == packet3) {
delay(200);
digitalWrite(txEnable, HIGH);
Serial.print(address);
Serial.print(",");
Serial.print(packet3);
Serial.print(",");
Serial.print(altimeter);
Serial.print(",");
Serial.print(heading);
Serial.print(",");
Serial.println(windspeed_kts);
digitalWrite(txEnable, LOW);
delay(50);
y200 = 0;
}
if(y200 == packet4) {
delay(200);
digitalWrite(txEnable, HIGH);
Serial.print(address);
Serial.print(",");
Serial.print(packet4);
Serial.print(",");
Serial.print(0);
Serial.print(",");
Serial.print(0);
Serial.print(",");
Serial.println(0);
digitalWrite(txEnable, LOW);
delay(50);
y200 = 0;
}
if(y200 == packet5) {
delay(200);
digitalWrite(txEnable, HIGH);
Serial.print(address);
Serial.print(",");
Serial.println(comRelease);
digitalWrite(txEnable, LOW);
delay(50);
y200 = 0;
}
}
}
else {
delay(20);
w = 1;
}
}
}