Board index FlightGear Support Interfacing

Arduino and Ethernet - flightgear generic protocol interface

Connecting two computers, using generic protocol, connecting with Matlab?

Arduino and Ethernet - flightgear generic protocol interface

Postby ScottBouch » Tue Mar 12, 2019 1:00 pm

Hi all,

I'd like to learn how to send property tree data over Ethernet to multiple Arduino boards using the generic protocol. And also send from multiple boards via Ethernet to the Flightgear property tree using generic protocol.

I've in the past figured out how to send serial data to Flightgear over USB utilising the generic protocol, but now want to use Ethernet so I can use multiple boards on a network, and direct the data by IP address. This will help to build a more robust system as the addressing is fixed; using serial USB with multiple boards, you can end up with different TTY ID allocations when you connect them up.

(See previous project: http://www.scottbouch.com/flightgear-sim-arduino-serial-hardware-2-axis-potentiometer-joystick.html)

The end application is taking analogue potentiometer values (throttles, stick x/y, rudder, wheel brakes, etc..), switch positions, push-buttons etc, and sending to Flightgear. Also reading data from Flightgear and using to illuminate indicator lamps, operate cockpit analogue indicators, etc...

I have searched for guidance and examples utilising Ethernet TCP / UDP for this, but am struggling to find anything. If anyone has any pointers, please can you let me know.

Many thanks, Scott.
User avatar
ScottBouch
 
Posts: 73
Joined: Wed Jun 22, 2016 3:14 pm
Location: Midlands, UK
OS: Manjaro,Mint,Ubuntu.

Re: Arduino and Ethernet - flightgear generic protocol inter

Postby ScottBouch » Wed Mar 13, 2019 6:37 am

Sorry, I think this belongs more in the "interfacing" forum area. But I can't move it over.

Cheers, Scott
User avatar
ScottBouch
 
Posts: 73
Joined: Wed Jun 22, 2016 3:14 pm
Location: Midlands, UK
OS: Manjaro,Mint,Ubuntu.


Re: Arduino and Ethernet - flightgear generic protocol inter

Postby ScottBouch » Mon Mar 18, 2019 8:54 pm

Hi,

So I've just started playing with hardware.

I'm having a crack with one of the Arduino Ethernet examples, but tried pointing it to the HTTP port of FGFS, in the hope I could pick up a property of the property tree and send it out over serial back to my PC..

Needless to say it didn't work first time as I'm taking the pot-luck approach as I haven't quite yet found documentation to explain how to do this.

The FGFS PC is on 192.168.0.1 and the Arduino is on 192.168.0.177. I attempted to grab /controls/flight/flaps. I'm just getting over serial "connection failed".

Do I need to set up a generic protocol xml file for the HTTP GET function to read a property, or can it directly access the property tree as I have tried?

Test Arduino code:
Code: Select all
/*
  Repeating Web client

 This sketch connects to a a web server and makes a request
 using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
 the Adafruit Ethernet shield, either one will work, as long as it's got
 a Wiznet Ethernet module on board.

 This example uses DNS, by assigning the Ethernet client with a MAC address,
 IP address, and DNS address.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created 19 Apr 2012
 by Tom Igoe
 modified 21 Jan 2014
 by Federico Vanzati

 http://www.arduino.cc/en/Tutorial/WebClientRepeating
 This code is in the public domain.

 */

#include <SPI.h>
#include <Ethernet.h>

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

// initialize the library instance:
EthernetClient client;

char server[] = "192.168.0.1";  // also change the Host line in httpRequest()
//IPAddress server(64,131,82,241);

unsigned long lastConnectionTime = 0;           // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10*1000;  // delay between updates, in milliseconds

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // start serial port:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    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.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
    Serial.print("My IP address: ");
    Serial.println(Ethernet.localIP());
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
}

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 5480)) {
    Serial.println("connecting...");
    // send the HTTP GET request:
    client.println("GET /controls/flight/flaps");
    client.println("Host: 192.168.0.1");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}


This example code was from the link below, but I modified it to use the FGFS PC's IP address instead of arduino.cc, and attempted to point the GET request at a property instead of their example text file:
https://www.arduino.cc/en/Tutorial/WebClientRepeating

I've had a stab at making a simple generic protocol .xml file and pointing the Arduino http GET function at it, but still, connection failed.

Code: Select all
[code]if (client.connect(server, 5480)) {
    Serial.println("connecting...");
    // send the HTTP GET request:
    client.println("GET /ethernet.xml");
    client.println("Host: 192.168.0.1");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();


    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");



It may be partially working, as when I unplug the Ethernet cable form the FGFS PC, the serial monitor, which repeatedly just prints "connection failed" slows down. (I can also Ping the Arduino Ethernet board.)

I must mention this is new to me, if that wasn't obvious already.

Cheers, Scott
User avatar
ScottBouch
 
Posts: 73
Joined: Wed Jun 22, 2016 3:14 pm
Location: Midlands, UK
OS: Manjaro,Mint,Ubuntu.

Re: Arduino and Ethernet - flightgear generic protocol inter

Postby SurferTim » Wed Mar 27, 2019 1:16 am

I'm just getting over serial "connection failed".

Did you allow the 5480 port through your firewall?
I would recommend using the IPAddress data type rather than char in the Arduino. If the dns server doesn't like your request, it can fail.
Fly low, fly slow, land on a dime. Twin Otter.
SurferTim
 
Posts: 344
Joined: Sun Dec 09, 2018 5:49 pm
Location: Miramar Beach, FL
Callsign: Pirate
Version: 2019.2.0
OS: Ubuntu 18.04

Re: Arduino and Ethernet - flightgear generic protocol inter

Postby ScottBouch » Thu Mar 28, 2019 9:50 am

Great thought on the firewall - thank you! I have no rules set up, so will try that next!
User avatar
ScottBouch
 
Posts: 73
Joined: Wed Jun 22, 2016 3:14 pm
Location: Midlands, UK
OS: Manjaro,Mint,Ubuntu.


Return to Interfacing

Who is online

Users browsing this forum: No registered users and 1 guest