Facebook
From Queen Prairie Dog, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 312
  1. //
  2. // Copyright 2015 Google Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. //     http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16.  
  17. // FirebaseDemo_ESP8266 is a sample that demo the different functions
  18. // of the FirebaseArduino API.
  19.  
  20. #include <ESP8266WiFi.h>
  21. #include <FirebaseArduino.h>
  22.  
  23. // Set these to run example.
  24. #define FIREBASE_HOST ""
  25. #define FIREBASE_AUTH ""
  26. #define WIFI_SSID ""
  27. #define WIFI_PASSWORD ""
  28.  
  29. void setup() {
  30.   Serial.begin(9600);
  31.  
  32.   // connect to wifi.
  33.   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  34.   Serial.print("connecting");
  35.   while (WiFi.status() != WL_CONNECTED) {
  36.     Serial.print(".");
  37.     delay(500);
  38.   }
  39.   Serial.println();
  40.   Serial.print("connected: ");
  41.   Serial.println(WiFi.localIP());
  42.  
  43.   Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  44. }
  45.  
  46. int n = 0;
  47.  
  48. void loop() {
  49.   // set value
  50.   Firebase.setFloat("number", 42.0);
  51.   // handle error
  52.   if (Firebase.failed()) {
  53.       Serial.print("setting /number failed:");
  54.       Serial.println(Firebase.error());  
  55.       return;
  56.   }
  57.   delay(1000);
  58.  
  59.   // update value
  60.   Firebase.setFloat("number", 43.0);
  61.   // handle error
  62.   if (Firebase.failed()) {
  63.       Serial.print("setting /number failed:");
  64.       Serial.println(Firebase.error());  
  65.       return;
  66.   }
  67.   delay(1000);
  68.  
  69.   // get value
  70.   Serial.print("number: ");
  71.   Serial.println(Firebase.getFloat("number"));
  72.   delay(1000);
  73.  
  74.   // remove value
  75.   Firebase.remove("number");
  76.   delay(1000);
  77.  
  78.   // set string value
  79.   Firebase.setString("message", "hello world");
  80.   // handle error
  81.   if (Firebase.failed()) {
  82.       Serial.print("setting /message failed:");
  83.       Serial.println(Firebase.error());  
  84.       return;
  85.   }
  86.   delay(1000);
  87.  
  88.   // set bool value
  89.   Firebase.setBool("truth", false);
  90.   // handle error
  91.   if (Firebase.failed()) {
  92.       Serial.print("setting /truth failed:");
  93.       Serial.println(Firebase.error());  
  94.       return;
  95.   }
  96.   delay(1000);
  97.  
  98.   // append a new value to /logs
  99.   String name = Firebase.pushInt("logs", n++);
  100.   // handle error
  101.   if (Firebase.failed()) {
  102.       Serial.print("pushing /logs failed:");
  103.       Serial.println(Firebase.error());  
  104.       return;
  105.   }
  106.   Serial.print("pushed: /logs/");
  107.   Serial.println(name);
  108.   delay(1000);
  109. }