Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
70
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.  
  6. ///////////////////////////////////
  7. //czujnik temperatury i wilgotnosci
  8. #define DHTPIN 2
  9.  
  10. //#define DHTTYPE DHT11   // DHT 11
  11. #define DHTTYPE DHT22   // DHT 22  (AM2302)
  12. //#define DHTTYPE DHT21   // DHT 21 (AM2301)
  13.  
  14. DHT dht(DHTPIN, DHTTYPE);
  15. //koniec
  16. ///////////////////////////////////
  17.  
  18. ///////////////////////////////////
  19. //radio
  20. RF24 radio(9,10);
  21.  
  22. //adresy kanałów komunikacyjnych
  23. byte addresses[][6] = {"1Node","2Node"};
  24. //koniec
  25. ///////////////////////////////////
  26.  
  27. /*
  28.  * tablica odczyty zawiera dane (float) z czterech sensorów
  29.  * odczyty[0] - temperatura
  30.  * odczyty[1] - wilgotność
  31.  * odczyty[2] - czujnik RF, napięcie na wejściu ADC
  32.  * odczyty[3] - fotorezystor, napięcie na wejściu ADC
  33.  */
  34. float odczyty[4];
  35. unsigned int instrukcja = 0;
  36. void odczytaj()
  37. {
  38.   // Reading temperature or humidity takes about 250 milliseconds!
  39.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  40.   /*
  41.   * tablica odczyty zawiera dane (float) z czterech sensorów
  42.   * odczyty[0] - temperatura
  43.   * odczyty[1] - wilgotność
  44.   * odczyty[2] - czujnik RF, napięcie na wejściu ADC
  45.   * odczyty[3] - fotorezystor, napięcie na wejściu ADC
  46.   */
  47.   odczyty[0] = dht.readTemperature();
  48.   odczyty[1] = dht.readHumidity();
  49.   odczyty[2] = analogRead(A0)*5.0/1024.0; //Wartość napięcia detektora RF/EMF;
  50.   odczyty[3] = analogRead(A1)*5.0/1024.0; //Wartość napięcia fotorezystora;
  51.  
  52.   // check if returns are valid, if they are NaN (not a number) then something went wrong!
  53.   if (isnan(odczyty[0]) || isnan(odczyty[1]))
  54.   {
  55.     //wartość awaryjna
  56.     odczyty[0] = 9999.9;
  57.     odczyty[1] = 9999.9;
  58.   }
  59. }
  60.  
  61. void texas_ranger()
  62. {
  63.   float odczyty_tmp[4];
  64.   odczyty_tmp[0] = odczyty[0];
  65.   odczyty_tmp[1] = odczyty[1];
  66.   odczyty_tmp[2] = odczyty[2];
  67.   odczyty_tmp[3] = odczyty[3];
  68.   odczytaj();
  69.   /*
  70.   * tablica odczyty zawiera dane (float) z czterech sensorów
  71.   * odczyty[0] - temperatura
  72.   * odczyty[1] - wilgotność
  73.   * odczyty[2] - czujnik RF, napięcie na wejściu ADC
  74.   * odczyty[3] - fotorezystor, napięcie na wejściu ADC
  75.   */
  76.   //warunki wysłania komunikatu do matki (odebranie int = 66)
  77.   if((odczyty_tmp[3]-odczyty[3])>1.0)
  78.   {
  79.     instrukcja = 66;
  80.   }
  81. }
  82.  
  83. void setup()
  84. {
  85.   dht.begin();
  86.  
  87.   // Setup and configure rf radio
  88.   radio.begin();// Start up the radio
  89.   radio.setPALevel(RF24_PA_MAX);
  90.   radio.setDataRate(RF24_1MBPS);
  91.   radio.setAutoAck(1);                    // Ensure autoACK is enabled
  92.   radio.setRetries(15,15);                // Max delay between retries & number of retries
  93.   radio.openWritingPipe(addresses[1]);
  94.   radio.openReadingPipe(1,addresses[0]);
  95.   odczytaj();
  96.   radio.startListening();                 // Start listening
  97. }
  98.  
  99. void loop()
  100. {
  101.   texas_ranger();
  102.   //obsługa nRF24
  103.   if(instrukcja == 66)//wyslij dane
  104.   {
  105.     instrukcja = 0;
  106.     radio.stopListening();
  107.     odczytaj();
  108.     radio.write( &odczyty, 4*sizeof(float) );
  109.     radio.startListening();
  110.   }
  111.   while(radio.available())
  112.   {
  113.    radio.read( &instrukcja, sizeof(unsigned int) );
  114.   }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement