Hello,
I’m trying to send data from Arduino Mega to a Wemos D1 ESP8266 board, could you please help? Something is wrong with the code but I can’t figure out what.
//Transmitter code
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
#define SAMPLES 20 //Number of samples you want to take everytime you loop
SoftwareSerial toESP(15,14);
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;
float Vcharg = 3.8;
String data = “”;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(115200);
while (!Serial) continue;
// Initialize the “link” serial port
// Use the lowest possible data rate to reduce error ratio
toESP.begin(4800);
toESP.print("From Arduino Mega 2560 Hello :wink: ");
}
// the loop routine runs over and over again forever:
void loop() {
//Reading PV module input status
// reading current from A2/supply voltage and calcultating average value
int Vr_valueavg = 0;
for (int i=0; i < SAMPLES; i++) {
Vr_valueavg = Vr_valueavg + analogRead(A2);
}
Vr_valueavg = Vr_valueavg /SAMPLES;
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 = 0;
for (int i=0; i < SAMPLES; i++) {
sensorValueavg = sensorValueavg + analogRead(A3);
}
sensorValueavg = sensorValueavg /SAMPLES;
float voltage_sensor = sensorValueavg * (5.0 / 1023.0);
float Iin = (voltage_sensor - 2.5)/ 0.185;
float Pin = Vin * Iin;
Serial.print(“PV module : Supply V=”);
Serial.print(Vin,3);
Serial.print(“V Supply I=”);
Serial.print(Iin,3);
Serial.print(“A P=”);
Serial.print(Pin,3);
Serial.print(“W”);
// 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 = 0;
for (int i=0; i < SAMPLES; i++) {
VoltageValueavg = VoltageValueavg + analogRead(A0);
}
VoltageValueavg = VoltageValueavg /SAMPLES;
float Vout = VoltageValueavg * (5.0 / 1023.0);
// reading current from A1/output current and calcultating average value
int ADCValueavg = 0;
for (int i=0; i < SAMPLES; i++) {
ADCValueavg = ADCValueavg + analogRead(A1);
}
ADCValueavg = ADCValueavg /SAMPLES;
float voltage_ADC = ADCValueavg * (5.0 / 1023.0);
float Iout = (voltage_ADC - 2.5)/ 0.1;
float Pout = Vout * Iout;
if (Vout > Vcharg){
Serial.print(" || Battery charging : Output V=");
Serial.print(Vout,3);
Serial.print(“V Ouput I =”);
Serial.print(Iout,3);
Serial.print(“A P=”);
Serial.print(Pout,3);
Serial.println(“W”);
}else if (Vout < Vcharg){
Serial.print(" Output V=");
Serial.print(Vout,3);
Serial.println(" || Status: Battery not charging; Note: V needs to be at least 3.8V to match battery rating");
}
delay(2000);
while(Serial.available()){
data = Serial.readString();
// Create the JSON document
StaticJsonDocument<3000> doc;
//getting data from sensors
doc[“Vin”] = Vin;
doc[“Iin”] = Iin;
doc[“Pin”] = Pin;
doc[“Vout”] = Vout;
doc[“Iout”] = Iout;
doc[“Pout”] = Pout;
serializeJson(doc,Serial);
toESP.write(toESP.read());
delay(2000);
}
}
ESP Code
/*
This code includes a part that is in the public domain and includes own inputs.
Voltage V, Unit V
Current I, Unit A
Power P, Unit W
*/
//Receiver code
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
#include “PageIndex.h”
SoftwareSerial toESP(0,1);
const char* ssid = “iPhone de Jelka”;
const char* password = “123456789”;
//
//const char* ssid = “WSG-Mobilna pracownia”;
//const char* password = “M0b1lnaPracown1a”;
ESP8266WebServer server(80);
void handle_Root() {
server.send(200, “text/html”, MAIN_page); //Send web page
}
void handle_NotFound() {
server.send(404,“text/plain”,“Page not found”);
}
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(115200);
Serial.println(“Jelka Bisa MPPT Project V, I and P measurements”);
WiFi.begin(ssid,password);
Serial.print(“Connecting network .”);
while(WiFi.status()!=WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(" done");
Serial.print("Successfully connected to: ");
Serial.println(ssid);
Serial.print("IP adress: ");
Serial.print(WiFi.localIP());
server.on("/", handle_Root); //–> This is to display web page.
server.begin(); //–> Start server
Serial.println(" HTTP server started");
}
// the loop routine runs over and over again forever:
void loop() {
StaticJsonDocument<3000> doc;
float Vin;
float Iin;
float Pin;
float Vout;
float Iout;
float Pout;
serializeJson(doc, Serial);
String data = “”;
if (Serial.available() > 0){
DeserializationError error = deserializeJson(doc, Serial);
if(error){
Serial.print(F((“deserializeJson() failed with code”)));
Serial.println(error.c_str());
messageReady = false;
return;
}
//Fetching values
Vin = doc["Vin"];
Iin = doc["Iin"];
Pin = doc["Pin"];
Vout = doc["Vout"];
Iout = doc["Iout"];
Pout = doc["Pout"];
Serial.print(“PV module : Supply V=”);
Serial.print(Vin,3);
Serial.print(“V Supply I=”);
Serial.print(Iin,3);
Serial.print(“A P=”);
Serial.print(Pin,3);
Serial.print(“W”);
Serial.print(" Battery charging : Output V=");
Serial.print(Vout,3);
Serial.print(“V Ouput I =”);
Serial.print(Iout,3);
Serial.print(“A P=”);
Serial.print(Pout,3);
Serial.println(“W”);
}
delay(2000);
server.handleClient();
}