Hello, I have a code that I am using to communicate between an arduino UNO and LOGO V8 Siemens.
everything works 5*, but if the network cable connection is interrupted a few times I leave data in the LOGO the communication is lost, I have to disconnect and reconnect the arduino to recover the connection. what can i do to solve it?
#include <SPI.h>
#include <Ethernet.h>
#include <MgsModbus.h>
#include <Wire.h>
#define GPS_RX 4
#define GPS_TX 3
#define GPS_Serial_Baud 9600
#include <SoftwareSerial.h>
#include <TinyGPS.h>
TinyGPS gps;
SoftwareSerial gpsSerial(GPS_RX, GPS_TX);
MgsModbus Mb; //Port 502
int cont = 0;
float GPS_HMI1, GPS_HMI2, GPS_HMI3, GPS_HMI4, GPS_HMI5, GPS_HMI6, flat, flon;
float GPS_LAT;
float GPS_LONG;
float angle;
float GPS_HMI_LAT = 38.123456;
float GPS_HMI_LONG = -7.123456;
byte mac[] = {0xE0, 0xDC, 0xDA0, 0x7A, 0xE5, 0x00 };
IPAddress ip(192, 168, 1, 6);
float toRadians(float angle) {
return (PI / 180) * angle;
}
float toDegrees(float rad) {
return (rad * 180) / PI;
}
void setup() {
Wire.begin();
Ethernet.begin(mac, ip);
delay(5000);
Serial.begin(9600); //9600
gpsSerial.begin(GPS_Serial_Baud);
}
void loop()
{
Mb.MbsRun();
// gps
bool newData = false;
cont = 5;
unsigned long chars;
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (gpsSerial.available())
{
char c = gpsSerial.read();
if (gps.encode(c)) // Atribui true para newData caso novos dados sejam recebidos
newData = true;
}
}
if (newData){
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
float GPS_LAT = flat;
float GPS_LONG = flon;
// int cont = gps.satellites();
float lat1 = toRadians(GPS_HMI_LAT);
float lat2 = toRadians(GPS_LAT);
float lng1 = toRadians(GPS_HMI_LONG);
float lng2 = toRadians(GPS_LONG);
float Y = sin(lng2 - lng1) * cos(lat2);
float X = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(lng2 - lng1);
float deg = toDegrees(atan2(Y, X));
// note that this implementation doesn't use the module, but angles lower than 0 get augmented by 360 only
if (deg < 0) {
deg = 360 + deg;
}
int angle = deg;
//Mb.MbsRun();
// Mb.MbData[0] = angle * 10;
// Serial.print("GPS_LAT: ");
// Serial.println(GPS_LAT, 6);
// Serial.print("GPS_LONG: ");
// Serial.println(GPS_LONG, 6);
// Serial.print("Angle: ");
// Serial.println(angle);
// Serial.print("GPS: ");
// Serial.println(cont);
Mb.MbsRun();
delay(10);
Mb.MbData[0] = angle * 10;
cont = 10;
//delay(10);
}
Mb.MbData[1] = cont;
}