MKR1000 and Modbus TCP slave

Hi,

Does anyone have a working example of a MKR1000 (or a arduino with the WiF101 shield) working as a slave with Modbus TCP.

Hi, see these links

https://github.com/JhonControl/ESP8266_ModbusTCP_Control
https://github.com/JhonControl/Arduino_ESP8266_Modbus_Industrial_Applications/tree/master/Arduino_ESP8266_%20Modbus_Industrial_Applications/3.%20ESP8266BridgeMBSlaveTCP-RTUhttps://github.com/andresarmento/modbus-esp8266https://github.com/JhonControl/Arduino_ESP8266_Modbus_Industrial_Applications/tree/master/Arduino_ESP8266_%20Modbus_Industrial_Applications/2.%20ESP8266%20Slave%20Modbus%20TCPhttps://github.com/bonelessc/MudbusWiFihttps://github.com/yaacov/ArduinoModbusSlaveTCP

Hi LamySae,

That was very generous of you to research and provide all those links. Thank you.

I tried to copy and modify the MudbusWiFi example, as I have used Mudbus with the Ethernet shield in the past and like it.

I substituted the #include <WiFi.h> with #include <WiFi101.h> and a few other related changes, but got lots of compile errors.
eg.

In file included from /Users/anthonywatts/Documents/Arduino/libraries/WiFi101/src/WiFi101.h:35:0,
               from /Users/anthonywatts/Dropbox/arduino/Modbus/test2/test2.ino:4:
/Users/anthonywatts/Documents/Arduino/libraries/WiFi101/src/WiFiClient.h:31:7: error: redefinition of 'class WiFiClient'
class WiFiClient : public Client {
     ^
In file included from /Users/anthonywatts/Documents/Arduino/libraries/WiFi/src/WiFi.h:31:0,
               from /Users/anthonywatts/Documents/Arduino/libraries/MudbusWiFi/MudbusWiFi.h:25,
               from /Users/anthonywatts/Dropbox/arduino/Modbus/test2/test2.ino:2:
/Users/anthonywatts/Documents/Arduino/libraries/WiFi/src/WiFiClient.h:27:7: error: previous definition of 'class WiFiClient'
class WiFiClient : public Client {
     ^

Here is a copy of the code I tried to compile

#include <SPI.h>
#include <MudbusWiFi.h>
#include <avr/pgmspace.h>
#include <WiFi101.h>
#include <WiFiUdp.h>
int status = WL_IDLE_STATUS;

MudbusWiFi Mb;
//Lamps
int Out2 = 2;
int Out3 = 3;
int Out4 = 4;

void setup()
{
Serial.begin(9600);

char ssid[] = "xxxx";    // your network SSID (name)
char pass[] = "yyyy";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

delay(200); 
Serial.println(". ");
Serial.println("------------------------------------------------------------------"); 
Serial.println(" WiFi Modbus control  -test2");
Serial.println("------------------------------------------------------------------"); 
Serial.println(" ");

// Assign a fixed IP -------------------------------------------
IPAddress ip(192,168,54,18);
// Load the fixed IP address -----------------------------------
WiFi.config(ip);

// Attempt to connect to WiFi network:--------------------------
while ( status != WL_CONNECTED) 
{
  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  status = WiFi.begin(ssid, pass);

  // wait 10 seconds for connection:
  delay(10000);
}
Serial.println("Connected to wifi");
printWiFiStatus();



pinMode(Out2, OUTPUT);               // assign Pin 2 as Output
pinMode(Out3, OUTPUT);               // assign Pin 3 as Output
pinMode(Out4, OUTPUT);               // assign Pin 4 as Output

}

void loop()
{
  Mb.Run(); // start MbTcp subroutine
  delay(1000);
  // Digital OUT 2 (as  Modbus Coil 1)
if   (Mb.C[0] == HIGH) {
  digitalWrite(Out2, HIGH);
}
else {
  digitalWrite(Out2, LOW);
}
// Digital OUT 3 (as  Modbus Coil 2)
if   (Mb.C[1] == HIGH) {
  digitalWrite(Out3, HIGH);
}
else {
  digitalWrite(Out3, LOW);
}
// Digital OUT 4 (as  Modbus Coil 3)
if   (Mb.C[2] == HIGH) {
  digitalWrite(Out4, HIGH);
}
else {
  digitalWrite(Out4, LOW);
}
}
//
//void printWifiData() {
//  // print your WiFi shield's IP address:
//  IPAddress ip = WiFi.localIP();
//  Serial.print("IP Address: ");
//  Serial.println(ip);
//
//  // print your MAC address:
//  byte mac[6];
//  WiFi.macAddress(mac);
//  Serial.print("MAC address: ");
//  Serial.print(mac[5], HEX);
//  Serial.print(":");
//  Serial.print(mac[4], HEX);
//  Serial.print(":");
//  Serial.print(mac[3], HEX);
//  Serial.print(":");
//  Serial.print(mac[2], HEX);
//  Serial.print(":");
//  Serial.print(mac[1], HEX);
//  Serial.print(":");
//  Serial.println(mac[0], HEX);
//
//}
//
//void printCurrentNet() {
//  // print the SSID of the network you're attached to:
//  Serial.print("SSID: ");
//  Serial.println(WiFi.SSID());
//
//  // print the MAC address of the router you're attached to:
//  byte bssid[6];
//  WiFi.BSSID(bssid);
//  Serial.print("BSSID: ");
//  Serial.print(bssid[5], HEX);
//  Serial.print(":");
//  Serial.print(bssid[4], HEX);
//  Serial.print(":");
//  Serial.print(bssid[3], HEX);
//  Serial.print(":");
//  Serial.print(bssid[2], HEX);
//  Serial.print(":");
//  Serial.print(bssid[1], HEX);
//  Serial.print(":");
//  Serial.println(bssid[0], HEX);
//
//  // print the received signal strength:
//  long rssi = WiFi.RSSI();
//  Serial.print("signal strength (RSSI):");
//  Serial.println(rssi);
//
//  // print the encryption type:
//  byte encryption = WiFi.encryptionType();
//  Serial.print("Encryption Type:");
//  Serial.println(encryption, HEX);
//  Serial.println();
//}

//----------------------------------------------------------------------------------------------------
void printWiFiStatus() 
{
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------