Advertisement
Guest User

wtfff

a guest
Aug 19th, 2018
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include "AlertMe.h" // This includes all of the references to WiFiManager and ArduinoJSON that we need
  4. #include "DHT.h"
  5. #define DHTPIN 4
  6. #define DHTTYPE DHT22
  7. ADC_MODE(ADC_VCC);
  8. DHT dht(DHTPIN, DHTTYPE);
  9. AlertMe alert;
  10.  
  11. byte config_pin = 12;
  12. const char* mqttServer = "192.168.1.107";
  13. const int mqttPort = 1883;
  14. const char* mqttUser = "mqttuser";
  15. const char* mqttPassword = "ibanez";
  16. WiFiClient espClient10;
  17. PubSubClient client (espClient10);
  18. const byte interruptPin = 14;
  19. volatile int doorState = 0;
  20.  
  21. String to_email = "omnitk@gmail.com"; // The email address to send the message to
  22. 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/
  23.  
  24.  
  25. void setup() {
  26. Serial.begin(115200);
  27. pinMode(interruptPin, INPUT_PULLUP);
  28. attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, CHANGE);
  29. pinMode(config_pin,INPUT_PULLUP);
  30. if(config_pin == LOW){ // Short pin to GND for config AP
  31. alert.config();
  32. }
  33.  
  34. alert.connect();
  35.  
  36. client.setServer(mqttServer, mqttPort);
  37. client.setCallback(callback);
  38.  
  39. while (!client.connected()) {
  40. Serial.println("Connecting to MQTT...");
  41.  
  42. if (client.connect("EspMessanger", mqttUser, mqttPassword )) {
  43. Serial.println("Connected");
  44. }
  45.  
  46. else {
  47. Serial.print("Failed with State ");
  48. Serial.print(client.state());
  49. delay(2000);
  50. }
  51. }
  52.  
  53. client.publish("esp/test", "Hello from EspMessanger");
  54. client.subscribe("esp/test");
  55. client.subscribe("switches/test");
  56.  
  57.  
  58. }
  59.  
  60. void handleInterrupt() {
  61. //debouncer ___________________
  62. static unsigned long last_interrupt_time = 0;
  63. unsigned long interrupt_time = millis();
  64. //debouncer ^^^^^^^^^^^^^^^^^^^
  65. // actual task to perform during interrupt
  66. if (interrupt_time - last_interrupt_time > 200) {
  67.  
  68. doorState = digitalRead(14);
  69. if (doorState != 0){
  70. client.publish("door/open", String(doorState).c_str(), true);
  71. Serial.println(alert.send("switches", "doorOpen", to_sms));
  72. } else {
  73. client.publish("door/open", String(doorState).c_str(), true);
  74. Serial.println(alert.send("switches", "The Garage Door Is CLOSED", to_sms));
  75. }
  76. }
  77. // debouncer _______________________
  78. last_interrupt_time = interrupt_time;
  79. // debouncer ^^^^^^^^^^^^^^^^^^^^^^^
  80. }
  81.  
  82. void callback(char* topic, byte* payload, unsigned int length) {
  83.  
  84. Serial.print("Message arrived in topic: ");
  85. Serial.println(topic);
  86.  
  87. Serial.print("Message:");
  88. for (int i = 0; i < length; i++) {
  89. Serial.print((char)payload[i]);
  90. }
  91.  
  92. Serial.println();
  93. Serial.println("-----------------------");
  94.  
  95. /*if ((char)payload[0] == '0') {
  96. digitalWrite(ledder, LOW);
  97. pinState = digitalRead(ledder);
  98. client.publish("switches/state", String(pinState).c_str(), true);
  99.  
  100. }
  101. if ((char)payload[0] == '1') {
  102. digitalWrite(ledder, HIGH);
  103. pinState = digitalRead(ledder);
  104. client.publish("switches/state", String(pinState).c_str(), true);
  105. alert.send("switches", "The switch was turned off", to_sms);
  106. }*/
  107. }
  108.  
  109. void reconnectMQTT() {
  110. if (WiFi.status() == WL_CONNECTED) {
  111. while (!client.connected()) {
  112. if (client.connect("EspMessanger", mqttUser, mqttPassword )) {
  113. client.publish("esp/test", "Hello again from EspMessanger!!");
  114. client.subscribe("esp/test");
  115. } else {
  116. delay(5000);
  117. }
  118. }
  119. }
  120. }
  121.  
  122. int timeSinceLastRead = 0;
  123.  
  124. void loop() {
  125. //////////
  126. // Reed Switch and System voltage
  127. //////////
  128. float volts = ESP.getVcc() / 1000.0;
  129. client.publish("system/voltage", String(volts,2).c_str(), true);
  130. //////////
  131. // DHT SENSOR
  132. //////////
  133. if(timeSinceLastRead > 10000) {
  134.  
  135. float h = dht.readHumidity();
  136. float t = dht.readTemperature();
  137. float f = dht.readTemperature(true);
  138.  
  139. if (isnan(h) || isnan(t) | isnan(f)) {
  140. Serial.println("Failed to read from DHT sensor!");
  141. timeSinceLastRead = 0;
  142. return;
  143. }
  144. client.publish("dht22/temp2", String(f,1).c_str(), true);
  145. client.publish("dht22/humidity2", String(h,1).c_str(), true);
  146.  
  147. timeSinceLastRead = 0;
  148.  
  149. }
  150. delay(2000);
  151. timeSinceLastRead += 2000;
  152.  
  153. if (!client.connected()){
  154. reconnectMQTT();
  155. }
  156.  
  157. client.loop();
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement