PLC ve ESP8266 ile Google Firebase Realtime Database Veri Aliş Verişi

 PLC ve ESP8266 ile Google Firebase Realtime Database Veri Aliş Verişi






Kod:




Güncelleme:

/*

   Created by Pi BOTS MakerHub

   Email: pibotsmakerhub@gmail.com

   Github: https://github.com/pibotsmakerhub

   Copyright (c) 2020 Pi BOTS MakerHub


   Bu Proje ESP8266 ile Google Firebase Realtime Database veri alis verisi icin gerceklestirilmistir.

   Mese Bilisim Yazilim Mekatronik olarak projeyi PLC projeleri icin duzenliyoruz.

   Amacimiz PLC nin Cep Telefonundan kontrol edilmesidir.

   Yukarida ismi verilen "Pi BOTS MakerHub" a tesekkur ederiz.

   Projenin yapilisi ve gerceklestirilmesi "PLC ve ESP8266 ile Google Firebase Realtime Database Veri Alis Verisi" isimli blogda guncellenerek yayinlanacaktir.

   Proje Blog adresi: https://blog.mesebilisim.com/2021/07/plc-ve-esp8266-ile-google-firebase.html

   Duzenleyen: Mese Mekatronik

   Tarih: 25.09.2021

*/



#include "FirebaseESP8266.h"  // Arduino IDE uzerinden library sekmesinden "Firebase ESP8266" isimli kutuphane yuklenecek

#include <ESP8266WiFi.h> // ESP nin Wifi agina baglanmasi icin


// https://console.firebase.google.com/u/0/project/fir-esp8266-led-kontrol-4/overview


#define FIREBASE_HOST "fir-xxx-default-rtdb.xxx-west1.firebasedatabase.app"

#define FIREBASE_AUTH "dExxxxxxxxxxxxxxb7oYn"

#define WIFI_SSID "Xxxxxxxxxx"

#define WIFI_PASSWORD "xxxxxxxxxx***"


int led = 2;     // NodeMCU icin LED


FirebaseData firebaseData;


FirebaseJson json;


unsigned long myTime1 = 0;

unsigned long myTime2 = 0;


String cihaz_seri_no = "0001";



void setup() {


  Serial.begin(115200);


  pinMode(led, OUTPUT);


  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  Serial.print("WiFi baglantisi yapiliyor.");

  while (WiFi.status() != WL_CONNECTED)

  {

    Serial.print(".");

    delay(300);

  }

  Serial.println();

  Serial.print("Baglandi. IP: ");

  Serial.println(WiFi.localIP());

  Serial.println();


  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

  Firebase.reconnectWiFi(true);


} // setup



void sensorUpdate() {


  float t, h;

  t = random(0, 100);

  h = random(0, 100);


  if (Firebase.setFloat(firebaseData, "/cihaz_seri_no/ID" + String(cihaz_seri_no) + "/temperature", t))

  {

    Serial.println("PASSED");

    Serial.println("PATH: " + firebaseData.dataPath());

    Serial.println("TYPE: " + firebaseData.dataType());

    Serial.println("ETag: " + firebaseData.ETag());

    Serial.println("------------------------------------");

    Serial.println();

  }

  else

  {

    Serial.println("FAILED");

    Serial.println("REASON: " + firebaseData.errorReason());

    Serial.println("------------------------------------");

    Serial.println();

  }


  if (Firebase.setFloat(firebaseData, "/cihaz_seri_no/ID" + String(cihaz_seri_no) + "/humidity", h))

  {

    Serial.println("PASSED");

    Serial.println("PATH: " + firebaseData.dataPath());

    Serial.println("TYPE: " + firebaseData.dataType());

    Serial.println("ETag: " + firebaseData.ETag());

    Serial.println("------------------------------------");

    Serial.println();

  }

  else

  {

    Serial.println("FAILED");

    Serial.println("REASON: " + firebaseData.errorReason());

    Serial.println("------------------------------------");

    Serial.println();

  }


} // sensorUpdate



void loop() {


  if (!Firebase.pathExist(firebaseData, "/cihaz_seri_no/ID" + String(cihaz_seri_no) + "/led")) {

    if (Firebase.setFloat(firebaseData, "/cihaz_seri_no/ID" + String(cihaz_seri_no) + "/led", 1))

    {

      Serial.println("PASSED");

      Serial.println("PATH: " + firebaseData.dataPath());

      Serial.println("TYPE: " + firebaseData.dataType());

      Serial.println("ETag: " + firebaseData.ETag());

      Serial.println("------------------------------------");

      Serial.println();

    }

    else

    {

      Serial.println("FAILED");

      Serial.println("REASON: " + firebaseData.errorReason());

      Serial.println("------------------------------------");

      Serial.println();

    }

  }


  myTime2 = millis() / 1000;


  if (myTime2 - myTime1 > 10) { // 10 sn.

    Serial.println("Zaman doldu. Yeni verileri set et!");

    sensorUpdate();

    myTime1 = millis() / 1000;

  }


  if (Firebase.getInt(firebaseData, "/cihaz_seri_no/ID" + String(cihaz_seri_no) + "/led")) {

    Serial.print("Led durum: ");

    Serial.println(firebaseData.intData());

    if (firebaseData.intData() == 1) {

      digitalWrite(led, 0);

    }

    else if (firebaseData.intData() == 0) {

      digitalWrite(led, 1);

    }

  }

  delay(500);


} // loop