Advertisement
Guest User

Untitled

a guest
Mar 15th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. /**
  2. * Example for reading temperature and humidity
  3. * using the DHT22, DS18B20 and ESP8266
  4. *
  5. */
  6.  
  7. #include <ESP8266WiFi.h>
  8. #include <WiFiClientSecure.h>
  9. #include <ESP8266WebServer.h>
  10. #include <ESP8266mDNS.h>
  11. #include <ESP8266HTTPUpdateServer.h>
  12. #include <time.h>
  13. #include <OneWire.h>
  14. #include <DallasTemperature.h>
  15. #include <Adafruit_Sensor.h>
  16. #include <Adafruit_BMP280.h>
  17. #include "Wire.h"
  18. #include "DHT.h"
  19. #include "Adafruit_SI1145.h"
  20.  
  21.  
  22.  
  23. #define DHTPIN 14 // what digital pin the DHT22 is conected to
  24. #define DHTTYPE DHT22 // there are multiple kinds of DHT sensors
  25. #define ONE_WIRE_BUS 2 // DS18B20 on ESP2866 pinX corresponds to D4 on physical board
  26. #define SDAI2CPIN 4 // GPIO4 I2C SDA bus (D2 psyhical)
  27. #define SCLI2CPIN 5 // GPIO5 I2C SCL bus (D1 psyhical)
  28. #define BMP280ADDR 0x76 // Set BMP280 I2C address
  29. #define SI1145ADDR 0x60 // Set BMP280 I2C address
  30.  
  31. DHT dht(DHTPIN, DHTTYPE);
  32. OneWire oneWire(ONE_WIRE_BUS);
  33. DallasTemperature sensors(&oneWire);
  34. Adafruit_BMP280 bmp280; // I2C
  35. Adafruit_SI1145 uv = Adafruit_SI1145(0x60);
  36.  
  37. const char* ssid = "AP_";
  38. const char* password = "******";
  39. const char* hostesp = "pws-webupdate";
  40. const char* update_path = "/firmware";
  41. const char* update_username = "****";
  42. const char* update_password = "****";
  43. const char* host = "****";
  44. const char* fingerprint = "****";
  45. #define PASSCODE "****"
  46. #define SENDDELAY 15
  47. int triggersend=0;
  48. int timezone = 1;
  49. int dst = 0;
  50.  
  51. ESP8266WebServer httpServer(80);
  52. ESP8266HTTPUpdateServer httpUpdater;
  53.  
  54. void setup() {
  55. Serial.println("Device Started");
  56. Serial.println("-------------------------------------");
  57. Serial.begin(9600);
  58. delay(10);
  59. Serial.print("Initializing BME280: ");
  60. Wire.begin(SDAI2CPIN, SCLI2CPIN);
  61. bmp280.begin(BMP280ADDR);
  62. uv.begin();
  63. Serial.print("Initializing DHT22: ");
  64. dht.begin();
  65. Serial.println();
  66. Serial.print("Initializing DS18B20: ");
  67. sensors.begin();
  68. Serial.println();
  69. Serial.print("Connecting to ");
  70. Serial.println(ssid);
  71. WiFi.begin(ssid, password);
  72. while (WiFi.status() != WL_CONNECTED) {
  73. delay(500);
  74. Serial.print(".");
  75. }
  76. Serial.println("");
  77. Serial.println("WiFi connected");
  78. digitalWrite(2, HIGH);
  79. Serial.println("IP address: ");
  80. Serial.println(WiFi.localIP());
  81.  
  82. configTime(timezone * 3600, dst, "pool.ntp.org", "time.nist.gov");
  83. Serial.println("\nWaiting for time");
  84. while (!time(nullptr)) {
  85. Serial.print(".");
  86. delay(1000);
  87. }
  88. Serial.println("");
  89.  
  90. httpUpdater.setup(&httpServer, update_path, update_username, update_password);
  91. MDNS.begin(hostesp);
  92. httpServer.begin();
  93.  
  94. MDNS.addService("http", "tcp", 80);
  95. Serial.printf("HTTPUpdateServer ready! Open http://%s.local%s in your browser and login with username '%s' and password '%s'\n", hostesp, update_path, update_username, update_password);
  96.  
  97. }
  98.  
  99.  
  100. void loop(){
  101. httpServer.handleClient();
  102. delay(5000);
  103.  
  104. float t = dht.readTemperature(); // Get temperature (in C) from DHT22 sensor
  105. float h = dht.readHumidity(); // Get humidity (in %) from DHT22 sensor
  106. float tDS18B20 = sensors.getTempCByIndex(0);
  107. float pres = bmp280.readPressure();
  108. float tempBMP = bmp280.readTemperature();
  109. float SeaPressure;
  110.  
  111. Serial.print("Temp: ");
  112. Serial.print(t);
  113. Serial.print("C ");
  114. Serial.print("Hum: ");
  115. Serial.print(h);
  116. Serial.println("% ");
  117. sensors.requestTemperatures();
  118. Serial.print("TempDS18B20: ");
  119. Serial.print(tDS18B20);
  120. //Serial.print(DS18B20.getTempCByIndex(0));
  121. Serial.println("C");
  122. Serial.print("TempBMP280: ");
  123. Serial.print(tempBMP);
  124. Serial.println("C");
  125. Serial.print("Pressure: ");
  126. SeaPressure = (pres / pow(1.0 - 0.0065 * 673 / (tempBMP + 273.15), 5.255))/100 ; // ICAO formula
  127. Serial.print(SeaPressure);
  128. Serial.println("hPa");
  129. Serial.print("Vis: "); Serial.println(uv.readVisible());
  130. Serial.print("IR: "); Serial.println(uv.readIR());
  131. float UVindex = uv.readUV();
  132. UVindex /= 100.0;
  133. Serial.print("UV: "); Serial.println(UVindex);
  134.  
  135.  
  136. Serial.print("Send in: ");
  137. Serial.print(SENDDELAY-triggersend);
  138. Serial.println("secs ");
  139. triggersend++;
  140.  
  141. // After 120 seconds, or 2 minutes, elapse, send the data through ESP8266.
  142. if(triggersend == SENDDELAY){
  143. sendData(); // Call function to send data to Thing Speak.
  144. triggersend = 0;
  145. }
  146. }
  147.  
  148.  
  149. void sendData() {
  150.  
  151. Serial.print("Connecting to ");
  152. Serial.println(host);
  153.  
  154. // Use WiFiClient class to create TCP connections
  155. WiFiClientSecure client;
  156. if (!client.connect(host, 443)) {
  157. Serial.println("Connection failed");
  158. return;
  159. }
  160.  
  161. if (client.verify(fingerprint, host)) {
  162. Serial.println("certificate matches");
  163. } else {
  164. Serial.println("certificate doesn't match");
  165. }
  166.  
  167. // We now create a URI for the request
  168. String url = "/meteo/posielamudaje.php?code=";
  169. url += PASSCODE;
  170. url += "&h=";
  171. url += dht.readHumidity();
  172. url += "&t=";
  173. url += dht.readTemperature();
  174. url += "&p=";
  175. url += ((bmp280.readPressure() / pow(1.0 - 0.0065 * 673 / (bmp280.readTemperature() + 273.15), 5.255))/100);
  176. url += "&t1=";
  177. url += bmp280.readTemperature();
  178. url += "&t2=";
  179. url += sensors.getTempCByIndex(0);
  180.  
  181. //Serial.print("Requesting URL: ");
  182. Serial.println(host + url);
  183.  
  184.  
  185. // This will send the request to the server
  186. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  187. "Host: " + host + "\r\n" +
  188. "User-Agent: ESP8266/1.0\r\n" +
  189. "Connection: close\r\n\r\n");
  190. delay(100);
  191.  
  192. // Read all the lines of the reply from server and print them to Serial
  193.  
  194. while(client.connected()){
  195. String line = client.readString();
  196. //String line = client.readStringUntil('\n');
  197. Serial.println(line);
  198. }
  199. client.stop();
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement