Basic C++ IOT Weather Station

Hi, I am making an IOT weather station based on a particle photon that sends data via webhook to one of my other projects. I am very new to c++ and programming in general.

What can I do better with my code? What can be optimized? And what are industry best practices that I can put into place?

Here's my code:

#include <Adafruit_DHT/Adafruit_DHT.h>

#define DHTPIN 2 

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
dht.begin();
}

void loop() {

delay(2000);

//Variables
//DHT11 sensor floats
float h = dht.getHumidity();
float t = dht.getTempCelcius();
float f = dht.getTempFarenheit();
float hi = dht.getHeatIndex();
//UV sensor floats
float sensorValue = analogRead(A0);
int UVLevel = 0;

//Error Retry
if (isnan(h) || isnan(t) || isnan(f)) 
{
Serial.println("Failed to read from DHT sensor!");
return;
}

if(sensorValue <=10)
{
     UVLevel = 0;
}
if(sensorValue <= 46 && sensorValue > 10)
{
     UVLevel = 1;
}
if(sensorValue <= 65 && sensorValue > 46)
{
     UVLevel = 2;
}
if(sensorValue <= 83 && sensorValue > 65)
{
     UVLevel = 3;
}
if(sensorValue <= 103 && sensorValue > 83)
{
     UVLevel = 4;
}
if(sensorValue <= 124 && sensorValue > 103)
{
     UVLevel = 5;
}
if(sensorValue <= 142 && sensorValue > 124)
{
     UVLevel = 6;
}
if(sensorValue <= 162 && sensorValue > 142)
{
     UVLevel = 7;
}
if(sensorValue <= 180 && sensorValue > 162)
{
     UVLevel = 8;
}
if(sensorValue <= 200 && sensorValue > 180)
{
     UVLevel = 9;
}
if(sensorValue <= 221 && sensorValue > 200)
{
     UVLevel = 10;
}
if(sensorValue <= 240 && sensorValue > 221)
{
     UVLevel = 11;
}
if(sensorValue > 240)
{
     UVLevel = 12;
}

//Serial Print DHT
Serial.println();
Serial.println();
Serial.print("Humid: ");
Serial.print(h);
Serial.print("%");
Serial.println();
Serial.print("Temp: ");
Serial.print(t);
Serial.print("C ");
Serial.println();
Serial.print("Apparent Temperature: ");
Serial.print(hi);
Serial.println();
Serial.println();

//Serial Print UV
Serial.print("UV Level =");
Serial.print(UVLevel);
Serial.println();

//Publish Data To Particle Cloud
Particle.publish("Humidity", String(h));
Particle.publish("Temperature", String(t));
Particle.publish("Apparent Temperature", String(hi));
Particle.publish("UV Index",String(UVLevel));
delay(5000);
}

For reference these are the sensors I am using:

DHT 11 Temp/Humidity sensor

UV sensor

Suggested good practice:

  1. Set a realistic list of requirements
  2. Research the solutions, libraries, templates, etc.
  3. Develop and test in small steps - make sure something is working correctly before adding another feature.

For Particle devices:

  1. Use the latest device OS (full release)
  2. Use SYSTEM_THREAD(ENABLED).
  3. Avoid using String objects use c-strings instead.
  4. Avoid using delay(); within loop() - you want to keep the loop cadence high - better to use software timers or millis() based time checks to trigger reading sensors on a regular basis.

That’s probably enough for now!

3 Likes

I would add to @armor’s comments that you have four Particle.publish() in a row which is at the limit of the max. publishing rate. I suggest you publish all your data in a single comma delimited string which you can parse at your receiving devices. You can use snprintf() to create the c-string to publish.

3 Likes