trying to set up buzzer

comboLock
Jonas Wittstrøm 2 years ago
parent 9029d25f17
commit 82e379bff3

@ -1,7 +1,7 @@
H4 Projekt
Display + buzzer:
- Få noget frem på displayet
- Få noget frem på displayet - DONE
- Sæt nogle conditions, som skal aktivere buzzeren
- Aktiver buzzer når "alarmen går"
- Lav en menu, som skal navigeres via rotary encoder

@ -0,0 +1,5 @@
const int buzzerPin = 9;
void setup() {
pinMode(buzzerPin, OUTPUT_LOW);
}

@ -1,227 +1,227 @@
#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
const char *ssid = "PET Aflytningsvogn #43";
const char *password = "zwr33htm";
const char *mqtt_server = "192.168.24.215";
// const char *mqtt_server = "test.mosquitto.org";
WiFiClient espClient;
PubSubClient client(espClient);
const int lm35_pin = 34; /* LM35 O/P pin */
const int mic_pin = 35;
const int light_pin = 32;
const int motion_pin = 33;
#define ADC_VREF_mV 3300.0 // in millivolt
#define ADC_RESOLUTION 4096.0
#define FILTER_LEN 15
float readADC_Avg(int pin)
{
float avg = analogRead(pin);
for (int i = 0; i < 10; i++)
{
avg = (avg + analogRead(pin)) / 2;
delay(20);
}
return avg;
}
void setup_wifi()
{
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect()
{
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client"))
{
Serial.println("connected");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(115200);
pinMode(lm35_pin, INPUT);
pinMode(mic_pin, INPUT);
pinMode(light_pin, INPUT);
pinMode(motion_pin, INPUT);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
setup_wifi();
client.setServer(mqtt_server, 1883);
}
#define SOUND_MEASURE_INTERVAL 1000 * 10
#define SOUND_HIGHEST_MEASURE_INTERVAL 200
#define TEMP_MEASURE_INTERVAL 1000 * 10
unsigned long previousTemperatureMillis = 0;
unsigned long previousSoundMillis = 0;
unsigned long previousHighestSoundMillis = 0;
bool lastMotionState;
uint16_t highestSound = 0;
uint16_t runningAverageSound = 0;
int j = 0;
void loop()
{
client.loop();
if (!client.connected())
{
reconnect();
}
j++;
unsigned long currentMillis = millis();
if (currentMillis - previousTemperatureMillis >= TEMP_MEASURE_INTERVAL)
{
// save the last time you blinked the LED
previousTemperatureMillis = currentMillis;
analogSetClockDiv(255);
float analogValueTemp = readADC_Avg(lm35_pin);
float analogValueLight = readADC_Avg(light_pin);
analogSetClockDiv(1);
float millivolts = analogValueTemp * (ADC_VREF_mV / ADC_RESOLUTION) * 2;
// float millivolts = analogReadMilliVolts(lm35_pin);
float celsius = millivolts / 10; // 6.5 is the callibration offset measured using an oscilliscope
Serial.print("raw= ");
Serial.print(analogValueTemp);
Serial.print(" in DegreeC= ");
Serial.println(celsius);
DynamicJsonDocument docTemp(1024);
docTemp["device"] = "esp32-1";
docTemp["type"] = "temperature";
docTemp["value"] = celsius;
char bufferTemp[1024];
size_t bufferTempSize = serializeJson(docTemp, bufferTemp);
client.publish("esp32/data", bufferTemp, bufferTempSize);
Serial.print("Light: ");
Serial.println(analogValueLight);
DynamicJsonDocument docLight(1024);
docLight["device"] = "esp32-1";
docLight["type"] = "light";
docLight["value"] = analogValueLight;
char bufferLight[1024];
size_t bufferLightSize = serializeJson(docLight, bufferLight);
client.publish("esp32/data", bufferLight, bufferLightSize);
}
uint16_t soundValue = analogRead(mic_pin);
if (soundValue > highestSound) {
highestSound = soundValue;
}
// #include <Arduino.h>
// // #include <WiFi.h>
// #include <PubSubClient.h>
// #include <ArduinoJson.h>
// const char *ssid = "PET Aflytningsvogn #43";
// const char *password = "zwr33htm";
// const char *mqtt_server = "192.168.24.215";
// // const char *mqtt_server = "test.mosquitto.org";
// WiFiClient espClient;
// PubSubClient client(espClient);
// const int lm35_pin = 34; /* LM35 O/P pin */
// const int mic_pin = 35;
// const int light_pin = 32;
// const int motion_pin = 33;
// #define ADC_VREF_mV 3300.0 // in millivolt
// #define ADC_RESOLUTION 4096.0
// #define FILTER_LEN 15
// float readADC_Avg(int pin)
// {
// float avg = analogRead(pin);
// for (int i = 0; i < 10; i++)
// {
// avg = (avg + analogRead(pin)) / 2;
// delay(20);
// }
// return avg;
// }
// void setup_wifi()
// {
// delay(10);
// // We start by connecting to a WiFi network
// Serial.println();
// Serial.print("Connecting to ");
// Serial.println(ssid);
// WiFi.begin(ssid, password);
// while (WiFi.status() != WL_CONNECTED)
// {
// delay(500);
// Serial.print(".");
// }
// Serial.println("");
// Serial.println("WiFi connected");
// Serial.println("IP address: ");
// Serial.println(WiFi.localIP());
// }
// void reconnect()
// {
// // Loop until we're reconnected
// while (!client.connected())
// {
// Serial.print("Attempting MQTT connection...");
// // Attempt to connect
// if (client.connect("ESP8266Client"))
// {
// Serial.println("connected");
// }
// else
// {
// Serial.print("failed, rc=");
// Serial.print(client.state());
// Serial.println(" try again in 5 seconds");
// // Wait 5 seconds before retrying
// delay(5000);
// }
// }
// }
// void setup()
// {
// Serial.begin(115200);
// pinMode(lm35_pin, INPUT);
// pinMode(mic_pin, INPUT);
// pinMode(light_pin, INPUT);
// pinMode(motion_pin, INPUT);
// pinMode(13, OUTPUT);
// digitalWrite(13, HIGH);
// setup_wifi();
// client.setServer(mqtt_server, 1883);
// }
// #define SOUND_MEASURE_INTERVAL 1000 * 10
// #define SOUND_HIGHEST_MEASURE_INTERVAL 200
// #define TEMP_MEASURE_INTERVAL 1000 * 10
// unsigned long previousTemperatureMillis = 0;
// unsigned long previousSoundMillis = 0;
// unsigned long previousHighestSoundMillis = 0;
// bool lastMotionState;
// uint16_t highestSound = 0;
// uint16_t runningAverageSound = 0;
// int j = 0;
// void loop()
// {
// client.loop();
// if (!client.connected())
// {
// reconnect();
// }
// j++;
// unsigned long currentMillis = millis();
// if (currentMillis - previousTemperatureMillis >= TEMP_MEASURE_INTERVAL)
// {
// // save the last time you blinked the LED
// previousTemperatureMillis = currentMillis;
// analogSetClockDiv(255);
// float analogValueTemp = readADC_Avg(lm35_pin);
// float analogValueLight = readADC_Avg(light_pin);
// analogSetClockDiv(1);
// float millivolts = analogValueTemp * (ADC_VREF_mV / ADC_RESOLUTION) * 2;
// // float millivolts = analogReadMilliVolts(lm35_pin);
// float celsius = millivolts / 10; // 6.5 is the callibration offset measured using an oscilliscope
// Serial.print("raw= ");
// Serial.print(analogValueTemp);
// Serial.print(" in DegreeC= ");
// Serial.println(celsius);
// DynamicJsonDocument docTemp(1024);
// docTemp["device"] = "esp32-1";
// docTemp["type"] = "temperature";
// docTemp["value"] = celsius;
// char bufferTemp[1024];
// size_t bufferTempSize = serializeJson(docTemp, bufferTemp);
// client.publish("esp32/data", bufferTemp, bufferTempSize);
// Serial.print("Light: ");
// Serial.println(analogValueLight);
// DynamicJsonDocument docLight(1024);
// docLight["device"] = "esp32-1";
// docLight["type"] = "light";
// docLight["value"] = analogValueLight;
// char bufferLight[1024];
// size_t bufferLightSize = serializeJson(docLight, bufferLight);
// client.publish("esp32/data", bufferLight, bufferLightSize);
// }
// uint16_t soundValue = analogRead(mic_pin);
// if (soundValue > highestSound) {
// highestSound = soundValue;
// }
if (currentMillis - previousHighestSoundMillis >= SOUND_HIGHEST_MEASURE_INTERVAL)
{
previousHighestSoundMillis = currentMillis;
// if (currentMillis - previousHighestSoundMillis >= SOUND_HIGHEST_MEASURE_INTERVAL)
// {
// previousHighestSoundMillis = currentMillis;
if (runningAverageSound == 0)
{
runningAverageSound = highestSound;
}
else
{
runningAverageSound = (runningAverageSound + highestSound) / 2;
}
// if (runningAverageSound == 0)
// {
// runningAverageSound = highestSound;
// }
// else
// {
// runningAverageSound = (runningAverageSound + highestSound) / 2;
// }
highestSound = 0;
}
// highestSound = 0;
// }
if (currentMillis - previousSoundMillis >= SOUND_MEASURE_INTERVAL)
{
previousSoundMillis = currentMillis;
// if (currentMillis - previousSoundMillis >= SOUND_MEASURE_INTERVAL)
// {
// previousSoundMillis = currentMillis;
Serial.print(j);
Serial.print(": sound: ");
Serial.println(runningAverageSound);
// Serial.print(j);
// Serial.print(": sound: ");
// Serial.println(runningAverageSound);
DynamicJsonDocument doc(1024);
// DynamicJsonDocument doc(1024);
doc["device"] = "esp32-1";
doc["type"] = "sound";
doc["value"] = runningAverageSound;
// doc["device"] = "esp32-1";
// doc["type"] = "sound";
// doc["value"] = runningAverageSound;
char buffer[1024];
size_t n = serializeJson(doc, buffer);
client.publish("esp32/data", buffer, n);
// char buffer[1024];
// size_t n = serializeJson(doc, buffer);
// client.publish("esp32/data", buffer, n);
runningAverageSound = 0;
j = 0;
}
// runningAverageSound = 0;
// j = 0;
// }
bool motionState = digitalRead(motion_pin);
// bool motionState = digitalRead(motion_pin);
if (lastMotionState != motionState) {
Serial.print("Motionstate: ");
Serial.println(motionState);
// if (lastMotionState != motionState) {
// Serial.print("Motionstate: ");
// Serial.println(motionState);
DynamicJsonDocument doc(1024);
// DynamicJsonDocument doc(1024);
doc["device"] = "esp32-1";
doc["type"] = "motion";
doc["value"] = motionState;
// doc["device"] = "esp32-1";
// doc["type"] = "motion";
// doc["value"] = motionState;
char buffer[1024];
size_t n = serializeJson(doc, buffer);
client.publish("esp32/data", buffer, n);
// char buffer[1024];
// size_t n = serializeJson(doc, buffer);
// client.publish("esp32/data", buffer, n);
lastMotionState = motionState;
}
}
// lastMotionState = motionState;
// }
// }
Loading…
Cancel
Save