Basic C++ IOT Weather Station

The name of the pictureThe name of the pictureThe name of the pictureClash 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







share|improve this question

















  • 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
















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







share|improve this question

















  • 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












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







share|improve this question













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









share|improve this question












share|improve this question




share|improve this question








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












  • 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










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.






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer













        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.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 14 at 17:23









        1201ProgramAlarm

        2,4752618




        2,4752618






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Chat program with C++ and SFML

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            Will my employers contract hold up in court?