This is my TPJ project that controls the SLA battery charging cycle with 2W 12V Solar Panel and provides real-time data for user via LCD screen and Bluetooth

Dependencies:   N5110 Terminal mbed

Automated Solar Power Harvesting System

• Controls the charging and discharging operation of power bank with solar panel as well as house hold load and informs the user via Bluetooth and Internet of Things (IoT)

• Operated by FRDM-K64F: Kinetis Development Board that has ARM Cortes-M4 processor

• Programed with Mbed online C/C++ IDE.

/media/uploads/Ozmos/whatsapp_image_2017-06-11_at_5.13.53_pm.jpeg

/media/uploads/Ozmos/alone.jpeg

Committer:
Ozmos
Date:
Thu Apr 20 13:28:42 2017 +0000
Revision:
1:ed2bd7349828
Parent:
gate1.cpp@0:aa0900f930e8
This is my TPJ project that controls the SLA battery charging cycle with 2W 12V Solar Panel and provides real-time data for user via LCD screen and Bluetooth

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ozmos 1:ed2bd7349828 1 /*
Ozmos 1:ed2bd7349828 2 TPJ project
Ozmos 1:ed2bd7349828 3 Automated Solar Power Harvesting System
Ozmos 1:ed2bd7349828 4
Ozmos 1:ed2bd7349828 5 It is created by Ozan SANCI
Ozmos 1:ed2bd7349828 6
Ozmos 1:ed2bd7349828 7 Description: This program controls charging operation of SLA battery(6V-4A) with 12V 2W Solar Panel and controls:
Ozmos 1:ed2bd7349828 8
Ozmos 1:ed2bd7349828 9 -load On/OFF
Ozmos 1:ed2bd7349828 10 -ADC real-time voltage on both solar and battery,
Ozmos 1:ed2bd7349828 11 -power consumption with ACS712 Current Sensor (-5 to 5A)
Ozmos 1:ed2bd7349828 12 -interfacing with user via Nokia 5110 screen as well as HC-05 Bluetooth module.
Ozmos 1:ed2bd7349828 13 -third part Android application, “Bluetooth Electronic” is used.
Ozmos 1:ed2bd7349828 14
Ozmos 1:ed2bd7349828 15 Additional note: The charging algorithms for the final demonstration have been changed slightly:
Ozmos 1:ed2bd7349828 16 Solar Panel and battery voltage thresholds, 6.5V, and 6.4V are changed to 6V.
Ozmos 1:ed2bd7349828 17 Because, instead of solar panel, A dc source is used since solar panel is not able to provide enough power in the Lab.
Ozmos 1:ed2bd7349828 18
Ozmos 1:ed2bd7349828 19 */
Ozmos 1:ed2bd7349828 20
Ozmos 0:aa0900f930e8 21 #include"mbed.h"
Ozmos 0:aa0900f930e8 22 #include "Terminal.h"
Ozmos 0:aa0900f930e8 23 #include "N5110.h"
Ozmos 0:aa0900f930e8 24 #include "string"
Ozmos 0:aa0900f930e8 25
Ozmos 0:aa0900f930e8 26 /*********************************Pin decleration*************/
Ozmos 0:aa0900f930e8 27 Serial pc(USBTX, USBRX);
Ozmos 0:aa0900f930e8 28 Serial blue(PTC15,PTC14);
Ozmos 0:aa0900f930e8 29 AnalogIn solar_voltage(A0);
Ozmos 0:aa0900f930e8 30 AnalogIn battery_voltage(A1);
Ozmos 0:aa0900f930e8 31 AnalogIn load_current(A2);
Ozmos 0:aa0900f930e8 32 DigitalOut solar_gate(D5);
Ozmos 0:aa0900f930e8 33 DigitalOut load_gate(D4);
Ozmos 0:aa0900f930e8 34 DigitalIn pushBtnLeft(D3);
Ozmos 0:aa0900f930e8 35 DigitalIn pushBtnRight(D2);
Ozmos 0:aa0900f930e8 36 DigitalOut blueLed(D0);
Ozmos 0:aa0900f930e8 37 /*******************************************************************/
Ozmos 0:aa0900f930e8 38 using std::string;
Ozmos 0:aa0900f930e8 39 /****************************Nokia5110 Pin Decleration*********/
Ozmos 0:aa0900f930e8 40 //SCE,RST,D/C,MOSI,SCLK,LED
Ozmos 0:aa0900f930e8 41 N5110 lcd(PTA0,PTC2,PTC4,PTD2,PTD1,PTC3);
Ozmos 0:aa0900f930e8 42 /*****************************************************************/
Ozmos 0:aa0900f930e8 43 #define solargate
Ozmos 0:aa0900f930e8 44 #define refVolt 3300
Ozmos 0:aa0900f930e8 45 float solarVoltage;
Ozmos 1:ed2bd7349828 46 unsigned char sGate = 'A'; //Initially gate is open
Ozmos 1:ed2bd7349828 47 unsigned char bGate = 'B';//Initially gate is open
Ozmos 0:aa0900f930e8 48
Ozmos 0:aa0900f930e8 49 string gateStat= "LR255G0B0"; // INTIALLY IT IS RED, GREEN IS LR0G255B0
Ozmos 0:aa0900f930e8 50 float batteryVoltage;
Ozmos 0:aa0900f930e8 51 /********************************************************Function Prototypes************/
Ozmos 0:aa0900f930e8 52
Ozmos 0:aa0900f930e8 53 float fnc_solarPanel(void);
Ozmos 0:aa0900f930e8 54 bool fnc_chargingstat(float i);
Ozmos 0:aa0900f930e8 55 float fnc_batteryVolt(void);
Ozmos 0:aa0900f930e8 56 bool fnc_loadstat(float i);
Ozmos 0:aa0900f930e8 57 float fnc_load_current(void);
Ozmos 0:aa0900f930e8 58
Ozmos 0:aa0900f930e8 59 /************************************************************M A I N************************/
Ozmos 0:aa0900f930e8 60 int main(){
Ozmos 0:aa0900f930e8 61 bool loadStatus, chargingStatus,screenOne,screenTwo;
Ozmos 0:aa0900f930e8 62 solarVoltage = 0.0;
Ozmos 0:aa0900f930e8 63 batteryVoltage = 0.0;
Ozmos 0:aa0900f930e8 64 float loadCurrent = 0.0;
Ozmos 0:aa0900f930e8 65 float usagePower = 0.0;
Ozmos 0:aa0900f930e8 66 float powerLeftinW = 0.0;
Ozmos 0:aa0900f930e8 67 int powerLeftinH = 0.0;
Ozmos 0:aa0900f930e8 68 char outputs[10];//it is used for conversion from solar volt (float) to char in order to print as string.
Ozmos 0:aa0900f930e8 69 char outputb[10];//it is used for conversion from battery charge(float) to char in order to print as string.
Ozmos 0:aa0900f930e8 70 char outputc[10];//it is used for conversion from current charge(float) to char in order to print as string.
Ozmos 0:aa0900f930e8 71 char blueInput;
Ozmos 0:aa0900f930e8 72 int left, right;
Ozmos 0:aa0900f930e8 73 lcd.init();
Ozmos 0:aa0900f930e8 74 lcd.normalMode();
Ozmos 0:aa0900f930e8 75 lcd.setBrightness(1.0);
Ozmos 0:aa0900f930e8 76
Ozmos 0:aa0900f930e8 77 loadStatus = false;
Ozmos 0:aa0900f930e8 78 chargingStatus = false;
Ozmos 0:aa0900f930e8 79 screenOne = true;
Ozmos 0:aa0900f930e8 80 screenTwo = false;
Ozmos 0:aa0900f930e8 81 right = 1;
Ozmos 0:aa0900f930e8 82 left = 0;
Ozmos 0:aa0900f930e8 83 blueLed = 0;
Ozmos 0:aa0900f930e8 84 while(1){
Ozmos 0:aa0900f930e8 85
Ozmos 0:aa0900f930e8 86 if (blue.readable()) {
Ozmos 0:aa0900f930e8 87 blueLed = 1;
Ozmos 0:aa0900f930e8 88
Ozmos 0:aa0900f930e8 89 blueInput = blue.getc();
Ozmos 0:aa0900f930e8 90 switch(blueInput){
Ozmos 0:aa0900f930e8 91 case 'B' : bGate = 'B'; break;
Ozmos 0:aa0900f930e8 92 case 'b' : bGate = 'b'; break;
Ozmos 0:aa0900f930e8 93 case 'A': sGate = 'A';break;
Ozmos 0:aa0900f930e8 94 case 'a' : sGate = 'a'; break;
Ozmos 0:aa0900f930e8 95
Ozmos 0:aa0900f930e8 96 }}
Ozmos 0:aa0900f930e8 97
Ozmos 0:aa0900f930e8 98
Ozmos 0:aa0900f930e8 99
Ozmos 0:aa0900f930e8 100
Ozmos 0:aa0900f930e8 101
Ozmos 0:aa0900f930e8 102
Ozmos 0:aa0900f930e8 103 right = pushBtnRight.read();
Ozmos 0:aa0900f930e8 104 left = pushBtnLeft.read();
Ozmos 0:aa0900f930e8 105 if(right){screenOne = false; screenTwo = true; }
Ozmos 0:aa0900f930e8 106 if(left){screenOne=true;screenTwo=false;}
Ozmos 0:aa0900f930e8 107 if(screenOne)
Ozmos 0:aa0900f930e8 108 {
Ozmos 0:aa0900f930e8 109
Ozmos 0:aa0900f930e8 110
Ozmos 0:aa0900f930e8 111 lcd.clear();
Ozmos 0:aa0900f930e8 112 solarVoltage=fnc_solarPanel();//solar voltge
Ozmos 0:aa0900f930e8 113 sprintf(outputs,"SolarV: %.2fV",solarVoltage);
Ozmos 0:aa0900f930e8 114 lcd.printString(outputs,0,0);
Ozmos 0:aa0900f930e8 115 //lcd.refresh();
Ozmos 0:aa0900f930e8 116 blue.printf("*S%s*",outputs);
Ozmos 0:aa0900f930e8 117
Ozmos 0:aa0900f930e8 118
Ozmos 0:aa0900f930e8 119 chargingStatus = fnc_chargingstat(solarVoltage); // return true if volt bigger than 6V else false and sets the gate
Ozmos 0:aa0900f930e8 120 if (chargingStatus){lcd.printString("Charging: ON ",0,1);/*lcd.refresh();*/blue.printf("*LR0G255B0*");}
Ozmos 0:aa0900f930e8 121 else {lcd.printString("Charging: OFF",0,1);blue.printf("*LR255G0B0*");}
Ozmos 0:aa0900f930e8 122 /*lcd.refresh();*/
Ozmos 0:aa0900f930e8 123 batteryVoltage = fnc_batteryVolt(); // return battery volt as float
Ozmos 0:aa0900f930e8 124 sprintf(outputb,"Battery: %.2fV",batteryVoltage); /*lcd.refresh();*/
Ozmos 0:aa0900f930e8 125 lcd.printString(outputb,0,2);
Ozmos 0:aa0900f930e8 126 /*lcd.refresh();*/
Ozmos 0:aa0900f930e8 127 blue.printf("*B%s*",outputb);
Ozmos 0:aa0900f930e8 128
Ozmos 0:aa0900f930e8 129 loadStatus = fnc_loadstat(batteryVoltage); // return true if volt higger than 6v else fals end sets the gate
Ozmos 0:aa0900f930e8 130 if (loadStatus){lcd.printString("Status: ON ",0,3);/*lcd.refresh();*/blue.printf("*MR0G255B0*");}
Ozmos 0:aa0900f930e8 131 else {lcd.printString("Status: OFF",0,3);blue.printf("*MR255G0B0*");}
Ozmos 0:aa0900f930e8 132 /*lcd.refresh();*/
Ozmos 0:aa0900f930e8 133 fnc_load_current(); // only return current as float and power calculations are required
Ozmos 0:aa0900f930e8 134 lcd.printString("Power Usage =>",0,5);
Ozmos 0:aa0900f930e8 135 lcd.refresh();
Ozmos 0:aa0900f930e8 136 fnc_load_current();
Ozmos 0:aa0900f930e8 137 loadCurrent = fnc_load_current();
Ozmos 0:aa0900f930e8 138 sprintf(outputc,"Load:%.2fmA",loadCurrent);
Ozmos 0:aa0900f930e8 139
Ozmos 0:aa0900f930e8 140 blue.printf("*E%s*",outputc);
Ozmos 0:aa0900f930e8 141 usagePower = loadCurrent * batteryVoltage;
Ozmos 0:aa0900f930e8 142
Ozmos 0:aa0900f930e8 143 sprintf(outputc,"L-Power:%.2fmW",usagePower);
Ozmos 0:aa0900f930e8 144
Ozmos 0:aa0900f930e8 145 blue.printf("*I%s*",outputc);
Ozmos 0:aa0900f930e8 146 powerLeftinW = 24 - usagePower;
Ozmos 0:aa0900f930e8 147 if(batteryVoltage <= 5) powerLeftinW=0.0;
Ozmos 0:aa0900f930e8 148 sprintf(outputc,"B-Power:%.1fkW",powerLeftinW);
Ozmos 0:aa0900f930e8 149 blue.printf("*I%s*",outputc);
Ozmos 0:aa0900f930e8 150
Ozmos 0:aa0900f930e8 151 powerLeftinH = 24 / usagePower;
Ozmos 0:aa0900f930e8 152 if(batteryVoltage <= 5) powerLeftinH=0.0;
Ozmos 0:aa0900f930e8 153 sprintf(outputc,"B-Hour:%dh", powerLeftinH);
Ozmos 0:aa0900f930e8 154
Ozmos 0:aa0900f930e8 155 blue.printf("*N%s*",outputc);
Ozmos 0:aa0900f930e8 156
Ozmos 0:aa0900f930e8 157 wait_ms(500);
Ozmos 0:aa0900f930e8 158 }//if screenone
Ozmos 0:aa0900f930e8 159
Ozmos 0:aa0900f930e8 160 if(screenTwo)
Ozmos 0:aa0900f930e8 161 {
Ozmos 0:aa0900f930e8 162 lcd.clear();
Ozmos 0:aa0900f930e8 163
Ozmos 0:aa0900f930e8 164 solarVoltage=fnc_solarPanel();//solar voltge
Ozmos 0:aa0900f930e8 165 sprintf(outputs,"SolarV: %.2fV",solarVoltage);
Ozmos 0:aa0900f930e8 166
Ozmos 0:aa0900f930e8 167 blue.printf("*S%s*",outputs);
Ozmos 0:aa0900f930e8 168
Ozmos 0:aa0900f930e8 169
Ozmos 0:aa0900f930e8 170 chargingStatus = fnc_chargingstat(solarVoltage); // return true if volt bigger than 6V else false and sets the gate
Ozmos 0:aa0900f930e8 171 if (chargingStatus){blue.printf("*LR0G255B0*");}
Ozmos 0:aa0900f930e8 172 else {blue.printf("*LR255G0B0*");}
Ozmos 0:aa0900f930e8 173
Ozmos 0:aa0900f930e8 174 batteryVoltage = fnc_batteryVolt(); // return battery volt as float
Ozmos 0:aa0900f930e8 175 sprintf(outputb,"Battery: %.2fV",batteryVoltage);
Ozmos 0:aa0900f930e8 176
Ozmos 0:aa0900f930e8 177
Ozmos 0:aa0900f930e8 178 blue.printf("*B%s*",outputb);
Ozmos 0:aa0900f930e8 179
Ozmos 0:aa0900f930e8 180 loadStatus = fnc_loadstat(batteryVoltage); // return true if volt higger than 6v else fals end sets the gate
Ozmos 0:aa0900f930e8 181 if (loadStatus){blue.printf("*MR0G255B0*");}
Ozmos 0:aa0900f930e8 182 else {blue.printf("*MR255G0B0*");}
Ozmos 0:aa0900f930e8 183
Ozmos 0:aa0900f930e8 184 fnc_load_current(); // only return current as float and power calculations are required
Ozmos 0:aa0900f930e8 185 //*******************************
Ozmos 0:aa0900f930e8 186
Ozmos 0:aa0900f930e8 187 loadCurrent = fnc_load_current();
Ozmos 0:aa0900f930e8 188 sprintf(outputc,"Load:%.2fmA",loadCurrent); /*lcd.refresh();*/
Ozmos 0:aa0900f930e8 189 lcd.printString(outputc,0,0);/*lcd.refresh();*/
Ozmos 0:aa0900f930e8 190 blue.printf("*E%s*",outputc);
Ozmos 0:aa0900f930e8 191 usagePower = loadCurrent * batteryVoltage;
Ozmos 0:aa0900f930e8 192
Ozmos 0:aa0900f930e8 193 sprintf(outputc,"L-Power:%.2fmW",usagePower); /*lcd.refresh();*/
Ozmos 0:aa0900f930e8 194 lcd.printString(outputc,0,1);/*lcd.refresh();*/
Ozmos 0:aa0900f930e8 195 blue.printf("*I%s*",outputc);
Ozmos 0:aa0900f930e8 196 powerLeftinW = 24 - usagePower;
Ozmos 0:aa0900f930e8 197 if(batteryVoltage <= 5) powerLeftinW=0.0;
Ozmos 0:aa0900f930e8 198 sprintf(outputc,"B-Power:%.1fkW",powerLeftinW); /*lcd.refresh();*/
Ozmos 0:aa0900f930e8 199
Ozmos 0:aa0900f930e8 200 blue.printf("*I%s*",outputc);
Ozmos 0:aa0900f930e8 201 lcd.printString(outputc,0,2);/*lcd.refresh();*/
Ozmos 0:aa0900f930e8 202 powerLeftinH = 24 / usagePower;
Ozmos 0:aa0900f930e8 203 if(batteryVoltage <= 5) powerLeftinH=0.0;
Ozmos 0:aa0900f930e8 204 sprintf(outputc,"B-Hour:%dh", powerLeftinH); /*lcd.refresh();*/
Ozmos 0:aa0900f930e8 205 lcd.printString(outputc,0,3);/*lcd.refresh();*/
Ozmos 0:aa0900f930e8 206 blue.printf("*N%s*",outputc);
Ozmos 0:aa0900f930e8 207 lcd.printString("",0,1);/*lcd.refresh();*/
Ozmos 0:aa0900f930e8 208 lcd.printString("<= Charger",0,5);
Ozmos 0:aa0900f930e8 209 lcd.refresh();
Ozmos 0:aa0900f930e8 210 wait_ms(500);
Ozmos 0:aa0900f930e8 211 }// if screentwo
Ozmos 0:aa0900f930e8 212
Ozmos 0:aa0900f930e8 213 }
Ozmos 0:aa0900f930e8 214 }
Ozmos 0:aa0900f930e8 215 /******************************************E N D M A I N*************************/
Ozmos 0:aa0900f930e8 216
Ozmos 0:aa0900f930e8 217
Ozmos 0:aa0900f930e8 218
Ozmos 0:aa0900f930e8 219 /******************************************Function Definations********************/
Ozmos 0:aa0900f930e8 220
Ozmos 0:aa0900f930e8 221 float fnc_solarPanel(void)//Solar Panel Voltage at A0 in -------------------------------------------
Ozmos 0:aa0900f930e8 222 {
Ozmos 0:aa0900f930e8 223 float solarVolt = ((solar_voltage.read() * (refVolt/1000.0))*2.63);
Ozmos 0:aa0900f930e8 224 return solarVolt;
Ozmos 0:aa0900f930e8 225 }
Ozmos 0:aa0900f930e8 226 //-------------------------------------------------------------------------------------------------------------
Ozmos 0:aa0900f930e8 227
Ozmos 0:aa0900f930e8 228
Ozmos 1:ed2bd7349828 229 bool fnc_chargingstat(float i)//it returns true if voltage on solar panel is enough.
Ozmos 0:aa0900f930e8 230 {
Ozmos 0:aa0900f930e8 231 if (i<6|| sGate =='a' )
Ozmos 0:aa0900f930e8 232 {
Ozmos 0:aa0900f930e8 233 solar_gate = 0;
Ozmos 0:aa0900f930e8 234 gateStat = "LR255GOBO";
Ozmos 0:aa0900f930e8 235 // blue.printf("*OR255G0B0*");
Ozmos 0:aa0900f930e8 236
Ozmos 0:aa0900f930e8 237 return false;
Ozmos 0:aa0900f930e8 238 }
Ozmos 0:aa0900f930e8 239 else {
Ozmos 0:aa0900f930e8 240 // blue.printf("*OR0G255B0*");
Ozmos 0:aa0900f930e8 241
Ozmos 0:aa0900f930e8 242 gateStat= "LROG255BO";
Ozmos 0:aa0900f930e8 243 solar_gate = 1; return true;}
Ozmos 0:aa0900f930e8 244
Ozmos 0:aa0900f930e8 245 }
Ozmos 1:ed2bd7349828 246 //------------------------------------------------------------------------------------------------------------
Ozmos 1:ed2bd7349828 247 float fnc_batteryVolt(void) //Returns battery volage as float
Ozmos 0:aa0900f930e8 248 {
Ozmos 0:aa0900f930e8 249 batteryVoltage = ((battery_voltage.read()*(refVolt/1000.0))*3.7789);
Ozmos 0:aa0900f930e8 250 return batteryVoltage;
Ozmos 0:aa0900f930e8 251 }
Ozmos 0:aa0900f930e8 252 //--------------------------------------------------------------------------------------------------
Ozmos 0:aa0900f930e8 253
Ozmos 0:aa0900f930e8 254 bool fnc_loadstat(float i)
Ozmos 0:aa0900f930e8 255 { //bool stat;
Ozmos 0:aa0900f930e8 256
Ozmos 0:aa0900f930e8 257 if (i <6 ){ blue.printf("*OR255G0B0*"); return false; }
Ozmos 0:aa0900f930e8 258 else{ blue.printf("*OR0G255B0*"); return true;}
Ozmos 0:aa0900f930e8 259
Ozmos 0:aa0900f930e8 260 // return stat;
Ozmos 0:aa0900f930e8 261 }
Ozmos 0:aa0900f930e8 262
Ozmos 0:aa0900f930e8 263 //-----------------------------------------------------------------------------------------------------------------------
Ozmos 0:aa0900f930e8 264 float fnc_load_current(void)
Ozmos 0:aa0900f930e8 265 {
Ozmos 0:aa0900f930e8 266 float loadcurrent = load_current.read();
Ozmos 0:aa0900f930e8 267 return loadcurrent;
Ozmos 0:aa0900f930e8 268 }
Ozmos 0:aa0900f930e8 269
Ozmos 0:aa0900f930e8 270 //--------------------------------------------------------------------------------------------------
Ozmos 0:aa0900f930e8 271