0% found this document useful (0 votes)
4 views2 pages

10sensors Integrated

The document contains an Arduino sketch for an IoT device that connects to WiFi and displays the current time on an OLED screen. It monitors ten sensors and controls corresponding LEDs based on their states, activating a buzzer at specific times if any sensors are inactive. The code also includes configurations for NTP time synchronization and pin definitions for various components.

Uploaded by

bhavya.doshi301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

10sensors Integrated

The document contains an Arduino sketch for an IoT device that connects to WiFi and displays the current time on an OLED screen. It monitors ten sensors and controls corresponding LEDs based on their states, activating a buzzer at specific times if any sensors are inactive. The code also includes configurations for NTP time synchronization and pin definitions for various components.

Uploaded by

bhavya.doshi301
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <WiFi.

h>
#include "time.h"
#include <Wire.h>
#include <U8g2lib.h>

// Define OLED Display (SH1106 or SSD1306, 128x64)


U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

// Pin Definitions
#define BUZZER_PIN 12 // GPIO 12 (should remain LOW during boot)
#define GREEN_LIGHT 25 // Green LED
#define RED_LIGHT 26 // Red LED

// Sensor and LED Arrays


const int SENSORS[10] = {34, 35, 36, 39, 32, 33, 14, 27, 4, 13}; // Input-only pins
first
const int LEDS[10] = {2, 15, 5, 18, 19, 21, 22, 23, 16, 17};

// WiFi Credentials
const char* ssid = "Galaxy S22";
const char* password = "1234567890";

// NTP Server Settings


const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 19800; // IST (UTC +5:30)
const int daylightOffset_sec = 0;

void setup() {
Serial.begin(115200);

// Set up sensor pins as INPUT


for (int i = 0; i < 10; i++) {
pinMode(SENSORS[i], INPUT);
}

// Set up LED pins as OUTPUT


for (int i = 0; i < 10; i++) {
pinMode(LEDS[i], OUTPUT);
}

pinMode(GREEN_LIGHT, OUTPUT);
pinMode(RED_LIGHT, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);

// Initialize OLED Display


u8g2.begin();
u8g2.setFont(u8g2_font_ncenB14_tr);

// Connect to WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");

// Sync time from NTP


configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.println("Time synced from NTP");
}

void loop() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to get time");
return;
}

// Get current time (HH:MM:SS)


char timeStr[10];
snprintf(timeStr, sizeof(timeStr), "%02d:%02d:%02d", timeinfo.tm_hour,
timeinfo.tm_min, timeinfo.tm_sec);

// Display time on OLED


u8g2.clearBuffer();
u8g2.setCursor(20, 30);
u8g2.print(timeStr);
u8g2.sendBuffer();
Serial.println(timeStr);

// Sensor and LED Logic


bool allSensorsActive = true;
for (int i = 0; i < 10; i++) {
int sensorState = digitalRead(SENSORS[i]);
digitalWrite(LEDS[i], sensorState);
if (sensorState == LOW) {
allSensorsActive = false;
}
}

// Control Green and Red LEDs


if (allSensorsActive) {
digitalWrite(GREEN_LIGHT, HIGH);
digitalWrite(RED_LIGHT, LOW);
} else {
digitalWrite(GREEN_LIGHT, LOW);
digitalWrite(RED_LIGHT, HIGH);
}

// Buzzer Logic
if ((timeinfo.tm_hour == 9 && timeinfo.tm_min == 0) || //first shift end
(timeinfo.tm_hour == 14 && timeinfo.tm_min == 0) || //second shift end
(timeinfo.tm_hour == 18 && timeinfo.tm_min == 0)) { //third shift end
if (!allSensorsActive) {
Serial.println("Buzzer Activated!");
digitalWrite(BUZZER_PIN, HIGH);
delay(60000);
digitalWrite(BUZZER_PIN, LOW);
}
}

delay(1000);
}

You might also like