Sunday, November 4, 2012

Serving Websites on Local Network with IIS Express

Whenever I look through documentation, a tutorial, or blog post on any subject, I feel like Brigadier General Jack D. Ripper in Dr. Strangelove, except it's "Clarity of Essence" I'm after and not "Purity". I go to great lengths to find resources that speak to me, but sometimes when a problem needs troubleshooting it's impossible to find anything but poorly worded (and typeset) posts. But, rather than complain, I think I can contribute to some documentation on a problem whose tail I was chasing for too long.

The modern goal of embedded electronics is to create a world of "smart" devices that can upload information to and be controlled from the Cloud -- also known as the Internet to the meteorologically disinclined. Such a networked group of devices makes it possible to monitor and control our stuff from mobile devices to the benefit of our convenience and the reduction of our energy consumption. From an extreme perspective, this turns our world into a sort of video game where we can lazily monitor our current states, stats and change things with the press of a button or tilt of a joystick. I don't necessarily advocate achieving that sort of control. Certain applications of this level of control rattle my conscience (drone warfare). However, it goes without saying some applications need to be around for the good of the environment and humanity. 

Because I'm interested in building this type of networked devices, I consequently have been learning to be able to build embedded circuits and create website interfaces. My tools of choice, the two most elegant tools for doing this, are Netduino and ASP MVC. Netduino allows you to program circuits in C# and provides access to the elegant .NET Micro library. The ASP MVC framework is a great tool for quickly building websites that will offer a REST API, be backed by a database, and will benefit from templated webpages. It too relies on C#, so all logic on both sides is written in the same language.

