Arduino Server Example

comment 0
Input, Python

Intro

This post explains a simple Arduino – Python communication over TCP/IP, which can be used in Grasshopper to send and receive values within Grasshopper, eventually to control digital outs of the board and to control a stepper motor. You can find a gh example file under:

The according Arduino file to upload onto your borad you can find under:

In order to be able to compile this Arduino script and upload it to the board, you need two libraries: Ethernet2 and AccelStepper. You can either download them from the internet or use the provided zip files in the folder:

For those, who don’t know yet, libraries are installed as follows:

arduino_install_lib

Hardware

The hardware used for this example are an Arduino Genuino 101, the Ethernet 2 shield, a stepper motor, and a Sparkfun driver board:

Arduino Genuino 101, ACT stepper motor, Sparkfun stepper motor driver

Arduino Genuino 101 with an Ethernet 2 shield, ACT stepper motor, Sparkfun stepper motor driver

Running the example files:

  1. Make sure you have set a static IP address, in this case starting with 192.168.10.xx (the Arduino Server IP is set to: 192.168.10.177)
  2. Connect your Arduino with the USB cable and the Ethernet 2 shield with an Ethernet cable to your computer. Power your your Arduino additionally with an external power supply (5-12V).
  3. Open the Arduino IDE (with update for Genuino 101), open the example file and upload the file to your board. Make sure you have set the correct Arduino Board and the right port:arduino_choose_port
  4. If the upload was successful (ignore the warnings), you have to open the serial port monitor to see the messages from the board:
    arduino_serial_port
    You should see the following message in the serial monitor: server address: 192.168.10.177
  5. Now open the gh example file and connect to the Arduino:
    gh-surface
    In order to know if the connection was successful, both the log messages ARDUINO CONNECTION STATE and ARDUINO CONNECTION SUCCESS must be TRUE. Also in the Arduino Serial Monitor on your PC you should see the following lines:

    This shows the response of the first string message request from the gh server. This first message is sent from gh by default in order to test if the connection was successful.
  6. This example file has 3 method types implemented, 1) to get a predefined string as an answer message from Arduino (= “get info msg”), 2) to set a DO on or off (“set DO on / set DO off”), and 3) to send a motor position to the connected stepper motor. You can test all three of them, if you have nothing connected to the Arduino outputs, you can still monitor the outputs in the Serial monitor.
  7. Watch out (just for Arduino Genuino): The example files do not work if you use DO 11-13 in your code as input or output pins, they are somehow used by the Ethernet Board 2.

Concept

The Arduino acts as a server, to which the gh client can connect to, communicate with and disconnect if needed. While the connection is established, the open socket on the Arduino board waits for incoming messages. If a message is received successfully, different callbacks can be executed according to the type of message. Once the callback method is completed, the server sends back the message, that the task is completed.

The transmission protocol for messages is:

The message types of this example are:

but they can be extended arbitrarily, according to your program.

Links

https://www.arduino.cc/
http://www.airspayce.com/mikem/arduino/AccelStepper/
https://github.com/adafruit/Ethernet2
http://www.arduino.org/learning/reference/ethernet-two-library

 

Leave a Reply