Skip to main content
. 2022 Sep 6;22(18):6733. doi: 10.3390/s22186733
Algorithm A2: Source Code RTU 2
  1. /*

  2.  * Created by Pi BOTS MakerHub

  3.  *

  4.  * Email: pibotsmakerhub@gmail.com

  5.  *

  6.  * Github: https://github.com/pibotsmakerhub

  7.  *

  8.  * Join Us on Telegram : https://t.me/pibots

  9.  * Copyright (c) 2020 Pi BOTS MakerHub

  10. */

  11. // REQUIRES the following Arduino libraries:

  12. // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library

  13. // - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

  14. #include "DHT.h"

  15. #define DHTPIN 2  // Digital pin connected to the DHT sensor

  16. #define DHTTYPE DHT11  // DHT 11

  17. DHT dht(DHTPIN, DHTTYPE);

  18. // Configuration of Analog input and outputs

  19. int sensorPin0 = A0;  // select the input pin for the potentiometer

  20. int sensorPin2 = A2;

  21. int sensorPin4 = A4;

  22. int ledPin = 13;  // select the pin for the LED

  23. int sensorValue0 = 0;  // variable to store the value coming from the sensor

  24. int sensorValue2 = 0;  // variable to store the value coming from the sensor

  25. int sensorValue4 = 0;  // variable to store the value coming from the sensor

  26. int Value0 = 0;  // variable to store the value coming from the sensor

  27. int Value2 = 0;  // variable to store the value coming from the sensor

  28. int Value4 = 0;  // variable to store the value coming from the sensor

  29. int Final_Value0 = 0;

  30. int Final_Value2 = 0;

  31. int Final_Value4 = 0;

  32. int i = 0;

  33. const int nodeID = 5;

  34. float h = 0.0;

  35. float t = 0.0;

  36. void setup() {

  37.   Serial.begin(38400);

  38.   //Serial.println(F("DHTxx test!"));

  39.   dht.begin();

  40. }

  41. void loop() {

  42.   // Wait a few seconds between measurements.

  43.   //delay(2);

  44.   h = dht.readHumidity();

  45.   // Read temperature as Celsius (the default)

  46.   t = dht.readTemperature();

  47.   // Check if any reads failed and exit early (to try again).

  48.   if (isnan(h) || isnan(t))

  49.   {

  50.  Serial.println(F("Failed to read from DHT sensor!"));

  51.  return;

  52.   }

  53.   //Serial.print(F(" Humidity: "));

  54.   //Serial.print(h);

  55.   //Serial.print(F("% Temperature: "));

  56.   //Serial.print(t);

  57.   //Serial.print(F("C \n"));

  58.   // read the value from the sensor:

  59.   sensorValue0 = analogRead(sensorPin0);

  60.   sensorValue2 = analogRead(sensorPin2);

  61.   sensorValue4 = analogRead(sensorPin4);

  62.   //Serial.println();

  63.   //Serial.println(sensorValue0);

  64.   //Serial.println(sensorValue2);

  65.   //Serial.println(sensorValue4);

  66.   //Serial.println();

  67.   Value0 = Value0+sensorValue0;

  68.   Value2 = Value2+sensorValue2;

  69.   Value4 = Value4+sensorValue4;

  70.   i = i+1;

  71.  

  72.   if (i==10)

  73.   {

  74.  Final_Value0 = Value0/10;

  75.  Final_Value2 = Value2/10;

  76.  Final_Value4 = Value4/10;

  77.  

  78.  Value0 = 0; // variable to store the value coming from the sensor

  79.  Value2 = 0; // variable to store the value coming from the sensor

  80.  Value4 = 0;

  81.  i = 0;

  82.  

  83.   //Serial.println("print value after 10 iterations");

  84.   //Serial.println(Final_Value0);

  85.   //delay(1000);

  86.   //Serial.println(Final_Value2);

  87.   //delay(1000);

  88.   //Serial.println(Final_Value4);

  89.   //delay(1000);

  90.   //Serial.println("Finish");

  91.   //Serial.println("Finish");

  92.   //Serial.println("Finish");

  93.  }

  94. Serial.print(nodeID); Serial.print(' ');

  95. Serial.print(Final_Value0); Serial.print(" "); // These for compatibilit$

  96. Serial.print(Final_Value2); Serial.print(" ");

  97. Serial.print(Final_Value4); Serial.print(" ");

  98. Serial.print(h); Serial.print(" ");

  99. Serial.print(t); Serial.println();

  100. delay(1000);

  101. }