1

I'm wondering if there's anything out there that maybe works pretty similarly to Firmata, runs on Unos or Megas, and can be controlled over Ethernet. I am writing a home automation system with .NET, and I see some options for supporting Firmata with .NET, but I want to be able to connect my devices over Ethernet.

1

I don't use .net but I have done this using php website to control a door lock over ethernet.

My solution was to use the ethernet to recieve commands and be able to use them on the arduino through http calls. And when it was done send a http call back to the server with some data. I was doing a door lock program for example. Please ignore anything talking about doors, locking, unlocking, or other requirements to certain calls, I am just providing examples about how it COULD be used, not that you need to include my door lock code in your project to get it to work.

As far as how that works out with .net that's up to you, however I can give you a hand getting your messages back and forth.

first you want to create the ethernet connection to your router. put this in your header section.

#include <UIPEthernet.h>
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(127, 0, 0, 1);

// initialize the library instance:
EthernetClient client;

char server[] = "www.example.com";
//IPAddress server(127,0,0,1);  // numeric IP for www.example.com (no DNS)

int waitTime = 3L;

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds
const unsigned long postingInterval = waitTime * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers

throw this in your main loop so it doesn't constantly make http requests spamming a server.

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

Create a method to handle httpRequest because shoving everything under the main is kinda sloppy.

Alright so there will be quite a bit here because http headers are kinda lengthy.

// 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, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
  Serial.println("String is " + readString);
  if(readString == "Ready" || readString == ""){  
    client.println("GET /command.txt HTTP/1.1");
    where = "web";
    waitTime = 5L;
  }
  else{
    // here is the basic format of sending a get command
    //this specific request is so that I can tell where the command came from. I had a web client / mobile app that could control the door AND you could also control it from a physical buttom. The door would have to reply either way to the web server (READ: .net application) in order to have the correct status always show up on the web end.

    client.print("GET /update.php?where=Arduino&command=Ready&status=");
    if(readString == "Reset"){
      client.print("Reset");
    }
    else if(isLocked){
      client.print("Locked");
    }
    else{
      client.print("Unlocked");
    }
    client.println(" HTTP/1.1");
    waitTime = 1L;
  }
  client.println("Host: www.example.com");
  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");
  }
}

TL;DR if you wanna use ethernet you need to use all of ethernet. Start by creating your connection to the router statically (watch for conflicts).

Then create a method of watching for incoming data or waiting for a command if there isn't one. (server call).

Then create A FULL HTTP REQUEST in order to pass your request through. Shorting any parts of this last part and you will either end up with bad request errors or will simply fail.

Let me know if you need anything else or if this was useful to you!

| improve this answer | |
  • Having to run a web server might be a bit of an uncomfortable requirement for how I'm coding this application. It's a VB .NET console application effectively (currently it has a form, but I'm going to get rid of it), and since I'm going to control various devices, including a few direct serial, and a few various Ethernet devices likely, I don't want to have it serve web just to control Arduinos. – ocdtrekkie Dec 2 '15 at 6:28
  • I was hoping for something a bit lighter on the server. I almost recall some examples where the actual "web server" bit was on the Arduino, which might be a more elegant solution, in terms of my automation controller just needing to make HTTP requests that way. I had just been hoping there might be something a bit more pre-packaged and already out there I didn't know about. – ocdtrekkie Dec 2 '15 at 6:29
  • You don't have to. You asked about how to use ethernet to send data. You can pass php to a single local page on a local computer. Have that write the data to a text file and have your vba program pick that up. How did you think ethernet worked? – Hyra Power Dec 2 '15 at 6:34
  • Also using arduino as a web server is very Ill advised – Hyra Power Dec 2 '15 at 6:35
  • But doesn't that single local page need some sort of web server on the PC to serve it up so it's reachable by the Arduino? What's the reason for ill-advisement on using the Arduino as a web server for similarly minimal commands? – ocdtrekkie Dec 2 '15 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.