Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This is my second Arduino sketch, I wanted to monitor the output generated from my solar panels and switch power onto our Nissan Leaf BEV
- * when sufficient solar power is available to minimise power useage from the grid.
- *
- * My first sketch, still currently in use, used a clip on current transformer, bridge rectifyer, load resistor and capacitor smoothing.
- * I used, and will continue to use, a Sonoff TH which turns on a remote Sonoff POW just inside the garage door. This enables me to see how much power
- * I have put into the car. I took the TH part to pieces expecting to find a thermistor only to find a I2C I/F. However I found a lead to switch high and low,
- * giving a "temmperature" of -19 to +3 degrees. Also found 3.3v to power the Arduino all input via 3.5mm jack and socket.
- * Problems are A, getting the clip on ampmeter into the solar box was difficult. B, smoothing was inadequate causing calculated
- * watts to vary. C, I needed to guess what the wattage was using other measurements and found that output was not consistant and varied across the range,
- * possibly caused by cable loops inside the box. D. To change the turn on threshold according to the season, I need to change the sketch and upload it.
- *
- * This system works very well on the Leaf, but I cannot confirm any other models. I have heard that the Renault Zoe can get grumpy if power is turned off and on,
- * it is from the same stable as the Leaf, so possibly someone was switching too quickly.
- *
- * To test the second sketch, I used a cyle led light that had a flashing option. I simply blanked off various number of flashes and checked that output was consistent
- * for the same number of blanked flashes. Variation was very small, mostly exactly the same.
- *
- * This second sketch utilises the flashing LED on the generation meter and grid meter to give a very accurate watt reading and eliminate intrusion into power boxes.
- * I have tested it on the grid meter and the readings are a very close match to a "proper" mains monitor.
- *
- * My eventual aim. sketch 3, is to also monitor the grid LED and output both readings to a Nextion and have a button on the screen to
- * adjust the turn on threshold remotely. I suspect I will have to learn about interrupts to do this.
- *
- * I apologise if my novice sketch causes some raised eyebrows among the experienced Arduino programmers.
- *
- * Terry Rowland, retired computer hardware engineer.
- */
- unsigned long blinkfirst; // note time of flash
- unsigned long blinkInterval; // calculated time measured between flashes
- int blinkmeter; // Flash from generation meter,0 = off, 1 = on, 1000/hr = 1Kwh;
- unsigned long blinksecond; // note time of next flash
- String chargeState="Reset"; //simply not on, off or blank
- unsigned long currentTime;
- int inPin = 7; // output from digital light sensor connected to digital pin 7
- unsigned long setWaitTime=120000; // used to set time between car charge on/off frequency, 2 minutes.
- int switchOff; // stop charge threshold, set in setup to switchOn - 200, simply a "reasonable" range
- volatile int switchOn=1900; // start charge watts threshold, plan to use nextion buttons to change this with the seasons
- unsigned long time;
- unsigned long waitTimeStart; // note time charge on/off check interval start
- unsigned long wattConversion = 3600000; // 1000 flashes/hr = 1 Kwh, taken from meter info on generation meter;
- // 1000 flashes/3600 secs = 1 Kw;
- // 1 flash/3.6 secs = 1 Kw;
- // 1 flash/3600 msecs = 1 kw;
- // 1 flash/3,600,000 msecs = 1 watt;
- // 3,600,000 divided by blinkInterval = watts
- int watts;
- void setup() // the setup routine runs once when you press reset or power on;
- {
- switchOff=(switchOn-200); // arbitory range, this way means Nextion button need change volatile switch On only.
- Serial.begin(9600);
- pinMode(9,OUTPUT);
- pinMode(inPin, INPUT);
- pinMode(13,OUTPUT);
- digitalWrite(LED_BUILTIN, LOW); // only used during debug/testing
- watts=0;
- digitalWrite(9, LOW); // to ensure sonoffTH is off to start with, it is on otherwise
- watts = constrain(watts, 0, 4000); // stops spurious values observed during debug/testing, probably cured by trim pot on photo sensor board
- time=millis();
- waitTimeStart=time;
- }
- void loop()
- {
- /* Serial.print(waitTimeStart);
- Serial.print(" Start Time "); Used for debug/testing
- Serial.print(latestTime);
- Serial.print(" latest time ");
- Serial.println((latestTime-waitTimeStart));
- */
- while(digitalRead(inPin)==HIGH) //high = LED off so wait for led to flash on
- {
- time=millis();
- currentTime=time;
- if ((currentTime-waitTimeStart)>setWaitTime) CheckChargeState();
- /* Serial.print(waitTimeStart);
- Serial.print(" Start Time 1, "); Used for debug/testing
- Serial.print(latestTime);
- Serial.print(" latest time 1,");
- Serial.println((latestTime-waitTimeStart));
- */
- }
- time=millis();
- blinkfirst=time; // time when led turns on
- blinkInterval=(blinkfirst-blinksecond);
- watts=(wattConversion/blinkInterval);
- if(blinkInterval<850)watts =0; // meter led on constantly when no generation gives blinkInterval of 800ms delay
- // Serial.println(blinkInterval); // used for debug/testing
- Serial.print(watts); // will o/p to Nextion next version meanwhile enables check of solar output, not convenient but useful.
- Serial.println(" Watts a"); // will o/p to Nextion next version
- delay(800); // kills possible de-bounce/flicker, 800ms is outside max generation of my solar panels
- while(digitalRead(inPin)==HIGH) //LED off, digital light sensor inverts light input
- {
- time=millis();
- currentTime=time;
- if ((currentTime-waitTimeStart)>setWaitTime) CheckChargeState();
- /* Serial.print(waitTimeStart);
- Serial.print(" Start Time 2 ");
- Serial.print(latestTime); used for debug/testing
- Serial.print(" latest time 2 ");
- Serial.println((currentTime-waitTimeStart));
- */
- }
- time=millis();
- blinksecond=time; // note when led turns on again
- blinkInterval=(blinksecond-blinkfirst); // calculate time beteen meter LED blinks.
- watts=(wattConversion/blinkInterval);
- if(blinkInterval<850)watts =0;
- Serial.print(watts); // will o/p to Nextion next version meanwhile enables check of solar output, not convenient but useful.
- Serial.println(" Watts b");
- /* meter led on constantly when no generation
- gives blinkInterval of 800 from de-bounce delay.
- Ensures car turns off if blinks stop unexpectedly and leave a value in watts.
- Under normal operation watts gradually decrease to turn car off.
- */
- /*Serial.println(blinkInterval); // used for debug or testing.
- Serial.print(watts);
- Serial.println(" Watts b");
- */
- delay(800); // de-bounce/flicker kill
- } // end of void loop
- void CheckChargeState()
- {
- if (watts > switchOn)
- {
- digitalWrite(9, HIGH); // change to LED_BUILTIN, LOW for testing
- chargeState="Car charging.";
- }
- if (watts < switchOff)
- {
- digitalWrite(9, LOW); // change to LED_BUILTIN, LOW for testing
- chargeState="Car not charging.";
- }
- Serial.println(chargeState); // will not need when Nextion working
- time=millis();
- waitTimeStart=time; //delay between charge stop/start
- /* Serial.print(waitTimeStart); //used for debug/testing
- Serial.println(" Start Time 3 ");
- */
- time=millis();
- currentTime=time;
- /*Serial.print(latestTime);
- Serial.print(" latest time 3 "); // used for debug/testing
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement