Hello everyone, for some time now I've been designing my solar system in the mountains using an Arduino to read the data and a raspberry to create the web page.
I'm using serial communication to send data from the Arduino to the raspberry and I came across an unusual problem that I can't solve.
This and the code I'm using, I'm reading the data with an RJ-45:
#include <ModbusMaster.h>
#define MAX485_DE 3
#define MAX485_RE_NEG 2
float MaximumInputVoltPVToday = 0;
float MinimumInputVoltPVToday = 0;
float MaximumBatteryVoltToday = 0;
float MinimumBatteryVoltToday = 0;
float ConsumedEnergyToday = 0;
float GeneratedEnergyToday = 0;
float TotalConsumed = 0;
float TotalGenerated = 0;
// instantiate ModbusMaster object
ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void setup()
{
Serial.begin(115200);
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
// Modbus communication runs at 115200 baud
// Modbus slave ID 1
node.begin(1, Serial);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
//-----------------------------------------
void Dati (void);
void loop()
{
Dati();
delay(5000);
}
void Dati ()
{
uint8_t resultMain;
resultMain = node.readInputRegisters(0x3300,19);
if (resultMain == node.ku8MBSuccess)
{
Serial.println("------------");
//Serial.print("Maximum input volt(PV) today: ");
MaximumInputVoltPVToday = (node.getResponseBuffer(0x00)/100.0f);
Serial.println(MaximumInputVoltPVToday);
//-----------------------------------------------
//Serial.print("Minimum input volt(PV) today: ");
MinimumInputVoltPVToday = (node.getResponseBuffer(0x01)/100.0f);
Serial.println(MinimumInputVoltPVToday);
//---------------------------------------------
//Serial.print("Maximum battery volt today: ");
MaximumBatteryVoltToday = (node.getResponseBuffer(0x02)/100.0f);
Serial.println(MaximumBatteryVoltToday);
//-----------------------------------------------
//Serial.print("Minimum battery volt today: ");
MinimumBatteryVoltToday = (node.getResponseBuffer(0x03)/100.0f);
Serial.println(MinimumBatteryVoltToday);
//-----------------------------------------------
//Serial.print("Consumed energy today: ");
ConsumedEnergyToday = (node.getResponseBuffer(0x04)/100.0f);
Serial.println(ConsumedEnergyToday);
//-----------------------------------------------
// Serial.print("Generated energy today: ");
GeneratedEnergyToday = (node.getResponseBuffer(0x0C)/100.0f);
Serial.println(GeneratedEnergyToday);
//-----------------------------------------------
//Serial.print("Total consumed: ");
TotalConsumed = (node.getResponseBuffer(0x0A)/100.0f);
Serial.println(TotalConsumed);
//-----------------------------------------------
//Serial.print("Total generated: ");
TotalGenerated = (node.getResponseBuffer(0x12)/100.0f);
Serial.println(TotalGenerated);
}
}
And in the serial port I read:
3??--------------
116.93
0.33
14.92
11.63
0.03
0.38
14.93
56.47
The values are right but I don't understand why I have that "dirt" at the beginning. Does anybody know what it's from? Clearly the raspberry, not knowing what it is, gives me the wrong thing and it's stuck...
Does anyone know how to eliminate it to get a clean data transmission?
Thank you very much, everyone