Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
60
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 DHT11   // 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.  unsigned long last_time=0;
  37.  
  38. void odczytaj()
  39. {
  40.   // Reading temperature or humidity takes about 250 milliseconds!
  41.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  42.   /*
  43.   * tablica odczyty zawiera dane (float) z czterech sensorów
  44.   * odczyty[0] - temperatura
  45.   * odczyty[1] - wilgotność
  46.   * odczyty[2] - czujnik RF, napięcie na wejściu ADC
  47.   * odczyty[3] - fotorezystor, napięcie na wejściu ADC
  48.   */
  49.   odczyty[0] = dht.readTemperature();
  50.   odczyty[1] = dht.readHumidity();
  51.   odczyty[2] = analogRead(A1); //fotorezystor
  52.  
  53.   // check if returns are valid, if they are NaN (not a number) then something went wrong!
  54.   if (isnan(odczyty[0]) || isnan(odczyty[1]))
  55.   {
  56.     //wartość awaryjna
  57.     odczyty[0] = 9999.9;
  58.     odczyty[1] = 9999.9;
  59.   }
  60. }
  61.  
  62. void texas_ranger()
  63. {
  64.   float odczyty_tmp[3];
  65.   odczyty_tmp[0] = odczyty[0];
  66.   odczyty_tmp[1] = odczyty[1];
  67.   odczyty_tmp[2] = odczyty[2]; //powyezej 500 ciemno !!
  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[0]-odczyty[0])>1.0)
  78.   {
  79.     instrukcja = 66;
  80.   }
  81. }
  82.  
  83. void setup()
  84. {
  85.   Serial.begin(9600);
  86.   dht.begin();
  87.  
  88.   // Setup and configure rf radio
  89.   radio.begin();// Start up the radio
  90.   radio.setPALevel(RF24_PA_MAX);
  91.   radio.setDataRate(RF24_1MBPS);
  92.   radio.setAutoAck(1);                    // Ensure autoACK is enabled
  93.   radio.setRetries(15,15);                // Max delay between retries & number of retries
  94.   radio.openWritingPipe(addresses[1]);
  95.   radio.openReadingPipe(1,addresses[0]);
  96.   odczytaj();
  97.   radio.startListening();                 // Start listening
  98.   last_time = millis();
  99. }
  100.  
  101. void loop()
  102. {
  103.     //Serial.println(odczyty[0]);
  104.     //Serial.println(odczyty[1]);
  105.     //Serial.println(odczyty[2]);
  106.     //delay(100);
  107.   texas_ranger();
  108.   //obsługa nRF24
  109.   if (millis()-last_time > 10000UL ) // aktualnie jest ustawione na co 10 sek
  110.   {
  111.     Serial.println(odczyty[0]);
  112.     Serial.println(odczyty[1]);
  113.     Serial.println(odczyty[2]);
  114.     instrukcja = 66;
  115.     last_time = millis();
  116.   }
  117.   if(instrukcja == 66)//wyslij dane
  118.   {
  119.     instrukcja = 0;
  120.     radio.stopListening();
  121.     odczytaj();
  122.     radio.write( &odczyty, 3*sizeof(float) );
  123.     radio.startListening();
  124.    
  125.   }
  126.   while(radio.available())
  127.   {
  128.    radio.read( &instrukcja, sizeof(unsigned int) );
  129.   }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement