Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Example for reading temperature and humidity
- * using the DHT22, DS18B20 and ESP8266
- *
- */
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266HTTPUpdateServer.h>
- #include <time.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BMP280.h>
- #include "Wire.h"
- #include "DHT.h"
- #include "Adafruit_SI1145.h"
- #define DHTPIN 14 // what digital pin the DHT22 is conected to
- #define DHTTYPE DHT22 // there are multiple kinds of DHT sensors
- #define ONE_WIRE_BUS 2 // DS18B20 on ESP2866 pinX corresponds to D4 on physical board
- #define SDAI2CPIN 4 // GPIO4 I2C SDA bus (D2 psyhical)
- #define SCLI2CPIN 5 // GPIO5 I2C SCL bus (D1 psyhical)
- #define BMP280ADDR 0x76 // Set BMP280 I2C address
- #define SI1145ADDR 0x60 // Set BMP280 I2C address
- DHT dht(DHTPIN, DHTTYPE);
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- Adafruit_BMP280 bmp280; // I2C
- Adafruit_SI1145 uv = Adafruit_SI1145(0x60);
- const char* ssid = "AP_";
- const char* password = "******";
- const char* hostesp = "pws-webupdate";
- const char* update_path = "/firmware";
- const char* update_username = "****";
- const char* update_password = "****";
- const char* host = "****";
- const char* fingerprint = "****";
- #define PASSCODE "****"
- #define SENDDELAY 15
- int triggersend=0;
- int timezone = 1;
- int dst = 0;
- ESP8266WebServer httpServer(80);
- ESP8266HTTPUpdateServer httpUpdater;
- void setup() {
- Serial.println("Device Started");
- Serial.println("-------------------------------------");
- Serial.begin(9600);
- delay(10);
- Serial.print("Initializing BME280: ");
- Wire.begin(SDAI2CPIN, SCLI2CPIN);
- bmp280.begin(BMP280ADDR);
- uv.begin();
- Serial.print("Initializing DHT22: ");
- dht.begin();
- Serial.println();
- Serial.print("Initializing DS18B20: ");
- sensors.begin();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- digitalWrite(2, HIGH);
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- configTime(timezone * 3600, dst, "pool.ntp.org", "time.nist.gov");
- Serial.println("\nWaiting for time");
- while (!time(nullptr)) {
- Serial.print(".");
- delay(1000);
- }
- Serial.println("");
- httpUpdater.setup(&httpServer, update_path, update_username, update_password);
- MDNS.begin(hostesp);
- httpServer.begin();
- MDNS.addService("http", "tcp", 80);
- 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);
- }
- void loop(){
- httpServer.handleClient();
- delay(5000);
- float t = dht.readTemperature(); // Get temperature (in C) from DHT22 sensor
- float h = dht.readHumidity(); // Get humidity (in %) from DHT22 sensor
- float tDS18B20 = sensors.getTempCByIndex(0);
- float pres = bmp280.readPressure();
- float tempBMP = bmp280.readTemperature();
- float SeaPressure;
- Serial.print("Temp: ");
- Serial.print(t);
- Serial.print("C ");
- Serial.print("Hum: ");
- Serial.print(h);
- Serial.println("% ");
- sensors.requestTemperatures();
- Serial.print("TempDS18B20: ");
- Serial.print(tDS18B20);
- //Serial.print(DS18B20.getTempCByIndex(0));
- Serial.println("C");
- Serial.print("TempBMP280: ");
- Serial.print(tempBMP);
- Serial.println("C");
- Serial.print("Pressure: ");
- SeaPressure = (pres / pow(1.0 - 0.0065 * 673 / (tempBMP + 273.15), 5.255))/100 ; // ICAO formula
- Serial.print(SeaPressure);
- Serial.println("hPa");
- Serial.print("Vis: "); Serial.println(uv.readVisible());
- Serial.print("IR: "); Serial.println(uv.readIR());
- float UVindex = uv.readUV();
- UVindex /= 100.0;
- Serial.print("UV: "); Serial.println(UVindex);
- Serial.print("Send in: ");
- Serial.print(SENDDELAY-triggersend);
- Serial.println("secs ");
- triggersend++;
- // After 120 seconds, or 2 minutes, elapse, send the data through ESP8266.
- if(triggersend == SENDDELAY){
- sendData(); // Call function to send data to Thing Speak.
- triggersend = 0;
- }
- }
- void sendData() {
- Serial.print("Connecting to ");
- Serial.println(host);
- // Use WiFiClient class to create TCP connections
- WiFiClientSecure client;
- if (!client.connect(host, 443)) {
- Serial.println("Connection failed");
- return;
- }
- if (client.verify(fingerprint, host)) {
- Serial.println("certificate matches");
- } else {
- Serial.println("certificate doesn't match");
- }
- // We now create a URI for the request
- String url = "/meteo/posielamudaje.php?code=";
- url += PASSCODE;
- url += "&h=";
- url += dht.readHumidity();
- url += "&t=";
- url += dht.readTemperature();
- url += "&p=";
- url += ((bmp280.readPressure() / pow(1.0 - 0.0065 * 673 / (bmp280.readTemperature() + 273.15), 5.255))/100);
- url += "&t1=";
- url += bmp280.readTemperature();
- url += "&t2=";
- url += sensors.getTempCByIndex(0);
- //Serial.print("Requesting URL: ");
- Serial.println(host + url);
- // This will send the request to the server
- client.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "User-Agent: ESP8266/1.0\r\n" +
- "Connection: close\r\n\r\n");
- delay(100);
- // Read all the lines of the reply from server and print them to Serial
- while(client.connected()){
- String line = client.readString();
- //String line = client.readStringUntil('\n');
- Serial.println(line);
- }
- client.stop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement