Some code

I am designing a system to optimise solar charging opportunities for:

  • laptop
  • phone
  • backup battery for phone
  • AA rechargeables
  • handvac

This is facilitated with a NodeMCU micro-controller ESP8266 (ESP-12E module):

This is so I don’t have to keep an eye on the system during the day to prevent the leisure battery from being damaged.

I am waiting for a solid state relay and then it will be plumbed into the system.

I am also waiting for some diodes so I can add a photo-resistor to the system. This will be used to send the micro controller into deep sleep in very low light conditions (i.e. night time) so as not to drain the leisure battery unnecessarily.

The micro-controller has WiFi built in, as you’ve probably noticed, so I can set it up as a web server for me to connect to it and see that it’s doing it’s job and change parameters.

Here is the basic code. The print commands could be stripped out to make the code leaner but I think I’ll leave them in for troubleshooting, although there’s not a lot to go wrong:

void setup() {
Serial.begin(230400);
pinMode(D1,OUTPUT);
const int analogInPin=A0;
float ratioFactor=0.8;
float Voltage=0.0;
float sensorValue=0;
float sensorValueSample=0;
for(int i=0;i<10;i++){
sensorValueSample=analogRead(analogInPin);
sensorValue=sensorValue+sensorValueSample;
Serial.println(sensorValueSample);
delay(50);
}
sensorValue=(float)sensorValue/10.0;
Voltage=(float)(sensorValue*20)/1023.0;
Voltage=(float)(Voltage*ratioFactor);
if(Voltage<=12.4){
digitalWrite(D1,LOW);
Serial.println("LOW");
}else{
if(Voltage>=13.0){
digitalWrite(D1,HIGH);
Serial.println("HIGH");
}else{
Serial.println("MID");
}
}
Serial.print("Average A0 input from 10 samples over 0.5s = ");
Serial.print(Voltage);
Serial.println("V");
Serial.print("Average digital output from 10 samples over 0.5s = ");
Serial.println(sensorValue);
Serial.println("Going into deep sleep for 19.5s");
ESP.deepSleep(19.5e6);
}
void loop() {
}

One comment

Comments are closed.