Advertisement
Al_Ninyo

DHT11_DS18B20_BMP180_LCD5110_ESP8266_Nano_v2

May 4th, 2015
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.02 KB | None | 0 0
  1. // LCD pins
  2.   // RST -> 6
  3.   // CE -> 7
  4.   // DC -> 5
  5.   // Din -> 4
  6.   // Clk -> 3
  7.   // VCC & Bl -> 3.3V
  8.  
  9. // DHTPIN 12
  10.   // Dallas pin 2
  11.  
  12. // ESP8266
  13.   // VCC & CH_PH -> 3.3V
  14.   // GND -> GND
  15.   // TX -> 0 (RX)
  16.   // RX -> 1 (TX)
  17.  
  18. // BMP180
  19.   // GND -> GND
  20.   // VIN -> 3.3V
  21.   // SDA -> A4
  22.   // SCL -> A5
  23.  
  24. // Подключаем датчик температуры и влажности DHT11
  25. #include "DHT.h"
  26. #define DHTPIN 12  // указываем пин подключения DHT11
  27. #define DHTTYPE DHT11
  28.  
  29. // Подключаем датчик атмосферного давления BMP180
  30. #include <Wire.h>
  31. #include <Adafruit_BMP085.h>
  32.  
  33. // Подключаем температурный датчик DS18B20
  34. #include <OneWire.h>
  35. #include <DallasTemperature.h>
  36. // Указываем пин подключения DS18B20
  37. #define ONE_WIRE_BUS 2
  38. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  39. OneWire oneWire(ONE_WIRE_BUS);
  40. // Pass our oneWire reference to Dallas Temperature.
  41. DallasTemperature sensors(&oneWire);
  42. // arrays to hold device address
  43. DeviceAddress insideThermometer;
  44.  
  45. // Подключаем LCD Nokia 5110
  46. #include <PCD8544.h>
  47. static PCD8544 lcd;
  48.  
  49. // Wi-Fi ini start
  50. #define SSID       "Al_HOME"
  51. #define PASSWORD   "08112004"
  52.  
  53. //#define server      "93.81.252.64"    //test.alninyo.ru
  54. #define server      "195.208.1.147"    //test.sc13.ru
  55.  
  56. #include "uartWIFI.h"
  57. #include <SoftwareSerial.h>
  58. WIFI wifi;
  59.  
  60. // Wi-Fi ini end
  61.  
  62. Adafruit_BMP085 bmp;
  63. DHT dht(DHTPIN, DHTTYPE);
  64.  
  65. int tempDS = 0;   // температура от DS18B20
  66. int lastTempDS = 0;
  67. int tempBMP = 0;// температура от BMP180
  68. int lastTempBMP = 0;
  69. int pressure = 0;   // атмосферное давление
  70. int lastPressure = 0;
  71. int h = 0;          // влажность
  72. int lastH = 0;
  73. int t = 0;        // температура от DHT11
  74. int lastT = 0;
  75. int i = 0;        // счётчик 1
  76. unsigned long count = 0; // счётчик 2
  77.  
  78. const unsigned long checkTime = 10000;  // интервал опроса датчиков
  79. unsigned long lastCheckTime = 0;  // переменная для хранения
  80.                                   // времени последнего опроса датчиков
  81. unsigned long lastConnectionTime = 0;
  82. const unsigned long postingInterval = 60000;
  83.  
  84. void MainLoop() {
  85.    sensors.requestTemperatures();
  86.    tempDS = sensors.getTempC(insideThermometer);
  87.    tempBMP = bmp.readTemperature();
  88.    pressure = bmp.readPressure()/133.3;
  89.    h = dht.readHumidity();
  90.    t = dht.readTemperature();
  91.    if ((lastTempDS != tempDS) ||
  92.           (lastT != t) ||
  93.           (lastTempBMP != tempBMP) ||
  94.           (lastPressure != pressure) ||
  95.           (lastH != h)){
  96.               lastTempDS = tempDS;
  97.               lastT = t;
  98.               lastTempBMP = tempBMP;
  99.               lastH = h;
  100.               lastPressure = pressure;
  101.               Serial.print(lastTempDS);
  102.               Serial.print(", ");
  103.               Serial.print(lastT);
  104.               Serial.print(", ");
  105.               Serial.print(lastTempBMP);
  106.               Serial.print(", ");
  107.               Serial.print(lastH);
  108.               Serial.print(", ");
  109.               Serial.print(lastPressure);
  110.               Serial.print(", ");
  111.               Serial.println(millis());
  112.           }
  113.     lcd.setCursor(0,0);
  114.     lcd.print("Темп. DS/DH/BM");
  115.     lcd.setCursor(0,1);
  116.     lcd.print(lastTempDS);
  117.     lcd.print("/");
  118.     lcd.print(lastT);
  119.     lcd.print("/");
  120.     lcd.print(lastTempBMP);
  121.     lcd.setCursor(0,2);
  122.     lcd.print("Влажность ");
  123.     lcd.print(lastH);
  124.     lcd.print(" %");
  125.     lcd.setCursor(0,3);
  126.     lcd.print("Давление ");
  127.     lcd.setCursor(0,4);
  128.     lcd.print(lastPressure);
  129.     lcd.print(" мм рт.ст.");
  130. }
  131.  
  132. void setup() {
  133.   Serial.begin(9600);
  134.   lcd.begin(84, 48);
  135.   lcd.setCursor(0,0);
  136.   lcd.print("Включаю BMP180");
  137.   if (!bmp.begin()) {
  138.     Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  139.         lcd.setCursor(0,0);
  140.         lcd.print("Проблема с BMP180!");
  141.     while (1) {}
  142.   }
  143.   delay(1000);
  144.   lcd.setCursor(0,0);
  145.   lcd.print("Включаю DS18b20");
  146.   delay(1000);
  147.   sensors.begin();
  148.   lcd.setCursor(0,0);
  149.   lcd.print("Включаю DHT11");
  150.   delay(1000);
  151.   dht.begin();
  152.   if (!sensors.getAddress(insideThermometer, 0)) {
  153.   Serial.println("Unable to find address for Device 0");
  154.   lcd.setCursor(0,1);
  155.   lcd.print("Проблема с DS18B20!");
  156.   }
  157.   // set the resolution to 9 bit
  158.   sensors.setResolution(insideThermometer, 9);
  159.   lcd.setCursor(0,0);
  160.   lcd.print(" Погода в доме");
  161.   lcd.setCursor(0,2);
  162.   lcd.print("   Версия 3   ");
  163.   lcd.setCursor(0,4);
  164.   lcd.print("   Загрузка   ");
  165.  
  166.   wifi.begin();
  167.   bool b = wifi.Initialize(STA, SSID, PASSWORD);
  168.   if(!b)
  169.   {
  170.     Serial.println("Init error");  
  171.   }
  172.   delay(8000);
  173.   String ipstring  = wifi.showIP();
  174.   Serial.println(ipstring);
  175. }
  176.  
  177. void loop() {
  178.   if (lastCheckTime == 0 && count == 0) {
  179.     MainLoop();
  180.   }
  181.   if ((millis()-lastCheckTime)>checkTime){
  182.     lastCheckTime = millis();
  183.       if (i == 6){
  184.         lcd.clear();
  185.         i = 0;
  186.       }
  187.     MainLoop();
  188.     i++;
  189.   }
  190.   // Wi-Fi start
  191.  
  192. char message[320];
  193.    // if you're not connected, and ten seconds have passed since
  194.   // your last connection, then connect again and send data:
  195.   if((millis() - lastConnectionTime > postingInterval) && (lastTempDS != tempDS || lastT != t
  196.                  || lastTempBMP != tempBMP || lastH != h || lastPressure != pressure)) {
  197.  
  198. //  String getRequest = "GET http://test.alninyo.ru/test.php?tIn=";
  199.   String getRequest = "GET http://test.sc13.ru/test.php?tIn=";
  200.     getRequest+= lastTempDS;
  201.     getRequest+= "&tOut=";
  202.     getRequest+= lastTempBMP;
  203.     getRequest+= "&h=";
  204.     getRequest+= lastH;
  205.     getRequest+= "&t=";
  206.     getRequest+= lastT;
  207.     getRequest+= "&pres=";
  208.     getRequest+= lastPressure;
  209.     getRequest+= "&c=08112004";
  210.     getRequest+= " HTTP/1.0\r\n\r\n";
  211.    
  212.     // if there's a successful connection:
  213.   if (wifi.ipConfig(TCP,server, 80)) {
  214.     Serial.println("connecting...");
  215.        
  216.     // send the HTTP PUT request:
  217.    
  218.     //wifi.Send("GET / HTTP/1.0\r\n\r\n");
  219.     wifi.Send(getRequest);
  220. //    Serial.println(getRequest);
  221.     // note the time that the connection was made:
  222.     lastConnectionTime = millis();
  223.     Serial.print("lastConnectionTime = ");
  224.     Serial.println(lastConnectionTime);
  225.     }
  226.   else {
  227.     // if you couldn't make a connection:
  228.     Serial.println("connection failed");
  229.     Serial.println("disconnecting.");
  230.     wifi.closeMux();
  231.     }  
  232.   }
  233.   // if there's incoming data from the net connection.
  234.   // send it out the serial port.  This is for debugging
  235.   // purposes only:
  236.   if(wifi.ReceiveMessage(message))
  237.   {
  238.       Serial.println(message);
  239.       wifi.closeMux();
  240.     count++;
  241.     lcd.setCursor(0,5);
  242.     lcd.print("Ушло ");
  243.     lcd.print(count);
  244.     lcd.print(" раз");  
  245.   }
  246.  
  247. // Wi-Fi end
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement