Skip to content

Instantly share code, notes, and snippets.

@TimBroddin
Created January 14, 2021 08:34
Show Gist options
  • Save TimBroddin/5a26f8a42f78ee2171968015d66928be to your computer and use it in GitHub Desktop.
Save TimBroddin/5a26f8a42f78ee2171968015d66928be to your computer and use it in GitHub Desktop.
ADC_MODE(33)
#include <timer.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiClient.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Ticker.h>
#define DHTPIN D2
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE, 15);
Timer<10> timer;
// DISPLAY
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define MQTT_SERVER "xxx.be"
#define MQTT_SERVERPORT 1883
#define MQTT_USERNAME "xxx"
#define MQTT_KEY "xxx"
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_USERNAME, MQTT_KEY);
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/temperature");
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/humidity");
Adafruit_MQTT_Publish movement = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/movement");
Adafruit_MQTT_Publish light = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/light");
Adafruit_MQTT_Publish sound = Adafruit_MQTT_Publish(&mqtt, MQTT_USERNAME "/sound");
Adafruit_MQTT_Subscribe laser = Adafruit_MQTT_Subscribe(&mqtt, MQTT_USERNAME "/laser", MQTT_QOS_1);
ESP8266WiFiMulti WiFiMulti;
uint32_t delayMS;
#define LASERPIN D1
#define PIRPIN D3
#define LIGHTPIN A0
#define MICPIN D6
bool soundDetected = false;
bool motionDetected = false;
bool sendStats(void *) {
MQTT_connect();
sensors_event_t event;
dht.temperature().getEvent(&event);
display.clearDisplay();
display.setCursor(0,0);
if (isnan(event.temperature)) {
display.println("T: <error>");
}
else {
display.print("T: " );
display.print((float)event.temperature);
display.print("C\n");
if (!temperature.publish((float)event.temperature)) {
Serial.println("Cant publish temperature");
}
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
display.println("H: <error>");
}
else {
display.print("H: " );
display.print((float)event.relative_humidity);
display.print("%\n");
if (!humidity.publish(event.relative_humidity)) {
Serial.println("Cant publish humidity");
}
}
float lightValue = map(analogRead(LIGHTPIN), 0, 1024, 0, 100);
display.print("Light: ");
display.print(lightValue);
display.print("%\n");
light.publish(lightValue);
if (soundDetected) {
display.println("Sound detected");
sound.publish(soundDetected);
}
if (motionDetected) {
display.println("Motion detected");
movement.publish(motionDetected);
}
soundDetected = false;
motionDetected = false;
display.display();
return true;
}
ICACHE_RAM_ATTR void detectsMovement() {
motionDetected = true;
}
ICACHE_RAM_ATTR void detectsSound() {
soundDetected = true;
}
void laserCallback(double state) {
Serial.println("LASER");
Serial.println(state);
if (state > 0) {
analogWrite(LASERPIN, state);
} else {
analogWrite(LASERPIN, 0);
}
}
void setup() {
Serial.begin(9600);
pinMode(PIRPIN, INPUT_PULLUP);
pinMode(MICPIN, INPUT);
pinMode(LIGHTPIN, INPUT);
pinMode(LASERPIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(PIRPIN), detectsMovement, RISING);
attachInterrupt(digitalPinToInterrupt(MICPIN), detectsSound, RISING);
Wire.begin(D4, D5);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Initialize device.
dht.begin();
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("VRT_Open", "");
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.println("Connecting to Wifi");
display.display();
while (WiFiMulti.run() != WL_CONNECTED) {
delay(100);
}
display.clearDisplay();
display.setCursor(0,0);
display.println("Connected to: ");
display.println(WiFi.SSID());
display.display();
laser.setCallback(laserCallback);
mqtt.subscribe(&laser);
analogWrite(LASERPIN, 1023);
timer.every(10*1000, sendStats);
}
void loop() {
mqtt.processPackets(10);
timer.tick();
}
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
Serial.println("MQTT Connected!");
}
mqtt.subscribe(&laser);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment