Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <FirebaseArduino.h>
- #include <ESP8266HTTPClient.h>
- // Settings
- #define FINGERPRINT "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A"
- #define HOST "abc.com"
- #define AUTH "*******"
- #define WIFI_SSID "***"
- #define WIFI_PASSWORD "***"
- #define UID "******"
- //const int relay = D0; // 16
- //const int sensor = D7;
- /*int lastSensorState = LOW;
- bool lastServerState = false;
- int state = LOW; // meaning relay is off.
- */
- int inputPins[3] = {D0, D2, D4}; // define list of sensors
- int outputPins[3] = {D1, D3, D5}; // defines list of pins for relays or triac.
- char* serverDevices[] = {"0", "1", "2"}; // this is list of id devices from server.
- int lastInputState[3] = {LOW, LOW, LOW};
- bool lastServerDeviceState[3] = {false, false, false};
- int state[3] = {LOW, LOW, LOW};
- String getUserDeviceURL(){
- String path;
- path = path +"/users/" UID;
- path = path + "/devices";
- return path;
- }
- // setUp Wifi
- void setupWifi() {
- // connect to wifi.
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to wifi");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.println();
- Serial.print("connected: ");
- Serial.println(WiFi.localIP());
- // Connect to the firebase server
- Firebase.begin(HOST, AUTH);
- }
- // Setup Devices as output mode
- void setupOutputPins() {
- int i;
- for (i = 0; i < 3; i = i + 1) {
- Serial.println(outputPins[i]);
- pinMode(outputPins[i], OUTPUT); // set relay as output
- digitalWrite(outputPins[i], HIGH); //set output is HIGH Relay is OFF, not sure why this. mostly if set to High light is on.:(
- }
- }
- // setup Sensor as Input
- void setupInputPins() {
- int i;
- for (i = 0; i < 3; i = i + 1) {
- Serial.printf("Input pin is: %d", inputPins[i]);
- pinMode(inputPins[i], INPUT); // setup Sensor pins as Input
- }
- }
- // Default of arduino setup function
- void setup() {
- Serial.begin(9600);
- setupWifi(); // wifi setup
- setupInputPins(); // input pins setup
- setupOutputPins(); // output pins setup
- }
- // Default of Arduino loop function
- void loop() {
- doLoop();
- }
- void doLoop() {
- int index;
- for (index = 0; index < 3; index = index + 1) {
- if (sensorStateIsChange(index)) {
- // sensor is change
- int currentSensorState = digitalRead(inputPins[index]);
- lastInputState[index] = currentSensorState; // set last state of Input sensor
- state[index] = !state[index];
- digitalWrite(outputPins[index], state[index]); // turn on or off device base on state.
- // now need sync state to server
- if (WiFi.status() == WL_CONNECTED) {
- serverSetState(index, currentSensorState ? true : false);
- }
- }
- // listen for server state
- if (WiFi.status() == WL_CONNECTED) {
- if (serverStateIsChange(index)) {
- // server state is change
- bool currentServerState = lastServerDeviceState[index];
- state[index] = currentServerState ? HIGH : LOW;
- }
- }
- Serial.printf("Index %d has state is: %d", index, state[index]);
- digitalWrite(outputPins[index], state[index]);
- }
- }
- bool sensorStateIsChange(int index) {
- bool returnValue = false;
- int currentSensorState = digitalRead(inputPins[index]);
- if (lastInputState[index] != currentSensorState) {
- // now we see sensor State change.
- returnValue = true;
- }
- return returnValue;
- }
- bool serverGetState(int index) {
- long int time = millis();
- long int timeout = 100;
- bool returnValue = lastServerDeviceState[index];
- while ( (time + timeout) > millis()) {
- //char deviceStateURL[] = userDeviceURL;
- //deviceStateURL = deviceStateURL + "/" + serverDevices[index] + "/state";
- returnValue = Firebase.getBool(getUserDeviceURL());
- }
- return returnValue;
- }
- void serverSetState(int index, bool s) {
- long int time = millis();
- long int timeout = 100;
- lastServerDeviceState[index] = s;
- while ( (time + timeout) > millis()) {
- Firebase.setBool(getUserDeviceURL(), s);
- if (Firebase.failed()) {
- Serial.print("Sync server state false.");
- Serial.println(Firebase.error());
- return;
- }
- }
- delay(300);
- }
- bool serverStateIsChange(int index) {
- bool returnValue = lastServerDeviceState[index];
- long int time = millis();
- long int timeout = 100;
- while ( (time + timeout) > millis()) {
- bool currentServerState = serverGetState(index);
- if (currentServerState != lastServerDeviceState[index] && (currentServerState == false || currentServerState == true)) {
- lastServerDeviceState[index] = currentServerState;
- returnValue = true;
- }
- }
- return returnValue;
- }
Add Comment
Please, Sign In to add comment