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.
gate1.cpp@0:aa0900f930e8, 2017-04-06 (annotated)
- Committer:
- Ozmos
- Date:
- Thu Apr 06 03:40:54 2017 +0000
- Revision:
- 0:aa0900f930e8
It is an automated solar controller system that operates with, 12V solar panel, SLA battery ratted 6V 4A, nokia 5110 screen, push buttons with LEDs and HC-05 Bluetooth module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ozmos | 0:aa0900f930e8 | 1 | #include"mbed.h" |
Ozmos | 0:aa0900f930e8 | 2 | #include "Terminal.h" |
Ozmos | 0:aa0900f930e8 | 3 | #include "N5110.h" |
Ozmos | 0:aa0900f930e8 | 4 | #include "string" |
Ozmos | 0:aa0900f930e8 | 5 | |
Ozmos | 0:aa0900f930e8 | 6 | /*********************************Pin decleration*************/ |
Ozmos | 0:aa0900f930e8 | 7 | Serial pc(USBTX, USBRX); |
Ozmos | 0:aa0900f930e8 | 8 | Serial blue(PTC15,PTC14); |
Ozmos | 0:aa0900f930e8 | 9 | AnalogIn solar_voltage(A0); |
Ozmos | 0:aa0900f930e8 | 10 | AnalogIn battery_voltage(A1); |
Ozmos | 0:aa0900f930e8 | 11 | AnalogIn load_current(A2); |
Ozmos | 0:aa0900f930e8 | 12 | DigitalOut solar_gate(D5); |
Ozmos | 0:aa0900f930e8 | 13 | DigitalOut load_gate(D4); |
Ozmos | 0:aa0900f930e8 | 14 | DigitalIn pushBtnLeft(D3); |
Ozmos | 0:aa0900f930e8 | 15 | DigitalIn pushBtnRight(D2); |
Ozmos | 0:aa0900f930e8 | 16 | DigitalOut blueLed(D0); |
Ozmos | 0:aa0900f930e8 | 17 | /*******************************************************************/ |
Ozmos | 0:aa0900f930e8 | 18 | using std::string; |
Ozmos | 0:aa0900f930e8 | 19 | /****************************Nokia5110 Pin Decleration*********/ |
Ozmos | 0:aa0900f930e8 | 20 | //SCE,RST,D/C,MOSI,SCLK,LED |
Ozmos | 0:aa0900f930e8 | 21 | N5110 lcd(PTA0,PTC2,PTC4,PTD2,PTD1,PTC3); |
Ozmos | 0:aa0900f930e8 | 22 | /*****************************************************************/ |
Ozmos | 0:aa0900f930e8 | 23 | #define solargate |
Ozmos | 0:aa0900f930e8 | 24 | #define refVolt 3300 |
Ozmos | 0:aa0900f930e8 | 25 | float solarVoltage; |
Ozmos | 0:aa0900f930e8 | 26 | unsigned char sGate = 'A'; |
Ozmos | 0:aa0900f930e8 | 27 | unsigned char bGate = 'B'; |
Ozmos | 0:aa0900f930e8 | 28 | |
Ozmos | 0:aa0900f930e8 | 29 | string gateStat= "LR255G0B0"; // INTIALLY IT IS RED, GREEN IS LR0G255B0 |
Ozmos | 0:aa0900f930e8 | 30 | float batteryVoltage; |
Ozmos | 0:aa0900f930e8 | 31 | /********************************************************Function Prototypes************/ |
Ozmos | 0:aa0900f930e8 | 32 | |
Ozmos | 0:aa0900f930e8 | 33 | float fnc_solarPanel(void); |
Ozmos | 0:aa0900f930e8 | 34 | bool fnc_chargingstat(float i); |
Ozmos | 0:aa0900f930e8 | 35 | float fnc_batteryVolt(void); |
Ozmos | 0:aa0900f930e8 | 36 | bool fnc_loadstat(float i); |
Ozmos | 0:aa0900f930e8 | 37 | float fnc_load_current(void); |
Ozmos | 0:aa0900f930e8 | 38 | |
Ozmos | 0:aa0900f930e8 | 39 | /************************************************************M A I N************************/ |
Ozmos | 0:aa0900f930e8 | 40 | int main(){ |
Ozmos | 0:aa0900f930e8 | 41 | bool loadStatus, chargingStatus,screenOne,screenTwo; |
Ozmos | 0:aa0900f930e8 | 42 | solarVoltage = 0.0; |
Ozmos | 0:aa0900f930e8 | 43 | batteryVoltage = 0.0; |
Ozmos | 0:aa0900f930e8 | 44 | float loadCurrent = 0.0; |
Ozmos | 0:aa0900f930e8 | 45 | float usagePower = 0.0; |
Ozmos | 0:aa0900f930e8 | 46 | float powerLeftinW = 0.0; |
Ozmos | 0:aa0900f930e8 | 47 | int powerLeftinH = 0.0; |
Ozmos | 0:aa0900f930e8 | 48 | char outputs[10];//it is used for conversion from solar volt (float) to char in order to print as string. |
Ozmos | 0:aa0900f930e8 | 49 | char outputb[10];//it is used for conversion from battery charge(float) to char in order to print as string. |
Ozmos | 0:aa0900f930e8 | 50 | char outputc[10];//it is used for conversion from current charge(float) to char in order to print as string. |
Ozmos | 0:aa0900f930e8 | 51 | char blueInput; |
Ozmos | 0:aa0900f930e8 | 52 | int left, right; |
Ozmos | 0:aa0900f930e8 | 53 | lcd.init(); |
Ozmos | 0:aa0900f930e8 | 54 | lcd.normalMode(); |
Ozmos | 0:aa0900f930e8 | 55 | lcd.setBrightness(1.0); |
Ozmos | 0:aa0900f930e8 | 56 | |
Ozmos | 0:aa0900f930e8 | 57 | loadStatus = false; |
Ozmos | 0:aa0900f930e8 | 58 | chargingStatus = false; |
Ozmos | 0:aa0900f930e8 | 59 | screenOne = true; |
Ozmos | 0:aa0900f930e8 | 60 | screenTwo = false; |
Ozmos | 0:aa0900f930e8 | 61 | right = 1; |
Ozmos | 0:aa0900f930e8 | 62 | left = 0; |
Ozmos | 0:aa0900f930e8 | 63 | blueLed = 0; |
Ozmos | 0:aa0900f930e8 | 64 | while(1){ |
Ozmos | 0:aa0900f930e8 | 65 | |
Ozmos | 0:aa0900f930e8 | 66 | if (blue.readable()) { |
Ozmos | 0:aa0900f930e8 | 67 | blueLed = 1; |
Ozmos | 0:aa0900f930e8 | 68 | |
Ozmos | 0:aa0900f930e8 | 69 | blueInput = blue.getc(); |
Ozmos | 0:aa0900f930e8 | 70 | switch(blueInput){ |
Ozmos | 0:aa0900f930e8 | 71 | case 'B' : bGate = 'B'; break; |
Ozmos | 0:aa0900f930e8 | 72 | case 'b' : bGate = 'b'; break; |
Ozmos | 0:aa0900f930e8 | 73 | case 'A': sGate = 'A';break; |
Ozmos | 0:aa0900f930e8 | 74 | case 'a' : sGate = 'a'; break; |
Ozmos | 0:aa0900f930e8 | 75 | |
Ozmos | 0:aa0900f930e8 | 76 | }} |
Ozmos | 0:aa0900f930e8 | 77 | |
Ozmos | 0:aa0900f930e8 | 78 | |
Ozmos | 0:aa0900f930e8 | 79 | |
Ozmos | 0:aa0900f930e8 | 80 | |
Ozmos | 0:aa0900f930e8 | 81 | |
Ozmos | 0:aa0900f930e8 | 82 | |
Ozmos | 0:aa0900f930e8 | 83 | right = pushBtnRight.read(); |
Ozmos | 0:aa0900f930e8 | 84 | left = pushBtnLeft.read(); |
Ozmos | 0:aa0900f930e8 | 85 | if(right){screenOne = false; screenTwo = true; } |
Ozmos | 0:aa0900f930e8 | 86 | if(left){screenOne=true;screenTwo=false;} |
Ozmos | 0:aa0900f930e8 | 87 | if(screenOne) |
Ozmos | 0:aa0900f930e8 | 88 | { |
Ozmos | 0:aa0900f930e8 | 89 | |
Ozmos | 0:aa0900f930e8 | 90 | |
Ozmos | 0:aa0900f930e8 | 91 | lcd.clear(); |
Ozmos | 0:aa0900f930e8 | 92 | solarVoltage=fnc_solarPanel();//solar voltge |
Ozmos | 0:aa0900f930e8 | 93 | sprintf(outputs,"SolarV: %.2fV",solarVoltage); |
Ozmos | 0:aa0900f930e8 | 94 | lcd.printString(outputs,0,0); |
Ozmos | 0:aa0900f930e8 | 95 | //lcd.refresh(); |
Ozmos | 0:aa0900f930e8 | 96 | blue.printf("*S%s*",outputs); |
Ozmos | 0:aa0900f930e8 | 97 | |
Ozmos | 0:aa0900f930e8 | 98 | |
Ozmos | 0:aa0900f930e8 | 99 | chargingStatus = fnc_chargingstat(solarVoltage); // return true if volt bigger than 6V else false and sets the gate |
Ozmos | 0:aa0900f930e8 | 100 | if (chargingStatus){lcd.printString("Charging: ON ",0,1);/*lcd.refresh();*/blue.printf("*LR0G255B0*");} |
Ozmos | 0:aa0900f930e8 | 101 | else {lcd.printString("Charging: OFF",0,1);blue.printf("*LR255G0B0*");} |
Ozmos | 0:aa0900f930e8 | 102 | /*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 103 | batteryVoltage = fnc_batteryVolt(); // return battery volt as float |
Ozmos | 0:aa0900f930e8 | 104 | sprintf(outputb,"Battery: %.2fV",batteryVoltage); /*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 105 | lcd.printString(outputb,0,2); |
Ozmos | 0:aa0900f930e8 | 106 | /*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 107 | blue.printf("*B%s*",outputb); |
Ozmos | 0:aa0900f930e8 | 108 | |
Ozmos | 0:aa0900f930e8 | 109 | loadStatus = fnc_loadstat(batteryVoltage); // return true if volt higger than 6v else fals end sets the gate |
Ozmos | 0:aa0900f930e8 | 110 | if (loadStatus){lcd.printString("Status: ON ",0,3);/*lcd.refresh();*/blue.printf("*MR0G255B0*");} |
Ozmos | 0:aa0900f930e8 | 111 | else {lcd.printString("Status: OFF",0,3);blue.printf("*MR255G0B0*");} |
Ozmos | 0:aa0900f930e8 | 112 | /*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 113 | fnc_load_current(); // only return current as float and power calculations are required |
Ozmos | 0:aa0900f930e8 | 114 | lcd.printString("Power Usage =>",0,5); |
Ozmos | 0:aa0900f930e8 | 115 | lcd.refresh(); |
Ozmos | 0:aa0900f930e8 | 116 | fnc_load_current(); |
Ozmos | 0:aa0900f930e8 | 117 | loadCurrent = fnc_load_current(); |
Ozmos | 0:aa0900f930e8 | 118 | sprintf(outputc,"Load:%.2fmA",loadCurrent); |
Ozmos | 0:aa0900f930e8 | 119 | |
Ozmos | 0:aa0900f930e8 | 120 | blue.printf("*E%s*",outputc); |
Ozmos | 0:aa0900f930e8 | 121 | usagePower = loadCurrent * batteryVoltage; |
Ozmos | 0:aa0900f930e8 | 122 | |
Ozmos | 0:aa0900f930e8 | 123 | sprintf(outputc,"L-Power:%.2fmW",usagePower); |
Ozmos | 0:aa0900f930e8 | 124 | |
Ozmos | 0:aa0900f930e8 | 125 | blue.printf("*I%s*",outputc); |
Ozmos | 0:aa0900f930e8 | 126 | powerLeftinW = 24 - usagePower; |
Ozmos | 0:aa0900f930e8 | 127 | if(batteryVoltage <= 5) powerLeftinW=0.0; |
Ozmos | 0:aa0900f930e8 | 128 | sprintf(outputc,"B-Power:%.1fkW",powerLeftinW); |
Ozmos | 0:aa0900f930e8 | 129 | blue.printf("*I%s*",outputc); |
Ozmos | 0:aa0900f930e8 | 130 | |
Ozmos | 0:aa0900f930e8 | 131 | powerLeftinH = 24 / usagePower; |
Ozmos | 0:aa0900f930e8 | 132 | if(batteryVoltage <= 5) powerLeftinH=0.0; |
Ozmos | 0:aa0900f930e8 | 133 | sprintf(outputc,"B-Hour:%dh", powerLeftinH); |
Ozmos | 0:aa0900f930e8 | 134 | |
Ozmos | 0:aa0900f930e8 | 135 | blue.printf("*N%s*",outputc); |
Ozmos | 0:aa0900f930e8 | 136 | |
Ozmos | 0:aa0900f930e8 | 137 | wait_ms(500); |
Ozmos | 0:aa0900f930e8 | 138 | }//if screenone |
Ozmos | 0:aa0900f930e8 | 139 | |
Ozmos | 0:aa0900f930e8 | 140 | if(screenTwo) |
Ozmos | 0:aa0900f930e8 | 141 | { |
Ozmos | 0:aa0900f930e8 | 142 | lcd.clear(); |
Ozmos | 0:aa0900f930e8 | 143 | |
Ozmos | 0:aa0900f930e8 | 144 | solarVoltage=fnc_solarPanel();//solar voltge |
Ozmos | 0:aa0900f930e8 | 145 | sprintf(outputs,"SolarV: %.2fV",solarVoltage); |
Ozmos | 0:aa0900f930e8 | 146 | |
Ozmos | 0:aa0900f930e8 | 147 | blue.printf("*S%s*",outputs); |
Ozmos | 0:aa0900f930e8 | 148 | |
Ozmos | 0:aa0900f930e8 | 149 | |
Ozmos | 0:aa0900f930e8 | 150 | chargingStatus = fnc_chargingstat(solarVoltage); // return true if volt bigger than 6V else false and sets the gate |
Ozmos | 0:aa0900f930e8 | 151 | if (chargingStatus){blue.printf("*LR0G255B0*");} |
Ozmos | 0:aa0900f930e8 | 152 | else {blue.printf("*LR255G0B0*");} |
Ozmos | 0:aa0900f930e8 | 153 | |
Ozmos | 0:aa0900f930e8 | 154 | batteryVoltage = fnc_batteryVolt(); // return battery volt as float |
Ozmos | 0:aa0900f930e8 | 155 | sprintf(outputb,"Battery: %.2fV",batteryVoltage); |
Ozmos | 0:aa0900f930e8 | 156 | |
Ozmos | 0:aa0900f930e8 | 157 | |
Ozmos | 0:aa0900f930e8 | 158 | blue.printf("*B%s*",outputb); |
Ozmos | 0:aa0900f930e8 | 159 | |
Ozmos | 0:aa0900f930e8 | 160 | loadStatus = fnc_loadstat(batteryVoltage); // return true if volt higger than 6v else fals end sets the gate |
Ozmos | 0:aa0900f930e8 | 161 | if (loadStatus){blue.printf("*MR0G255B0*");} |
Ozmos | 0:aa0900f930e8 | 162 | else {blue.printf("*MR255G0B0*");} |
Ozmos | 0:aa0900f930e8 | 163 | |
Ozmos | 0:aa0900f930e8 | 164 | fnc_load_current(); // only return current as float and power calculations are required |
Ozmos | 0:aa0900f930e8 | 165 | //******************************* |
Ozmos | 0:aa0900f930e8 | 166 | |
Ozmos | 0:aa0900f930e8 | 167 | loadCurrent = fnc_load_current(); |
Ozmos | 0:aa0900f930e8 | 168 | sprintf(outputc,"Load:%.2fmA",loadCurrent); /*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 169 | lcd.printString(outputc,0,0);/*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 170 | blue.printf("*E%s*",outputc); |
Ozmos | 0:aa0900f930e8 | 171 | usagePower = loadCurrent * batteryVoltage; |
Ozmos | 0:aa0900f930e8 | 172 | |
Ozmos | 0:aa0900f930e8 | 173 | sprintf(outputc,"L-Power:%.2fmW",usagePower); /*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 174 | lcd.printString(outputc,0,1);/*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 175 | blue.printf("*I%s*",outputc); |
Ozmos | 0:aa0900f930e8 | 176 | powerLeftinW = 24 - usagePower; |
Ozmos | 0:aa0900f930e8 | 177 | if(batteryVoltage <= 5) powerLeftinW=0.0; |
Ozmos | 0:aa0900f930e8 | 178 | sprintf(outputc,"B-Power:%.1fkW",powerLeftinW); /*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 179 | |
Ozmos | 0:aa0900f930e8 | 180 | blue.printf("*I%s*",outputc); |
Ozmos | 0:aa0900f930e8 | 181 | lcd.printString(outputc,0,2);/*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 182 | powerLeftinH = 24 / usagePower; |
Ozmos | 0:aa0900f930e8 | 183 | if(batteryVoltage <= 5) powerLeftinH=0.0; |
Ozmos | 0:aa0900f930e8 | 184 | sprintf(outputc,"B-Hour:%dh", powerLeftinH); /*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 185 | lcd.printString(outputc,0,3);/*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 186 | blue.printf("*N%s*",outputc); |
Ozmos | 0:aa0900f930e8 | 187 | lcd.printString("",0,1);/*lcd.refresh();*/ |
Ozmos | 0:aa0900f930e8 | 188 | lcd.printString("<= Charger",0,5); |
Ozmos | 0:aa0900f930e8 | 189 | lcd.refresh(); |
Ozmos | 0:aa0900f930e8 | 190 | wait_ms(500); |
Ozmos | 0:aa0900f930e8 | 191 | }// if screentwo |
Ozmos | 0:aa0900f930e8 | 192 | |
Ozmos | 0:aa0900f930e8 | 193 | } |
Ozmos | 0:aa0900f930e8 | 194 | } |
Ozmos | 0:aa0900f930e8 | 195 | /******************************************E N D M A I N*************************/ |
Ozmos | 0:aa0900f930e8 | 196 | |
Ozmos | 0:aa0900f930e8 | 197 | |
Ozmos | 0:aa0900f930e8 | 198 | |
Ozmos | 0:aa0900f930e8 | 199 | /******************************************Function Definations********************/ |
Ozmos | 0:aa0900f930e8 | 200 | |
Ozmos | 0:aa0900f930e8 | 201 | float fnc_solarPanel(void)//Solar Panel Voltage at A0 in ------------------------------------------- |
Ozmos | 0:aa0900f930e8 | 202 | { |
Ozmos | 0:aa0900f930e8 | 203 | float solarVolt = ((solar_voltage.read() * (refVolt/1000.0))*2.63); |
Ozmos | 0:aa0900f930e8 | 204 | return solarVolt; |
Ozmos | 0:aa0900f930e8 | 205 | } |
Ozmos | 0:aa0900f930e8 | 206 | //------------------------------------------------------------------------------------------------------------- |
Ozmos | 0:aa0900f930e8 | 207 | |
Ozmos | 0:aa0900f930e8 | 208 | |
Ozmos | 0:aa0900f930e8 | 209 | bool fnc_chargingstat(float i)//------------------------------------------------------------------------- |
Ozmos | 0:aa0900f930e8 | 210 | { |
Ozmos | 0:aa0900f930e8 | 211 | if (i<6|| sGate =='a' ) |
Ozmos | 0:aa0900f930e8 | 212 | { |
Ozmos | 0:aa0900f930e8 | 213 | solar_gate = 0; |
Ozmos | 0:aa0900f930e8 | 214 | gateStat = "LR255GOBO"; |
Ozmos | 0:aa0900f930e8 | 215 | // blue.printf("*OR255G0B0*"); |
Ozmos | 0:aa0900f930e8 | 216 | |
Ozmos | 0:aa0900f930e8 | 217 | return false; |
Ozmos | 0:aa0900f930e8 | 218 | } |
Ozmos | 0:aa0900f930e8 | 219 | else { |
Ozmos | 0:aa0900f930e8 | 220 | // blue.printf("*OR0G255B0*"); |
Ozmos | 0:aa0900f930e8 | 221 | |
Ozmos | 0:aa0900f930e8 | 222 | gateStat= "LROG255BO"; |
Ozmos | 0:aa0900f930e8 | 223 | solar_gate = 1; return true;} |
Ozmos | 0:aa0900f930e8 | 224 | |
Ozmos | 0:aa0900f930e8 | 225 | } |
Ozmos | 0:aa0900f930e8 | 226 | //------------------------------------------------------------------------------------------------------------- |
Ozmos | 0:aa0900f930e8 | 227 | float fnc_batteryVolt(void) |
Ozmos | 0:aa0900f930e8 | 228 | { |
Ozmos | 0:aa0900f930e8 | 229 | batteryVoltage = ((battery_voltage.read()*(refVolt/1000.0))*3.7789); |
Ozmos | 0:aa0900f930e8 | 230 | return batteryVoltage; |
Ozmos | 0:aa0900f930e8 | 231 | } |
Ozmos | 0:aa0900f930e8 | 232 | //-------------------------------------------------------------------------------------------------- |
Ozmos | 0:aa0900f930e8 | 233 | |
Ozmos | 0:aa0900f930e8 | 234 | bool fnc_loadstat(float i) |
Ozmos | 0:aa0900f930e8 | 235 | { //bool stat; |
Ozmos | 0:aa0900f930e8 | 236 | |
Ozmos | 0:aa0900f930e8 | 237 | if (i <6 ){ blue.printf("*OR255G0B0*"); return false; } |
Ozmos | 0:aa0900f930e8 | 238 | else{ blue.printf("*OR0G255B0*"); return true;} |
Ozmos | 0:aa0900f930e8 | 239 | |
Ozmos | 0:aa0900f930e8 | 240 | // return stat; |
Ozmos | 0:aa0900f930e8 | 241 | } |
Ozmos | 0:aa0900f930e8 | 242 | |
Ozmos | 0:aa0900f930e8 | 243 | //----------------------------------------------------------------------------------------------------------------------- |
Ozmos | 0:aa0900f930e8 | 244 | float fnc_load_current(void) |
Ozmos | 0:aa0900f930e8 | 245 | { |
Ozmos | 0:aa0900f930e8 | 246 | float loadcurrent = load_current.read(); |
Ozmos | 0:aa0900f930e8 | 247 | return loadcurrent; |
Ozmos | 0:aa0900f930e8 | 248 | } |
Ozmos | 0:aa0900f930e8 | 249 | |
Ozmos | 0:aa0900f930e8 | 250 | //-------------------------------------------------------------------------------------------------- |
Ozmos | 0:aa0900f930e8 | 251 |