Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include "AlertMe.h" // This includes all of the references to WiFiManager and ArduinoJSON that we need
- #include "DHT.h"
- #define DHTPIN 4
- #define DHTTYPE DHT22
- ADC_MODE(ADC_VCC);
- DHT dht(DHTPIN, DHTTYPE);
- AlertMe alert;
- byte config_pin = 12;
- const char* mqttServer = "192.168.1.107";
- const int mqttPort = 1883;
- const char* mqttUser = "mqttuser";
- const char* mqttPassword = "ibanez";
- WiFiClient espClient10;
- PubSubClient client (espClient10);
- const byte interruptPin = 14;
- volatile int doorState = 0;
- String to_email = "omnitk@gmail.com"; // The email address to send the message to
- String to_sms = "9032209124@txt.att.net"; // Look up your carrier's Email-to-SMS gateway here: https://martinfitzpatrick.name/list-of-email-to-sms-gateways/
- void setup() {
- Serial.begin(115200);
- pinMode(interruptPin, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, CHANGE);
- pinMode(config_pin,INPUT_PULLUP);
- if(config_pin == LOW){ // Short pin to GND for config AP
- alert.config();
- }
- alert.connect();
- client.setServer(mqttServer, mqttPort);
- client.setCallback(callback);
- while (!client.connected()) {
- Serial.println("Connecting to MQTT...");
- if (client.connect("EspMessanger", mqttUser, mqttPassword )) {
- Serial.println("Connected");
- }
- else {
- Serial.print("Failed with State ");
- Serial.print(client.state());
- delay(2000);
- }
- }
- client.publish("esp/test", "Hello from EspMessanger");
- client.subscribe("esp/test");
- client.subscribe("switches/test");
- }
- void handleInterrupt() {
- //debouncer ___________________
- static unsigned long last_interrupt_time = 0;
- unsigned long interrupt_time = millis();
- //debouncer ^^^^^^^^^^^^^^^^^^^
- // actual task to perform during interrupt
- if (interrupt_time - last_interrupt_time > 200) {
- doorState = digitalRead(14);
- if (doorState != 0){
- client.publish("door/open", String(doorState).c_str(), true);
- Serial.println(alert.send("switches", "doorOpen", to_sms));
- } else {
- client.publish("door/open", String(doorState).c_str(), true);
- Serial.println(alert.send("switches", "The Garage Door Is CLOSED", to_sms));
- }
- }
- // debouncer _______________________
- last_interrupt_time = interrupt_time;
- // debouncer ^^^^^^^^^^^^^^^^^^^^^^^
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived in topic: ");
- Serial.println(topic);
- Serial.print("Message:");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- Serial.println("-----------------------");
- /*if ((char)payload[0] == '0') {
- digitalWrite(ledder, LOW);
- pinState = digitalRead(ledder);
- client.publish("switches/state", String(pinState).c_str(), true);
- }
- if ((char)payload[0] == '1') {
- digitalWrite(ledder, HIGH);
- pinState = digitalRead(ledder);
- client.publish("switches/state", String(pinState).c_str(), true);
- alert.send("switches", "The switch was turned off", to_sms);
- }*/
- }
- void reconnectMQTT() {
- if (WiFi.status() == WL_CONNECTED) {
- while (!client.connected()) {
- if (client.connect("EspMessanger", mqttUser, mqttPassword )) {
- client.publish("esp/test", "Hello again from EspMessanger!!");
- client.subscribe("esp/test");
- } else {
- delay(5000);
- }
- }
- }
- }
- int timeSinceLastRead = 0;
- void loop() {
- //////////
- // Reed Switch and System voltage
- //////////
- float volts = ESP.getVcc() / 1000.0;
- client.publish("system/voltage", String(volts,2).c_str(), true);
- //////////
- // DHT SENSOR
- //////////
- if(timeSinceLastRead > 10000) {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- float f = dht.readTemperature(true);
- if (isnan(h) || isnan(t) | isnan(f)) {
- Serial.println("Failed to read from DHT sensor!");
- timeSinceLastRead = 0;
- return;
- }
- client.publish("dht22/temp2", String(f,1).c_str(), true);
- client.publish("dht22/humidity2", String(h,1).c_str(), true);
- timeSinceLastRead = 0;
- }
- delay(2000);
- timeSinceLastRead += 2000;
- if (!client.connected()){
- reconnectMQTT();
- }
- client.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement