Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------------------------Include the NodeMCU ESP8266 Library
- #include <ESP8266WiFi.h>
- #include <WiFiClientSecure.h>
- #include <FirebaseArduino.h>
- #include <ArduinoJson.h>
- #include <ESP8266HTTPClient.h>
- //----------------------------------------
- //--> library suhu //
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- //----------------------------------------
- #define FIREBASE_HOST "smtsuhu-default-rtdb.firebaseio.com" //--> URL address of your Firebase Realtime Database.https://smtsuhu-default-rtdb.firebaseio.com/
- #define FIREBASE_AUTH "BZ12ZZwYiZlSGQrtMyIM3Lac00vUIFB3d57j39ti" //--> Your firebase database secret code.
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
- #define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- #define ONE_WIRE_BUS 14 // sensor diletakkan di pin D5
- #define pinBuzzerSuhu 12 // sensor diletakkan di pin D6
- #define pinBuzzerPintu 13 // sensor diletakkan di pin D7
- #define sensorPintu 2 // sensor diletakkan di pin D4
- #define inputReset 15 // sensor diletakkan di pin D8
- //-- End library suhu --//
- #define ON_Board_LED 2 //--> Defining an On Board LED, used for indicators when the process of connecting to a wifi router
- // setup sensor
- OneWire oneWire(ONE_WIRE_BUS);
- // berikan nama variabel,masukkan ke pustaka Dallas
- DallasTemperature sensorSuhu(&oneWire);
- float suhuSekarang;
- long millisSuhu;
- int timerVal = 300;
- int timerState = 1;
- //variable pintu
- int S = 60; // count seconds
- long millisPintu;
- int pintuState = 1;
- //-- End setup suhu --//
- //----------------------------------------SSID and Password of your WiFi router.
- const char* ssid = "Bandha.id"; //--> Your wifi name or SSID.
- const char* password = "soadZ443v"; //--> Your wifi password.
- //----------------------------------------
- //----------------------------------------Host & httpsPort
- const char* host = "script.google.com";
- const int httpsPort = 443;
- //----------------------------------------
- WiFiClientSecure client; //--> Create a WiFiClientSecure object.
- String GAS_ID = "AKfycbwCmASMhQBeC34a4rJo-wQyQXQJnQljYweXx8RizSi13LVlfh4"; //--> spreadsheet script ID
- //============================================================================== void setup
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- delay(500);
- WiFi.begin(ssid, password); //--> Connect to your WiFi router
- Serial.println("");
- //------------ Setup suhu ---------------//
- pinMode(pinBuzzerSuhu, OUTPUT);
- pinMode(pinBuzzerPintu, OUTPUT);
- sensorSuhu.begin();
- pinMode(sensorPintu, INPUT_PULLUP); //input sensor pintu
- pinMode(inputReset, INPUT_PULLUP); //input sensor reset
- //------------ End setup suhu ---------------//
- pinMode(ON_Board_LED,OUTPUT); //--> On Board LED port Direction output
- digitalWrite(ON_Board_LED, HIGH); //--> Turn off Led On Board
- //----------------------------------------Wait for connection
- Serial.print("Connecting");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- //----------------------------------------Make the On Board Flashing LED on the process of connecting to the wifi router.
- digitalWrite(ON_Board_LED, LOW);
- delay(250);
- digitalWrite(ON_Board_LED, HIGH);
- delay(250);
- //----------------------------------------
- }
- //----------------------------------------
- digitalWrite(ON_Board_LED, HIGH); //--> Turn off the On Board LED when it is connected to the wifi router.
- //----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor
- Serial.println("");
- Serial.print("Successfully connected to : ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- Serial.println();
- //----------------------------------------
- //----------------------------------------Firebase Realtime Database Configuration.
- Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
- //----------------------------------------
- client.setInsecure();
- //------------ Setup oled ---------------//
- // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
- if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
- Serial.println(F("SSD1306 allocation failed"));
- for(;;); // Don't proceed, loop forever
- }
- delay(2000);
- display.clearDisplay();
- display.setTextColor(WHITE);
- //------------ End setup oled ---------------//
- }
- //==============================================================================
- //============================================================================== void loop
- void loop() {
- suhuSekarang = ambilSuhu();
- int sensorState = digitalRead(sensorPintu);
- Serial.println(suhuSekarang);
- Serial.println(sensorState);
- sendData(suhuSekarang); //--> Calls the sendData Subroutine
- //----------------------------------------Send data in the form of random value data to the Firebase Realtime Database.
- Firebase.setFloat("DataKulkas/Suhu",suhuSekarang); //--> Command or code to send data or update data (String data type) to the Firebase Realtime Database.
- //Firebase.pushString("/RandomVal/Value", dataSend);
- // Conditions for handling errors.
- if (Firebase.failed()) {
- Serial.print("Setting /Suhu failed :");
- Serial.println(Firebase.error());
- delay(500);
- return;
- }
- //----------------------------------------Send data in the form of random value data to the Firebase Realtime Database.
- Firebase.setFloat("DataKulkas/Pintu",sensorState); //--> Command or code to send data or update data (String data type) to the Firebase Realtime Database.
- //Firebase.pushString("/RandomVal/Value", dataSend);
- // Conditions for handling errors.
- if (Firebase.failed()) {
- Serial.print("Setting /Pintu failed :");
- Serial.println(Firebase.error());
- delay(500);
- return;
- }
- Serial.println("Setting successful");
- Serial.println();
- //----------------------------------------
- display.setTextSize(1);
- display.setCursor(0,0);
- display.print("Suhu: ");
- display.setTextSize(1);
- display.setCursor(40,0);
- display.print(suhuSekarang);
- display.print(" ");
- display.setTextSize(1);
- display.cp437(true);
- display.write(167);
- display.setTextSize(1);
- display.print("C");
- display.display();
- if(suhuSekarang < 0)
- {
- //display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(0,15);
- display.print("Suhu terlalu rendah");
- display.display();
- alarmSuhuOn();
- }
- else if(suhuSekarang >= 0 && suhuSekarang <= 10.00)
- {
- //display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(0,15);
- display.print("Suhu normal");
- display.display();
- alarmSuhuOff();
- timerState = 1;
- }
- else if(suhuSekarang > 10.00)
- {
- if(millis() > millisSuhu + 1000)
- {
- millisSuhu = millis();
- if(timerState == 1)
- {
- timerVal--;
- //display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(0,15);
- display.print("Awas Suhu Tinggi.!");
- display.setTextSize(1);
- display.setCursor(100,15);
- display.print(timerVal);
- display.display();
- Serial.println(timerVal);
- if(timerVal < 100)
- {
- //display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(100,15);
- display.print(timerVal);
- display.display();
- }
- if(timerVal < 10)
- {
- //display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(100,15);
- display.print(timerVal);
- display.display();
- }
- if(timerVal <= 0)
- {
- //display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(0,15);
- display.print("Suhu tinggi > 5 M");
- display.setTextSize(1);
- display.setCursor(100,15);
- display.print(timerVal);
- display.display();
- Serial.println(timerVal);
- alarmSuhuOn();
- timerState = 0;
- }
- }
- }
- }
- /*-------------- End blok Kulkas ----------------*/
- /*-------------- Blok pintu ---------------*/
- if(sensorState == 0)
- {
- if(millis() > millisPintu + 1000)
- {
- millisPintu = millis();
- if(pintuState == 1)
- {
- S--;
- //display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(0, 30);
- display.print("Awas Pintu Terbuka.!");
- display.setTextSize(1);
- display.setCursor(0, 50);
- display.print(S);
- display.display();
- if(S < 10)
- {
- //display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(0, 30);
- display.print("Awas Pintu Terbuka.!");
- display.setTextSize(1);
- display.setCursor(0, 50);
- display.print(S);
- display.display();
- }
- if(S <= 0)
- {
- //display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(0, 30);
- display.print("Segera Tutup Pintu.!");
- display.setTextSize(1);
- display.setCursor(0, 50);
- display.print(S);
- display.display();
- alarmPintuOn();
- pintuState = 0;
- }
- }
- }
- }
- else if(sensorState == 1)
- {
- //lcd.clear();
- //display.clearDisplay();
- display.setTextSize(1);
- display.setCursor(0, 30);
- display.print("Pintu Tertutup.!");
- display.display();
- alarmPintuOff();
- pintuState = 1;
- S = 60;
- }
- /*-------------- end blok pintu ---------------*/
- //----------- Script for created variable suhu and pintu ------------//
- }
- //----------- End Void loop() -----------
- //==============================================================================
- //=========== void sendData
- float ambilSuhu()
- {
- sensorSuhu.requestTemperatures();
- float suhu = sensorSuhu.getTempCByIndex(0);
- return suhu;
- }
- /*----------- End get value sensor suhu ---------------*/
- /*----------- Created buzzer alarm -------------*/
- void alarmSuhuOn()
- {
- digitalWrite(pinBuzzerSuhu, HIGH);
- }
- void alarmSuhuOff()
- {
- digitalWrite(pinBuzzerSuhu, LOW);
- }
- void alarmPintuOn()
- {
- digitalWrite(pinBuzzerPintu, HIGH);
- }
- void alarmPintuOff()
- {
- digitalWrite(pinBuzzerPintu, LOW);
- }
- /*----------- End buzzer alarm -------------*/
- //============================================================================== void sendData
- // Subroutine for sending data to Google Sheets
- void sendData(float tem) {
- Serial.println("==========");
- Serial.print("connecting to ");
- Serial.println(host);
- //----------------------------------------Connect to Google host
- if (!client.connect(host, httpsPort)) {
- Serial.println("connection failed");
- return;
- }
- //----------------------------------------
- //----------------------------------------Processing data and sending data
- String string_temperature = String(tem);
- // String string_temperature = String(tem, DEC);
- String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature;
- Serial.print("requesting URL: ");
- Serial.println(url);
- client.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "User-Agent: BuildFailureDetectorESP8266\r\n" +
- "Connection: close\r\n\r\n");
- Serial.println("request sent");
- //----------------------------------------
- //----------------------------------------Checking whether the data was sent successfully or not
- while (client.connected()) {
- String line = client.readStringUntil('\n');
- if (line == "\r") {
- Serial.println("headers received");
- break;
- }
- }
- String line = client.readStringUntil('\n');
- if (line.startsWith("{\"state\":\"success\"")) {
- Serial.println("esp8266/Arduino CI successfull!");
- } else {
- Serial.println("esp8266/Arduino CI has failed");
- }
- Serial.print("reply was : ");
- Serial.println(line);
- Serial.println("closing connection");
- Serial.println("==========");
- Serial.println();
- //----------------------------------------
- }
- //==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement