Doorlock
HenrikBojsenNehm 2 years ago
parent aa4c99b1f1
commit e9562246e5

@ -1,42 +0,0 @@
#include <Arduino.h>
const int lm35_pin = 34; /* LM35 O/P pin */
#define ADC_VREF_mV 3300.0 // in millivolt
#define ADC_RESOLUTION 4096.0
#define FILTER_LEN 15
uint32_t AN_Pot1_Buffer[FILTER_LEN] = {0};
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()
{
Serial.begin(115200);
pinMode(lm35_pin, INPUT);
analogSetClockDiv(255);
}
void loop()
{
float analogValue = readADC_Avg(lm35_pin);
float millivolts = analogValue * (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(analogValue);
Serial.print(" in DegreeC= ");
Serial.println(celsius);
delay(1000);
}

@ -1,5 +1,5 @@
#include <Arduino.h>
#include <WiFi.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
@ -12,27 +12,6 @@ const char *mqtt_server = "192.168.24.215";
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);
@ -80,33 +59,11 @@ void reconnect()
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();
@ -114,114 +71,4 @@ void loop()
{
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 (runningAverageSound == 0)
{
runningAverageSound = highestSound;
}
else
{
runningAverageSound = (runningAverageSound + highestSound) / 2;
}
highestSound = 0;
}
if (currentMillis - previousSoundMillis >= SOUND_MEASURE_INTERVAL)
{
previousSoundMillis = currentMillis;
Serial.print(j);
Serial.print(": sound: ");
Serial.println(runningAverageSound);
DynamicJsonDocument doc(1024);
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);
runningAverageSound = 0;
j = 0;
}
bool motionState = digitalRead(motion_pin);
if (lastMotionState != motionState) {
Serial.print("Motionstate: ");
Serial.println(motionState);
DynamicJsonDocument doc(1024);
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);
lastMotionState = motionState;
}
}

@ -1,84 +0,0 @@
#include <Arduino.h>
const int lm35_pin = 34; /* LM35 O/P pin */
const int mic_pin = 35;
void setup()
{
Serial.begin(115200);
pinMode(lm35_pin, INPUT);
pinMode(mic_pin, INPUT);
}
uint16_t highest = 0;
uint16_t highest2 = 0;
uint16_t runningAverage = 0;
unsigned long previousSendMillis = 0;
unsigned long previousHighMillis = 0;
#define MEASURE_INTERVAL 1000 * 10
int i = 0;
void loop()
{
uint16_t value = analogRead(mic_pin);
if (value > highest)
{
highest = value;
}
if (value > highest2)
{
highest2 = value;
}
// if (value > 100)
// {
// if (runningAverage == 0)
// {
// runningAverage = value;
// }
// else
// {
// runningAverage = (runningAverage + value) / 2;
// }
// }
i++;
unsigned long currentMillis = millis();
if (currentMillis - previousHighMillis >= 200)
{
previousHighMillis = currentMillis;
if (runningAverage == 0)
{
runningAverage = highest2;
}
else
{
runningAverage = (runningAverage + highest2) / 2;
}
highest2 = 0;
}
if (currentMillis - previousSendMillis >= MEASURE_INTERVAL)
{
previousSendMillis = currentMillis;
Serial.print("average: ");
Serial.println(runningAverage);
runningAverage = 0;
Serial.print(i);
Serial.print(": highest: ");
Serial.println(highest);
highest = 0;
i = 0;
}
}
Loading…
Cancel
Save