How to program Arduino based PLC through Ethernet

How to program Arduino based PLC through Ethernet

This post it shows how to install Ariadne bootloader that allows to programming Arduino based PLC’s of Industrial Shields through Ethernet TFTP Client. Then it is also showed an example of how to program it.

The requirements for this example are:

M-Dunio needs the new Ariadne bootloader to be programmed through Ethernet. How to upload the new bootloader? 

First we need to download the new bootloader from this link. After that we have to replac for the original M-Duino bootloader (without losing it). IMPORTANT: don’t delate o replace the original bootloader file. We give a new name to the original M-DUINO bootloader file like mduino-original.hex and save the new bootloader file to this directory with the name of mduino.hex. 

On Windows this directory can be on:

C:\Users\User\AppData\Local\Arduino15\packages\industrialshields\hardware\avr\1.0.3-rc1\bootloaders

On Linux this directory can be on:

$HOME/.arduino15/packages/industrialshields/hardware/avr/1.0.3-rc1/bootloaders

After place the new mduino.hex bootloader on this directory we can proceed to upload the bootloader. Follow this older post to upload the bootloader.

The default built-in network settings of the bootloader are listed below.

  • IP Address: 192.168.1.128

  • Subnet Mask: 255.255.255.0

  • Gateway: 192.168.1.1

  • MAC Address: 0xDE.0xAD.0xBE.0xEF.0xFE.0xED

  • TFTP Negotiation Port: 69

  • TFTP Data Port: 46969 

 

The main reason to upload the Ariadne bootloader is to program your Arduino based PLC through Ethernet connection TFTP Flashing, next it is explained how to do it:

Listening Server

IMPORTANT: this bootloader is made to program the Arduino board while it is doing a reset. Of course we want to program the Arduino based PLC kilometers ago and we don’t want a worker pending on click the reset button. So, we have to implement a server that allows as to make a reset to the equipment while we are sending the binary file.

To do not need a manual reset, we have to implement a server that will be listening on 2000 port. After get some data through this port the Arduino based PLC will make a reset as we expect.

So, on your original sketch you have to implement a code like that to be able to make the reset through you are sending the binary sketch:

#include <SPI> 
#include <Ethernet>
#include <avr/wdt.h>


EthernetServer serverReset = EthernetServer(2000);

byte Mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte IP[] = {192, 168, 1, 128};
byte Subnet[] = {255, 255, 255, 0};
byte Gateway[] = {192, 168, 1, 1};

void setup() {
  wdt_disable();
  serverReset.begin();
}

////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  EthernetClient clientReset = serverReset.available();
  if (clientReset) { 
    wdt_enable(WDTO_1S); // enable the watchdog to make a reset in 1S
    while(true); Wait the watchdog reset
  }

 

Converting your sketch to the right format:

Unlike serial flashing that uses HEX files to flash the Arduino, the TFTP server implemented on the bootloader works with binary files. This means that you have to manually convert your sketch. Likely Arduino have a tool for it. To use this first build your sketch inside Arduino IDE using verify button. After that you have to navigate to the directory HEX file. You can see this directory on the last line on temporally build folder checking verbose output during compilation in Arduino IDE’s preference dialog.

On Windows:         

This directory can be on:

C:\DocumentsandSettings\Administrator\Local Settings\Temp\build3823826.tmp\Blink.ino.hex

After located the file open the Command prompt. And insert:

"C:\Program Files\Arduino\hardware\tools\avr\bin\avr-objcopy.exe" -I ihex [sketchname].ino.hex -O binary [sketchname].ino.bin

With this command converts the hex file to binary file.

On Linux or OS X:            

Go to the directory where your sketch hex file is located, this can be:

/tmp/arduino_build_917371/Blink.ino.hex

On MacOS you’ll need to go to Arduino’s preferences, and check the “Show verbose output during compilation” checkbox. After that, when you compile, you will see the path for the compiled .hex file in the last line of the compilation output. There you will find a folder named something like build2429295348207572157.tmp. That is where the Arduino temporary build files reside. Enter the directory and make sure that there is a .elf or a .hex file with the same name as your sketch. This is the file that you need to convert. To convert this file you have to run one of the following commands in a terminal.

avr-objcopy -I ihex [sketch].hex -O binary [sketch].bin

 

TFTP client to upload the sketch to Arduino based PLC

How we have to send the binary file? 

