Advertisement
Guest User

temp_sms

a guest
May 31st, 2020
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.36 KB | None | 0 0
  1. /*********
  2.   Rui Santos
  3.   Complete project details at https://RandomNerdTutorials.com  
  4. *********/
  5.  
  6. // Import required libraries
  7. #ifdef ESP32
  8.   #include <WiFi.h>
  9.   #include <ESPAsyncWebServer.h>
  10. #else
  11.   #include <Arduino.h>
  12.   #include <ESP8266WiFi.h>
  13.   #include <Hash.h>
  14.   #include <ESPAsyncTCP.h>
  15.   #include <ESPAsyncWebServer.h>
  16. #endif
  17. #include <OneWire.h>
  18. #include <DallasTemperature.h>
  19.  
  20. // Data wire is connected to GPIO 4
  21. #define ONE_WIRE_BUS 13
  22.  
  23. // Setup a oneWire instance to communicate with any OneWire devices
  24. OneWire oneWire(ONE_WIRE_BUS);
  25.  
  26. // Pass our oneWire reference to Dallas Temperature sensor
  27. DallasTemperature sensors(&oneWire);
  28.  
  29. // Replace with your network credentials
  30. const char* ssid = "BlackHawk";
  31. const char* password = "password";
  32.  
  33. //webpassword
  34. const char* http_username = "admin";
  35. const char* http_password = "p@ssw0rd";
  36.  
  37. const char* PARAMETER_INPUT_1 = "state";
  38. const int output = 2;
  39.  
  40.  
  41. // Create AsyncWebServer object on port 80
  42. AsyncWebServer server(80);
  43.  
  44. String readDSTemperatureC() {
  45.   // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  46.   sensors.requestTemperatures();
  47.   float tempC = sensors.getTempCByIndex(0);
  48.  
  49.   if(tempC == -127.00) {
  50.     Serial.println("Failed to read from DS18B20 sensor");
  51.     return "--";
  52.   } else {
  53.     Serial.print("Temperature Celsius: ");
  54.     Serial.println(tempC);
  55.   }
  56.   return String(tempC);
  57. }
  58.  
  59. const char index_html[] PROGMEM = R"rawliteral(
  60. <!DOCTYPE HTML><html>
  61. <head>
  62.  <title>ServerRoom Monitoring</title>
  63.  <meta name="viewport" content="width=device-width, initial-scale=1">
  64.  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  65.   <style>
  66.     html {
  67.      font-family: Arial;
  68.      display: inline-block;
  69.      margin: 0px auto;
  70.      text-align: center;
  71.     }
  72.     h2 { font-size: 3.0rem; }
  73.     p { font-size: 3.0rem; }
  74.     .units { font-size: 1.2rem; }
  75.     .ds-labels{
  76.       font-size: 1.5rem;
  77.       vertical-align:middle;
  78.       padding-bottom: 15px;
  79.     }
  80.   </style>
  81. </head>
  82. <body>
  83.   <h2>ServerRoomTemp Monitoring</h2>
  84.   <button onclick="logoutButton()">Logout</button>
  85.   %BUTTONPLACEHOLDER%
  86.   <h2>ESP DS18B20 Server</h2>
  87.   <p>
  88.     <i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
  89.     <span class="ds-labels">Temperature Celsius</span>
  90.     <span id="temperaturec">%TEMPERATUREC%</span>
  91.     <sup class="units">&deg;C</sup>
  92.   </p>
  93. </body>
  94. <script>
  95. //logout button
  96.  
  97. function logoutButton() {
  98.   var xhr = new XMLHttpRequest();
  99.   xhr.open("GET", "/logout", true);
  100.   xhr.send();
  101.   setTimeout(function(){ window.open("/logged-out","_self"); }, 1000);
  102. }
  103.  
  104.  
  105.  
  106. setInterval(function ( ) {
  107.   var xhttp = new XMLHttpRequest();
  108.   xhttp.onreadystatechange = function() {
  109.     if (this.readyState == 4 && this.status == 200) {
  110.       document.getElementById("temperaturec").innerHTML = this.responseText;
  111.     }
  112.   };
  113.   xhttp.open("GET", "/temperaturec", true);
  114.   xhttp.send();
  115. }, 10000) ;
  116.  
  117. </script>
  118. </html>)rawliteral";
  119.  
  120. //logout_html
  121. const char logout_html[] PROGMEM = R"rawliteral(
  122. <!DOCTYPE HTML><html>
  123. <head>
  124.   <meta name="viewport" content="width=device-width, initial-scale=1">
  125. </head>
  126. <body>
  127.   <p>Logged out or <a href="/">return to homepage</a>.</p>
  128.   <p><strong>Note:</strong> close all web browser tabs to complete the logout process.</p>
  129. </body>
  130. </html>
  131. )rawliteral";
  132.  
  133. // Replaces placeholder with DHT values
  134. String processor(const String& var){
  135.  //Serial.println(var);
  136.  if(var == "TEMPERATUREC"){
  137.    return readDSTemperatureC();
  138.  }
  139.  
  140.  return String();
  141. }
  142.  
  143. // SIM card PIN (leave empty, if not defined)
  144. const char simPIN[]   = "";
  145.  
  146. // Your phone number to send SMS: + (plus sign) and country code, for Portugal +351, followed by phone number
  147. // SMS_TARGET Example for Portugal +351XXXXXXXXX
  148. #define SMS_TARGET  "+639162396438"
  149. #define Recepient1  "+639534135986"
  150.  
  151. // Define your temperature Threshold (in this case it's 28.0 degrees Celsius)
  152. float temperatureThreshold = 28.0;
  153.  
  154. // Flag variable to keep track if alert SMS was sent or not
  155. bool smsSent = false;
  156.  
  157. // Configure TinyGSM library
  158. #define TINY_GSM_MODEM_SIM800      // Modem is SIM800
  159. #define TINY_GSM_RX_BUFFER   1024  // Set RX buffer to 1Kb
  160.  
  161. #include <Wire.h>
  162. #include <TinyGsmClient.h>
  163. #include <OneWire.h>
  164. #include <DallasTemperature.h>
  165.  
  166. // GPIO where the DS18B20 is connected to
  167. const int oneWireBus = 13;    
  168.  
  169. // Setup a oneWire instance to communicate with any OneWire devices
  170. OneWire oneWire(oneWireBus);
  171.  
  172. // Pass our oneWire reference to Dallas Temperature sensor
  173. DallasTemperature sensors(&oneWire);
  174.  
  175. // TTGO T-Call pins
  176. #define MODEM_RST            5
  177. #define MODEM_PWKEY          4
  178. #define MODEM_POWER_ON       23
  179. #define MODEM_TX             27
  180. #define MODEM_RX             26
  181. #define I2C_SDA              21
  182. #define I2C_SCL              22
  183.  
  184. // Set serial for debug console (to Serial Monitor, default speed 115200)
  185. #define SerialMon Serial
  186. // Set serial for AT commands (to SIM800 module)
  187. #define SerialAT  Serial1
  188.  
  189. // Define the serial console for debug prints, if needed
  190. //#define DUMP_AT_COMMANDS
  191.  
  192. #ifdef DUMP_AT_COMMANDS
  193.  #include <StreamDebugger.h>
  194.  StreamDebugger debugger(SerialAT, SerialMon);
  195.  TinyGsm modem(debugger);
  196. #else
  197.  TinyGsm modem(SerialAT);
  198. #endif
  199.  
  200. #define IP5306_ADDR          0x75
  201. #define IP5306_REG_SYS_CTL0  0x00
  202.  
  203. bool setPowerBoostKeepOn(int en){
  204.  Wire.beginTransmission(IP5306_ADDR);
  205.  Wire.write(IP5306_REG_SYS_CTL0);
  206.  if (en) {
  207.    Wire.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
  208.  } else {
  209.    Wire.write(0x35); // 0x37 is default reg value
  210.  }
  211.  return Wire.endTransmission() == 0;
  212. }
  213.  
  214. void setup(){
  215.  // Serial port for debugging purposes
  216.  Serial.begin(115200);
  217.  Serial.println();
  218.  
  219.  // Keep power when running from battery
  220.  Wire.begin(I2C_SDA, I2C_SCL);
  221.  bool isOk = setPowerBoostKeepOn(1);
  222.  SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));
  223.  
  224.  // Set modem reset, enable, power pins
  225.  pinMode(MODEM_PWKEY, OUTPUT);
  226.  pinMode(MODEM_RST, OUTPUT);
  227.  pinMode(MODEM_POWER_ON, OUTPUT);
  228.  digitalWrite(MODEM_PWKEY, LOW);
  229.  digitalWrite(MODEM_RST, HIGH);
  230.  digitalWrite(MODEM_POWER_ON, HIGH);
  231.  
  232.  // Set GSM module baud rate and UART pins
  233.  SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
  234.  delay(3000);
  235.  
  236.  // Restart SIM800 module, it takes quite some time
  237.  // To skip it, call init() instead of restart()
  238.  SerialMon.println("Initializing modem...");
  239.  modem.restart();
  240.  // use modem.init() if you don't need the complete restart
  241.  
  242.  // Unlock your SIM card with a PIN if needed
  243.  if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
  244.    modem.simUnlock(simPIN);
  245.  }
  246.  
  247.  // Start up the DS18B20 library
  248.  sensors.begin();
  249.  
  250.  // Connect to Wi-Fi
  251.  WiFi.begin(ssid, password);
  252.  Serial.println("Connecting to WiFi");
  253.  while (WiFi.status() != WL_CONNECTED) {
  254.    delay(500);
  255.    Serial.print("Connecting to WiFi..");
  256.  }
  257.  Serial.println();
  258.  
  259.  // Print ESP Local IP Address
  260.  Serial.println(WiFi.localIP());
  261.  
  262.  // Route for root / web page
  263.  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  264.    if(!request->authenticate(http_username, http_password))
  265.      return request->requestAuthentication();
  266.    request->send_P(200, "text/html", index_html, processor);
  267.  });
  268.    
  269.  //server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  270.    //request->send_P(200, "text/html", index_html, processor);
  271.  //});
  272.  server.on("/logout", HTTP_GET, [](AsyncWebServerRequest *request){
  273.    request->send(401);
  274.  });
  275.  
  276.  server.on("/logged-out", HTTP_GET, [](AsyncWebServerRequest *request){
  277.    request->send_P(200, "text/html", logout_html, processor);
  278.  });
  279.  server.on("/temperaturec", HTTP_GET, [](AsyncWebServerRequest *request){
  280.    request->send_P(200, "text/plain", readDSTemperatureC().c_str());
  281.  });
  282.  
  283.  // Start server
  284.  server.begin();
  285. }
  286.  
  287. void loop(){
  288.  sensors.requestTemperatures();
  289.  // Temperature in Celsius degrees
  290.  float temperature = sensors.getTempCByIndex(0);
  291.  SerialMon.print(temperature);
  292.  SerialMon.println("*C");
  293.  
  294.  
  295.  // Check if temperature is above threshold and if it needs to send the SMS alert
  296.  if((temperature > temperatureThreshold) && !smsSent){
  297.    String smsMessage = String("Please check the Server Room! Temperature is now: ") +
  298.           String(temperature) + String("C");
  299.    //if(modem.sendSMS(SMS_TARGET, smsMessage)){
  300.    if(modem.sendSMS(SMS_TARGET, smsMessage)){
  301.           modem.sendSMS(Recepient1, smsMessage) + String(temperature) + String("C");
  302.           SerialMon.println(smsMessage);        
  303.           smsSent = true;        
  304.    }
  305.     else{
  306.      SerialMon.println("SMS failed to send");
  307.    }
  308.  }
  309.  // Check if temperature is below threshold and if it needs to send the SMS alert
  310.  else if((temperature <= temperatureThreshold) && smsSent){
  311.    String smsMessage = String("Temperature is now within the normal range: ") +
  312.           String(temperature) + String("C");
  313.    if(modem.sendSMS(SMS_TARGET, smsMessage)){
  314.      modem.sendSMS(Recepient1, smsMessage) + String(temperature) + String("C");
  315.      SerialMon.println(smsMessage);
  316.      smsSent = false;
  317.    
  318.    }
  319.    else{
  320.      SerialMon.println("SMS failed to send");
  321.    }
  322.  }
  323.  delay(2000);
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement