In this example we will control an RGB led connected to our Wemos Mini from an application running on our PC. In this example I will use VB.net to write the application but you can use any language that has the ability to use the serial port.
I wanted to have a GUI but you can easily have a command line, also the app could be created in C#. I don’t really have a preference in that area. I just decided to do it in VB.net for a change rather than C#
Layout
Here is the layout, the LED is connected to pins 6, 7 and 8 of the Wemos Mini and we use 3v3 to power it. I had an RGB led breakout, common anode type
Arduino Code
The Arduino part is quite straightforward, basically it listens for a character on the serial port and then performs an action based on the character recieved. Our PC application will send numbers 1 to 6, each of these numbers will tie in with either switching an individual LED on or off on the RGB led.
int redPin = D6;
int greenPin = D7;
int bluePin = D8;
void setup()
{
// declare the serial comm at 9600 baud rate
Serial.begin(9600);
// output pins
pinMode(redPin, OUTPUT); // red
pinMode(greenPin, OUTPUT); // green
pinMode(bluePin, OUTPUT); // blue
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, HIGH);
}
void loop()
{
// call the returned value from GetFromSerial() function
switch(GetFromSerial())
{
case '1':
digitalWrite(redPin, LOW);
break;
case '2':
digitalWrite(redPin, HIGH);
break;
case '3':
digitalWrite(greenPin, LOW);
break;
case '4':
digitalWrite(greenPin, HIGH);
break;
case '5':
digitalWrite(bluePin, LOW);
break;
case '6':
digitalWrite(bluePin, HIGH);
break;
}
}
// read the serial port
int GetFromSerial()
{
while (Serial.available()<=0) {
}
return Serial.read();
}
Application
This was written in Visual Studio 2010.
I wanted to have a combo box with a list of com ports, the user would then select the correct one and press a button to connect, there would also be a button to disconnect. There would be 6 more buttons on the form these would switch the red, green and blue leds on and off
This is the GUI I designed in Visual Studio
Now for the code for the application
Imports System.IO.Ports
Public Class frmRGBLed
Dim WithEvents serialPort As New SerialPort
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
Try
serialPort.BaudRate = 9600
serialPort.PortName = cboPorts.SelectedItem.ToString
serialPort.Parity = Parity.None
serialPort.DataBits = 8
serialPort.StopBits = 1
serialPort.Open()
serialPort.Encoding = System.Text.Encoding.Default
If serialPort.IsOpen Then
btnConnect.Visible = False
cboPorts.Enabled = False
btnDisconnect.Visible = True
End If
Catch
serialPort.Close()
End Try
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
Try
serialPort.Close()
btnConnect.Visible = True
btnDisconnect.Visible = False
cboPorts.Enabled = True
Exit Sub
Catch
MessageBox.Show("Problem closing port")
End Try
End Sub
Private Sub blnRedOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles blnRedOn.Click
serialPort.Write("1")
End Sub
Private Sub btnRedOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRedOff.Click
serialPort.Write("2")
End Sub
Private Sub btnGreenOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGreenOn.Click
serialPort.Write("3")
End Sub
Private Sub btnGreenOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGreenOff.Click
serialPort.Write("4")
End Sub
Private Sub btmBlueOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmBlueOn.Click
serialPort.Write("5")
End Sub
Private Sub btnBlueOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBlueOff.Click
serialPort.Write("6")
End Sub
Private Sub GetSerialPortNames()
For Each sport As String In My.Computer.Ports.SerialPortNames
cboPorts.Items.Add(sport)
Next
End Sub
Private Sub frmRGBLed_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
GetSerialPortNames()
cboPorts.SelectedIndex = 0
Catch
MsgBox("No ports connected.")
End Try
End Sub
End Class
You could add any other functionality you wanted, maybe a status bar. Automatic detection of the Wemos rather than using a combobox and buttons
Links
The complete code is in the following github location, I always find it easier to check out the code on your own machine. There is also the fritzing schematics and code for the Arduino IDE
https://github.com/esp8266learning/projects/tree/master/Wemos%20RGB%20Led
The ESP 8266 is intended for WiFi connections. Where in your code is the WiFi connection established.
Fair enough but if directly connected to a PC you can still control it using a com port – that’s all this is showing