// Mega2560 Serial1 JSON 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 #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; // sendLineLength, receiveLineLength SerialComs coms(250, 10); // 250 sendlength, 10 receive (nothing received) // sendLineLength must be > json string // but must be < 250 so whole line fits in Serial RX buffer // receiveLineLength must match other sides sendLineLength // sendLineLength must match other sides receiveLineLength StaticJsonDocument<1000> doc; // allocates on stack 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; //getting data from sensors doc["Vin"] = Vin; doc["Iin"] = Iin; doc["Pin"] = Pin; doc["Vout"] = Vout; doc["Iout"] = Iout; doc["Pout"] = Pout; if (coms.textToSend.isEmpty()) { serializeJson(doc, coms.textToSend); // Serial.println(coms.textToSend); } }