You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
199 lines
4.7 KiB
C++
199 lines
4.7 KiB
C++
#include <Arduino.h>
|
|
#include <WiFi.h>
|
|
#include <PubSubClient.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
const int deviceId = 1;
|
|
|
|
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["deviceId"] = deviceId;
|
|
docTemp["value"] = celsius;
|
|
|
|
char bufferTemp[1024];
|
|
size_t bufferTempSize = serializeJson(docTemp, bufferTemp);
|
|
client.publish("temperature", bufferTemp, bufferTempSize);
|
|
|
|
Serial.print("Light: ");
|
|
Serial.println(analogValueLight);
|
|
|
|
DynamicJsonDocument docLight(1024);
|
|
|
|
docLight["deviceId"] = deviceId;
|
|
docLight["value"] = analogValueLight;
|
|
|
|
char bufferLight[1024];
|
|
size_t bufferLightSize = serializeJson(docLight, bufferLight);
|
|
client.publish("light", bufferLight, bufferLightSize);
|
|
}
|
|
|
|
bool motionState = digitalRead(motion_pin);
|
|
|
|
if (lastMotionState != motionState)
|
|
{
|
|
Serial.print("Motionstate: ");
|
|
Serial.println(motionState);
|
|
|
|
DynamicJsonDocument doc(1024);
|
|
|
|
doc["deviceId"] = deviceId;
|
|
doc["value"] = motionState;
|
|
|
|
char buffer[1024];
|
|
size_t n = serializeJson(doc, buffer);
|
|
client.publish("motion", buffer, n);
|
|
|
|
lastMotionState = motionState;
|
|
}
|
|
|
|
uint16_t soundValue = analogRead(mic_pin);
|
|
|
|
if (soundValue > 3072 && (currentMillis - previousHighestSoundMillis > 1000))
|
|
{
|
|
previousHighestSoundMillis = currentMillis;
|
|
|
|
DynamicJsonDocument doc(1024);
|
|
|
|
doc["deviceId"] = deviceId;
|
|
doc["value"] = soundValue;
|
|
|
|
char buffer[1024];
|
|
size_t n = serializeJson(doc, buffer);
|
|
client.publish("sound", buffer, n);
|
|
}
|
|
} |