Advertisement
Guest User

Arduino LAMP/WAMP/MAMP and more

a guest
May 21st, 2015
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.47 KB | None | 0 0
  1. Basically - your Arduino (with an ethernet shield or similar) - should be set up to look like a server to the outside world. It's going to be connected to your router in some manner. Your router (hopefully) will have to be set up in a particular manner:
  2.  
  3. Dynamic DNS will need to be set up on it (see http://dyn.com/home-users/ for details - also review your router's setup information).
  4.  
  5. Basically, your cable modem (or whatever) is assigned an IP address from your ISP; your router then translates this "public" address into a private address using NAT (Network Address Translation - see http://en.wikipedia.org/wiki/Network_address_translation). This also acts as a rudimentary firewall, preventing the outside world from seeing anything but your cable modem and router - but not the network inside the firewall - because those addresses have been deemed as "unroutable" - in order to facilitate the building of private networks in the limited address space of IPV4. There are three blocks of these addresses - 10.x.x.x (pre-CIDR class A), 172.16-31.x.x (class B), and 192.168.x.x (class C) - see http://tools.ietf.org/html/rfc1918 for details.
  6.  
  7. Most home routers are set up to use the 192.168.x.x block, doled out via DHCP usually. What NAT does is translate between the public address (which the router usually gets via DHCP from your ISP), and the internal address assigned to each computer on the network by the router. It does this for only a few ports - generally ports 80 (http), 25 (SMTP) and 110 (POP3) - to allow you to browse the web, and also send/receive email from your computer (the email ports are also generally blocked to allow only one-way so your computer can't use them for spamming or other purposes - but not always). Sometimes there's ports also open for SSH, Telnet, NTP (time), and News servers. Oh - also IRC.
  8.  
  9. In short - there are these addresses, and ports already "open" - to allow the outside world to communicate with your computer and vice-versa.
  10.  
  11. You need to set up your Arduino to communicate on it's own address and port; you also need to set up the router to allow for this communication to the outside world. This is known as "punching a hole" in your firewall. Basically - you are weakening your firewall just a little bit more to allow communications to another device on the inside of the firewall from the outside world.
  12.  
  13. But first, you need to set up Dynamic DNS. Once that is done, if you know your public IP address (easy to get from the router - and the Dynamic DNS setup can facilitate pushing this information to a public server or something so that, should it change - which is can and likely will over time as the DHCP lease renews periodically - it is still "known") - you can set up a port that - when communicating with it - will send that info to that port on your public address. Then - your router will see it, and via the magic of NAT (and setting up the "hole" in your router's firewall configuration) - it can send that data to an internal address/port for the Arduino. Another setting on the router should be to assign from the router's DHCP pool a given known address based on the MAC address for the Arduino's ethernet shield - so the Arduino always gets the same address from the router, and it isn't hopping everywhere (note, this is also sometimes done on the other side by your ISP for your router's DHCP lease - so you always get the same IP from their pool, based on the MAC address - this is also why you need to "re-provision" a router, if you buy a new one - or "clone" the MAC address of your old router).
  14.  
  15. You need to pick the port carefully - it can't be one of the ports I already mentioned - those are taken for those services. But you could use other ports. If - for instance - you want to deal with web traffic - a port in the 8000 range could be used (8000 and 8080 are popular, but others in the range could be used). For a list of ports, some of their "official" and "unofficial" uses - and which ones you should potentially stay away from (and which are virtually "free for all") - see http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
  16.  
  17. So - now you have the Arduino on the inside of the firewall, and it can be communicated with from the outside world via the NAT hole punch and Dynamic DNS. Assuming you understood and could get all of the above set up, you now need to communicate with it. This is where it can get complicated.
  18.  
  19. One thing - your ISP may block certain or all ports, except for a very small few; you should research if this is the case:
  20.  
  21. https://support.hostgator.com/articles/general-help/technical/commonly-used-port-numbers
  22.  
  23. So - once you have everything picked out - you then need to do something on the Arduino side to listen for traffic on the port. I'm not going to delve deeply into this - those tutorials I mentioned at the beginning of this should get you started. Basically, it's going to listen for traffic, decode the commands, then do something with them. It should then verify that what it was commanded happened (hopefully you'll have some kind of feedback), and respond back with an acknowledgement signal (which also may include or be "sensor data" - say the outside server sent a command that said "read the temperature in the tomato patch" - the Arduino would do so, and respond with either an error response or with the temperature value - or maybe nothing, which would also indicate a potential fault).
  24.  
  25. On the server side, it needs to be able to send those commands, wait for replies, check for errors, re-send if no reply is received (and then finally error if say, 10 tries are made and no response whatsoever is received). It also needs to take input from the outside world (ie - a web page that it serves up, with a form or something on it that the user can click on, which sends POST or GET data from the page to the server program - either syncronously or asynchronously - the latter is best to use as it is non-blocking, but you'll need to learn how to do this either from the browser via AJAX, or using PHP and spawning PHP processes and callbacks, or using JSONP, or any/all of the above).
  26.  
  27. Again - those tutorials should cover many or most of this; plus I have thrown more than a few terms about to help you along (if going the AJAX route - learn to love JQuery as well).
  28.  
  29. Your server will probably need to store (or "log") data in your MySQL database - again, I am sorry but this is a very advanced topic, and to explain it would take many paragraphs - but google "PHP MySQL" and such, and play with it - it isn't that difficult, provided you give it some time and consideration.
  30.  
  31. Oh - note that the above could also be accomplished without HostGator being involved. You could - for instance - put a "server computer" on the inside of your firewall (instead of the Arduino) - let it get it's IP address via DHCP (and MAC) from your router, and use Dynamic DNS to publish things up so that you can access the computer from the outside world. You could then put a LAMP/WAMP/MAMP stack on that machine (depending on the operating system) - and that would become your server. Hook the Arduino to one of the USB ports, have the system discover it, then use PHP (and more than a bit of magic) to communicate with the virtual serial port the Arduino is connected to - and thus communicate with the Arduino via serial. This is a bit more dangerous - as now, instead of just exposing a small microcontroller to the outside world, you are exposing part of a computer system - which if you don't take proper security precautions (and keep the system up-to-date as well) - could lead to someone taking over that computer or the entire network due to a bug in the security or something. This will need ongoing and continuous research and monitoring (welcome to becoming a sysadmin!). It isn't for the light-hearted, but it can make things more convenient - and since you'd have control over the server, if you then - say - wanted to implement your system using Node.js (or io.js) - you would have that freedom to do so (Node.js and io.js are both the same kind of platform - namely "server-side javascript" - io.js split from Node.js earlier, and the fork of the codebases still hasn't been reconciled completely).
  32.  
  33. Finally - as a part of the security - you should have some way to make sure that only authorized machines can contact and control the microcontroller. This can be done in a variety of manners - one way might be to take the date from the Arduino, along with a particular known "string", combine them as a string, then MD5 sum the result - and send that to the requesting computer (which might also send it's own hash to be appended to your generated hash) - if done right (along with some queries to determine certain browser info - maybe by reading a cookie or something?) - you can prevent a lot of problems - but again, doing this properly is beyond the scope here.
  34.  
  35. Hopefully, though, I have whetted your appetite for more. Keep posting here on the forums, and research your questions here and elsewhere (more than likely numerous people have been down the road you are one - heck, I know of 2 or 3 "garden management systems using the Arduino" - so seek them out as well!).
  36.  
  37. Good luck - as you can see, this is anything but an easy kind of system to implement. It is very diverse, and requires quite a bit of knowledge (both working knowledge, and book knowledge) to implement this successfully and properly. So - instead of just a "mental model" - right it down completely - start with the "big blocks" - then break each one down into small sub-tasks - so you don't get overwhelmed and give up too early.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement