Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********
- Rui Santos
- Complete project details at https://RandomNerdTutorials.com
- *********/
- // Import required libraries
- #ifdef ESP32
- #include <WiFi.h>
- #include <ESPAsyncWebServer.h>
- #else
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <Hash.h>
- #include <ESPAsyncTCP.h>
- #include <ESPAsyncWebServer.h>
- #endif
- #include <OneWire.h>
- #include <DallasTemperature.h>
- // Data wire is connected to GPIO 4
- #define ONE_WIRE_BUS 13
- // Setup a oneWire instance to communicate with any OneWire devices
- OneWire oneWire(ONE_WIRE_BUS);
- // Pass our oneWire reference to Dallas Temperature sensor
- DallasTemperature sensors(&oneWire);
- // Replace with your network credentials
- const char* ssid = "BlackHawk";
- const char* password = "password";
- //webpassword
- const char* http_username = "admin";
- const char* http_password = "p@ssw0rd";
- const char* PARAMETER_INPUT_1 = "state";
- const int output = 2;
- // Create AsyncWebServer object on port 80
- AsyncWebServer server(80);
- String readDSTemperatureC() {
- // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
- sensors.requestTemperatures();
- float tempC = sensors.getTempCByIndex(0);
- if(tempC == -127.00) {
- Serial.println("Failed to read from DS18B20 sensor");
- return "--";
- } else {
- Serial.print("Temperature Celsius: ");
- Serial.println(tempC);
- }
- return String(tempC);
- }
- const char index_html[] PROGMEM = R"rawliteral(
- <!DOCTYPE HTML><html>
- <head>
- <title>ServerRoom Monitoring</title>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
- <style>
- html {
- font-family: Arial;
- display: inline-block;
- margin: 0px auto;
- text-align: center;
- }
- h2 { font-size: 3.0rem; }
- p { font-size: 3.0rem; }
- .units { font-size: 1.2rem; }
- .ds-labels{
- font-size: 1.5rem;
- vertical-align:middle;
- padding-bottom: 15px;
- }
- </style>
- </head>
- <body>
- <h2>ServerRoomTemp Monitoring</h2>
- <button onclick="logoutButton()">Logout</button>
- %BUTTONPLACEHOLDER%
- <h2>ESP DS18B20 Server</h2>
- <p>
- <i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
- <span class="ds-labels">Temperature Celsius</span>
- <span id="temperaturec">%TEMPERATUREC%</span>
- <sup class="units">°C</sup>
- </p>
- </body>
- <script>
- //logout button
- function logoutButton() {
- var xhr = new XMLHttpRequest();
- xhr.open("GET", "/logout", true);
- xhr.send();
- setTimeout(function(){ window.open("/logged-out","_self"); }, 1000);
- }
- setInterval(function ( ) {
- var xhttp = new XMLHttpRequest();
- xhttp.onreadystatechange = function() {
- if (this.readyState == 4 && this.status == 200) {
- document.getElementById("temperaturec").innerHTML = this.responseText;
- }
- };
- xhttp.open("GET", "/temperaturec", true);
- xhttp.send();
- }, 10000) ;
- </script>
- </html>)rawliteral";
- //logout_html
- const char logout_html[] PROGMEM = R"rawliteral(
- <!DOCTYPE HTML><html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- </head>
- <body>
- <p>Logged out or <a href="/">return to homepage</a>.</p>
- <p><strong>Note:</strong> close all web browser tabs to complete the logout process.</p>
- </body>
- </html>
- )rawliteral";
- // Replaces placeholder with DHT values
- String processor(const String& var){
- //Serial.println(var);
- if(var == "TEMPERATUREC"){
- return readDSTemperatureC();
- }
- return String();
- }
- // SIM card PIN (leave empty, if not defined)
- const char simPIN[] = "";
- // Your phone number to send SMS: + (plus sign) and country code, for Portugal +351, followed by phone number
- // SMS_TARGET Example for Portugal +351XXXXXXXXX
- #define SMS_TARGET "+639162396438"
- #define Recepient1 "+639534135986"
- // Define your temperature Threshold (in this case it's 28.0 degrees Celsius)
- float temperatureThreshold = 28.0;
- // Flag variable to keep track if alert SMS was sent or not
- bool smsSent = false;
- // Configure TinyGSM library
- #define TINY_GSM_MODEM_SIM800 // Modem is SIM800
- #define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
- #include <Wire.h>
- #include <TinyGsmClient.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- // GPIO where the DS18B20 is connected to
- const int oneWireBus = 13;
- // Setup a oneWire instance to communicate with any OneWire devices
- OneWire oneWire(oneWireBus);
- // Pass our oneWire reference to Dallas Temperature sensor
- DallasTemperature sensors(&oneWire);
- // TTGO T-Call pins
- #define MODEM_RST 5
- #define MODEM_PWKEY 4
- #define MODEM_POWER_ON 23
- #define MODEM_TX 27
- #define MODEM_RX 26
- #define I2C_SDA 21
- #define I2C_SCL 22
- // Set serial for debug console (to Serial Monitor, default speed 115200)
- #define SerialMon Serial
- // Set serial for AT commands (to SIM800 module)
- #define SerialAT Serial1
- // Define the serial console for debug prints, if needed
- //#define DUMP_AT_COMMANDS
- #ifdef DUMP_AT_COMMANDS
- #include <StreamDebugger.h>
- StreamDebugger debugger(SerialAT, SerialMon);
- TinyGsm modem(debugger);
- #else
- TinyGsm modem(SerialAT);
- #endif
- #define IP5306_ADDR 0x75
- #define IP5306_REG_SYS_CTL0 0x00
- bool setPowerBoostKeepOn(int en){
- Wire.beginTransmission(IP5306_ADDR);
- Wire.write(IP5306_REG_SYS_CTL0);
- if (en) {
- Wire.write(0x37); // Set bit1: 1 enable 0 disable boost keep on
- } else {
- Wire.write(0x35); // 0x37 is default reg value
- }
- return Wire.endTransmission() == 0;
- }
- void setup(){
- // Serial port for debugging purposes
- Serial.begin(115200);
- Serial.println();
- // Keep power when running from battery
- Wire.begin(I2C_SDA, I2C_SCL);
- bool isOk = setPowerBoostKeepOn(1);
- SerialMon.println(String("IP5306 KeepOn ") + (isOk ? "OK" : "FAIL"));
- // Set modem reset, enable, power pins
- pinMode(MODEM_PWKEY, OUTPUT);
- pinMode(MODEM_RST, OUTPUT);
- pinMode(MODEM_POWER_ON, OUTPUT);
- digitalWrite(MODEM_PWKEY, LOW);
- digitalWrite(MODEM_RST, HIGH);
- digitalWrite(MODEM_POWER_ON, HIGH);
- // Set GSM module baud rate and UART pins
- SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
- delay(3000);
- // Restart SIM800 module, it takes quite some time
- // To skip it, call init() instead of restart()
- SerialMon.println("Initializing modem...");
- modem.restart();
- // use modem.init() if you don't need the complete restart
- // Unlock your SIM card with a PIN if needed
- if (strlen(simPIN) && modem.getSimStatus() != 3 ) {
- modem.simUnlock(simPIN);
- }
- // Start up the DS18B20 library
- sensors.begin();
- // Connect to Wi-Fi
- WiFi.begin(ssid, password);
- Serial.println("Connecting to WiFi");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print("Connecting to WiFi..");
- }
- Serial.println();
- // Print ESP Local IP Address
- Serial.println(WiFi.localIP());
- // Route for root / web page
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- if(!request->authenticate(http_username, http_password))
- return request->requestAuthentication();
- request->send_P(200, "text/html", index_html, processor);
- });
- //server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- //request->send_P(200, "text/html", index_html, processor);
- //});
- server.on("/logout", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(401);
- });
- server.on("/logged-out", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/html", logout_html, processor);
- });
- server.on("/temperaturec", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", readDSTemperatureC().c_str());
- });
- // Start server
- server.begin();
- }
- void loop(){
- sensors.requestTemperatures();
- // Temperature in Celsius degrees
- float temperature = sensors.getTempCByIndex(0);
- SerialMon.print(temperature);
- SerialMon.println("*C");
- // Check if temperature is above threshold and if it needs to send the SMS alert
- if((temperature > temperatureThreshold) && !smsSent){
- String smsMessage = String("Please check the Server Room! Temperature is now: ") +
- String(temperature) + String("C");
- //if(modem.sendSMS(SMS_TARGET, smsMessage)){
- if(modem.sendSMS(SMS_TARGET, smsMessage)){
- modem.sendSMS(Recepient1, smsMessage) + String(temperature) + String("C");
- SerialMon.println(smsMessage);
- smsSent = true;
- }
- else{
- SerialMon.println("SMS failed to send");
- }
- }
- // Check if temperature is below threshold and if it needs to send the SMS alert
- else if((temperature <= temperatureThreshold) && smsSent){
- String smsMessage = String("Temperature is now within the normal range: ") +
- String(temperature) + String("C");
- if(modem.sendSMS(SMS_TARGET, smsMessage)){
- modem.sendSMS(Recepient1, smsMessage) + String(temperature) + String("C");
- SerialMon.println(smsMessage);
- smsSent = false;
- }
- else{
- SerialMon.println("SMS failed to send");
- }
- }
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement