Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Forecast calculation code from Henrik Ekblad @ mysensors.org: https://www.mysensors.org/build/pressure
- */
- #include "WiFiEsp.h"
- #include <Wire.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_TSL2561_U.h>
- #include <Adafruit_MPL115A2.h>
- #include "Adafruit_SGP30.h"
- #include <DHT.h>
- #include <DHT_U.h>
- #define DHTPIN 2 // Pin which is connected to the DHT sensor.
- #define DHTTYPE DHT22 // DHT 22 (AM2302)
- bool debug = false;
- Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
- Adafruit_MPL115A2 mpl115a2;
- Adafruit_SGP30 sgp;
- DHT_Unified dht(DHTPIN, DHTTYPE);
- // Sleep time between reads (in seconds). Do not change this value as the forecast algorithm needs a sample every minute.
- const unsigned long SLEEP_TIME = 60000;
- const char *weather[] = { "stable", "sunny", "cloudy", "unstable", "thunderstorm", "unknown" };
- enum FORECAST
- {
- STABLE = 0, // "Stable Weather Pattern"
- SUNNY = 1, // "Slowly rising Good Weather", "Clear/Sunny "
- CLOUDY = 2, // "Slowly falling L-Pressure ", "Cloudy/Rain "
- UNSTABLE = 3, // "Quickly rising H-Press", "Not Stable"
- THUNDERSTORM = 4, // "Quickly falling L-Press", "Thunderstorm"
- UNKNOWN = 5 // "Unknown (More Time needed)
- };
- float lastPressure = -1;
- float lastTemp = -1;
- int lastForecast = -1;
- const int LAST_SAMPLES_COUNT = 5;
- float lastPressureSamples[LAST_SAMPLES_COUNT];
- // this CONVERSION_FACTOR is used to convert from Pa to kPa in forecast algorithm
- // get kPa/h be dividing hPa by 10
- #define CONVERSION_FACTOR (1.0/10.0)
- int minuteCount = 0;
- bool firstRound = true;
- // average value is used in forecast algorithm.
- float pressureAvg;
- // average after 2 hours is used as reference value for the next iteration.
- float pressureAvg2;
- float dP_dt;
- bool metric;
- const char ssid[] = "SHARK_2G"; // your network SSID (name)
- const char pass[] = "Growth24"; // your network password
- int status = WL_IDLE_STATUS; // the Wifi radio's status
- float pressure = 1500;
- //String forecast = "4";
- //int forecast = -1;
- String light = "0";
- String temperature = "0";
- String humidity = "0";
- String humidityState = "0";
- String luxRequest = "";
- String pressureRequest = "";
- String PPM = "0";
- const char server[] = "192.168.1.227";
- // Initialize the Ethernet client object
- WiFiEspClient client;
- void setup()
- {
- // initialize serial for debugging
- Serial.begin(115200);
- if (Serial) {
- debug = true;
- } else {
- debug = false;
- }
- // initialize serial for ESP module
- Serial1.begin(9600);
- // initialize ESP module
- WiFi.init(&Serial1);
- // check for the presence of the shield
- if (WiFi.status() == WL_NO_SHIELD) {
- if (debug) {
- Serial.println("WiFi shield not present");
- }
- // don't continue
- while (true);
- }
- // attempt to connect to WiFi network
- while ( status != WL_CONNECTED) {
- if (debug) {
- Serial.print("Attempting to connect to WPA SSID: ");
- Serial.println(ssid);
- }
- // Connect to WPA/WPA2 network
- status = WiFi.begin(ssid, pass);
- }
- // you're connected now, so print out the data
- if (debug) {
- Serial.println("You're connected to the network");
- }
- if (debug) {
- printWifiStatus();
- Serial.println();
- }
- initSensors();
- }
- void loop()
- {
- // if there are incoming bytes available
- // from the server, read them and print them
- while (client.available()) {
- char c = client.read();
- if (debug) {
- Serial.write(c);
- }
- }
- getSensors();
- //int forecast = sample(pressure);
- //Serial.println(forecast);
- request(luxRequest);
- delay(60000);
- }
- void printWifiStatus()
- {
- // print the SSID of the network you're attached to
- Serial.print("SSID: ");
- Serial.println(WiFi.SSID());
- // print your WiFi shield's IP address
- IPAddress ip = WiFi.localIP();
- Serial.print("IP Address: ");
- Serial.println(ip);
- // print the received signal strength
- long rssi = WiFi.RSSI();
- Serial.print("Signal strength (RSSI):");
- Serial.print(rssi);
- Serial.println(" dBm");
- }
- void initSensors() {
- /* Initialise the sensors */
- dht.begin();
- if (!tsl.begin())
- {
- /* There was a problem detecting the TSL2561 ... check your connections */
- if (debug) {
- Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
- }
- while (1);
- }
- if (! sgp.begin()) {
- if (debug) {
- Serial.println("Sensor not found :(");
- }
- while (1);
- }
- /* Display some basic information on this sensor */
- displaySensorDetails();
- /* Setup the sensor gain and integration time */
- configureSensor();
- mpl115a2.begin();
- }
- void displaySensorDetails(void)
- {
- sensor_t sensor;
- tsl.getSensor(&sensor);
- if (debug) {
- Serial.println("------------------------------------");
- Serial.print ("Sensor: "); Serial.println(sensor.name);
- Serial.print ("Driver Ver: "); Serial.println(sensor.version);
- Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
- Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux");
- Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux");
- Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux");
- Serial.println("------------------------------------");
- Serial.println("");
- }
- dht.temperature().getSensor(&sensor);
- if (debug) {
- Serial.println("------------------------------------");
- Serial.println("Temperature");
- Serial.print ("Sensor: "); Serial.println(sensor.name);
- Serial.print ("Driver Ver: "); Serial.println(sensor.version);
- Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
- Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
- Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
- Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
- Serial.println("------------------------------------");
- }
- // Print humidity sensor details.
- dht.humidity().getSensor(&sensor);
- if (debug) {
- Serial.println("------------------------------------");
- Serial.println("Humidity");
- Serial.print ("Sensor: "); Serial.println(sensor.name);
- Serial.print ("Driver Ver: "); Serial.println(sensor.version);
- Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
- Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
- Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
- Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
- Serial.println("------------------------------------");
- }
- // Set delay between sensor readings based on sensor details.
- //delayMS = sensor.min_delay / 1000;
- if (debug) {
- Serial.print("Found SGP30 serial #");
- Serial.print(sgp.serialnumber[0], HEX);
- Serial.print(sgp.serialnumber[1], HEX);
- Serial.println(sgp.serialnumber[2], HEX);
- }
- delay(500);
- }
- void configureSensor(void)
- {
- /* You can also manually set the gain or enable auto-gain support */
- // tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
- // tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
- tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */
- /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
- tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */
- // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */
- // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */
- /* Update these values depending on what you've set above! */
- if (debug) {
- Serial.println("------------------------------------");
- Serial.print ("Gain: "); Serial.println("Auto");
- Serial.print ("Timing: "); Serial.println("13 ms");
- Serial.println("------------------------------------");
- }
- }
- void getSensors() {
- /* Get a new sensor event */
- sensors_event_t event;
- sensors_event_t event2;
- tsl.getEvent(&event);
- dht.temperature().getEvent(&event2);
- sgp.IAQmeasure();
- /* Display the results (light is measured in lux) */
- if (event.light)
- {
- if (debug) {
- Serial.print(event.light); Serial.println(" lux");
- }
- light = event.light;
- }
- else
- {
- /* If event.light = 0 lux the sensor is probably saturated
- and no reliable data could be generated! */
- if (debug) {
- Serial.println("Sensor overload");
- }
- }
- float pressurehPA = 0, temperatureC = 0;
- pressurehPA = 10 * mpl115a2.getPressure();
- if (debug) {
- Serial.print("Pressure (hPa): "); Serial.print(pressurehPA, 4); Serial.println(" hPa");
- }
- pressure = pressurehPA;
- //temperatureC = mpl115a2.getTemperature();
- //Serial.print("Temp (*C): "); Serial.print(temperatureC, 1); Serial.println(" *C");
- if (isnan(event2.temperature)) {
- if (debug) {
- Serial.println("Error reading temperature!");
- }
- }
- else {
- if (debug) {
- Serial.print("Temperature: ");
- Serial.print(event2.temperature);
- Serial.println(" *C");
- }
- temperature = event2.temperature;
- }
- // Get humidity event and print its value.
- dht.humidity().getEvent(&event2);
- if (isnan(event2.relative_humidity)) {
- if (debug) {
- Serial.println("Error reading humidity!");
- }
- }
- else {
- if (debug) {
- Serial.print("Humidity: ");
- Serial.print(event2.relative_humidity);
- Serial.println("%");
- }
- humidity = event2.relative_humidity;
- }
- PPM = sgp.eCO2;
- }
- void request(String requestHeader) {
- while ( status != WL_CONNECTED) {
- if (debug) {
- Serial.print("Attempting to connect to WPA SSID: ");
- Serial.println(ssid);
- }
- // Connect to WPA/WPA2 network
- status = WiFi.begin(ssid, pass);
- }
- int forecast = sample(pressure);
- String pressureRequest = String("GET /json.htm?type=command¶m=udevice&idx=215&nvalue=0&svalue=");
- pressureRequest = pressureRequest + String(pressure);
- pressureRequest = pressureRequest + String(";");
- pressureRequest = pressureRequest + forecast;
- pressureRequest = pressureRequest + String(" HTTP/1.1");
- if (debug) {
- Serial.println(pressureRequest);
- }
- String luxRequest = String("GET /json.htm?type=command¶m=udevice&idx=214&svalue=");
- luxRequest = luxRequest + String(light);
- luxRequest = luxRequest + String(" HTTP/1.1");
- String temperatureRequest = String("GET /json.htm?type=command¶m=udevice&idx=219&nvalue=0&svalue=");
- temperatureRequest = temperatureRequest + String(temperature);
- temperatureRequest = temperatureRequest + String(" HTTP/1.1");
- String humidityRequest = String("GET /json.htm?type=command¶m=udevice&idx=217&nvalue=");
- humidityRequest = humidityRequest + String(humidity);
- humidityRequest = humidityRequest + String("&svalue=");
- humidityRequest = humidityRequest + String(humidityState);
- humidityRequest = humidityRequest + String(" HTTP/1.1");
- String ppmRequest = String("GET /json.htm?type=command¶m=udevice&idx=222&nvalue=");
- ppmRequest = ppmRequest + String(PPM);
- ppmRequest = ppmRequest + String(" HTTP/1.1");
- if (debug) {
- Serial.println("Starting connection to server...");
- }
- // if you get a connection, report back via serial
- if (client.connect(server, 8080)) {
- if (debug) {
- Serial.println("Connected to server");
- }
- // Make a HTTP request
- client.println(pressureRequest);
- client.println("Host: 192.168.1.227");
- client.println("Connection: close");
- client.println();
- }
- if (!client.connected()) {
- if (debug) {
- Serial.println();
- Serial.println("Disconnecting from server...");
- }
- client.flush();
- client.stop();
- //status = WiFi.disconnect();
- // do nothing forevermore
- //while (true);
- }
- if (client.connect(server, 8080)) {
- if (debug) {
- Serial.println("Connected to server");
- }
- // Make a HTTP request
- client.println(luxRequest);
- client.println("Host: 192.168.1.227");
- client.println("Connection: close");
- client.println();
- }
- if (!client.connected()) {
- if (debug) {
- Serial.println();
- Serial.println("Disconnecting from server...");
- }
- client.flush();
- client.stop();
- //status = WiFi.disconnect();
- // do nothing forevermore
- //while (true);
- }
- if (client.connect(server, 8080)) {
- if (debug) {
- Serial.println("Connected to server");
- }
- // Make a HTTP request
- client.println(temperatureRequest);
- client.println("Host: 192.168.1.227");
- client.println("Connection: close");
- client.println();
- }
- if (!client.connected()) {
- if (debug) {
- Serial.println();
- Serial.println("Disconnecting from server...");
- }
- client.flush();
- client.stop();
- //status = WiFi.disconnect();
- // do nothing forevermore
- //while (true);
- }
- while (client.connected()) {
- while (client.available()) {
- char ch = client.read();
- if (debug) {
- Serial.write(ch);
- }
- }
- }
- if (client.connect(server, 8080)) {
- if (debug) {
- Serial.println("Connected to server");
- }
- // Make a HTTP request
- client.println(humidityRequest);
- client.println("Host: 192.168.1.227");
- client.println("Connection: close");
- client.println();
- }
- while (client.connected()) {
- while (client.available()) {
- char ch = client.read();
- if (debug) {
- Serial.write(ch);
- }
- }
- }
- if (!client.connected()) {
- if (debug) {
- Serial.println();
- Serial.println("Disconnecting from server...");
- }
- client.flush();
- client.stop();
- //status = WiFi.disconnect();
- // do nothing forevermore
- //while (true);
- }
- if (client.connect(server, 8080)) {
- if (debug) {
- Serial.println("Connected to server");
- }
- // Make a HTTP request
- client.println(ppmRequest);
- client.println("Host: 192.168.1.227");
- client.println("Connection: close");
- client.println();
- }
- if (!client.connected()) {
- if (debug) {
- Serial.println();
- Serial.println("Disconnecting from server...");
- }
- client.flush();
- client.stop();
- status = WiFi.disconnect();
- // do nothing forevermore
- //while (true);
- }
- while (client.connected()) {
- while (client.available()) {
- char ch = client.read();
- if (debug) {
- Serial.write(ch);
- }
- }
- }
- }
- float getLastPressureSamplesAverage()
- {
- float lastPressureSamplesAverage = 0;
- for (int i = 0; i < LAST_SAMPLES_COUNT; i++)
- {
- lastPressureSamplesAverage += lastPressureSamples[i];
- }
- lastPressureSamplesAverage /= LAST_SAMPLES_COUNT;
- return lastPressureSamplesAverage;
- }
- // Pressure in hPa --> forecast done by calculating kPa/h
- int sample(float pressure)
- {
- // Calculate the average of the last n minutes.
- int index = minuteCount % LAST_SAMPLES_COUNT;
- lastPressureSamples[index] = pressure;
- minuteCount++;
- if (minuteCount > 185)
- {
- minuteCount = 6;
- }
- if (minuteCount == 5)
- {
- pressureAvg = getLastPressureSamplesAverage();
- }
- else if (minuteCount == 35)
- {
- float lastPressureAvg = getLastPressureSamplesAverage();
- float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
- if (firstRound) // first time initial 3 hour
- {
- dP_dt = change * 2; // note this is for t = 0.5hour
- }
- else
- {
- dP_dt = change / 1.5; // divide by 1.5 as this is the difference in time from 0 value.
- }
- }
- else if (minuteCount == 65)
- {
- float lastPressureAvg = getLastPressureSamplesAverage();
- float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
- if (firstRound) //first time initial 3 hour
- {
- dP_dt = change; //note this is for t = 1 hour
- }
- else
- {
- dP_dt = change / 2; //divide by 2 as this is the difference in time from 0 value
- }
- }
- else if (minuteCount == 95)
- {
- float lastPressureAvg = getLastPressureSamplesAverage();
- float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
- if (firstRound) // first time initial 3 hour
- {
- dP_dt = change / 1.5; // note this is for t = 1.5 hour
- }
- else
- {
- dP_dt = change / 2.5; // divide by 2.5 as this is the difference in time from 0 value
- }
- }
- else if (minuteCount == 125)
- {
- float lastPressureAvg = getLastPressureSamplesAverage();
- pressureAvg2 = lastPressureAvg; // store for later use.
- float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
- if (firstRound) // first time initial 3 hour
- {
- dP_dt = change / 2; // note this is for t = 2 hour
- }
- else
- {
- dP_dt = change / 3; // divide by 3 as this is the difference in time from 0 value
- }
- }
- else if (minuteCount == 155)
- {
- float lastPressureAvg = getLastPressureSamplesAverage();
- float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
- if (firstRound) // first time initial 3 hour
- {
- dP_dt = change / 2.5; // note this is for t = 2.5 hour
- }
- else
- {
- dP_dt = change / 3.5; // divide by 3.5 as this is the difference in time from 0 value
- }
- }
- else if (minuteCount == 185)
- {
- float lastPressureAvg = getLastPressureSamplesAverage();
- float change = (lastPressureAvg - pressureAvg) * CONVERSION_FACTOR;
- if (firstRound) // first time initial 3 hour
- {
- dP_dt = change / 3; // note this is for t = 3 hour
- }
- else
- {
- dP_dt = change / 4; // divide by 4 as this is the difference in time from 0 value
- }
- pressureAvg = pressureAvg2; // Equating the pressure at 0 to the pressure at 2 hour after 3 hours have past.
- firstRound = false; // flag to let you know that this is on the past 3 hour mark. Initialized to 0 outside main loop.
- }
- int forecast = UNKNOWN;
- if (minuteCount < 35 && firstRound) //if time is less than 35 min on the first 3 hour interval.
- {
- forecast = UNKNOWN;
- }
- else if (dP_dt < (-0.25))
- {
- forecast = THUNDERSTORM;
- }
- else if (dP_dt > 0.25)
- {
- forecast = UNSTABLE;
- }
- else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05)))
- {
- forecast = CLOUDY;
- }
- else if ((dP_dt > 0.05) && (dP_dt < 0.25))
- {
- forecast = SUNNY;
- }
- else if ((dP_dt > (-0.05)) && (dP_dt < 0.05))
- {
- forecast = STABLE;
- }
- else
- {
- forecast = UNKNOWN;
- }
- if (debug) {
- Serial.print(F("Forecast at minute "));
- Serial.print(minuteCount);
- Serial.print(F(" dP/dt = "));
- Serial.print(dP_dt);
- Serial.print(F("kPa/h --> "));
- Serial.println(weather[forecast]);
- }
- return forecast;
- }
Advertisement
Add Comment
Please, Sign In to add comment