Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
7.9 kB
18
Indexable
Never
#include "DHT.h"
#include <SFE_BMP180.h>
#include <Wire.h>
#include <ESP8266WiFi.h>

#define DHTTYPE DHT22
#define DHTPIN 2
#define ALTITUDE 62.0 // Altitude of Gubin

DHT dht(DHTPIN, DHTTYPE);
SFE_BMP180 pressure;

string apiKey         = "JUSTAPLACEHOLDER";

const char* ssid      = "ssid";
const char* password  = "haslo";
const char* host      = "184.106.153.149";

void setup() {

  pinMode(12, OUTPUT);  // G LED
  pinMode(15, OUTPUT);  // R LED
  pinMode(13, OUTPUT);  // B LED
  
  Serial.begin(115200); //  <-- inicjalizacja konsoli szeregowej
  
  dht.begin();          //  <-- inicjalizacja czujnika DHT
  if (pressure.begin()) //  <-- inicjalizacja czujnika BMP
    Serial.println("BMP180 init success");
  else
  {
    Serial.println("BMP180 init fail\n\n");    
  }
  
                        // polaczenie z wifi
  WiFi.mode(WIFI_STA);
   Serial.print("WiFi mode: ");
   Serial.println(WiFiMode());
  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");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

                                      // pomiary
  char status;
  double T,P,p0,a;
  float t = dht.readTemperature();  // odczyt temperatury
  float h = dht.readHumidity();     // odczyt wilgotności powietrza
 
  // Loop here getting pressure readings every 10 seconds.
  // If you want sea-level-compensated pressure, as used in weather reports,
  // you will need to know the altitude at which your measurements are taken.
  // We're using a constant called ALTITUDE in this sketch:
  
  Serial.println("BMP180:");
  Serial.print("provided altitude: ");
  Serial.print(ALTITUDE,0);
  Serial.print(" meters, ");
  Serial.print(ALTITUDE*3.28084,0);
  Serial.println(" feet");
  
  // If you want to measure altitude, and not pressure, you will instead need
  // to provide a known baseline pressure. This is shown at the end of the sketch.

  // You must first get a temperature measurement to perform a pressure reading.
  
  // Start a temperature measurement:
  // If request is successful, the number of ms to wait is returned.
  // If request is unsuccessful, 0 is returned.

  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);

    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Function returns 1 if successful, 0 if failure.

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Print out the measurement:
      Serial.print("temperature: ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      Serial.print((9.0/5.0)*T+32.0,2);
      Serial.println(" deg F");
      
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.

      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          // Print out the measurement:
          Serial.print("absolute pressure: ");
          Serial.print(P,2);
          Serial.print(" mb, ");
          Serial.print(P*0.0295333727,2);
          Serial.println(" inHg");

          // The pressure sensor returns abolute pressure, which varies with altitude.
          // To remove the effects of altitude, use the sealevel function and your current altitude.
          // This number is commonly used in weather reports.
          // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
          // Result: p0 = sea-level compensated pressure in mb

          p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
          Serial.print("relative (sea-level) pressure: ");
          Serial.print(p0,2);
          Serial.print(" mb, ");
          Serial.print(p0*0.0295333727,2);
          Serial.println(" inHg");

          // On the other hand, if you want to determine your altitude from the pressure reading,
          // use the altitude function along with a baseline pressure (sea-level or other).
          // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb.
          // Result: a = altitude in m.

          a = pressure.altitude(P,p0);
          Serial.print("computed altitude: ");
          Serial.print(a,0);
          Serial.print(" meters, ");
          Serial.print(a*3.28084,0);
          Serial.println(" feet");
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

      // czy odczytano wartości DHT11?
      if (isnan(t) || isnan(h))
      {
        // NIE -> informacja o błędzie
        Serial.println(">>>>>>>>>>>>>>>>>>>>Blad odczytu danych z czujnika!");
      }
      else
      {
        // TAK -> wysyłamy wyniki przez port szeregowy
        Serial.print("DHT22: Wilgotnosc: ");
        Serial.print(h);
        Serial.print(" % ");
        Serial.print("Temperatura: ");
        Serial.print(t);
        Serial.println(" *C");
      }
      Serial.println();  
       int f = 10*analogRead(A0);
                                            // koniec pomiarów
  
                                    //przygotowanie danych do wysłania na serwer Thingspeak  
// convert T to string
  char bufT[16];
  String strT = dtostrf(T, 4, 1, bufT);
// convert h to string
  char bufh[16];
  String strh = dtostrf(h, 4, 1, bufh);
// convert P to string
  char bufP[16];
  String strP = dtostrf(P, 4, 1, bufP);
// convert t to string
  char buft[16];
  String strt = dtostrf(t, 4, 1, buft);
// convert f to string
  char buff[16];
  String strf = dtostrf(f, 4, 1, buff);
// prepare GET string
  String getStr = "GET /update?api_key=";
  getStr += apiKey;
  getStr +="&field1=";
  getStr += String(strT);
  getStr +="&field2=";
  getStr += String(strP);
  getStr +="&field3=";
  getStr += String(strt);
  getStr +="&field4=";
  getStr += String(strh);
  getStr +="&field5=";
  getStr += String(strf);
  getStr += "\r\n\r\n";

   
                                                          // TCP connection
  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  Serial.print("do wyslania:");
  Serial.println(getStr);
  
  // This will send the request to the server
  
  client.print(getStr);
  digitalWrite(12, HIGH);
  delay(100);
  digitalWrite(12, LOW);
  /*
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  */

  Serial.println("closing connection");
  Serial.println();

// thingspeak needs 15 sec delay between updates
    delay(15000);
  //delay(600000);

}