#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <Servo.h>
#include <WiFi.h>
#include <HTTPClient.h>
Servo myservo;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE, A4, A5);
static const unsigned char image_Alert_9x8_bits[] U8X8_PROGMEM = {0x10,0x00,0x38,0x00,0x28,0x00,0x6c,0x00,0x6c,0x00,0xfe,0x00,0xee,0x00,0xff,0x01};
#define SENSOR_PIN A0
const int sampleWindow = 50;
const int threshold = 80;
const float referenceValue = 0.0744;
char buffer[32];
const char* ssid = "YourWiFiSSID"; // Update with your WiFi SSID
const char* password = "YourWiFiPassword"; // Update with your WiFi password
const String apiKey = "8A9REPRTD4IP4G6L";
void setup() {
myservo.attach(9);
myservo.write(0);
u8g2.begin();
pinMode(SENSOR_PIN, INPUT);
Serial.begin(115200);
connectToWiFi();
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
int sensorValue = analogRead(SENSOR_PIN);
int SERVOval = map(sensorValue,0,82.77,0,180);
u8g2.clearBuffer();
unsigned long currentMillis = millis();
double dB = 20 * log10(sensorValue /(double)referenceValue);
int dBLevel = dB;
if (dB >= threshold) {
myservo.write(SERVOval);
} else {
myservo.write(0);
}
delay(sampleWindow);
u8g2.setBitmapMode(1);
u8g2.drawFrame(0, 0, 128, 64);
u8g2.setFont(u8g2_font_helvB08_tr);
u8g2.drawStr(9, 23, "NOISE:");
u8g2.setFont(u8g2_font_profont22_tr);
sprintf(buffer,"%d dB", dBLevel);
u8g2.drawStr(58, 23, buffer);
u8g2.setFont(u8g2_font_helvB08_tr);
u8g2.drawStr(9, 51, "LEVEL:");
u8g2.sendBuffer();
Serial.print("Analog Value: ");
Serial.print(sensorValue);
Serial.print(" | Decibels: ");
Serial.println(dB);
// Fetch temperature data
String temperatureData = fetchTemperature();
float temperature = parseTemperature(temperatureData);
displayTemperature(temperature);
} else {
Serial.println("WiFi not connected!");
connectToWiFi();
}
delay(5000); // Fetch data every 5 seconds
}
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
}
String fetchTemperature() {
HTTPClient http;
String url = "https://api.thingspeak.com/channels/2472958/fields/1/last.json?api_key=" + apiKey;
http.begin(url);
int httpResponseCode = http.GET();
String temperatureData = "";
if (httpResponseCode == HTTP_CODE_OK) {
temperatureData = http.getString();
return temperatureData;
} else {
Serial.println("Error fetching temperature data.");
}
http.end();
return temperatureData;
}
float parseTemperature(String data) {
int index = data.indexOf("\"field1\":");
if (index != -1) {
index = data.indexOf(":", index);
if (index != -1) {
int endIndex = data.indexOf(",", index);
if (endIndex == -1) {
endIndex = data.indexOf("}", index);
}
if (endIndex != -1) {
String temperatureString = data.substring(index + 1, endIndex);
return temperatureString.toFloat();
}
}
}
Serial.println("Fetching Temperature.");
return 0.0;
}
void displayTemperature(float temperature) {
// Display temperature on the screen
}