Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "DHT.h"
  2. #include "nRF24L01.h" //NRF24L01 library created by TMRh20 https://github.com/TMRh20/RF24
  3. #include "RF24.h"
  4. #include "SPI.h"
  5. #include "string.h"
  6.  
  7.  
  8. ///////////////////////////////////
  9. //czujnik temperatury i wilgotnosci
  10. #define DHTPIN 2
  11.  
  12. //#define DHTTYPE DHT11   // DHT 11
  13. #define DHTTYPE DHT22   // DHT 22  (AM2302)
  14. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
  15.  
  16. DHT dht(DHTPIN, DHTTYPE);
  17. //koniec
  18. ///////////////////////////////////
  19.  
  20. ///////////////////////////////////
  21. //radio
  22. RF24 radio(9,10);
  23.  
  24. //adresy kanałów komunikacyjnych
  25. byte addresses[][6] = {"1Node","2Node"};
  26. //koniec
  27. ///////////////////////////////////
  28.  
  29. /*
  30.  * tablica odczyty zawiera dane (float) z czterech sensorów
  31.  * odczyty[0] - temperatura
  32.  * odczyty[1] - wilgotność
  33.  * odczyty[3] - czujnik RF, napięcie na wejściu ADC
  34.  * odczyty[2] - fotorezystor, napięcie na wejściu ADC
  35.  * odczyty[4] - zmienna \room/ u mnie 1
  36.  */
  37. float odczyty[5];
  38. unsigned int instrukcja = 0;
  39. unsigned long last_time=0;
  40. bool light;
  41. String data;
  42.  
  43. void odczytaj()
  44. {
  45.   // Reading temperature or humidity takes about 250 milliseconds!
  46.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  47.   /*
  48.   * tablica odczyty zawiera dane (float) z czterech sensorów
  49.   * odczyty[0] - temperatura
  50.   * odczyty[1] - wilgotność
  51.   * odczyty[3] - czujnik RF, napięcie na wejściu ADC
  52.   * odczyty[2] - fotorezystor, napięcie na wejściu ADC
  53.   */
  54.  odczyty[0] = dht.readTemperature();
  55.   odczyty[1] = dht.readHumidity();
  56.   odczyty[3] = analogRead(A0)*5.0/1024.0; //Wartość napięcia detektora RF/EMF;
  57.   odczyty[2] = analogRead(A1)*5.0/1024.0; //Wartość napięcia fotorezystora;
  58.   // check if returns are valid, if they are NaN (not a number) then something went wrong!
  59.   if (isnan(odczyty[0]) || isnan(odczyty[1]))
  60.   {
  61.     //wartość awaryjna
  62.     odczyty[0] = 9999.9;
  63.     odczyty[1] = 9999.9;
  64.   }
  65.   parser();
  66. }
  67.  
  68. void texas_ranger()
  69. {
  70.   float odczyty_tmp[4];
  71.   odczyty_tmp[0] = odczyty[0];
  72.   odczyty_tmp[1] = odczyty[1];
  73.   odczyty_tmp[3] = odczyty[3];
  74.   odczyty_tmp[2] = odczyty[2];
  75.   odczytaj();
  76.   /*
  77.   * tablica odczyty zawiera dane (float) z czterech sensorów
  78.   * odczyty[0] - temperatura
  79.   * odczyty[1] - wilgotność
  80.   * odczyty[3] - czujnik RF, napięcie na wejściu ADC
  81.   * odczyty[2] - fotorezystor, napięcie na wejściu ADC
  82.   */
  83.   //warunki wysłania komunikatu do matki (odebranie int = 66)
  84.   if((odczyty_tmp[2]-odczyty[2])>1.0)
  85.   {
  86.     instrukcja = 66;
  87.   }
  88. }
  89.  
  90. void parser()
  91. {
  92.  data = "";
  93.  data += String(odczyty[0]);
  94.  data += ";";
  95.  data += String(odczyty[1]);
  96.  data += ";";
  97.  data += String(odczyty[2]);
  98.  data += ";";
  99.  data += String(odczyty[3]);
  100.  data += ";";
  101.  data += String(odczyty[4]);
  102.  data += ";";
  103. }
  104.  
  105. void binary_light()
  106. {
  107.  if (odczyty[2] < 2.5)
  108.  {
  109.    light = true;
  110.  }
  111.  else
  112.  {
  113.    light = false;
  114.  }
  115. }
  116.  
  117. void setup()
  118. {
  119.   Serial.begin(9600);
  120.   dht.begin();
  121.   odczyty[4] = 1.0;
  122.   // Setup and configure rf radio
  123.   radio.begin();// Start up the radio
  124.   radio.setPALevel(RF24_PA_MAX);
  125.   radio.setDataRate(RF24_1MBPS);
  126.   radio.setAutoAck(1);                    // Ensure autoACK is enabled
  127.   radio.setRetries(15,15);                // Max delay between retries & number of retries
  128.   radio.openWritingPipe(addresses[1]);
  129.   radio.openReadingPipe(1,addresses[0]);
  130.   odczytaj();
  131.   radio.startListening();                 // Start listening
  132.   last_time = millis();
  133. }
  134.  
  135. void loop()
  136. {
  137.     //Serial.println(odczyty[0]);
  138.     //Serial.println(odczyty[1]);
  139.     //Serial.println(odczyty[3]);
  140.     //delay(100);
  141.   texas_ranger();
  142.   //obsługa nRF24
  143.   if (millis()-last_time > 20000UL ) // aktualnie jest ustawione na co 10 sek
  144.   {
  145.     Serial.println(data);
  146.     //Serial.println(data.length());
  147.     //Serial.println(odczyty[3]);
  148.     //Serial.println(odczyty[2]);
  149.  
  150.     instrukcja = 66;
  151.     last_time = millis();
  152.   }
  153.   if(instrukcja == 66)//wyslij dane
  154.   {
  155.     instrukcja = 0;
  156.     radio.stopListening();
  157.     odczytaj();
  158.     radio.write( &data, 32*sizeof(char) );
  159.     radio.startListening();
  160.    
  161.   }
  162.   while(radio.available())
  163.   {
  164.    radio.read( &instrukcja, sizeof(unsigned int) );
  165.   }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement