Connect To The Arduino With C
Connect To The Arduino With C
Last Updated on Tuesday, 19 February 2013 21:46 Written by Administrator Sunday, 13 September 2009 12:12 One of the coolest things about the Arduino environment is its compatibility with almost anything. After doing a few projects using only the board and some other components, I started to realise the limitations of only using such a hardware based environment. Being able to make a robot was one thing, but wouldnt it be much cooler to be able to, say, email you when something happened or control it from a computer program? That is what Ive done on this project. Using an Arduino board, Visual C# and a tutorial from the Microsoft 'Coding for Fun' site, Ive made a simple program thats controlled from a Windows Form. The user can turn the LED on an Arduino board on and off, then e-mail the state of it to their e-mail address. Its a proof of concept project, that could hopefully be applied to more useful things. Before going on to explaining the program, Id like to say a bit about what I think is the coolest platform for this sort of thing - C#. I spent hours trying to send e-mails using command prompt and php scripts, but C# deals with all the tricky low level coding so its a lot easier to make much cooler programs. For home robotics projects, Id recommend C# to absolutely anyone. Right then, on to the project. Below is the form as it would appear when it first opens. The user can press the On button, which would then turn the LED on the arduino board on.
Once the On button has been pressed, the Off button becomes pressable:
And once the Off button is pressed and an e-mail address is entered:
Arduino Code
void setup() { pinMode(13, OUTPUT); Serial.begin(9600); } void loop() { if(Serial.available()) { int c = Serial.read(); if (c == '1') { digitalWrite(13,HIGH); } else if (c == '0') { digitalWrite(13,LOW); }
} }
C# Code
I'd recommend putting in some error checking if this were a real project, which I haven't done here. The two main areas for checking are the e-mail address and the connection to the serial port. using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms; System.IO.Ports;
namespace SerialCommunication { public partial class Form1 : Form { bool ledState = false; public Form1() { InitializeComponent(); serialPort1.PortName = "COM4"; serialPort1.BaudRate = 9600; serialPort1.Open(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen) serialPort1.Close(); } private void buttonOn_Click(object sender, EventArgs e) { serialPort1.Write("1"); textBox1.Text = "LED is on!"; buttonOn.Enabled = false; buttonOff.Enabled = true; ledState = true; }
private void buttonOff_Click(object sender, EventArgs e) { serialPort1.Write("0"); textBox1.Text = "LED is off!"; buttonOn.Enabled = true; buttonOff.Enabled = false; ledState = false; } private void send_mail(string address) { System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add(address); message.Subject = "Hi there."; message.From = new System.Net.Mail.MailAddress(" you@example.com"); if (ledState == true) { message.Body = "The LED is on"; } if (ledState == false) { message.Body = "The LED is off"; } System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.ukgateway.net"); smtp.Send(message); } private void buttonSend_Click(object sender, EventArgs e) { send_mail(textBox_mail.Text); } } }