0

As a PIN goes high I need to trigger an event that will send the IP or MAC of the arduino and the PIN state over the wired Ethernet to a local computer. I am using an Ethernet shield to send and receive from the arduino but I am stumped on how to send and process the information at the PC. I will have 42 Arduinos on the network all set to send PIN information over the wired Ethernet. Sounds simple but after a days research I am now looking at other solutions. I currently have the faithful Uno with the network shield as my prototype. For the final install I can purchase any of the arduino range like the Yun,Duo etc. if this would help. Any direction would be welcomed.

  • Both of the comprehensive answers by Majenko and Damiano Verzulli are good, but I would be asking if you want to push or pull the information from the 42 Arduinos. To push it you want to make them HTTP clients and have them contact a central server when the pin changes. To pull the information they need to be HTTP servers which a central client polls from time to time. It might be easier for the Arduinos to establish a telnet connection and send (push) information that way. – Nick Gammon Sep 9 '15 at 21:01
1

The simplest method is to set up a web server on your PC. You haven't mentioned what OS you run (which is kind of critical), so looking into WAMP or LAMP ("Windows/Apache/MySQL/PHP", or "Linux/Apache/MySQL/PHP") is probably your best option.

Once you have a working web server you can pretty much use verbatim the Ethernet examples for connecting to a remote website ("Web Client"). That website would be your local PC address, and you send whatever parameters you want.

You then write a small PHP script (or whatever language you program your chosen web server in) to accept that web request and process it accordingly.

In general you never need to explicitly send the IP address since it is encapsulated as part of the actual socket connection information and is generally made available through various means to your web page (see $_SERVER in PHP for example).

If you want to get more "low level" and lighter weight then you would most likely be looking at writing a UDP server on your computer which receives UDP requests from the Arduinos and processes them as needed. The content of a UDP request is entirely up to you, but programming a server for dealing with it is beyond the scope of an answer here - suffice it to say there are plenty of resources online available for all the major operating systems and programming languages on how to write UDP servers.

1

In my opinion, a perfect solution for the "As a PIN goes high I need to trigger an event" problem is brought by interrupts. In the official documentation I read: "...Arduino has two different kinds of interrupts: external, and pin change..." and the latter seems very suited for your needs. So my idea (but, please, be aware that I'm not at all an Arduino developer... yet) is to simply develop an "interrupt-handler routine", and properly attach such a routine to your PINs. There are plenty of details to analyze (as well as plenty of documentation about Arduino interrupt handling) but... the idea should be clear.

Please, while developing such a routine DON'T be tempted to push all of your networking code inside the routine itself: for reasons deeply and clearly explained by @NickGammon, interrupt handlers should be executed as fast as possible. Here follow an important part of Nick's article:

[...]

Hints for writing ISRs

In brief, keep them short! While an ISR is executing other interrupts cannot be processed. So you could easily miss button presses, or incoming serial communications, if you try to do too much. In particular, you should not try to do debugging "prints" inside an ISR. The time taken to do those is likely to cause more problems than they solve.

A reasonable thing to do is set a single-byte flag, and then test that flag in the main loop function. Or, store an incoming byte from a serial port into a buffer. The inbuilt timer interrupts keep track of elapsed time by firing every time the internal timer overflows, and thus you can work out elapsed time by knowing how many times the timer overflowed.

[...]


As for the "...will send the IP or MAC of the arduino and the PIN state over the wired Ethernet to a local computer...", @Majenko already answered that "source IP" and "source MAC" (of your Arduino) are not needed to be sent explicitely by your application, as if you're using an Ethernet shield (...and, on top of it, the IP protocol stack) it will be the shield itself in dealing with the "encapsulation" process, by which your data (the data "sent" by your own code) will be encapsulated in a proper Ethernet frame (that will include the source MAC) and a containing IPv4 packet (that will include the "source IP").


As for the "...send [the] state over the wired Ethernet to a local computer..." you're missing some very important details, like which operating system your local computer will be running and, more important, which programming language are you using to write your "server-application code".

The LAMP/WAMP approach proposed by @Majenko is relatively easy to implement, as the WebServer (Apache) will bring you a ready-made approach to handle network requests: afterall, you need a "server" application (an application "listening" to a socket, waiting for incoming messages/requests coming from network clients) and a web-server like Apache is exactly such an application.

Anyway, should you need something more specifically suited for your needs, nothing prevent you to write your own "server". There are plenty of libraries for plenty of programming-languages.

As I'm confident with PERL, I suggest you checking one of the many tutorial describing PERL client-server programming. Please note that PERL is a cross-platform language, supported also on Windows systems.

A final note: While implementing your "server-code", please check/consider if you need a "blocking server" (one who can handle only one single request at a time) or a "non-blocking server" (one who can handle multiple requests, coming from multiple arduinos, at the same time).

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.