Advertisement
HonokaXD

Main code 1.3

Jun 18th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. #include <FirebaseArduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <Wire.h>
  4. #include <BH1750.h>
  5. const char* ssid     = /*"chaiyawat6636"*/"My ASUS";
  6. const char* password = /*"0878202424"*/"Jesus123";
  7. #define FIREBASE_HOST  "espdemo-551fa.firebaseio.com"
  8. #define FIREBASE_AUTH  "XWjA9bS3HxjNSBieqKWzAPXMEps2pDUR92dlqzwH"
  9. //BH1750 lightMeter;
  10.  
  11. void setup()
  12. {
  13.   Initialization();
  14.   wifiConfig();
  15.   Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  16.   Firebase.stream("/Lightstatus");
  17. }
  18.  
  19.  
  20. void loop()
  21. {
  22.   onOffLight(int pin);
  23.   delay(1000);
  24. }
  25.  
  26. void Initialization()
  27. {
  28.     Serial.begin(9600);
  29.     pinMode(13, OUTPUT); // Initialize the I2C bus (BH1750 library doesn't do this automatically)
  30.     Wire.begin(2,0); // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);
  31. //    lightMeter.begin();
  32.     Serial.println(F("Measure Light Test begin"));
  33. }
  34.  
  35. void wifiConfig()
  36. {
  37.   int count = 0;
  38.   Serial.println("Starting....");
  39.   WiFi.begin(ssid, password);
  40.   Serial.println("Connecting wifi..");
  41.    while (WiFi.status() != WL_CONNECTED)
  42.    {
  43.       delay(250);
  44.       Serial.print(".");
  45.       count++;
  46.       if(count == 30)
  47.       {
  48.         Serial.println("Wifi Connection Failed");
  49.         exit;
  50.       }
  51.    }
  52.    Serial.println("WiFi connected");  
  53.    Serial.println("IP address: ");
  54.    Serial.println(WiFi.localIP());
  55.   if(WiFi.status() == WL_CONNECTED)
  56.   { Serial.println("Wifi Connection Success"); }
  57.   else
  58.   { Serial.println("Wifi Connection Failed"); exit; }
  59. }
  60.  
  61. bool onOffLight(int pin)
  62. {
  63.   int light;
  64.   Firebase.getInt("/Lightstatus",&light);
  65.   if(light){ digitalWrite(pin,lightCommand); return true; }
  66.   else{ Serial.print("onOffLight ERROR"); return false; }
  67. }
  68.  
  69. void measureLight()
  70. {
  71.   float lux = lightMeter.readLightLevel();
  72.   Firebase.setFloat("/Lux",lux);
  73.   Serial.print("Light: ");
  74.   Serial.print(lux);
  75.   Serial.println(" lx");
  76.   analogWrite(13,255);
  77.   if(lux < 50)
  78.   {
  79.     Serial.println("too less light");
  80.     analogWrite(13,255);  
  81.   }
  82.   if(lux > 100)
  83.   {
  84.     Serial.println("too much light");
  85.     analogWrite(13,0);
  86.     analogWrite(13,255);
  87.   }
  88. //  float lux_ratio = (40.0-lux)/40.0;
  89. //  if(lux_ratio < 0) lux_ratio = 0;
  90. //  analogWrite(13,255*lux_ratio);
  91. //  Serial.println(255*lux_ratio);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement