Advertisement
Syehaji93

Monitoring suhu dengan nodemcu firebase google spreadsheet

Mar 29th, 2022
1,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.69 KB | None | 0 0
  1. //----------------------------------------Include the NodeMCU ESP8266 Library
  2. #include <ESP8266WiFi.h>
  3. #include <WiFiClientSecure.h>
  4. #include <FirebaseArduino.h>
  5. #include <ArduinoJson.h>
  6. #include <ESP8266HTTPClient.h>
  7. //----------------------------------------
  8. //--> library suhu  //
  9. #include <OneWire.h>
  10. #include <DallasTemperature.h>
  11. #include <SPI.h>
  12. #include <Wire.h>
  13. #include <Adafruit_GFX.h>
  14. #include <Adafruit_SSD1306.h>
  15. //----------------------------------------
  16.  
  17. #define FIREBASE_HOST "smtsuhu-default-rtdb.firebaseio.com" //--> URL address of your Firebase Realtime Database.https://smtsuhu-default-rtdb.firebaseio.com/
  18. #define FIREBASE_AUTH "BZ12ZZwYiZlSGQrtMyIM3Lac00vUIFB3d57j39ti" //--> Your firebase database secret code.
  19.  
  20. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  21. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  22.  
  23. #define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  24. #define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
  25. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  26.  
  27. #define ONE_WIRE_BUS 14  // sensor diletakkan di pin D5
  28. #define pinBuzzerSuhu 12  // sensor diletakkan di pin D6
  29. #define pinBuzzerPintu 13  // sensor diletakkan di pin D7
  30. #define sensorPintu  2 // sensor diletakkan di pin D4
  31. #define inputReset  15 // sensor diletakkan di pin D8
  32. //-- End library suhu --//
  33.  
  34. #define ON_Board_LED 2  //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router
  35.  
  36. // setup sensor
  37. OneWire oneWire(ONE_WIRE_BUS);
  38.  
  39. // berikan nama variabel,masukkan ke pustaka Dallas
  40. DallasTemperature sensorSuhu(&oneWire);
  41.  
  42. float suhuSekarang;
  43. long millisSuhu;
  44. int timerVal = 300;
  45. int timerState = 1;
  46.  
  47. //variable pintu
  48. int S = 60; // count seconds
  49. long millisPintu;
  50. int pintuState = 1;
  51. //-- End setup suhu --//
  52.  
  53. //----------------------------------------SSID and Password of your WiFi router.
  54. const char* ssid = "Bandha.id"; //--> Your wifi name or SSID.
  55. const char* password = "soadZ443v"; //--> Your wifi password.
  56. //----------------------------------------
  57.  
  58. //----------------------------------------Host & httpsPort
  59. const char* host = "script.google.com";
  60. const int httpsPort = 443;
  61. //----------------------------------------
  62.  
  63. WiFiClientSecure client; //--> Create a WiFiClientSecure object.
  64.  
  65. String GAS_ID = "AKfycbwCmASMhQBeC34a4rJo-wQyQXQJnQljYweXx8RizSi13LVlfh4"; //--> spreadsheet script ID
  66.  
  67. //============================================================================== void setup
  68. void setup() {
  69.   // put your setup code here, to run once:
  70.   Serial.begin(115200);
  71.   delay(500);
  72.  
  73.   WiFi.begin(ssid, password); //--> Connect to your WiFi router
  74.   Serial.println("");
  75.  
  76.    //------------ Setup suhu ---------------//
  77.   pinMode(pinBuzzerSuhu, OUTPUT);
  78.   pinMode(pinBuzzerPintu, OUTPUT);
  79.   sensorSuhu.begin();
  80.  
  81.   pinMode(sensorPintu, INPUT_PULLUP); //input sensor pintu
  82.   pinMode(inputReset, INPUT_PULLUP); //input sensor reset
  83.   //------------ End setup suhu ---------------//
  84.    
  85.   pinMode(ON_Board_LED,OUTPUT); //--> On Board LED port Direction output
  86.   digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board
  87.  
  88.   //----------------------------------------Wait for connection
  89.   Serial.print("Connecting");
  90.   while (WiFi.status() != WL_CONNECTED) {
  91.     Serial.print(".");
  92.     //----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
  93.     digitalWrite(ON_Board_LED, LOW);
  94.     delay(250);
  95.     digitalWrite(ON_Board_LED, HIGH);
  96.     delay(250);
  97.     //----------------------------------------
  98.   }
  99.   //----------------------------------------
  100.   digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
  101.   //----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor
  102.   Serial.println("");
  103.   Serial.print("Successfully connected to : ");
  104.   Serial.println(ssid);
  105.   Serial.print("IP address: ");
  106.   Serial.println(WiFi.localIP());
  107.   Serial.println();
  108.   //----------------------------------------
  109.  
  110. //----------------------------------------Firebase Realtime Database Configuration.
  111.   Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  112.   //----------------------------------------
  113.  
  114.   client.setInsecure();
  115.  
  116.   //------------ Setup oled ---------------//
  117.   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  118.   if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
  119.     Serial.println(F("SSD1306 allocation failed"));
  120.     for(;;); // Don't proceed, loop forever
  121.   }
  122.   delay(2000);
  123.   display.clearDisplay();
  124.   display.setTextColor(WHITE);
  125.   //------------ End setup oled ---------------//
  126. }
  127. //==============================================================================
  128. //============================================================================== void loop
  129. void loop() {
  130.  
  131.   suhuSekarang = ambilSuhu();
  132.   int sensorState = digitalRead(sensorPintu);
  133.   Serial.println(suhuSekarang);
  134.   Serial.println(sensorState);
  135.  
  136.   sendData(suhuSekarang); //--> Calls the sendData Subroutine
  137.  
  138.   //----------------------------------------Send data in the form of random value data to the Firebase Realtime Database.
  139.   Firebase.setFloat("DataKulkas/Suhu",suhuSekarang); //--> Command or code to send data or update data (String data type) to the Firebase Realtime Database.
  140.   //Firebase.pushString("/RandomVal/Value", dataSend);
  141.  
  142.   // Conditions for handling errors.
  143.   if (Firebase.failed()) {
  144.       Serial.print("Setting /Suhu failed :");
  145.       Serial.println(Firebase.error());  
  146.       delay(500);
  147.       return;
  148.   }
  149.  
  150.   //----------------------------------------Send data in the form of random value data to the Firebase Realtime Database.
  151.   Firebase.setFloat("DataKulkas/Pintu",sensorState); //--> Command or code to send data or update data (String data type) to the Firebase Realtime Database.
  152.   //Firebase.pushString("/RandomVal/Value", dataSend);
  153.  
  154.   // Conditions for handling errors.
  155.   if (Firebase.failed()) {
  156.       Serial.print("Setting /Pintu failed :");
  157.       Serial.println(Firebase.error());  
  158.       delay(500);
  159.       return;
  160.   }
  161.  
  162.   Serial.println("Setting successful");
  163.   Serial.println();
  164.   //----------------------------------------
  165.  
  166.   display.setTextSize(1);
  167.   display.setCursor(0,0);
  168.   display.print("Suhu: ");
  169.   display.setTextSize(1);
  170.   display.setCursor(40,0);
  171.   display.print(suhuSekarang);
  172.   display.print(" ");
  173.   display.setTextSize(1);
  174.   display.cp437(true);
  175.   display.write(167);
  176.   display.setTextSize(1);
  177.   display.print("C");
  178.   display.display();
  179.  
  180.   if(suhuSekarang < 0)
  181.   {
  182.     //display.clearDisplay();
  183.     display.setTextSize(1);
  184.     display.setCursor(0,15);
  185.     display.print("Suhu terlalu rendah");
  186.     display.display();
  187.     alarmSuhuOn();
  188.   }
  189.   else if(suhuSekarang >= 0 && suhuSekarang <= 10.00)
  190.   {
  191.     //display.clearDisplay();
  192.     display.setTextSize(1);
  193.     display.setCursor(0,15);
  194.     display.print("Suhu normal");
  195.     display.display();
  196.     alarmSuhuOff();
  197.     timerState = 1;
  198.   }
  199.   else if(suhuSekarang > 10.00)
  200.   {
  201.     if(millis() > millisSuhu + 1000)
  202.     {
  203.       millisSuhu = millis();
  204.       if(timerState == 1)
  205.       {
  206.         timerVal--;
  207.         //display.clearDisplay();
  208.         display.setTextSize(1);
  209.         display.setCursor(0,15);
  210.         display.print("Awas Suhu Tinggi.!");
  211.         display.setTextSize(1);
  212.         display.setCursor(100,15);
  213.         display.print(timerVal);
  214.         display.display();
  215.         Serial.println(timerVal);
  216.  
  217.         if(timerVal < 100)
  218.         {
  219.           //display.clearDisplay();
  220.           display.setTextSize(1);
  221.           display.setCursor(100,15);
  222.           display.print(timerVal);
  223.           display.display();
  224.         }
  225.  
  226.         if(timerVal < 10)
  227.         {
  228.           //display.clearDisplay();
  229.           display.setTextSize(1);
  230.           display.setCursor(100,15);
  231.           display.print(timerVal);
  232.           display.display();
  233.         }
  234.        
  235.         if(timerVal <= 0)
  236.         {
  237.           //display.clearDisplay();
  238.           display.setTextSize(1);
  239.           display.setCursor(0,15);
  240.           display.print("Suhu tinggi > 5 M");
  241.           display.setTextSize(1);
  242.           display.setCursor(100,15);
  243.           display.print(timerVal);
  244.           display.display();
  245.           Serial.println(timerVal);
  246.           alarmSuhuOn();
  247.           timerState = 0;
  248.         }
  249.       }
  250.     }
  251.   }
  252.   /*-------------- End blok Kulkas ----------------*/
  253.  
  254.   /*-------------- Blok pintu ---------------*/
  255.   if(sensorState == 0)
  256.   {
  257.     if(millis() > millisPintu + 1000)
  258.     {
  259.       millisPintu = millis();
  260.       if(pintuState == 1)
  261.       {
  262.         S--;
  263.         //display.clearDisplay();
  264.         display.setTextSize(1);
  265.         display.setCursor(0, 30);
  266.         display.print("Awas Pintu Terbuka.!");
  267.         display.setTextSize(1);
  268.         display.setCursor(0, 50);
  269.         display.print(S);
  270.         display.display();
  271.        
  272.         if(S < 10)
  273.         {
  274.           //display.clearDisplay();
  275.           display.setTextSize(1);
  276.           display.setCursor(0, 30);
  277.           display.print("Awas Pintu Terbuka.!");
  278.           display.setTextSize(1);
  279.           display.setCursor(0, 50);
  280.           display.print(S);
  281.           display.display();
  282.         }
  283.        
  284.         if(S <= 0)
  285.         {
  286.           //display.clearDisplay();
  287.           display.setTextSize(1);
  288.           display.setCursor(0, 30);
  289.           display.print("Segera Tutup Pintu.!");
  290.           display.setTextSize(1);
  291.           display.setCursor(0, 50);
  292.           display.print(S);
  293.           display.display();
  294.           alarmPintuOn();
  295.          
  296.           pintuState = 0;
  297.         }
  298.       }
  299.     }
  300.   }
  301.   else if(sensorState == 1)
  302.   {
  303.     //lcd.clear();
  304.     //display.clearDisplay();
  305.     display.setTextSize(1);
  306.     display.setCursor(0, 30);
  307.     display.print("Pintu Tertutup.!");
  308.     display.display();
  309.     alarmPintuOff();
  310.     pintuState = 1;
  311.     S = 60;
  312.   }
  313.   /*-------------- end blok pintu ---------------*/
  314.  
  315.   //----------- Script for created variable suhu and pintu ------------//
  316. }
  317. //----------- End Void loop() -----------
  318. //==============================================================================
  319. //=========== void sendData
  320. float ambilSuhu()
  321. {
  322.   sensorSuhu.requestTemperatures();
  323.   float suhu = sensorSuhu.getTempCByIndex(0);
  324.   return suhu;
  325. }
  326. /*----------- End get value sensor suhu ---------------*/
  327.  
  328. /*----------- Created buzzer alarm -------------*/
  329. void alarmSuhuOn()
  330. {
  331.   digitalWrite(pinBuzzerSuhu, HIGH);
  332. }
  333.  
  334. void alarmSuhuOff()
  335. {
  336.   digitalWrite(pinBuzzerSuhu, LOW);
  337. }
  338.  
  339. void alarmPintuOn()
  340. {
  341.   digitalWrite(pinBuzzerPintu, HIGH);
  342. }
  343.  
  344. void alarmPintuOff()
  345. {
  346.   digitalWrite(pinBuzzerPintu, LOW);
  347. }
  348. /*----------- End buzzer alarm -------------*/
  349.  
  350. //============================================================================== void sendData
  351. // Subroutine for sending data to Google Sheets
  352. void sendData(float tem) {
  353.   Serial.println("==========");
  354.   Serial.print("connecting to ");
  355.   Serial.println(host);
  356.  
  357.   //----------------------------------------Connect to Google host
  358.   if (!client.connect(host, httpsPort)) {
  359.     Serial.println("connection failed");
  360.     return;
  361.   }
  362.   //----------------------------------------
  363.  
  364.   //----------------------------------------Processing data and sending data
  365.   String string_temperature =  String(tem);
  366.   // String string_temperature =  String(tem, DEC);
  367.   String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature;
  368.   Serial.print("requesting URL: ");
  369.   Serial.println(url);
  370.  
  371.   client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  372.          "Host: " + host + "\r\n" +
  373.          "User-Agent: BuildFailureDetectorESP8266\r\n" +
  374.          "Connection: close\r\n\r\n");
  375.  
  376.   Serial.println("request sent");
  377.   //----------------------------------------
  378.  
  379.   //----------------------------------------Checking whether the data was sent successfully or not
  380.   while (client.connected()) {
  381.     String line = client.readStringUntil('\n');
  382.     if (line == "\r") {
  383.       Serial.println("headers received");
  384.       break;
  385.     }
  386.   }
  387.   String line = client.readStringUntil('\n');
  388.   if (line.startsWith("{\"state\":\"success\"")) {
  389.     Serial.println("esp8266/Arduino CI successfull!");
  390.   } else {
  391.     Serial.println("esp8266/Arduino CI has failed");
  392.   }
  393.   Serial.print("reply was : ");
  394.   Serial.println(line);
  395.   Serial.println("closing connection");
  396.   Serial.println("==========");
  397.   Serial.println();
  398.   //----------------------------------------
  399. }
  400. //==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement