/* Ethernet Modbus TCP Client Toggle This sketch read the register of a Modbus TCP server connected : Victron cerbo GX on and off every second. Circuit: (tested on MKR 1010 + ETH Shield) - Any Arduino MKR Board - MKR ETH Shield created 16 July 2018 by Sandeep Mistry, adapted by JL Duhamel 2021 V6 : se connecte sur le routeur Dlink, mais pas sur le cerbo, pourtant en DHCP mais pas remarqué sur la liste de Luci... "Failed to configure Ethernet using DHCP Connecté sur le CERBO via le DLINK!!! en ethernet!! V9 : essai en Cerbo direct ethernet => échoué */ #include #include #include // ArduinoModbus depends on the ArduinoRS485 library #include // Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield // The IP address will be dependent on your local network: byte mac[] = { //0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED // wifi MKR board 0xA8, 0x61, 0x0A, 0xAE, 0x45, 0x2D //OF ETHERNET MKR BOARD !!!!!!!!!!!! }; IPAddress ip(192, 168, 1, 177); // IP of MKR via router //IPAddress ip(169, 254, 72, 205); //IP of MKR direct on Cerbo GX //ATTENTION, LOCAL ADRESS DELIVERED BY CERBO CHANGE AT EACH CONNEXION!!!! //IPAddress server(169, 254, 1, 2); //IP Ethernet du Cerbo direct RJ45 // update with the IP Address of your Modbus server IPAddress server(192, 168, 1, 246); //IP Cerbo via Router // update with the IP Address of your Modbus server EthernetClient ethClient; ModbusTCPClient modbusTCPClient(ethClient); void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } Serial.println("Aux ETH ethernet------------------------------"); // Check for Ethernet hardware present // start the Ethernet connection and the server: //Ethernet.begin(mac);//, ip); int i=0; int cnxETH = 0; //Ethernet.init(5); //MKR ETH do { Serial.println("Essai Ethernet : " + String( i++)); cnxETH = Ethernet.begin(mac); if (cnxETH == 0) { Serial.println("Failed to configure Ethernet using DHCP"); //=> HERE WHEN PLUGED DIRECTLY TO CERBO // no point in carrying on, so do nothing forevermore: if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); while (true) { delay(1); // do nothing, no point running without Ethernet hardware } } if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); } else { // try to congifure using IP address instead of DHCP: Serial.println("Test ethernet with forced IP..."); delay(5000); Ethernet.begin(mac,ip); delay(1000); Serial.println(Ethernet.localIP()); if (Ethernet.localIP()>0) cnxETH=true; } //while(true); } else Serial.println("Ethernet Ok"); if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found."); } else if (Ethernet.hardwareStatus() == EthernetW5100) { Serial.println("W5100 Ethernet controller detected."); } else if (Ethernet.hardwareStatus() == EthernetW5200) { Serial.println("W5200 Ethernet controller detected."); } else if (Ethernet.hardwareStatus() == EthernetW5500) { Serial.println("W5500 Ethernet controller detected."); //!!!!!!yes!!! } delay(2000); } while (cnxETH == 0); // give the Ethernet shield a second to initialize: delay(1000); Serial.println("connecting serveur..."); //------------------------------connection to internet, just to try------------ // if you get a connection, report back via serial: if (ethClient.connect(server, 80)) { Serial.println("connected internet"); // Make a HTTP request: ethClient.println("GET /search?q=arduino HTTP/1.0"); ethClient.println(); } else { // kf you didn't get a connection to the server: Serial.println("connection internet failed"); } //------------------------------connexion to cerbo ------------ if (!modbusTCPClient.begin(server)) { //, 502)) { Serial.print("IP = "); Serial.println(Ethernet.localIP()); Serial.println("Modbus TCP Client failed to connect!"); } else { Serial.println("Modbus TCP Client connected"); Serial.println("--connexion Ok--"); } Serial.println("-----end setup"); } void loop() { if (!modbusTCPClient.connected()) { // client not connected, start the Modbus TCP client Serial.println("Attempting to connect to Modbus TCP server"); if (!modbusTCPClient.begin(server)) { Serial.print("IP = "); Serial.println(Ethernet.localIP()); Serial.println("Modbus TCP Client failed to connect!"); } else { Serial.println("Modbus TCP Client connected"); Serial.println("--connexion Ok--"); } } else { // client connected long RegCerbo=0; Serial.print("prod daily KWh MPPT :"); RegCerbo= modbusTCPClient.holdingRegisterRead(224,784); //id, adr Serial.println(RegCerbo/10); long RegCerboU=0; RegCerboU= modbusTCPClient.holdingRegisterRead(224,776); Serial.print("U="+String (RegCerboU/100)); long RegCerboI=0; RegCerboI= modbusTCPClient.holdingRegisterRead(224,777); Serial.print(" I="+String (RegCerboI/10)); Serial.println(" P="+String (RegCerboU * RegCerboI /1000)); Serial.println("----"); // wait for 10 second delay(10000); } }