// Mega2560 Serial1 CSV to ESP2866 //https://forum.arduino.cc/t/arduino-mega-to-esp8266-communication-via-tx-rx/857016/67 // download SafeString V4.1.0+ library from the Arduino Library manager or from // https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html #include "SerialComs.h" // use Mega HardwareSerial Serial1 #define toESP Serial1 #define SAMPLES 20 //Number of samples you want to take everytime you loop // do the reads in setup if you need to do initial reads int voltageValue;// = analogRead(A0); //From Sunnybuddy output int ADCValue;// = analogRead(A1); // From Sensor output int Vr_value;// = analogRead(A2); // From solar panel int sensorValue;// = analogRead(A3); // From solar panel float R1 = 470000; // These 2 resistors build up a Voltage divider to bring down PV voltage down to Arduino operating voltage float R2 = 100000; // default send/receive length 60 chars SerialComs coms; void setup() { Serial.begin(115200); for (int i = 10; i > 0; i--) { Serial.print(' '); Serial.print(i); delay(500); } Serial.println(); SafeString::setOutput(Serial); // enable error msgs and debug toESP.begin(9600); // Initialize the "link" serial port if (!coms.connect(toESP)) { while (1) { Serial.println(F("Out of memory")); delay(3000); } } Serial.println(F("Mega2560 Setup finished.")); Serial.println(F("waiting for connection.")); // if you want a startup msg //coms.textToSend = F("StartedUp"); // let the other side know we have just booted up } // The Atmel datasheets for the micro-processors Arduino uses for Uno/Mega2560 advises // the user that the first A/D reading after changing the reference voltage may be incorrect and should be ignored. // 20 samples takes about 2mS to complete int readAnalog(int Apin) { long analogValue = analogRead(Apin); // allow for sum // discard the first one analogValue = 0; for (int i = 0; i < SAMPLES; i++) { analogValue = analogValue + analogRead(Apin); } analogValue = analogValue / SAMPLES; return (int)analogValue; } // the 4 adc reads in this loop take about 10mS // comd takes about 1sec to send 100chars at 9600 baud void loop() { coms.sendAndReceive(); // must do this every loop //Reading PV module input status // reading current from A2/supply voltage and calcultating average value int Vr_valueavg = readAnalog(A2); float Vr = Vr_valueavg * (5.0 / 1023.0); float Vin = Vr * ((R1 + R2) / R2); // reading current from A3/supply current and calcultating average value int sensorValueavg = readAnalog(A3); float voltage_sensor = sensorValueavg * (5.0 / 1023.0); float Iin = (voltage_sensor - 2.5) / 0.185; float Pin = Vin * Iin; // reading voltage from A0/output voltage and calcultating average value, // we are interested in value above 3.8V as it is our rated battery charging voltage int VoltageValueavg = readAnalog(A0); float Vout = VoltageValueavg * (5.0 / 1023.0); // reading current from A1/output current and calcultating average value int ADCValueavg = readAnalog(A1); float voltage_ADC = ADCValueavg * (5.0 / 1023.0); float Iout = (voltage_ADC - 2.5) / 0.1; float Pout = Vout * Iout; if (coms.textToSend.isEmpty()) { coms.textToSend.print(Vin, 3, 7, true); coms.textToSend.print(','); coms.textToSend.print(Iin, 3, 7, true); coms.textToSend.print(','); coms.textToSend.print(Pin, 3, 7, true); coms.textToSend.print(','); coms.textToSend.print(Vout, 3, 7, true); coms.textToSend.print(','); coms.textToSend.print(Iout, 3, 7, true); coms.textToSend.print(','); coms.textToSend.print(Pout, 3, 7, true); // Serial.println(coms.textToSend); //debugging } }