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