Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.60 KB | None | 0 0
  1. // Station 1 Microcontroller Script
  2.  
  3. // Initialize libraries
  4. #include <Wire.h>
  5. #include <Adafruit_ADS1015.h>
  6. #include "hw_timer.h"
  7. #include <Filters.h>                      //This library does a massive work check it's .cpp file
  8. #include "FirebaseESP8266.h"
  9. #include <ESP8266WiFi.h>
  10. #include <LiquidCrystal_I2C.h>
  11.  
  12. // Define all objects and constant variables
  13. Adafruit_ADS1115 ads1015;
  14. FirebaseData firebaseData;
  15. LiquidCrystal_I2C lcd(0x27, 16, 2);
  16. RunningStatistics inputStats_voltage;              
  17. RunningStatistics inputStats_current;
  18. #define ACS_Pin A0                        
  19. #define FIREBASE_HOST "myiotcloud-258019.firebaseio.com"            
  20. #define FIREBASE_AUTH "XHCMDEOzgC3Xd9FgEHvidY7nSusDLAWPz855XZW5"      
  21. const char WIFI_SSID[]        = "WIFI_SSID";
  22. const char WIFI_PASSWORD[]    = "WIFI_PASSWORD";
  23. const int sensorIn = A0;
  24. const byte zcPin = 12;
  25. const byte pwmPin = 13;  
  26. const int relay = 2;
  27.  
  28. // Initialize all variables
  29. int mVperAmp = 185; //Sensitivity of 5A current module
  30. double Voltage = 0;
  31. double VRMS = 0;
  32. double AmpsRMS = 0;
  33. byte fade = 1;
  34. byte state = 1;
  35. byte tarBrightness = 255;
  36. byte curBrightness = 0;
  37. byte zcState = 0; // 0 = ready; 1 = processing;
  38. float ACS_Value_Voltage;                              
  39. float ACS_Value_Current;
  40. float testFrequency = 60; // Set based on using 120V 60Hz AC source                  
  41. float windowLength = 1000.0/testFrequency; // Provides period of 16.7 ms
  42. float intercept_voltage = -0.04; // adjusted based on calibration testing
  43. float slope_voltage = 0.004; // adjusted based on calibration testing
  44. float intercept_current = 0;
  45. float slope_current = 0.0016;
  46. float Volts_TRMS;
  47. float Amps_TRMS;
  48. unsigned long printPeriod = 500;
  49. unsigned long previousMillis = 0;
  50.  
  51. void setup(){
  52.   Serial.begin(115200);
  53.  
  54.   //Define pin modes
  55.   pinMode(zcPin, INPUT_PULLUP);
  56.   pinMode(pwmPin, OUTPUT);
  57.   pinMode(ACS_Pin,INPUT);  
  58.   attachInterrupt(zcPin, zcDetectISR, RISING);    // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  59.   pinMode(relay, OUTPUT);
  60.  
  61.   // Initialize AC dimmer, LCD, ADS1115, ACS712, ZMPT101B
  62.   hw_timer_init(NMI_SOURCE, 0);
  63.   hw_timer_set_func(dimTimerISR);
  64.   ads1015.begin();
  65.   inputStats_voltage.setWindowSecs( windowLength );     //Set the window length
  66.   inputStats_current.setWindowSecs( windowLength );
  67.   lcd.init();
  68.   lcd.backlight();
  69.  
  70.   // Connect to WiFi and Firebase
  71.   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  72.   Serial.print("Connecting to Wi-Fi");
  73.   while (WiFi.status() != WL_CONNECTED)
  74.   {
  75.     Serial.print(".");
  76.     delay(300);
  77.   }
  78.   Serial.println();
  79.   Serial.print("Connected with IP: ");
  80.   Serial.println(WiFi.localIP());
  81.   Serial.println();
  82.   Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); //, WIFI_SSID, WIFI_PASSWORD);
  83.   Firebase.reconnectWiFi(true);
  84. }
  85.  
  86. void loop(){
  87.     // Check if relay has been switched to ON
  88.     if (Firebase.getString(firebaseData, "/ChargingStation1/station1Relay")){
  89.       if (firebaseData.stringData() == "ON"){
  90.  
  91.         // Set relay ON and set AC dimmer value according to Firebase
  92.         digitalWrite(relay, LOW);
  93.         Firebase.getInt(firebaseData, "/ChargingStation1/AC_dimmer_value");
  94.         int num = firebaseData.intData();
  95.         tarBrightness = num;
  96.  
  97.         // Get time and loop while the time is not finished
  98.         Firebase.getFloat(firebaseData, "/ChargingStation1/time");
  99.         float time = firebaseData.floatData() * 1000;
  100.         currentMillis = millis();
  101.         Serial.print("Time: "); Serial.println(time);
  102.         while(millis() - currentMillis <= time){
  103.  
  104.           // Read raw current and voltage values and convert to True RMS values
  105.           ACS_Value_Voltage = analogRead(A0);
  106.           ACS_Value_Current = ads1015.readADC_SingleEnded(1);
  107.           inputStats_voltage.input(ACS_Value_Voltage);  // log to Stats function
  108.           inputStats_current.input(ACS_Value_Current);    
  109.           Volts_TRMS = intercept_voltage + slope_voltage * inputStats_voltage.sigma();
  110.           Volts_TRMS = Volts_TRMS * 161;
  111.           Amps_TRMS = intercept_current + slope_current * inputStats_current.sigma();  
  112.           float Watts_TRMS = Amps_TRMS * Volts_TRMS;
  113.  
  114.           // Upload current, voltage and power values to Firebase and display on LCD
  115.           Serial.print("Current: "); Serial.println(Amps_TRMS);
  116.           Serial.print("Voltage: "); Serial.println(Volts_TRMS);
  117.           Serial.print("Power: "); Serial.println(Watts_TRMS);
  118.  
  119.           if (Firebase.setFloat(firebaseData, "/ChargingStation1/CurrentValue", Amps_TRMS)){
  120.           } else {
  121.             Serial.println(firebaseData.errorReason());
  122.           }
  123.      
  124.           if (Firebase.setFloat(firebaseData, "/ChargingStation1/VoltageValue", Volts_TRMS)){
  125.           } else {
  126.             Serial.println(firebaseData.errorReason());
  127.           }
  128.  
  129.           if (Firebase.setFloat(firebaseData, "/ChargingStation1/PowerValue", Watts_TRMS)){
  130.           } else {
  131.             Serial.println(firebaseData.errorReason());
  132.           }
  133.  
  134.           lcd.clear();
  135.           lcd.setCursor(1,0);
  136.           lcd.print("Station 1");
  137.           lcd.setCursor(1,1);
  138.           lcd.print(String(round(Volts_TRMS*100)/100) + " V " + String((Amps_TRMS*100/100)) + " A");
  139.         }
  140.  
  141.         // After charging is complete, reset values on Firebase and wait for next user command
  142.         Firebase.setString(firebaseData, "/ChargingStation1/ChargingRate", "0");
  143.         Firebase.setString(firebaseData, "/ChargingStation1/station1Relay", "Off");
  144.         Firebase.setInt(firebaseData, "/ChargingStation1/CanBeSupplied", 0);
  145.         Serial.print("TIME IS FINISHED");
  146.       } else {
  147.         digitalWrite(relay, HIGH);
  148.       }
  149.     }
  150.     delay(250);
  151. }
  152.  
  153. void dimTimerISR() {
  154.     if (fade == 1) {
  155.       if (curBrightness > tarBrightness || (state == 0 && curBrightness > 0)) {
  156.         --curBrightness;
  157.       }
  158.       else if (curBrightness < tarBrightness && state == 1 && curBrightness < 255) {
  159.         ++curBrightness;
  160.       }
  161.     }
  162.     else {
  163.       if (state == 1) {
  164.         curBrightness = tarBrightness;
  165.       }
  166.       else {
  167.         curBrightness = 0;
  168.       }
  169.     }
  170.    
  171.     if (curBrightness == 0) {
  172.       state = 0;
  173.       digitalWrite(pwmPin, 0);
  174.     }
  175.     else if (curBrightness == 255) {
  176.       state = 1;
  177.       digitalWrite(pwmPin, 1);
  178.     }
  179.     else {
  180.       digitalWrite(pwmPin, 1);
  181.     }
  182.    
  183.     zcState = 0;
  184. }
  185.  
  186. void zcDetectISR() {
  187.   if (zcState == 0) {
  188.     zcState = 1;
  189.    
  190.    
  191.     if (curBrightness < 255 && curBrightness > 0) {
  192.       digitalWrite(pwmPin, 0);
  193.       int dimDelay = 30 * (255 - curBrightness) + 400;//400
  194.       hw_timer_arm(dimDelay);
  195.     }
  196.   }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement