0

MODBUS TCP via WiFi Shield
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: MODBUS TCP via WiFi Shield

by ibbba on Tue Aug 25, 2015 5:37 pm

Hello,I just finished a working Modbus TCP library that works with standard Arduino Ethernet shield,Sparkfun Thing (Esp8266 based board) and should work with Adafruit CC3000 Wifi shield.
I don't have the Adafruit shield,so if somebody can give it a try and report will be very useful.
The used board must me defined in the supplied header file.

ModbusTCPSlave.zip
(12.28 KiB) Downloaded 639 times

ibbba
 
Posts: 4
Joined: Tue Aug 25, 2015 5:31 pm

Re: MODBUS TCP via WiFi Shield

by frank_dunn1 on Thu Aug 27, 2015 6:04 am

hi ibbba

I tested your library on a olimex esp8266 board , it seems to work fine. With a 200msec poll delay it runs, with a 100msec delay i get reboots .
The olimex board have gpio5 connected to a relay , how can I change this without changing the header file ?

frank_dunn1
 
Posts: 7
Joined: Fri Apr 24, 2015 4:26 pm

Re: MODBUS TCP via WiFi Shield

by ibbba on Thu Aug 27, 2015 2:05 pm

I personally use to poll not less than one second,so I take your test like a speed test... Ok good.... Regarding The output led that I use to monitor can be ridefined in the header file,or you can delete every call to digitalWrite in The cpp file... Up to you... I hope somebody can test it with The Adafruit shield

ibbba
 
Posts: 4
Joined: Tue Aug 25, 2015 5:31 pm

Re: MODBUS TCP via WiFi Shield

by frank_dunn1 on Sat Aug 29, 2015 5:29 am

Hi ibbba

I notice when I test your library with modbusmaster that stays connected , the code in the main loop never executes until I disconnect the modbusmaster .

frank_dunn1
 
Posts: 7
Joined: Fri Apr 24, 2015 4:26 pm

Re: MODBUS TCP via WiFi Shield

by ibbba on Tue Sep 01, 2015 12:39 pm

frank_dunn1 wrote:Hi ibbba

I notice when I test your library with modbusmaster that stays connected , the code in the main loop never executes until I disconnect the modbusmaster .


Yes this is an issue due to the Wifi library...if you check with the standard ethernet shield you will notice that the loop is excuted normally...to avoid this you have to close the connection after each request...I use a Rasperry PI to poll with minimalmodbus python library and closing the connection after each request do the job.

ibbba
 
Posts: 4
Joined: Tue Aug 25, 2015 5:31 pm

Re: MODBUS TCP via WiFi Shield

by frank_dunn1 on Tue Sep 01, 2015 2:13 pm

hi ibbba

I tested the code using the Ticker library to call functions in the main loop and this works fine while the client stays connected .

frank_dunn1
 
Posts: 7
Joined: Fri Apr 24, 2015 4:26 pm

Re: MODBUS TCP via WiFi Shield

by rody2571 on Tue Sep 01, 2015 3:35 pm

Hi abba,
I tested your library with my ESP8266 but when I'm compiling the program I get some fails.

Arduino:1.6.5 (Windows 8.1), Placa:"Generic ESP8266 Module, Serial, 80 MHz, 40MHz, DIO, 115200, 4M (3M SPIFFS)"

In file included from C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:29:0:
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.h:25:0: warning: "UDP_TX_PACKET_MAX_SIZE" redefined [enabled by default]
#define UDP_TX_PACKET_MAX_SIZE 24
^
In file included from C:\Program Files (x86)\Arduino\libraries\WiFi\src\utility/wifi_drv.h:26:0,
from C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:26:
C:\Users\SMART-PC\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\ESP8266WiFi\src/WiFiUdp.h:28:0: note: this is the location of the previous definition
#define UDP_TX_PACKET_MAX_SIZE 8192
^
In file included from C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:29:0:
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.h:27:7: error: redefinition of 'class WiFiUDP'
class WiFiUDP : public UDP {
^
In file included from C:\Program Files (x86)\Arduino\libraries\WiFi\src\utility/wifi_drv.h:26:0,
from C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:26:
C:\Users\SMART-PC\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.5-947-g39819f0\libraries\ESP8266WiFi\src/WiFiUdp.h:32:7: error: previous definition of 'class WiFiUDP'
class WiFiUDP : public UDP, public SList<WiFiUDP> {
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In constructor 'WiFiUDP::WiFiUDP()':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:35:22: error: class 'WiFiUDP' does not have any field named '_sock'
WiFiUDP::WiFiUDP() : _sock(NO_SOCKET_AVAIL) {}
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual uint8_t WiFiUDP::begin(uint16_t)':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:45:9: error: '_sock' was not declared in this scope
_sock = sock;
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:46:9: error: '_port' was not declared in this scope
_port = port;
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::available()':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:56:7: error: '_sock' was not declared in this scope
if (_sock != NO_SOCKET_AVAIL)
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual void WiFiUDP::stop()':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:66:8: error: '_sock' was not declared in this scope
if (_sock == NO_SOCKET_AVAIL)
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:69:26: error: '_sock' was not declared in this scope
ServerDrv::stopClient(_sock);
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::beginPacket(IPAddress, uint16_t)':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:88:7: error: '_sock' was not declared in this scope
if (_sock == NO_SOCKET_AVAIL)
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:90:7: error: '_sock' was not declared in this scope
if (_sock != NO_SOCKET_AVAIL)
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::endPacket()':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:101:32: error: '_sock' was not declared in this scope
return ServerDrv::sendUdpData(_sock);
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual size_t WiFiUDP::write(const uint8_t*, size_t)':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:111:27: error: '_sock' was not declared in this scope
ServerDrv::insertDataBuf(_sock, buffer, size);
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::read()':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:125:23: error: '_sock' was not declared in this scope
ServerDrv::getData(_sock, &b);
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::read(unsigned char*, size_t)':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:137:31: error: '_sock' was not declared in this scope
if (!ServerDrv::getDataBuf(_sock, buffer, &size))
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual int WiFiUDP::peek()':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:152:22: error: '_sock' was not declared in this scope
ServerDrv::getData(_sock, &b, 1);
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual IPAddress WiFiUDP::remoteIP()':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:167:25: error: '_sock' was not declared in this scope
WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
^
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp: In member function 'virtual uint16_t WiFiUDP::remotePort()':
C:\Program Files (x86)\Arduino\libraries\WiFi\src\WiFiUdp.cpp:177:25: error: '_sock' was not declared in this scope
WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
^
Error de compilación

This report would have more information with
"Show verbose output during compilation"
activala desde Archivo > Preferencias

rody2571
 
Posts: 1
Joined: Tue Sep 01, 2015 3:24 pm

Re: MODBUS TCP via WiFi Shield

by frank_dunn1 on Tue Sep 01, 2015 3:57 pm

hi rody2571

i use windows 7 and installed the library from library manager > add zip library . In File > Preferences : Additional Board Manager URLs: http://arduino.esp8266.com/stable/packa ... index.json ,do you have the same ? I have no compile errors and it works ,

frank_dunn1
 
Posts: 7
Joined: Fri Apr 24, 2015 4:26 pm

Re: MODBUS TCP via WiFi Shield

by ibbba on Wed Sep 02, 2015 11:30 am

frank_dunn1 wrote:hi ibbba

I tested the code using the Ticker library to call functions in the main loop and this works fine while the client stays connected .


Maybe because the Ticker library use the Watchdog timer to tick the calls,so it generates an interrupt that "exits" from the main loop temporally ....good to know

ibbba
 
Posts: 4
Joined: Tue Aug 25, 2015 5:31 pm

Re: MODBUS TCP via WiFi Shield

by Jinn on Sun Sep 27, 2015 12:11 pm

Hi,

I tried to compile the ModbusESP8266Slave.ino and i get this error messages:


Arduino: 1.6.5 (Windows 8.1), Board: "Arduino/Genuino Uno"

Using library ESP8266WiFi in folder: C:\Users\joach_000\Documents\Arduino\libraries\ESP8266WiFi

Using library ModbusTCPSlave in folder: C:\Users\joach_000\Documents\Arduino\libraries\ModbusTCPSlave (legacy)



WARNING: library ESP8266WiFi claims to run on [esp8266] architecture(s) and may be incompatible with your current board which runs on [avr] architecture(s).



C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10605 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard -IC:\Users\joach_000\Documents\Arduino\libraries\ESP8266WiFi\src -IC:\Users\joach_000\Documents\Arduino\libraries\ModbusTCPSlave C:\Users\JOACH_~1\AppData\Local\Temp\build2601187019232498030.tmp\ModbusESP8266Slave.cpp -o C:\Users\JOACH_~1\AppData\Local\Temp\build2601187019232498030.tmp\ModbusESP8266Slave.cpp.o

In file included from C:\Users\joach_000\Documents\Arduino\libraries\ESP8266WiFi\src/ESP8266WiFi.h:32:0,
from ModbusESP8266Slave.ino:1:
C:\Users\joach_000\Documents\Arduino\libraries\ESP8266WiFi\src/WiFiClient.h:28:18: fatal error: memory: No such file or directory
#include <memory>
^
compilation terminated.
Error compiling.

Can somebody help me please?

Jinn

Jinn
 
Posts: 1
Joined: Sun Sep 27, 2015 12:05 pm

Re: MODBUS TCP via WiFi Shield

by adafruit_support_mike on Mon Sep 28, 2015 12:48 am

Where did you get the ESP8266WiFi library?

adafruit_support_mike
 
Posts: 45817
Joined: Thu Feb 11, 2010 2:51 pm

Re: MODBUS TCP via WiFi Shield

by blue_lightning on Fri Oct 16, 2015 2:54 am

Hi frank_dunn1!
I have got the ESP8266 up and running with the “ModbusTCPSlave.zip” library. But I also need to reconnect the TCP Modbus master simulator (http://en.radzio.dxp.pl/modbus-master-simulator/) to get an updated value.

You used the Ticker library to call functions in the main loop, could you provide me with an example sketch with the ModbusTCPSlave and the Ticker library??

By The Way, the TCP-modbus standard port is 502 but the ModbusTCPSlave library is set to 1502!

blue_lightning
 
Posts: 1
Joined: Fri Oct 16, 2015 2:36 am

Re: MODBUS TCP via WiFi Shield

by ductsoup on Wed Jan 27, 2016 8:31 pm

I think I'm seeing the same problem as blue_lightning and the others on a Feather HUZZAH with ESP8266 WiFi and the ESP8266WiFi library that came with version 1.6.5 of the Arduino IDE. I had no luck with the modified example posted so I started merging the changes back into the GitHub source. Pretty much need to see if I can reproduce the Ticker library work around for ESP8266 at this point.

Code: Select all | TOGGLE FULL SIZE
>modpoll -m tcp -t  4:float -r 40001 192.168.1.36

modpoll - FieldTalk(tm) Modbus(R) Polling Utility
Copyright (c) 2002-2006 FOCUS Software Engineering Pty Ltd
Visit http://www.modbusdriver.com for Modbus libraries and tools.

Protocol configuration: MODBUS/TCP
Slave configuration...: Address/Id = 1, start reference = 40001, count = 1
Communication.........: 192.168.1.36, port 502
Data type.............: 32-bit float, output (holding) register table

Polling slave (Ctrl-C to stop) ...
[40001]: 3.141593
Polling slave (Ctrl-C to stop) ...
TCP/IP connection was closed by remote peer!
>

ductsoup
 
Posts: 164
Joined: Sun Oct 12, 2014 7:26 am

Re: MODBUS TCP via WiFi Shield

by ductsoup on Thu Jan 28, 2016 7:43 pm

Here's one solution to the connection closed problem.

Apparently the ESP8266 WiFi library treats client connections a little different than the Adafruit CC3000 and Arduino Ethernet libraries. The client instance has to be moved into the MODBUS server class so it persists and doesn't automatically terminate the connection. The side effect of doing that is the slave can only respond to one master at a time. In practice, that's normally the case so it's not a major limitation.

It's a solution but not a great one so I'll share it here and ask for comments or pulls to https://github.com/ductsoup/ModbusTCP.

So far it looks a little ugly but seems quite solid.
Attachments
testMODBUS.7z
(5.18 KiB) Downloaded 344 times

ductsoup
 
Posts: 164
Joined: Sun Oct 12, 2014 7:26 am

Please be positive and constructive with your questions and comments.