Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Station 1 Microcontroller Script
- // Initialize libraries
- #include <Wire.h>
- #include <Adafruit_ADS1015.h>
- #include "hw_timer.h"
- #include <Filters.h> //This library does a massive work check it's .cpp file
- #include "FirebaseESP8266.h"
- #include <ESP8266WiFi.h>
- #include <LiquidCrystal_I2C.h>
- // Define all objects and constant variables
- Adafruit_ADS1115 ads1015;
- FirebaseData firebaseData;
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- RunningStatistics inputStats_voltage;
- RunningStatistics inputStats_current;
- #define ACS_Pin A0
- #define FIREBASE_HOST "myiotcloud-258019.firebaseio.com"
- #define FIREBASE_AUTH "XHCMDEOzgC3Xd9FgEHvidY7nSusDLAWPz855XZW5"
- const char WIFI_SSID[] = "WIFI_SSID";
- const char WIFI_PASSWORD[] = "WIFI_PASSWORD";
- const int sensorIn = A0;
- const byte zcPin = 12;
- const byte pwmPin = 13;
- const int relay = 2;
- // Initialize all variables
- int mVperAmp = 185; //Sensitivity of 5A current module
- double Voltage = 0;
- double VRMS = 0;
- double AmpsRMS = 0;
- byte fade = 1;
- byte state = 1;
- byte tarBrightness = 255;
- byte curBrightness = 0;
- byte zcState = 0; // 0 = ready; 1 = processing;
- float ACS_Value_Voltage;
- float ACS_Value_Current;
- float testFrequency = 60; // Set based on using 120V 60Hz AC source
- float windowLength = 1000.0/testFrequency; // Provides period of 16.7 ms
- float intercept_voltage = -0.04; // adjusted based on calibration testing
- float slope_voltage = 0.004; // adjusted based on calibration testing
- float intercept_current = 0;
- float slope_current = 0.0016;
- float Volts_TRMS;
- float Amps_TRMS;
- unsigned long printPeriod = 500;
- unsigned long previousMillis = 0;
- void setup(){
- Serial.begin(115200);
- //Define pin modes
- pinMode(zcPin, INPUT_PULLUP);
- pinMode(pwmPin, OUTPUT);
- pinMode(ACS_Pin,INPUT);
- attachInterrupt(zcPin, zcDetectISR, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
- pinMode(relay, OUTPUT);
- // Initialize AC dimmer, LCD, ADS1115, ACS712, ZMPT101B
- hw_timer_init(NMI_SOURCE, 0);
- hw_timer_set_func(dimTimerISR);
- ads1015.begin();
- inputStats_voltage.setWindowSecs( windowLength ); //Set the window length
- inputStats_current.setWindowSecs( windowLength );
- lcd.init();
- lcd.backlight();
- // Connect to WiFi and Firebase
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to Wi-Fi");
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(300);
- }
- Serial.println();
- Serial.print("Connected with IP: ");
- Serial.println(WiFi.localIP());
- Serial.println();
- Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); //, WIFI_SSID, WIFI_PASSWORD);
- Firebase.reconnectWiFi(true);
- }
- void loop(){
- // Check if relay has been switched to ON
- if (Firebase.getString(firebaseData, "/ChargingStation1/station1Relay")){
- if (firebaseData.stringData() == "ON"){
- // Set relay ON and set AC dimmer value according to Firebase
- digitalWrite(relay, LOW);
- Firebase.getInt(firebaseData, "/ChargingStation1/AC_dimmer_value");
- int num = firebaseData.intData();
- tarBrightness = num;
- // Get time and loop while the time is not finished
- Firebase.getFloat(firebaseData, "/ChargingStation1/time");
- float time = firebaseData.floatData() * 1000;
- currentMillis = millis();
- Serial.print("Time: "); Serial.println(time);
- while(millis() - currentMillis <= time){
- // Read raw current and voltage values and convert to True RMS values
- ACS_Value_Voltage = analogRead(A0);
- ACS_Value_Current = ads1015.readADC_SingleEnded(1);
- inputStats_voltage.input(ACS_Value_Voltage); // log to Stats function
- inputStats_current.input(ACS_Value_Current);
- Volts_TRMS = intercept_voltage + slope_voltage * inputStats_voltage.sigma();
- Volts_TRMS = Volts_TRMS * 161;
- Amps_TRMS = intercept_current + slope_current * inputStats_current.sigma();
- float Watts_TRMS = Amps_TRMS * Volts_TRMS;
- // Upload current, voltage and power values to Firebase and display on LCD
- Serial.print("Current: "); Serial.println(Amps_TRMS);
- Serial.print("Voltage: "); Serial.println(Volts_TRMS);
- Serial.print("Power: "); Serial.println(Watts_TRMS);
- if (Firebase.setFloat(firebaseData, "/ChargingStation1/CurrentValue", Amps_TRMS)){
- } else {
- Serial.println(firebaseData.errorReason());
- }
- if (Firebase.setFloat(firebaseData, "/ChargingStation1/VoltageValue", Volts_TRMS)){
- } else {
- Serial.println(firebaseData.errorReason());
- }
- if (Firebase.setFloat(firebaseData, "/ChargingStation1/PowerValue", Watts_TRMS)){
- } else {
- Serial.println(firebaseData.errorReason());
- }
- lcd.clear();
- lcd.setCursor(1,0);
- lcd.print("Station 1");
- lcd.setCursor(1,1);
- lcd.print(String(round(Volts_TRMS*100)/100) + " V " + String((Amps_TRMS*100/100)) + " A");
- }
- // After charging is complete, reset values on Firebase and wait for next user command
- Firebase.setString(firebaseData, "/ChargingStation1/ChargingRate", "0");
- Firebase.setString(firebaseData, "/ChargingStation1/station1Relay", "Off");
- Firebase.setInt(firebaseData, "/ChargingStation1/CanBeSupplied", 0);
- Serial.print("TIME IS FINISHED");
- } else {
- digitalWrite(relay, HIGH);
- }
- }
- delay(250);
- }
- void dimTimerISR() {
- if (fade == 1) {
- if (curBrightness > tarBrightness || (state == 0 && curBrightness > 0)) {
- --curBrightness;
- }
- else if (curBrightness < tarBrightness && state == 1 && curBrightness < 255) {
- ++curBrightness;
- }
- }
- else {
- if (state == 1) {
- curBrightness = tarBrightness;
- }
- else {
- curBrightness = 0;
- }
- }
- if (curBrightness == 0) {
- state = 0;
- digitalWrite(pwmPin, 0);
- }
- else if (curBrightness == 255) {
- state = 1;
- digitalWrite(pwmPin, 1);
- }
- else {
- digitalWrite(pwmPin, 1);
- }
- zcState = 0;
- }
- void zcDetectISR() {
- if (zcState == 0) {
- zcState = 1;
- if (curBrightness < 255 && curBrightness > 0) {
- digitalWrite(pwmPin, 0);
- int dimDelay = 30 * (255 - curBrightness) + 400;//400
- hw_timer_arm(dimDelay);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement