From 9ebe9f7828a2cd15bc07766c55128ea0f9b20f09 Mon Sep 17 00:00:00 2001 From: AdamLotfalla <144452542+AdamLotfalla@users.noreply.github.com> Date: Wed, 4 Dec 2024 03:51:27 +0200 Subject: [PATCH] buzzer functionality, TSL, and reduced code time --- src/main.cpp | 268 +++++++++++++++++++++++++++++++++----------------- src/pitches.h | 94 ++++++++++++++++++ 2 files changed, 273 insertions(+), 89 deletions(-) create mode 100644 src/pitches.h diff --git a/src/main.cpp b/src/main.cpp index b7080c4..f0b7c23 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,12 +5,39 @@ #include #include #include -#include -#define WIFI_SSID "Yurichi" -#define WIFI_PASSWORD "12348765" +#include //https://github.com/jacobstim/Adafruit_TSL2561 +#include +#include "pitches.h" //library + +//---------------------------------------_Wi-Fi definitions_---------------------------------------// + +#define WIFI_SSID "Redmi Note 10S" +#define WIFI_PASSWORD "01010101" //OPPO Reno8 T 5G, 12348765 +//Yurichi, 12348765 + +//https://docs.google.com/spreadsheets/d/1S6_IL7yn1GuPK6xeMa3itCuH75X0g4UemVOapFCJXmc/edit?gid=0#gid=0 +//1S6_IL7yn1GuPK6xeMa3itCuH75X0g4UemVOapFCJXmc + +String URL = "https://script.google.com/macros/s/AKfycbxnqfEGZMHjn2Ebd_D0tm0VSgEZOGyk9OVzxktNx8H7G_xwJ2qzl26o78IyMAwsav0F/exec?sts=write"; + + +//---------------------------------------_TSL2561 interrupt adresses_---------------------------------------// +#define TSL2561_ADDR 0x39 // Default I2C address for TSL2561 +#define COMMAND_BIT 0x80 // Command bit to specify register +// TSL2561 Registers +#define THRESHLOWLOW 0x02 +#define THRESHLOWHIGH 0x03 +#define THRESHHIGHLOW 0x04 +#define THRESHHIGHHIGH 0x05 +#define INTERRUPT_CONTROL 0x06 +#define COMMAND_CLEAR_INTERRUPT 0xC0 + +#define TSL2561_INT_PIN 12 + +//---------------------------------------_EPD definitions_---------------------------------------// #define MAX_DISPLAY_BUFFER_SIZE 800 #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8)) @@ -19,36 +46,45 @@ #define GxEPD2_DRIVER_CLASS GxEPD2_290_C90c //GDEM029C90 128x296, SSD1680, (FPC-7519 rev.b) GxEPD2_DISPLAY_CLASS display(GxEPD2_DRIVER_CLASS(/*CS=5*/ EPD_CS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); +//---------------------------------------_Pin definitions_---------------------------------------// + #define PIR_in_pin 27 #define PIR_out_pin 26 +#define led 33 +#define buzzer 32 -#define led 32 -//https://docs.google.com/spreadsheets/d/1S6_IL7yn1GuPK6xeMa3itCuH75X0g4UemVOapFCJXmc/edit?gid=0#gid=0 -//1S6_IL7yn1GuPK6xeMa3itCuH75X0g4UemVOapFCJXmc - -String URL = "https://script.google.com/macros/s/AKfycbxnqfEGZMHjn2Ebd_D0tm0VSgEZOGyk9OVzxktNx8H7G_xwJ2qzl26o78IyMAwsav0F/exec?sts=write"; - - - +//---------------------------------------_Variable declarations_---------------------------------------// U8G2_FOR_ADAFRUIT_GFX u8g2Fonts; DHT DHT_SENSOR(25,DHT22); Adafruit_TSL2561_Unified TSL = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345); + short int Temperature_danger = 0; short int Humidity_danger = 0; bool People_count_danger = false; +bool Light_danger = false; + const int Temperature_upper_threashold = 21; const int Temperature_lower_threashold = 16; const int Humidity_upper_threashold = 60; const int Humidity_lower_threashold = 40; +const int People_count_upper_threashold = 5; +const int Light_upper_threashold = 500; + bool DHT_fault = false; +bool TSL2561_fault = false; + int People_count = 0; -const int People_count_upper_threashold = 5; std::queue gateLog; //0 = inside, 1 = outside +bool TSL_interrupt_triggered = false; + float prevTemp; float prevHumidity; +float prevLight; int prevPeople_count; +//---------------------------------------_Interrupt handlers_---------------------------------------// + void innerPIRtrigger (){ gateLog.push(0); } @@ -57,40 +93,83 @@ void outerPIRtrigger(){ gateLog.push(1); } -// void displaySensorDetails(void) -// { -// sensor_t sensor; -// TSL.getSensor(&sensor); -// 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(""); -// 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 */ +//---------------------------------------_Functions_---------------------------------------// + +void displaySensorDetails(void) +{ + sensor_t sensor; + TSL.getSensor(&sensor); + 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(""); + delay(500); +} + +void configureSensor(void) +{ + TSL.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */ + + TSL.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */ + + /* Configure interrupt thresholds */ + // First: convert lux value to raw sensor value, using "sunlight" approximation. + // Other approximations, see Adafruit_TSL2561_U.h + uint16_t threshold = TSL.calculateRawCH0(Light_upper_threashold, TSL2561_APPROXCHRATIO_LED); + TSL.setInterruptThreshold(0,threshold); + Serial.println(threshold); + + /* Enable level interrupt, trigger interrupt after 5 integration times + -> Because integration time is 13ms by default, this means the threshold + needs to be exceeded for more than 65ms before the interrupt is triggered. + Maximum value is 15; put to 1 for immediate triggering; put to 0 to have + the interrupt triggered after every integration time (regardless of whether + the thresholds were exceeded or not) + */ + TSL.setInterruptControl(TSL2561_INTERRUPTCTL_LEVEL, 1); + TSL.clearLevelInterrupt(); +} + +void clearInterrupt() { + Wire.beginTransmission(TSL2561_ADDR); + Wire.write(COMMAND_CLEAR_INTERRUPT); // Command to clear interrupt + Wire.endTransmission(); +} + +void WarningTone() { + int note = NOTE_A6; // Moderate-pitched tone + int duration = 150; // Short duration for a quick beep -// /* 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! */ -// Serial.println("------------------------------------"); -// Serial.print ("Gain: "); Serial.println("Auto"); -// Serial.print ("Timing: "); Serial.println("13 ms"); -// Serial.println("------------------------------------"); -// } + tone(buzzer, note, duration); + delay(duration + 50); // Small delay to ensure the tone finishes properly + noTone(buzzer); // Turn off the buzzer +} + +void DangerTone() { + // Define a danger tone melody with sharp high and low alternating notes + int melody[] = {NOTE_B7, NOTE_F7, NOTE_B7, NOTE_F7, NOTE_B7, NOTE_F7}; + int noteDurations[] = {8, 8, 8, 8, 8, 8}; // Short duration for each note + + for (int repeat = 0; repeat < 3; repeat++) { // Repeat the sequence 3 times for urgency + for (int thisNote = 0; thisNote < 6; thisNote++) { + int noteDuration = 1000 / noteDurations[thisNote]; + tone(buzzer, melody[thisNote], noteDuration); + delay(noteDuration * 1.3); // Add a slight pause between notes + noTone(buzzer); // Stop the tone before the next note + } + delay(300); // Add a short pause between sequences + } +} + +void Light_interrupt() { + WarningTone(); + TSL_interrupt_triggered = true; +} void setup() { @@ -98,25 +177,27 @@ void setup() Serial.begin(115200); u8g2Fonts.begin(display); //connect u8g2 procedures to Adafruit GFX DHT_SENSOR.begin(); + pinMode(PIR_in_pin, INPUT_PULLDOWN); pinMode(PIR_out_pin, INPUT_PULLDOWN); + pinMode(TSL2561_INT_PIN, INPUT_PULLUP); + pinMode(buzzer, OUTPUT); + + digitalWrite(buzzer, HIGH); + attachInterrupt(digitalPinToInterrupt( PIR_in_pin ), innerPIRtrigger ,RISING); attachInterrupt(digitalPinToInterrupt( PIR_out_pin ), outerPIRtrigger ,RISING); + attachInterrupt(digitalPinToInterrupt(TSL2561_INT_PIN), Light_interrupt, FALLING); - // if(!TSL.begin()) - // { - // /* There was a problem detecting the TSL2561 ... check your connections */ - // Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!"); - // while(1); - // } - - // /* Display some basic information on this sensor */ - // displaySensorDetails(); - - // /* Setup the sensor gain and integration time */ - // configureSensor(); + if(!TSL.begin()) + { + Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!"); + while(1); + } + displaySensorDetails(); + configureSensor(); display.setRotation(1); @@ -147,26 +228,12 @@ void loop() { digitalWrite(led, LOW); } + sensors_event_t event; + TSL.getEvent(&event); + float temperature = DHT_SENSOR.readTemperature(); float humidity = DHT_SENSOR.readHumidity(); - - - // /* Get a new sensor event */ - // sensors_event_t event; - // TSL.getEvent(&event); - - // /* Display the results (light is measured in lux) */ - // if (event.light) - // { - // Serial.print(event.light); Serial.println(" lux"); - // } - // else - // { - // /* If event.light = 0 lux the sensor is probably saturated - // and no reliable data could be generated! */ - // Serial.println("Sensor overload"); - // } - // delay(250); + float Light = event.light; while(gateLog.size() >= 2){ bool first = gateLog.front(); @@ -177,9 +244,6 @@ void loop() { if(first == 0 && second == 1){People_count--;} else if( first == 1 && second == 0){People_count++;} } - Serial.println(); - Serial.println(People_count); - delay(500); URL = "https://script.google.com/macros/s/AKfycbysSuNL1ltvjkaQBunL6vNLS5m6BGEgk7yWyvXBOT2bIYknJyMW1LEHgN6IjDdEn2i6/exec?sts=write"; @@ -190,8 +254,9 @@ void loop() { HTTPClient http; // HTTP GET Request. http.begin(URL.c_str()); - // http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); - // // Gets the HTTP status code. + // http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); // commented out to remove extra delay + + // Gets the HTTP status code. int httpCode = http.GET(); String payload; if (httpCode > 0) { @@ -208,9 +273,21 @@ void loop() { if(humidity > Humidity_upper_threashold){ Humidity_danger = 1;}else if(humidity < Humidity_lower_threashold){ Humidity_danger = -1;}else{Humidity_danger = 0;} if(People_count >= People_count_upper_threashold){ People_count_danger = true;}else{ People_count_danger = false;} if(isnan(temperature) || isnan(humidity)){DHT_fault = true;}else{DHT_fault = false;} + if(Light > Light_upper_threashold){Light_danger = true;}else{Light_danger = false;} + if(Light == 0){TSL2561_fault = true;}else{TSL2561_fault = false;} - //----------------------- - if(temperature != prevTemp || humidity != prevHumidity || People_count != prevPeople_count){ + if(TSL_interrupt_triggered){ + clearInterrupt(); + TSL_interrupt_triggered = false; + } + + if(Temperature_danger || Humidity_danger || People_count_danger || Light_danger){ + DangerTone(); + delay(100); + } + + //--------------------_Display_-------------------- + if(temperature != prevTemp || humidity != prevHumidity || People_count != prevPeople_count || Light != prevLight){ display.firstPage(); u8g2Fonts.setBackgroundColor(GxEPD_WHITE); @@ -234,7 +311,7 @@ void loop() { } else if(Temperature_danger == -1){ u8g2Fonts.setFont(u8g2_font_twelvedings_t_all); - u8g2Fonts.drawGlyph(8,27, '\u007B'); + u8g2Fonts.drawGlyph(8,27, '\u007D'); } //----------------------- u8g2Fonts.setForegroundColor(Humidity_danger || DHT_fault? 0xF800: 0x0000); @@ -268,23 +345,36 @@ void loop() { u8g2Fonts.setFont(u8g2_font_twelvedings_t_all); u8g2Fonts.drawGlyph(8,77, '\u0021'); } + //------------------------ + u8g2Fonts.setForegroundColor(Light_danger? 0xF800: 0x0000); + u8g2Fonts.setFont(u8g2_font_fub17_tr); + u8g2Fonts.setCursor(25,105); + u8g2Fonts.printf("Ligth intensity: %.0f", Light); + u8g2Fonts.setFont(u8g2_font_cu12_t_symbols); + u8g2Fonts.printf(" lux"); + + if(TSL2561_fault){ + u8g2Fonts.setFont(u8g2_font_twelvedings_t_all); + u8g2Fonts.drawGlyph(8,105, '\u0047'); + } + else if(Light_danger){ + u8g2Fonts.setFont(u8g2_font_twelvedings_t_all); + u8g2Fonts.drawGlyph(8,105, '\u0021'); + } }while(display.nextPage()); display.hibernate(); } + //--------------------_End of display_-------------------- prevTemp = temperature; prevHumidity = humidity; prevPeople_count = People_count; + prevLight = Light; auto time = millis(); - // esp_sleep_enable_timer_wakeup((30000 - ((int)time % 30000))*1000); - // esp_sleep_enable_ext0_wakeup((gpio_num_t)17,HIGH); - // esp_sleep_enable_ext0_wakeup((gpio_num_t)7,HIGH); - // esp_light_sleep_start(); - - while(time % 30000 > 50){time = millis();} + while(time % 30000 >= 50){time = millis(); delay(10);} } \ No newline at end of file diff --git a/src/pitches.h b/src/pitches.h new file mode 100644 index 0000000..f6a1841 --- /dev/null +++ b/src/pitches.h @@ -0,0 +1,94 @@ +/************************************************* + * Public Constants + *************************************************/ + +#define NOTE_B0 31 +#define NOTE_C1 33 +#define NOTE_CS1 35 +#define NOTE_D1 37 +#define NOTE_DS1 39 +#define NOTE_E1 41 +#define NOTE_F1 44 +#define NOTE_FS1 46 +#define NOTE_G1 49 +#define NOTE_GS1 52 +#define NOTE_A1 55 +#define NOTE_AS1 58 +#define NOTE_B1 62 +#define NOTE_C2 65 +#define NOTE_CS2 69 +#define NOTE_D2 73 +#define NOTE_DS2 78 +#define NOTE_E2 82 +#define NOTE_F2 87 +#define NOTE_FS2 93 +#define NOTE_G2 98 +#define NOTE_GS2 104 +#define NOTE_A2 110 +#define NOTE_AS2 117 +#define NOTE_B2 123 +#define NOTE_C3 131 +#define NOTE_CS3 139 +#define NOTE_D3 147 +#define NOTE_DS3 156 +#define NOTE_E3 165 +#define NOTE_F3 175 +#define NOTE_FS3 185 +#define NOTE_G3 196 +#define NOTE_GS3 208 +#define NOTE_A3 220 +#define NOTE_AS3 233 +#define NOTE_B3 247 +#define NOTE_C4 262 +#define NOTE_CS4 277 +#define NOTE_D4 294 +#define NOTE_DS4 311 +#define NOTE_E4 330 +#define NOTE_F4 349 +#define NOTE_FS4 370 +#define NOTE_G4 392 +#define NOTE_GS4 415 +#define NOTE_A4 440 +#define NOTE_AS4 466 +#define NOTE_B4 494 +#define NOTE_C5 523 +#define NOTE_CS5 554 +#define NOTE_D5 587 +#define NOTE_DS5 622 +#define NOTE_E5 659 +#define NOTE_F5 698 +#define NOTE_FS5 740 +#define NOTE_G5 784 +#define NOTE_GS5 831 +#define NOTE_A5 880 +#define NOTE_AS5 932 +#define NOTE_B5 988 +#define NOTE_C6 1047 +#define NOTE_CS6 1109 +#define NOTE_D6 1175 +#define NOTE_DS6 1245 +#define NOTE_E6 1319 +#define NOTE_F6 1397 +#define NOTE_FS6 1480 +#define NOTE_G6 1568 +#define NOTE_GS6 1661 +#define NOTE_A6 1760 +#define NOTE_AS6 1865 +#define NOTE_B6 1976 +#define NOTE_C7 2093 +#define NOTE_CS7 2217 +#define NOTE_D7 2349 +#define NOTE_DS7 2489 +#define NOTE_E7 2637 +#define NOTE_F7 2794 +#define NOTE_FS7 2960 +#define NOTE_G7 3136 +#define NOTE_GS7 3322 +#define NOTE_A7 3520 +#define NOTE_AS7 3729 +#define NOTE_B7 3951 +#define NOTE_C8 4186 +#define NOTE_CS8 4435 +#define NOTE_D8 4699 +#define NOTE_DS8 4978 + \ No newline at end of file