After building a simple website with ASP MVC, I wanted to be able to send requests to it on my local network from Netduino.  To see if this was possible, I needed to first try to connect to the website from my Android phone. When I ran my website from Visual Studio, it only served the website to "localhost" on a particular port (http://localhost:<5 digit port>/). I couldn't type this URL into my phone and access the site.

I found out that Visual Studio runs the project and serves it by using IIS Express, a scaled down version of the commercial IIS server developed by Microsoft. In the project properties, there is a field to specify which URL you want to address the site. By default it is what I already mentioned, localhost. However, there is a way to make IIS Express serve the website when it receives a request for a different URL. Once this is achieved, changing what's called a URL reservation and security settings allow it to serve to computers, or phones, on the local network.

Here are the steps to follow:
  1. Find Documents/IISExpress/config/applicationhost.xml. This has a section <sites> and in it there is a section <bindings>. These list all the URLs that will represent the site. Copy the existing localhost binding and replace localhost with your IP address. Find your IP address with ipconfig in the command line and look at the top of the output for the IPv4 label.
  2. Add a URL reservation. This will grant the right for others on the network to request a URL from IIS Express and is done at the command line by typing:
    • netsh http add urlacl url=http://<your IP>:<5 digit port>/ user=everyone (Italics represents parameters to be passed.)
  3. Add a firewall rule to allow inbound requests on the port. Go to Control Panel > Windows Firewall > Advanced Settings > Inbound Rules > Add Rule. Name the rule after your site (since it will be the only one running on the chosen port). Change the scope Remote IP Address to "Local subnet". Make sure Profiles is limited to Private. Protocols and Ports should be set to TCP and the port you found in Visual Studio. 
I found that I didn't need to change the URL specified in the properties of the project in Visual Studio so long as it was localhost. In fact, it even worked when I changed it to the IP address URL. 

The problem with all other walk through posts is that they recommended the use of machine name. This never worked for me. The way I described does not require the application to be run with administrator privileges or other nonsense. 

In conclusion, I haven't created any applications that connect the Netduino and my ASP MVC project yet, but at the very least, I know I can access my service on my local network since my phone can browse the site. More to come.

Saturday, November 3, 2012

A Micropost on Microcontrollers

Microcontrollers allow you to control circuits by using a programming language. Real, physical circuit components can become variables in your virtual programming world and you can act on them as you would on virtual objects in your program. Since programs can access networks, one can collect data produced by a circuit, process it into information, and send it to a remote database via the internet, in effect automating scientific data recording. The reverse process allows information to be sent to the microcontroller via the internet (from the result of a website interaction) and the behavior of the circuit can be changed based on those remote inputs.

The programs that control your circuit would live on the microcontroller and they are able to read voltages and produce voltages in the circuit. When voltages are read, they represent different states of the circuit e.g. whether a button is on or off or a sensor measurement. When voltage is produced, the program can alter the behavior of the circuit. When reading and writing are combined, one can utilize feedback and alter the behavior of the circuit based on the states it is in. The produced voltages can provide enough power for smaller circuits, but if more power is needed, smaller circuits can be activated with this lower voltage to drive higher power applications.

Microcontrollers are a tool that allow for, oftentimes, a simpler, more efficient, and faster implementation of various circuit behaviors. This is not to say that they are more energy efficient than a well designed, single-purpose circuit. For example, if we need to build a circuit with an LED that blinks every second, this can be implemented without a microcontroller, however by using a microcontroller, we have the ability to easily code this behavior using object-oriented methods. If we want to tailor the light's behavior later to change its blink rate, for example, this can be done with lines of code instead of wires and capacitors. The power and simplicity of a programming language allows us to manipulate the electrical inputs and outputs without knowing about different circuits and circuit components that would create the behavior we want. This allows a single circuit connected to a microcontroller to have an incredible number of behaviors.

The microcontroller is connected to a program virtually, however to be useful it must be physically connected to the world with sensor circuits and actuator circuits. Sensors take the world and convert it to electricity. Actuators take electricity and change the world. The microcontroller is the bridge between ideas and physical form.

Networks

I was able to finally connect my Netduino to the local network at my house. This allowed me to run sample code that makes it possible to turn the on-board LED on and off from my browser anywhere in my house via WiFi.

I want to summarize the most salient things that I needed to know to get this to work. These ideas made the whole project easier to understand.

First off, the idea of networks is simple. Devices need to send data i.e. electric pulses and signals to one another by some medium (e.g. copper wires, fiber optics, etc.). These pulses can represent things from emails to pictures to numbers because of the plasticity of binary. (For a good read on the origins of binary aka digital representation, read Turing's Cathedral.) The vocabulary used to describe networks is not simple, however. The people who created network theory really loved to call things by esoteric names like "protocol" and give everything initialisms.

Networks, by their very nature, require addresses. When we want to send data to another device, it should conjure up the metaphor of sending a letter. We can only do this if we know friend's address and our own. Even the stamp has a role in the metaphor, but I will describe that later. There are two schemes for addressing electronic devices.
  1. Every device that can be networked is uniquely identified by a MAC address. Unique means that out of all electronics made that can be networked, your device is the only device that has that number. This is a way to identify your device on a network e.g. laptop or Netduino. 
  2. The internet has its own address system called IP (Internet Protocol) addressing. This makes it possible to group networks together logically. This means you can look at the digits that form the IP address and deduce that two networks are related if certain digits are the same. This logical structure wouldn't be possible with the MAC address alone. Think of a ZIP code: there is a relationship between location and the numbering. Any ZIP starting with "98" is in a certain region of the USA. You can achieve the same type of awareness by looking at an IP. Anything starting with "192.168" belongs to the local network, for example. Now, if you were given a house number (i.e. a MAC address) you wouldn't be able to tell me much about where it is located in the country.
Both of these addresses give a sense of identity to your device, but the IP address can change according to its relationship to the network. The "arp" command gives you a table of all the devices connected on your network and shows you the MAC and its assigned IP. ARP stands for Address Resolution Protocol.

Your router that connects you to the Internet serves its own control interface that can be accessed at 192.168.1.1. It lets you configure local IP addressing, security, and passwords. The most important configuration that occurs in the router is something called DHCP (Dynamic Host Configuration Protocol). As the name suggests, it configures the hosts on the network dynamically (isn't that so simple to understand?) In easier terms, it assigns IP addresses to all the hosts i.e. devices on the network depending on which IP addresses are not in use or are restricted. This means devices won't necessarily receive static (unchanging) addresses which is good for security reasons. However, DHCP reservations can be made so that certain IP addresses will always associate with a particular device (MAC address).

The final thing that helped me work on this project is demystifying the Internet in my mind by viewing it simply as a network. I feel as though the Internet is referred to as though it were God -- the first letters are even capitalized as such -- and people feel like the answers and resources it can produce are magical. In reality, it is a group of devices that hold a lot of data and information. It is huge and it stores a lot of knowledge, but it's not god. Think of your home network as a small "internet". If the information you share is important, it might be a really important internet. However, the main difference between the Internet and a local network (LAN) is that the Internet fulfills the idea of a "concatenated network" where many separate networks are linked. Your LAN is not concatenated because it represents one network. Connect your LAN with your neighbor's and it becomes closer to becoming a full-fledged Internet.

As for my earlier postage stamp analogy, we all pay a price to connect to the infrastructure that makes wide communication possible. Our electrons become the messengers and they roll down fiber optic roads like mail trucks to do our bidding.

I hope to write another post that will summarize the idea of Network Interfaces and how to make connections in C#.

Wednesday, October 24, 2012

Problem Solving

I want to share my approach to solving problems. These techniques are especially suited for math problems, but they work for most other problems I've solved. I believe that a lot of problem solving is organizing your work and information. When a problem is confusing, it is probably because it hasn't been drawn out in an organized and systematic way. I typically separate my work into a few sections:

  • Restatement of the problem, both written and verbal.
  • A diagram or drawing of the problem to visualize the symbols. This is probably the most important process for me because my ability to see visual patterns and transformations is strong. This and the previous step will help you think about and define symbols that you introduce in the next step.
  • Table of symbols, with column headers for values (initial or other), the value's measurement unit, and description of what the symbol represents. This table should also include conditions on the variables you've introduced e.g. statements like "x is positive" or "V not equal to 0".
  • A written statement of approach that describes how the problem should generally be solved. This statement is a prediction of the necessary steps to get an answer. When solving programming problems, this is usually done with pseudocode.
  • Manipulation and reconfiguration of equations or functions e.g. differentiation, integration, refactoring. It  is always good to state what facts you use to make jumps in logic for your own reference and for others to follow along. The more you explain yourself in writing or another durable way, the better value your work has as a reference for yourself in the future and for others.
  • Numerical calculations. This can be a subtle step that ruins your answer. Do calculations carefully, substitute the correct values by referring to symbol table and only after you've fully completed the last step. Be particularly wary of sign.
  • Solution and concluding remarks.  View all your solutions as something you'll need to go back to in the future when you forget, so write down a little journal entry stating the process and how it differed from what you expected.
This is a short list, but it is very helpful for approaching many problems. Another great resource for problem solving is Stewart Calculus: Principles of Problem Solving.  If you want to master the problem, teach it and speak the solution to someone. 

Monday, October 8, 2012

Slide Rules

I spent a good portion of the day learning about slide rules because I received my Concise No. 28 Circular Slide rule yesterday and I want to start using it for calculations. I believe that it encourages good calculation habits because it demands efficient calculations. I looked for reference materials by searching through manuals at http://sliderule.ozmanor.com/man/man-download.html and I found that the Keuffel & Esser manual had the best information on the theory of slide rules. It quickly allowed me to grasp the ideas behind a slide rule and therefore its operation.

NB: I only understood the manual because I understand the properties of logarithms and exponents. Please read up on that before you try to understand the following breakdown of what I learned.

The section on slide rule theory starts by comparing logarithmic slide rule operation to linear slide ruler operation. With the linear slide ruler, one can perform addition and subtraction by setting units together side by side. Since we can perform addition and subtraction with rulers, we can do something more interesting -- add and subtract scaled units. It turns out that using logarithmic scaled units performs multiplication and division.

Most operations can be done with plain logarithmic scales. I won't go into the plain scales, but I will go into a cool variant called the "folded scale," which typically starts at Pi, but could start at any number. If the folded scale starts at Pi (in other words, if the index is Pi), when the slide is closed, Pi is above "1" on the regular scale and the indexes are lined up. So, by moving the cursor to any position on the regular scale, the number it lands on will be the multiplier of Pi. The product of Pi and this number will be under the cursor on the folded scale. The folded scale also goes through a change in distance equal to the change you make on the unfolded scale. Because of this, products that fall off the end of a linear slide rule scale can usually be picked up on the folded scale and read off. On my circular slide rule, I don't need to worry about ever falling off the edge because a circle has no edges.

The inverse scale is a variant of the original scale. Simply, it is the original logarithmic scale laid down from right to left instead of from left to right. It is laid down like this because the entire length of the original scale is log(10). When you set the cursor, you break it into two pieces. One piece is length log(N) and the other length log(10) - log(N) = log(10/N). Since log(10/N) is the same as the inverse with a different characteristic, if you measure it, you are measuring log(1/N). So, the cursor would be on 1/N if you lay a scale down from the right.

The greatest power of a slide rule is not in doing single multiplication and division problems. It's greatest power is to view scale (K x) and proportion (a/b = c/d)  relationships at a glance. By multiplying by a number, K, you can find all products of K by setting the cursor to your desired multiplicand. By setting two numbers on top of each other, you are in effect setting all other marks between the scales to reflect that proportion. To find an equivalent proportion, simply set the cursor. It's remarkable that we don't use slide rules still because of this amazing property.

After writing this, I realized that the slide rule is an example of the broader topic of transformations. Transformations in general are used to make operations easier or faster to compute. The most famous in electrical engineering are the Laplace, Fourier, and Phasor transforms. Where logarithms transform the operation of multiplication to the easier operation of addition, these transformations change functions to make differential equations easier to solve.

Saturday, October 6, 2012

Why Blog?

This blog will serve as my lab notebook for any experiments or projects that I undertake. While I don't necessarily have grand hypotheses to test, I would like to at the very least record the smaller hypotheses I make on a daily basis and the processes I go through to test them as I build or repair things. My posts should share the most important things that I learn on any given day that help me achieve my project goals. I would like posts to follow this skeleton (unless I have something random to share)
  1. A problem and my process, apparatus, and solution
  2. One thing I learned
  3. New capabilities I gained from what I learned
  4. Mistakes I made while trying to solve the problem
  5. Strategies for solving a given problem and next steps if no solution is found
  6. Experiment log and results
  7. Diagrams, pictures, and memory aids
  8. Links to relevant resources
I hope that by having a good place to post my work, I will do better about recording my progress and create a good routine. The blog description is loose enough to allow for mathematical content and engineering content, so I'll do my best to post on both subjects. If I include any mathematical expressions, I will post them as images, but you can contact me to receive LaTeX source. Same goes for any programs I post about.