Send Data from Arduino to ASP.NET C# Web Application

A few days ago I wrote a post entitled ‘Control Arduino from ASP.NET C# Web Application Interface‘. Obviously its a topic of interest with folks on the internet, because it has quickly recieved more views than any of my previous posts.

In response to the popularity of that post (and because I am such a nice guy), I have decided to write this follow up post, showing you how to do the exact oposite.

By adding a few lines of code to my previous example, I can not only send commands to my Arduino, but I can get responses from it directly to my web application. Calabunga, dude!

This project doesnt require any hardware changes to the setup in the origional post. In case you need to see that again, dont worry, I got ya covered.

wiring

Next, we need to add a few lines of code telling our Arduino how to respond to the loop in our sketch. We dont need to make any changes to our declarations or setup().

In my example, I want my Arduino to respond to the input from my web application with a little confirmation message. We do this by adding a few lines of code to the loop() in our Sketch, telling our Arduino how to respond. This doesnt require any changes to our declarations or setup().

void loop()
{
  if (Serial.available() > 0)
  {
    int val = Serial.read();
    if (val == '1')
    {
      digitalWrite(blue, HIGH);
      delay(time);
      digitalWrite(blue, LOW);
      Serial.println("Blue is On");
    }
    if (val == '2')
    {
      digitalWrite(green, HIGH);
      delay(time);
      digitalWrite(green, LOW);
      Serial.println("Green is On");
    }
    if (val == '3')
    {
      digitalWrite(orange, HIGH);
      delay(time);
      digitalWrite(orange, LOW);
      Serial.println("Orange is On");
    }
    if (val == '4')
    {
      digitalWrite(red, HIGH);
      delay(time);
      digitalWrite(red, LOW);
      Serial.println("Red is On");
    }
    Serial.flush();
  }
}

Now, whenever we turn an LED on using our web application our Arduino is going to print a repsonse that the web application can then grab and display to us.

Once youve uploaded your modified sketch, all thats left to do is modify our web app so it knows to look for a response and then knows where and how to display it.

We will start with our code behind.

protected void Page_Load(object sender, EventArgs e)
        {
            //set initial label text
            Label1.Text = "";
            //set com port for arduino
            ardo = new SerialPort();
            ardo.PortName = "COM4";
            ardo.BaudRate = 9600;
        }
        protected void BlueOn(object sender, EventArgs e)
        {
            ardo.DataReceived += OnReceived;
            string blue = "1";
            ardo.Open();
            ardo.Write(blue);
            System.Threading.Thread.Sleep(2000);
            ardo.Close();
        }
        protected void GreenOn(object sender, EventArgs e)
        {
            ardo.DataReceived += OnReceived;
            string green = "2";
            ardo.Open();
            ardo.Write(green);
            System.Threading.Thread.Sleep(2000);
            ardo.Close();
        }
        protected void OrangeOn(object sender, EventArgs e)
        {
            ardo.DataReceived += OnReceived;
            string orange = "3";
            ardo.Open();
            ardo.Write(orange);
            System.Threading.Thread.Sleep(2000);
            ardo.Close();
        }
        protected void RedOn(object sender, EventArgs e)
        {
            ardo.DataReceived += OnReceived;
            string red = "4";
            ardo.Open();
            ardo.Write(red);
            System.Threading.Thread.Sleep(2000);
            ardo.Close();
        }
        protected void OnReceived(object sender, SerialDataReceivedEventArgs c)
        {
                try
                {
                    // write out text coming back from the arduino
                    Label1.Text = ardo.ReadLine();
                }
                catch (Exception)
                {
                    Label1.Text = ("COM Return Error");
                }
        }

See what I did there? No? Well, I break it down for you. We edited each of our OnClick event handlers to run the OnRecieved event handler and allow 2 seconds before doing so (our Arduino needs to run its LED blink operation before it can write text to the serial). Our OnRecieved handler simply reads the text from our Arduino serial port, then puts that text in our Label1 object. If it doesnt get the text, it gives us a little canned error message.

Last but not least, we add our Label object to our ASP.NET page. In my example, I placed it right above my first button.

<asp:Label ID="Label1" runat="server" BorderStyle="None" 
     Font-Names="Segoe UI" Font-Size="Medium" 
     ForeColor="Red" style="text-align: center" Text="Label" 
     Width="314px"></asp:Label>

Now, cue the curtian because…

thatsallfolks

Advertisements

3 responses to “Send Data from Arduino to ASP.NET C# Web Application

  1. Pingback: Send data from Arduino to ASP.NET C# web apps | freetronicsblog·

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s