On Windows:         

First to send the binary file we have to send something through the port 2000. Using the Command Prompt or a useful tool as Puttin, we open a telnet conection and we send something to the M-DUINO:

Connection: telnet

IP: 192.168.1.128

Port: 2000

Message: one character

IMPORTANT: If we do not implement the previous commented code, we must do a manual reset of the equipment to upload the new sketch to the M-DUINO based PLC. 

Using Command Prompt we type

telnet

After that the command prompt open the telnet options, the we have to connect to the server and type

open 192.168.1.128 open

Then you will see that M-DUINO is making a reset. Automatically after this step you have to send the binary file. REMEMBER: you have few seconds between to open the telnet connection and to send the binary file. Actually we have to do it before the bootloader finish and start the main program that is on M-DUINO.

 

Also using Command Prompt we can send to our Arduino based PLC using a tftp client that we may have on our computer:

 
tftp [-i] [<Host>] [{get | put}] <Source> [<Destination>]
 

Parameter

Description

-i

Specifies binary image transfer mode (also called octet mode). In binary image mode, the file is transferred in one-byte units. Use this mode when transferring binary files. If -i is omitted, the file is transferred in ASCII mode. This is the default transfer mode. This mode converts the end-of-line (EOL) characters to an appropriate format for the specified computer. Use this mode when transferring text files. If a file transfer is successful, the data transfer rate is displayed.

<Host>

Specifies the local or remote computer.

put

Transfers the file Source on the local computer to the file Destination on the remote computer. Because the TFTP protocol does not support user authentication, the user must be logged onto the remote computer, and the files must be writable on the remote computer.

get

Transfers the file Destination on the remote computer to the file Source on the local computer.

<Source>

Specifies the file to transfer.

<Destination>

Specifies where to transfer the file.

 

So, our command will be like this:

tftp [-i] 192.168.1.128 PUT [sketch].ino.bin

Now our M-DUINO is running the new sketch. IMPORTANT: remember to implement in every sketch the Listening Server if not you will need a manual reset. 

 

On Linux or OS X:           

We should do the same on Linux or OS X. Using the terminal we type.

telnet 192.168.1.128 2000

a

After that we have to go back using Ctrl + [ + Enter and type a

q

Doing this you will see that the M-DUINO is making a reset. Then you have to send the binary file  

On some Linux before you use tftp, we should load the ip_conntrack_tftp module or the tftp client won’t to acknowledge(ACK) the incoming packets. That is needed because TFTP is insecure and it is not enabled by default. To enable it just open a terminal and run:

modprobe ip_conntrack_tftp

As root using su or sudo.

After that open a terminal as a regular user and type

tftp [ip] [port]

For the default Ariadne Industrial Shields bootloader

tftp 192.168.1.128 69

Actually the default port of tftp is 69, it is not necessary to type it if you don’t use another port. 

Now you should have been greeted by the

tftp>

First you have to enter this command

tftp> mode octet

This way you tell the TFTP client to send binary data. This is absolutely necessary as if your client is in netascii mode, uploading will fail. After this  is better to implement the next following commands to get more information during the process. But they are not needed.

tftp> trace

tftp> verbose

Now we just to type the next command.

tftp> put [sketch].bin

Now our M-DUINO is running the new sketch. IMPORTANT: remember to implement in every sketch the Listening Server if not you will need a manual reset. 

 

See also:

HOW TO READ AN ENCODER WITH ARDUINO BASED PLC’SHOW TO TOUCH UP DATA FROM THE EEPROMHOW TO USE STEPPER MOTOR WITH ARDBOX ANALOGHOW TO CONNECT AND CONFIGURE A LOAD CELL USING HX711 TRANSMITTER WITH OUR EQUIPMENT’SHOW TO GENERATE A DIFFERENT PWM’S SIGNALS WITH OURS EQUIPMENT’SHOW TO USE THE WATCHDOGRS-485 COMMUNICATION EXAMPLE USING OUR LIBRARIESHOW TO CREATE A WEB SERVER TO READ A SD CARD WITH OUR PLC’SHOW TO CONNECT A SD CARD TO A PLCHOW TO UPLOAD A BOOTLOADER ON A ARDBOX/M-DUINO PLC, HOW TO READ ENCODER SPEED THROUGH AN INTERRUPT WITH AN ARDUINO BASED PLC’S

Comments are closed.