Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.63 KB | None | 0 0
  1. #include <dht.h>
  2. #include <SPI.h>
  3. #include <Ethernet.h>
  4. #include <IRremote.h>
  5. #define DHT22_PIN 2
  6.  
  7. dht DHT;
  8.  
  9. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  10. byte gateway[] = {192, 168, 1, 1 };
  11. byte subnet[] = {255, 255, 255, 0 };
  12. IPAddress ip(192, 168, 1, 220);
  13.  
  14. float MAX_TEMP = 25.0;
  15. float MIN_TEMP = 22.0;
  16. int MAX_HUMID = 60;
  17. int MIN_HUMID = 40;
  18. int last_condey_status = 0;
  19.  
  20. unsigned int TurnCondeyOff[68]     = {2700,2600,750,700,700,700,700,700,750,700,750,700,750,650,750,700,700,700,750,700,750,650,750,700,700,700,750,700,700,700,700,700,750,700,700,700,750,650,750,700,750,700,700,700,750,650,750,700,700,700,750,650,750,700,750,700,750,700,700,2000,750,2000,750,1950,750,2000,750};
  21. unsigned int TurnCondeyMaxOn[68]   = {2650,2650,750,2000,750,700,700,700,700,700,750,700,700,700,750,650,750,700,750,700,700,700,700,700,750,700,700,700,750,650,750,700,700,700,750,700,700,700,750,700,700,700,750,650,800,700,700,750,700,650,750,700,700,700,750,650,800,650,750,700,700,2000,750,1950,750,2000,750};
  22. unsigned int TurnCondeyWaterOn[68] = {2650,2650,750,2000,750,700,700,700,700,700,750,700,700,700,750,650,750,700,750,700,700,700,700,700,750,700,700,700,750,650,750,700,700,700,750,700,700,700,750,700,700,700,750,650,800,700,700,750,700,650,750,700,700,700,750,650,800,650,750,700,700,2000,750,1950,750,2000,750};
  23. unsigned int TurnCondeyAuto[68]    = {2650,2650,750,2000,750,700,700,700,700,700,750,700,700,700,750,650,750,700,750,700,700,700,700,700,750,700,700,700,750,650,750,700,700,700,750,700,700,700,750,700,700,700,750,650,800,700,700,750,700,650,750,700,700,700,750,650,800,650,750,700,700,2000,750,1950,750,2000,750};
  24. IRsend irsend;
  25.  
  26. int dht_status = 0;
  27. char serverName[] = "myserver.ru";
  28. byte server[] = {192, 168, 1, 5 };
  29.  
  30. EthernetClient client;
  31.  
  32. void setup()
  33. {
  34.   Serial.begin(9600);
  35.   Serial.print("LIBRARY VERSION: ");
  36.   Serial.println(DHT_LIB_VERSION);
  37.   delay(1000);
  38.   Serial.println("Try to configure Ethernet using DHCP...");
  39.   // start the Ethernet connection:  
  40.   if(Ethernet.begin(mac) == 0) {
  41.     Serial.println("Failed to configure Ethernet using DHCP. Using manual config.");
  42.     Ethernet.begin(mac, ip, gateway);
  43.   }
  44.   PrintIPtoSerial();
  45. }
  46.  
  47. void loop()
  48. {
  49.   Serial.println("status,\tHumidity (%),\tTemperature (C)");
  50.   // READ DATA
  51.   int chk = DHT.read22(DHT22_PIN);
  52.   switch (chk)
  53.   {
  54.   case DHTLIB_OK:  
  55.     dht_status = 200;
  56.     Serial.print("OK,\t");
  57.     break;
  58.   case DHTLIB_ERROR_CHECKSUM:
  59.     dht_status = 501;
  60.     Serial.print("Checksum error,\t");
  61.     break;
  62.   case DHTLIB_ERROR_TIMEOUT:
  63.     dht_status = 504;
  64.     Serial.print("Time out error,\t");
  65.     break;
  66.   default:
  67.     dht_status = 500;
  68.     Serial.print("Unknown error,\t");
  69.     break;
  70.   }
  71.   // DISPLAY DATA
  72.   Serial.print(DHT.humidity, 1);
  73.   Serial.print(",\t");
  74.   Serial.println(DHT.temperature, 1);
  75.   SendDataToServer(dht_status, DHT.temperature, DHT.humidity);
  76.   WorkWithCondey(DHT.temperature, DHT.humidity);
  77.   Serial.println();
  78.   delay(1000);
  79. }
  80.  
  81. boolean SendDataToServer(int d_stat, float temp, int humidity) {
  82.   if (client.connect(server, 80)) {
  83.     char buf[80];
  84.     int temp1 = (temp - (int)temp) * 100;
  85.     int humidityl = (humidity - (int)humidity) * 100;
  86.     Serial.println("Sending information to weather server");
  87.     // Make a HTTP request:
  88.     sprintf(buf, "GET /meteo.php?S=%d&T=%0d.%d&H=%0d.%d&CS=%d HTTP/1.1", (int)d_stat, (int)temp, abs(temp1), (int)humidity, abs(humidityl), last_condey_status);
  89.     client.println(buf);
  90.     client.println("Host: myserver.ru");
  91.     client.println("Connection: close");
  92.     client.println();
  93.     client.stop();
  94.     return true;
  95.   }
  96.   else {
  97.     // if you didn't get a connection to the server:
  98.     Serial.println("Connection to weather server failed");
  99.     client.stop();
  100.     return false;
  101.   }
  102. }
  103.  
  104.  
  105. void WorkWithCondey(float temp, int humidity) {
  106.   int status = 0;
  107.  
  108.   if(temp > MAX_TEMP) {
  109.     status=1;
  110.   }
  111.   if(temp < MIN_TEMP) {
  112.     status=0;
  113.   }
  114.  
  115.   if(humidity > MAX_HUMID) {
  116.     status = status+3;
  117.   }
  118.   if(humidity < MIN_HUMID) {
  119.     status = status;
  120.   }
  121.  
  122.   String condey_text_status = "";
  123.  
  124.   if(status != last_condey_status) {
  125.     last_condey_status = status;   
  126.     if(status == 0) {
  127.       irsend.sendRaw(TurnCondeyOff,68,38);
  128.     }
  129.     if(status == 1) {
  130.       irsend.sendRaw(TurnCondeyMaxOn,68,38);
  131.     }
  132.     if(status == 3) {
  133.       irsend.sendRaw(TurnCondeyWaterOn,68,38);
  134.     }
  135.     if(status == 4) {
  136.       irsend.sendRaw(TurnCondeyAuto,68,38);
  137.     }
  138.   }
  139.   Serial.print("Condition status: ");
  140.   Serial.println(status);
  141. }
  142.  
  143.  
  144. void PrintIPtoSerial() {
  145.   Serial.print("My Local IP address: ");
  146.   Serial.println(Ethernet.localIP());
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement