Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
45
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, tutaj identyfikator 1.0
  36.  */
  37. float odczyty[5];
  38. unsigned int instrukcja = 0;
  39. unsigned long last_time=0;
  40. String data;
  41.  
  42. void odczytaj()
  43. {
  44.   // Reading temperature or humidity takes about 250 milliseconds!
  45.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  46.   /*
  47.   * tablica odczyty zawiera dane (float) z czterech sensorów
  48.   * odczyty[0] - temperatura
  49.   * odczyty[1] - wilgotność
  50.   * odczyty[2] - fotorezystor, napięcie na wejściu ADC
  51.   * odczyty[3] - czujnik RF, napięcie na wejściu ADC
  52.   * odczyty[4] - zmienna room, tutaj identyfikator 1.0
  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] = 999.9;
  63.     odczyty[1] = 999.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.  * odczyty[4] - zmienna room, tutaj identyfikator 1.0
  83.  */
  84.   //warunki wysłania komunikatu do matki (odebranie int = 66)
  85.   if((odczyty_tmp[2]-odczyty[2])>1.0)
  86.   {
  87.     instrukcja = 66;
  88.   }
  89. }
  90.  
  91. void parser()
  92. {
  93.  data = "";
  94.  data += String(odczyty[0]);
  95.  data += ";";
  96.  data += String(odczyty[1]);
  97.  data += ";";
  98.  data += String(odczyty[2]);
  99.  data += ";";
  100.  data += String(odczyty[3]);
  101.  data += ";";
  102.  data += String(odczyty[4]);
  103.  data += ";";
  104. }
  105.  
  106. void setup()
  107. {
  108.   Serial.begin(9600);
  109.   dht.begin();
  110.   odczyty[4] = 1.0;
  111.   // Setup and configure rf radio
  112.   radio.begin();// Start up the radio
  113.   radio.setPALevel(RF24_PA_MAX);
  114.   radio.setDataRate(RF24_1MBPS);
  115.   radio.setAutoAck(1);                    // Ensure autoACK is enabled
  116.   radio.setRetries(15,15);                // Max delay between retries & number of retries
  117.   radio.openWritingPipe(addresses[1]);
  118.   radio.openReadingPipe(1,addresses[0]);
  119.   odczytaj();
  120.   radio.startListening();                 // Start listening
  121.   last_time = millis();
  122. }
  123.  
  124. void loop()
  125. {
  126.   texas_ranger();
  127.   //obsługa nRF24
  128.   if (millis()-last_time > 20000UL ) // aktualnie jest ustawione na co 20 sek
  129.   {
  130.     Serial.println(data);
  131.     instrukcja = 66;
  132.     last_time = millis();
  133.   }
  134.   if(instrukcja == 66)//wyslij dane
  135.   {
  136.     instrukcja = 0;
  137.     radio.stopListening();
  138.     odczytaj();
  139.     radio.write( &data, 32*sizeof(char) );
  140.     radio.startListening();
  141.   }
  142.   while(radio.available())
  143.   {
  144.    radio.read( &instrukcja, sizeof(unsigned int) );
  145.   }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement