Interfacing with an Espruino from c#

Written by ben-<span class="more"></span>wendt

Communication with an Espruino is done by sending JavaScript in string format over a serial port interface. This can be done in c# using the System.IO.Ports.SerialPort class. You can see the default serial port connection settings for interfacing on the Espruino site.

The following code will check abevigoda.com to see if Abe Vigoda is still alive. If he is still alive, a blue light will turn on. If he has passed away, a red light will display.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient client = new WebClient();

            Stream Abe = client.OpenRead("http://www.abevigoda.com/");
            StreamReader reader = new StreamReader(Abe);
            string HTML = reader.ReadToEnd();

            int LEDNumber;
            if (HTML.Contains("alive"))
            {
                LEDNumber = 3;
            }
            else
            {
                LEDNumber = 1;
            }

            SerialPort port = new SerialPort(
                "COM4",
                9600,
                Parity.None,
                8,
                StopBits.One
            );

            port.Open();
            port.Write("digitalWrite(LED" + LEDNumber + ", 1);n");
            port.Close();

        }
    }
}

Here’s the current results:

espruino with blue light