Basic C++ IOT Weather Station

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
4
down vote
favorite
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 optimised? 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)
For reference these are the sensors I am using:
DHT 11 Temp/Humidity sensor
UV sensor
c++ arduino
add a comment |Â
up vote
4
down vote
favorite
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 optimised? 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)
For reference these are the sensors I am using:
DHT 11 Temp/Humidity sensor
UV sensor
c++ arduino
1
Indentation seems off, is this on purpose or a mistake from formatting?
â yuri
May 14 at 16:13
The code is as is in my editor. Now you mention it, it does look odd. @yuri
â Xander
May 14 at 16:28
1
@Xander Replace TAB characters by four spaces first before copying your code into the question.
â ÃÂìýÃÂñ á¿¥Ã栨Â
May 14 at 16:46
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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 optimised? 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)
For reference these are the sensors I am using:
DHT 11 Temp/Humidity sensor
UV sensor
c++ arduino
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 optimised? 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)
For reference these are the sensors I am using:
DHT 11 Temp/Humidity sensor
UV sensor
c++ arduino
edited May 14 at 16:18
Sam Onela
5,77461543
5,77461543
asked May 14 at 16:08
Xander
385
385
1
Indentation seems off, is this on purpose or a mistake from formatting?
â yuri
May 14 at 16:13
The code is as is in my editor. Now you mention it, it does look odd. @yuri
â Xander
May 14 at 16:28
1
@Xander Replace TAB characters by four spaces first before copying your code into the question.
â ÃÂìýÃÂñ á¿¥Ã栨Â
May 14 at 16:46
add a comment |Â
1
Indentation seems off, is this on purpose or a mistake from formatting?
â yuri
May 14 at 16:13
The code is as is in my editor. Now you mention it, it does look odd. @yuri
â Xander
May 14 at 16:28
1
@Xander Replace TAB characters by four spaces first before copying your code into the question.
â ÃÂìýÃÂñ á¿¥Ã栨Â
May 14 at 16:46
1
1
Indentation seems off, is this on purpose or a mistake from formatting?
â yuri
May 14 at 16:13
Indentation seems off, is this on purpose or a mistake from formatting?
â yuri
May 14 at 16:13
The code is as is in my editor. Now you mention it, it does look odd. @yuri
â Xander
May 14 at 16:28
The code is as is in my editor. Now you mention it, it does look odd. @yuri
â Xander
May 14 at 16:28
1
1
@Xander Replace TAB characters by four spaces first before copying your code into the question.
â ÃÂìýÃÂñ á¿¥Ã栨Â
May 14 at 16:46
@Xander Replace TAB characters by four spaces first before copying your code into the question.
â ÃÂìýÃÂñ á¿¥Ã栨Â
May 14 at 16:46
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
6
down vote
accepted
I'll assume the indentation is an artifact of pasting the code here.
The big problem here is that long series of if statements. A simple improvement is to make use of the else clause, along with knowledge of what values you've tested for already, it can be simplified to
if (sensorValue <= 10)
UVLevel = 0;
else if (sensorValue <= 46)
UVLevel = 1;
// etc.
But we can do better. Since the range of values you're checking with is contiguous, and the result (UVLevel) is linear, we can set up an array to hold the data, then just use a loop to find the right place.
static values = 10, 46, 65, /* ... */, 240 ;
UVLevel = std::size(values); // 12
for (int i = 0; i < std::size(values); ++i)
if (values[i] <= sensorValue)
UVLevel = i;
break;
}
We initially set UVLevel the the maximum value so that if the sensor value is greater than 240 we get the correct result without having to check after the loop.
For a larger range of possible values, the for loop used here can be replaced with a more complicated binary search.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
I'll assume the indentation is an artifact of pasting the code here.
The big problem here is that long series of if statements. A simple improvement is to make use of the else clause, along with knowledge of what values you've tested for already, it can be simplified to
if (sensorValue <= 10)
UVLevel = 0;
else if (sensorValue <= 46)
UVLevel = 1;
// etc.
But we can do better. Since the range of values you're checking with is contiguous, and the result (UVLevel) is linear, we can set up an array to hold the data, then just use a loop to find the right place.
static values = 10, 46, 65, /* ... */, 240 ;
UVLevel = std::size(values); // 12
for (int i = 0; i < std::size(values); ++i)
if (values[i] <= sensorValue)
UVLevel = i;
break;
}
We initially set UVLevel the the maximum value so that if the sensor value is greater than 240 we get the correct result without having to check after the loop.
For a larger range of possible values, the for loop used here can be replaced with a more complicated binary search.
add a comment |Â
up vote
6
down vote
accepted
I'll assume the indentation is an artifact of pasting the code here.
The big problem here is that long series of if statements. A simple improvement is to make use of the else clause, along with knowledge of what values you've tested for already, it can be simplified to
if (sensorValue <= 10)
UVLevel = 0;
else if (sensorValue <= 46)
UVLevel = 1;
// etc.
But we can do better. Since the range of values you're checking with is contiguous, and the result (UVLevel) is linear, we can set up an array to hold the data, then just use a loop to find the right place.
static values = 10, 46, 65, /* ... */, 240 ;
UVLevel = std::size(values); // 12
for (int i = 0; i < std::size(values); ++i)
if (values[i] <= sensorValue)
UVLevel = i;
break;
}
We initially set UVLevel the the maximum value so that if the sensor value is greater than 240 we get the correct result without having to check after the loop.
For a larger range of possible values, the for loop used here can be replaced with a more complicated binary search.
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
I'll assume the indentation is an artifact of pasting the code here.
The big problem here is that long series of if statements. A simple improvement is to make use of the else clause, along with knowledge of what values you've tested for already, it can be simplified to
if (sensorValue <= 10)
UVLevel = 0;
else if (sensorValue <= 46)
UVLevel = 1;
// etc.
But we can do better. Since the range of values you're checking with is contiguous, and the result (UVLevel) is linear, we can set up an array to hold the data, then just use a loop to find the right place.
static values = 10, 46, 65, /* ... */, 240 ;
UVLevel = std::size(values); // 12
for (int i = 0; i < std::size(values); ++i)
if (values[i] <= sensorValue)
UVLevel = i;
break;
}
We initially set UVLevel the the maximum value so that if the sensor value is greater than 240 we get the correct result without having to check after the loop.
For a larger range of possible values, the for loop used here can be replaced with a more complicated binary search.
I'll assume the indentation is an artifact of pasting the code here.
The big problem here is that long series of if statements. A simple improvement is to make use of the else clause, along with knowledge of what values you've tested for already, it can be simplified to
if (sensorValue <= 10)
UVLevel = 0;
else if (sensorValue <= 46)
UVLevel = 1;
// etc.
But we can do better. Since the range of values you're checking with is contiguous, and the result (UVLevel) is linear, we can set up an array to hold the data, then just use a loop to find the right place.
static values = 10, 46, 65, /* ... */, 240 ;
UVLevel = std::size(values); // 12
for (int i = 0; i < std::size(values); ++i)
if (values[i] <= sensorValue)
UVLevel = i;
break;
}
We initially set UVLevel the the maximum value so that if the sensor value is greater than 240 we get the correct result without having to check after the loop.
For a larger range of possible values, the for loop used here can be replaced with a more complicated binary search.
answered May 14 at 17:23
1201ProgramAlarm
2,4752618
2,4752618
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194373%2fbasic-c-iot-weather-station%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Indentation seems off, is this on purpose or a mistake from formatting?
â yuri
May 14 at 16:13
The code is as is in my editor. Now you mention it, it does look odd. @yuri
â Xander
May 14 at 16:28
1
@Xander Replace TAB characters by four spaces first before copying your code into the question.
â ÃÂìýÃÂñ á¿¥Ã栨Â
May 14 at 16:46