tabvn

arduino_switch_3_Gang

Aug 6th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.76 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <FirebaseArduino.h>
  3. #include <ESP8266HTTPClient.h>
  4.  
  5.  
  6. // Settings
  7. #define FINGERPRINT "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A"
  8. #define HOST "abc.com"
  9. #define AUTH "*******"
  10. #define WIFI_SSID "***"
  11. #define WIFI_PASSWORD "***"
  12. #define UID "******"
  13.  
  14.  
  15. //const int relay = D0; // 16
  16. //const int sensor = D7;
  17.  
  18. /*int lastSensorState = LOW;
  19.   bool lastServerState = false;
  20.   int state = LOW; // meaning relay is off.
  21. */
  22.  
  23.  
  24.  
  25. int inputPins[3] = {D0, D2, D4}; // define list of sensors
  26. int outputPins[3] = {D1, D3, D5}; // defines list of pins for relays or triac.
  27. char* serverDevices[] = {"0", "1", "2"}; // this is list of id devices from server.
  28.  
  29.  
  30. int lastInputState[3] = {LOW, LOW, LOW};
  31. bool lastServerDeviceState[3] = {false, false, false};
  32. int state[3] = {LOW, LOW, LOW};
  33.  
  34.  
  35.  
  36. String getUserDeviceURL(){
  37.   String path;
  38.  
  39.   path = path +"/users/" UID;
  40.   path = path + "/devices";
  41.  
  42.   return path;
  43. }
  44.  
  45. // setUp Wifi
  46.  
  47. void setupWifi() {
  48.  
  49.   // connect to wifi.
  50.   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  51.   Serial.print("Connecting to wifi");
  52.   while (WiFi.status() != WL_CONNECTED) {
  53.     Serial.print(".");
  54.     delay(500);
  55.   }
  56.   Serial.println();
  57.   Serial.print("connected: ");
  58.   Serial.println(WiFi.localIP());
  59.  
  60.   // Connect to the firebase server
  61.   Firebase.begin(HOST, AUTH);
  62. }
  63.  
  64. // Setup Devices as output mode
  65.  
  66. void setupOutputPins() {
  67.  
  68.   int i;
  69.   for (i = 0; i < 3; i = i + 1) {
  70.     Serial.println(outputPins[i]);
  71.  
  72.     pinMode(outputPins[i], OUTPUT); // set relay as output
  73.  
  74.     digitalWrite(outputPins[i], HIGH); //set output is HIGH Relay is OFF, not sure why this. mostly if set to High light is on.:(
  75.  
  76.  
  77.   }
  78. }
  79.  
  80.  
  81. // setup Sensor as Input
  82.  
  83. void setupInputPins() {
  84.  
  85.   int i;
  86.   for (i = 0; i < 3; i = i + 1) {
  87.     Serial.printf("Input pin is: %d", inputPins[i]);
  88.     pinMode(inputPins[i], INPUT); // setup Sensor pins as Input
  89.   }
  90.  
  91. }
  92.  
  93.  
  94.  
  95.  
  96. // Default of arduino setup function
  97. void setup() {
  98.   Serial.begin(9600);
  99.   setupWifi(); // wifi setup
  100.   setupInputPins(); // input pins setup
  101.   setupOutputPins(); // output pins setup
  102. }
  103. // Default of Arduino loop function
  104. void loop() {
  105.   doLoop();
  106.  
  107. }
  108.  
  109.  
  110.  
  111.  
  112. void doLoop() {
  113.  
  114.   int index;
  115.  
  116.   for (index = 0; index < 3; index = index + 1) {
  117.  
  118.  
  119.     if (sensorStateIsChange(index)) {
  120.       // sensor is change
  121.       int currentSensorState = digitalRead(inputPins[index]);
  122.       lastInputState[index] = currentSensorState; // set last state of Input sensor
  123.  
  124.       state[index] = !state[index];
  125.  
  126.       digitalWrite(outputPins[index], state[index]); // turn on or off device base on state.
  127.  
  128.       // now need sync state to server
  129.       if (WiFi.status() == WL_CONNECTED) {
  130.         serverSetState(index, currentSensorState ? true : false);
  131.       }
  132.     }
  133.  
  134.     // listen for server state
  135.     if (WiFi.status() == WL_CONNECTED) {
  136.       if (serverStateIsChange(index)) {
  137.         // server state is change
  138.         bool currentServerState = lastServerDeviceState[index];
  139.  
  140.         state[index] = currentServerState ? HIGH : LOW;
  141.       }
  142.     }
  143.  
  144.     Serial.printf("Index %d has state is:  %d", index, state[index]);
  145.     digitalWrite(outputPins[index], state[index]);
  146.   }
  147.  
  148.  
  149. }
  150.  
  151.  
  152.  
  153. bool sensorStateIsChange(int index) {
  154.   bool returnValue = false;
  155.   int currentSensorState = digitalRead(inputPins[index]);
  156.   if (lastInputState[index] != currentSensorState) {
  157.     // now we see sensor State change.
  158.     returnValue = true;
  159.   }
  160.   return returnValue;
  161. }
  162.  
  163. bool serverGetState(int index) {
  164.   long int time = millis();
  165.   long int timeout = 100;
  166.  
  167.   bool returnValue = lastServerDeviceState[index];
  168.  
  169.   while ( (time + timeout) > millis()) {
  170.     //char deviceStateURL[] = userDeviceURL;
  171.     //deviceStateURL = deviceStateURL + "/" + serverDevices[index] + "/state";
  172.     returnValue =  Firebase.getBool(getUserDeviceURL());
  173.   }
  174.  
  175.   return returnValue;
  176.  
  177. }
  178.  
  179. void serverSetState(int index, bool s) {
  180.  
  181.   long int time = millis();
  182.   long int timeout = 100;
  183.   lastServerDeviceState[index] = s;
  184.  
  185.   while ( (time + timeout) > millis()) {
  186.    
  187.  
  188.     Firebase.setBool(getUserDeviceURL(), s);
  189.     if (Firebase.failed()) {
  190.       Serial.print("Sync server state false.");
  191.       Serial.println(Firebase.error());
  192.       return;
  193.     }
  194.   }
  195.   delay(300);
  196. }
  197.  
  198. bool serverStateIsChange(int index) {
  199.   bool returnValue = lastServerDeviceState[index];
  200.   long int time = millis();
  201.   long int timeout = 100;
  202.  
  203.   while ( (time + timeout) > millis()) {
  204.     bool currentServerState = serverGetState(index);
  205.     if (currentServerState != lastServerDeviceState[index] && (currentServerState == false || currentServerState == true)) {
  206.       lastServerDeviceState[index] = currentServerState;
  207.       returnValue = true;
  208.     }
  209.   }
  210.  
  211.   return returnValue;
  212. }
Add Comment
Please, Sign In to add comment