Created
May 2, 2019 09:57
-
-
Save S-M-Salaquzzaman/220f52796988ae0b26fe6614b2ea2eb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Firmware of the Project: | |
/* | |
Name of Project : IoT Based Weather Monitoring System | |
Target Device : ESP8266 | |
Firmware : NodeMCU 1.0 Firmware | |
Clock Frequency : 80MHz | |
UART Baud Rate : 115200 | |
Flash Size : 4 M (3M SPIFFS) | |
Author : Dept. of EEE, BUBT | |
Last Update : 02:39AM, 01 October, 2017 | |
*/ | |
#include <Wire.h> | |
#include <ESP8266WiFi.h> | |
#include "Adafruit_MQTT.h" | |
#include "Adafruit_MQTT_Client.h" | |
#include "DHT.h" | |
#include <stdint.h> | |
#include "SparkFunBME280.h" | |
#include "SPI.h" | |
#define WLAN_SSID "AndroidAP" | |
#define WLAN_PASS "bubt.eee.e.12th" | |
#define AIO_SERVER "io.adafruit.com" | |
#define AIO_SERVERPORT 1883 | |
#define AIO_USERNAME "irfanmazhari8" | |
#define AIO_KEY "8aed2d2260364756a334c3b05bca6e57" | |
#define DHTTYPE DHT11 | |
// SCL D1 | |
// SDA D2 | |
// Do not Connect D8 | |
#define LDR_in A0 | |
#define DHTPIN D3 | |
#define relayPin D4 | |
DHT dht(DHTPIN, DHTTYPE); | |
BME280 mySensor; | |
WiFiClient client; | |
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); | |
Adafruit_MQTT_Publish Light = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/light"); | |
Adafruit_MQTT_Publish TempC = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperature"); | |
Adafruit_MQTT_Publish Hum = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humidity"); | |
Adafruit_MQTT_Publish Alt = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/altitude"); | |
Adafruit_MQTT_Publish Pres = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/pressure"); | |
Adafruit_MQTT_Publish H_Index = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/heatIndex"); | |
Adafruit_MQTT_Publish Rain = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/rain"); | |
int light; | |
float BMP280_tempC; | |
float hum; | |
float alttitude; | |
float pressure; | |
float DHT11_tempC; | |
float hic; | |
float rain; | |
//-------------------------------------------------- | |
void setup() | |
{ | |
Serial.begin(115200); | |
delay(10); | |
dht.begin(); | |
delay(10); | |
mySensor.settings.commInterface = I2C_MODE; | |
mySensor.settings.I2CAddress = 0x76; | |
mySensor.settings.runMode = 3; | |
mySensor.settings.tStandby = 0; | |
mySensor.settings.filter = 0; | |
mySensor.settings.tempOverSample = 1; | |
mySensor.settings.pressOverSample = 1; | |
mySensor.settings.humidOverSample = 1; | |
delay(10); | |
mySensor.begin(), HEX; | |
mySensor.readRegister(BME280_CHIP_ID_REG), HEX; | |
mySensor.readRegister(BME280_RST_REG), HEX; | |
mySensor.readRegister(BME280_CTRL_MEAS_REG), HEX; | |
mySensor.readRegister(BME280_CTRL_HUMIDITY_REG), HEX; | |
pinMode(LDR_in, INPUT); | |
pinMode(relayPin, OUTPUT); | |
delay(10); | |
digitalWrite(relayPin, LOW); | |
Serial.println(""); | |
Serial.println(""); | |
Serial.print("Connecting to WiFi.."); | |
delay(1000); | |
WiFi.begin(WLAN_SSID, WLAN_PASS); // Connect to WiFi | |
delay(100); | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(1000); // Delay 1000ms then re-check | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(WLAN_SSID); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
delay(3000); | |
MQTT_connect(); | |
} | |
//-------------------------------------------------- | |
void loop() | |
{ | |
delay(30000); | |
MQTT_connect(); | |
light = analogRead(A0); | |
readTmpRh(); | |
readBMP280(); | |
readRain(); | |
Light.publish(light); | |
TempC.publish(BMP280_tempC); | |
Hum.publish(hum); | |
Alt.publish(alttitude); | |
Pres.publish(pressure); | |
H_Index.publish(hic); | |
Rain.publish(rain); | |
} | |
//------------------------------------------------- | |
void MQTT_connect() | |
{ | |
int8_t ret; | |
if (mqtt.connected()) | |
return; | |
Serial.println("Connecting to MQTT server..."); | |
delay(2000); | |
uint8_t retries = 10; | |
while ((ret = mqtt.connect()) != 0) // connect will return 0 for connected | |
{ | |
Serial.println(mqtt.connectErrorString(ret)); | |
Serial.println("Retrying MQTT connection in 5 seconds..."); | |
mqtt.disconnect(); | |
delay(5000); | |
retries--; | |
if (retries == 0) | |
while (1); | |
} | |
Serial.println("MQTT server connected."); | |
Serial.println("System is ready!"); | |
Serial.println(""); | |
} | |
//---------------------------------------------- | |
void readTmpRh() | |
{ | |
hum = dht.readHumidity(); | |
hum = hum - 20; | |
DHT11_tempC = dht.readTemperature(); | |
if (isnan(hum) || isnan(DHT11_tempC)) | |
{ | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
hic = dht.computeHeatIndex(DHT11_tempC, hum, false); | |
Serial.print("Heat index: "); | |
Serial.print(hic); | |
Serial.print(" *C"); | |
Serial.println(""); | |
Serial.print("Humidity: "); | |
Serial.print(hum); | |
Serial.println("%"); | |
} | |
//-------------------------------------------------------- | |
void readBMP280() | |
{ | |
Serial.print("Temperature: "); | |
BMP280_tempC = mySensor.readTempC(); | |
Serial.print(BMP280_tempC); | |
Serial.println(" Degree C"); | |
Serial.print("Pressure: "); | |
pressure = mySensor.readFloatPressure(); | |
Serial.print(pressure); | |
Serial.println(" Pascal"); | |
Serial.print("Altitude: "); | |
alttitude = mySensor.readFloatAltitudeMeters(); | |
Serial.print(alttitude); | |
Serial.println(" Meter"); | |
Serial.println(""); | |
} | |
//---------------------------------------------------- | |
void readRain() | |
{ | |
digitalWrite(D4, HIGH); | |
delay(500); | |
int rainADC = analogRead(A0); | |
rain = (1024 - rainADC)/20; | |
delay(100); | |
digitalWrite(D4, LOW); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment