#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
//***************************add above
#include <SD.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// #include <LiquidCrystal.h>
//********************* add
// const char* ssid="D-Link2";
// const char* password = "@06/63/47@00@43/?@";
const char* ssid = "Wokwi-GUEST"; // Wi-Fi network name
const char* password = ""; // Wi-Fi password
const char* apikey = "nmEH4A4XqTO7O97OZgvUz3ZF4P9MMY8L";
// const char* serverName = "https://asksensors.com/viewSensor.html?id=11831";
const char* serverName = "https://api.asksensors.com/write/nmEH4A4XqTO7O97OZgvUz3ZF4P9MMY8L";
//***************************
#define BMP_SCK (12)
// was in 13 sck
#define BMP_MISO (27)
// was in 12 sdo
#define BMP_MOSI (14)
// was in 11 sdi mosi
#define BMP_CS (15)
// was in 10 bmp
// Pin for DHT22 sensor
#define DHTPIN 4
// #define DHTPIN 7
#define DHTTYPE DHT22
// Pin for sd card
#define CS_PIN 5
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
//Adafruit_BMP280 bmp ;
Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);
// Initialize LCD
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buttonPin = 2;
// was in 6 bottun
bool lastButtonState = false;
bool buttonState = false;
unsigned long debounceDuration = 50; // millis
unsigned long lastDebounceTime = 0;
File dataFile ;
int count = 0;
void setup() {
// Initialize serial communication
Serial.begin(9600);
dht.begin();
//********************* add
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED){
delay(1000);
Serial.println("connecting to wifi ....");
}
Serial.println("connected to wifi ....");
//**********************
if (!bmp.begin(0x76)) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
// Initialize DHT sensor
// lcd.begin(16,2);
lcd.init();
lcd.backlight();
pinMode(buttonPin, INPUT_PULLUP);
pinMode (13 , OUTPUT ) ;
lcd . print (" Smart Weather ") ;
lcd . setCursor (0, 1) ;
lcd . print (" Station ") ;
delay (2000) ;
lcd . clear () ;
lcd . print (" Initializing ...") ;
delay (1000) ;
lcd . clear () ;
lcd . print (" Press button to") ;
delay (1000) ;
lcd . setCursor (0, 1) ;
lcd . print (" cycle data ") ;
delay (1000) ;
//*************************************** add sd
if (!SD.begin(CS_PIN)) {
lcd.clear () ;
Serial.println("SD Card initialization failed!");
lcd . setCursor (0, 1) ;
lcd.print("SD Card failed") ;
delay(1000) ;
lcd.clear() ;
Serial.println("Reinsert card ");
lcd . setCursor (0, 1) ;
lcd.print("Reinsert card") ;
delay(1000) ;
while (1);
}
lcd.clear () ;
Serial.println("SD Card initialized ");
lcd . setCursor (0, 1) ;
lcd.print ("SD Card initialized ") ;
// delay (1000) ;
// lcd.clear () ;
}
void loop() {
int reading = digitalRead (buttonPin) ;
if ( reading != lastButtonState ) {
lastDebounceTime = millis () ;
}
if (( millis () - lastDebounceTime ) > debounceDuration ) {
if ( reading != buttonState ) {
buttonState = reading ;
if ( buttonState == LOW ) {
count = count + 1 ;
display() ;
}
}
}
lastButtonState = reading ;
}
void display(){
// Read temperature and humidity data from DHT sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Read temperature and humidity data from bmp sensor
// float Pressure = bmp.readPressure() / 100.0F;
float Pressure = bmp.readPressure() / 100.0F;
//************************add
HTTPClient http ;
//***************************
if(count == 1){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature:");
// Display temperature on serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Display temperature on LCD
lcd.setCursor(12, 0);
lcd.print(temperature);
lcd.setCursor(0, 1);
delay (1000) ;
lcd.print (" Writing to SD ...") ;
delay (1000) ;
dataFile = SD.open ("weather.txt", FILE_WRITE ) ;
if (dataFile) {
dataFile.print (" Temperature : ") ;
dataFile.print (temperature ) ;
dataFile.println (" C") ;
dataFile.close () ;
lcd.clear() ;
lcd.print (" Data written to SD") ;
delay (1000) ;
} else {
lcd.clear () ;
lcd.print (" Error writing to SD") ;
delay (1000) ;
}
if(temperature < 15){
digitalWrite (2 , HIGH ) ;
}else{
digitalWrite (2 , LOW ) ;
}
//*******************add
String url =String(serverName) +"?module1="+String(temperature);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode>0){
Serial.print(" data sent successfully : "+ String(httpResponseCode));
}else{
Serial.print(" error sending data : "+ String(httpResponseCode));
}
http.end();
//********************
}
if(count == 2){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity:");
// Display Humidity on serial monitor
Serial.print(" Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Display Humidity on LCD
lcd.setCursor(12, 0);
lcd.print(humidity);
delay (1000) ;
lcd.setCursor(0, 1);
lcd.print (" Writing to SD ...") ;
delay (1000) ;
dataFile = SD.open ("weather.txt", FILE_WRITE ) ;
if (dataFile) {
dataFile.print (" Humidity : ") ;
dataFile.print (humidity) ;
dataFile.println (" %") ;
dataFile.close () ;
lcd.clear() ;
lcd.print (" Data written to SD") ;
delay (1000) ;
} else {
lcd.clear () ;
lcd.print (" Error writing to SD") ;
delay (1000) ;
}
//*******************add
String url =String(serverName) +"?module2="+String(humidity);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode>0){
Serial.print(" data sent successfully : "+ String(httpResponseCode));
}else{
Serial.print(" error sending data : "+ String(httpResponseCode));
}
http.end();
//********************
}
if(count == 3){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pressure:");
// Display Pressure on serial monitor
Serial.print("Pressure = ");
Serial.print(Pressure);
Serial.println(" hPa");
// Display Humidity on LCD
lcd.setCursor(12, 0);
lcd.print(Pressure);
delay (1000) ;
count =0 ;
lcd.setCursor(0, 1);
lcd.print (" Writing to SD ...") ;
delay (1000) ;
dataFile = SD.open ("weather.txt", FILE_WRITE ) ;
if (dataFile) {
dataFile.print (" Pressure : ") ;
dataFile.print (Pressure) ;
dataFile.println (" hPa") ;
dataFile.close () ;
lcd.clear() ;
lcd.print (" Data written to SD") ;
delay (1000) ;
} else {
lcd.clear () ;
lcd.print (" Error writing to SD") ;
delay (1000) ;
}
//*******************add
String url =String(serverName) +"?module3="+String(Pressure);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode>0){
Serial.print(" data sent successfully : "+ String(httpResponseCode));
}else{
Serial.print(" error sending data : "+ String(httpResponseCode));
}
http.end();
//********************
}
}