Tutorial: ESP8266 + PHP and MySQL Database

I hear you guys

I see it in the view counts, comments, and emails asking for information about my ESP8266 and MySQL project I made two years ago. People are having trouble with their ESP8266 and MySQL Database projects. The reasons why I haven’t posted about it since then is because I haven’t been doing much with the ESP8266, and the project is so old that the way it’s programed is out-of-date. Development for the ESP8266 has evolved so much and made it easier. You no longer have to deal with AT commands with all of the libraries and tutorials for the Arduino IDE.

I’m going to do a quick tutorial to explain how I’ve been doing my ESP8266 with MySQL project. It’s not going to have much code but more explaining so you get what’s happening.

Before we start, some links for you

The ESP8266 blog post that everyone is showing up to see: https://mwhprojects.wordpress.com/2015/01/18/esp8266-with-a-mysql-database/

The Github repo for this, though sort of irrelevant at this point because it’s outdated: https://github.com/mwhprojects/Arduino-ESP8266

The recent NodeMCU project that showed me what’s new with the ESP8266: https://mwhprojects.wordpress.com/category/projects-2/nodemcu/garage-monitor/

The Github repo for that: https://github.com/mwhprojects/NodeMCU-MySQL

GETing values and INSERTing them

Here’s what happens in the code uploaded onto the ESP8266/NodeMCU (see Github):

  1. The ESP8266 connects to the webhost.
  2. The switch values are read.
  3. These switch values are then inserted into a URL which the ESP8266 tries to load.
  4. Repeat.

I think that step three is what people are most interested in, so let me explain that a little further. PHP has the GET method, which basically reads variables that are in the URL itself. For instance, if we have a URL like: thisisanexample.com/example.php?a=1&b=2, we could use PHP to get the values of a and b. In PHP, we’d write $somevariable = $_GET[‘a’]; and again for b. We can then use PHP to connect up to a MySQL database and insert $somevariable and whatever else you’d like into the table.

In the PHP code (see Github), the file begins with code that checks for variables in the URL as explained above. If there is, it uses the GET method to get the variables and then inserts them into the MySQL database table.

Just a heads up, the deep sleep in the NodeMCU hasn’t been working for me, as I explained with my NodeMCU project posts. Keep that in mind if you plan on adopting the code. Heck, let me know what happens with your project as it could just be something with my hardware.

So now what?

Like good old XDA… YOU TELL ME.

I’m not an expert with the ESP8266 but I’d like to help if I can. Please let me know if there’s any clarifications I should make to this post or any more specifics on what problems you’re having with a similar project.

Thanks a lot for reading and good luck with your projects!

The lamp that WAS meant to be! (WS2812B LED Box)

A little while ago, I was experimenting with a WS2812B LED and toying with the idea of making a basic lamp. Things didn’t work out and the project was scrapped but I still had the internals of it ready on a breadboard so I gave it another go, while taking in some inspiration from the comments on that post which mentioned a product that’s more or less a small table-top spotlight.


From my previous experiments, I already had a WS2812B LED soldered to some wires. For this project, I used only 22AWG stranded wire. For isolation and to hold things in place, I used hot glue.


With the LED ready, I started by putting one of my Attiny85 breakout boards onto the perfboard.


I then completed the soldering of the LED and potentiometers to the perfboard. The potentiometers are all connected to each other by Vcc and Ground. It seems like the connections in between one another weren’t very good as the LED would go wild at times, changing color or blinking for no reason. After some wiggling with the wires, I found which connections were weak and reflowed them.

… But even reflowing the connections didn’t work. The only thing that did fix the problems was putting force on the connections in a certain way. Once I got it working, I quickly hot glued everything. I know that’s horrible, but, being such a no frills project, I’m not very concerned. After the circuit was completed, I trimmed down the perfboard since the rest of it wasn’t needed.


The potentiometer and LEDs were all 5mm^2 so it wasn’t difficult cutting out appropriate sized holes for them. I first put in the LED and taped it to hold it in place before adding hot glue.


After the LED was in place, I fed the power supply wires (female jumper wires) through a hole I cut near the bottom corner of the box. The potentiometers come with a washer and nut so I didn’t need any hot glue to keep those in place. This is my first time using these kind of potentiometers in a project and, even without fancy knobs, I think they look great. They certainly look better than tiny trimpots, which is what I would have used otherwise.


And that’s it! The placement of the potentiometers was planned specifically to double as a way to lay the box down on an angle.

I hope you liked this simple project of mine. I recently ordered a set of these LEDs for an idea for the next Light Show.

PS. My main website, www.mwhprojects.com, was recently updated!

VB Interface for Arduino Tutorial Series Part 3: VB Code

This is part 3 of the Creating a Visual Basic Interface for Arduino Tutorial.

In the last part of the tutorial, we’re going to program that Windows form we design in the previous part of the tutorial. If you haven’t already downloaded the code, you can grab it at GitHub.

Understanding the Process

Before we start taking a look at the code behind our form controls, let’s lay out what a typical use of the program should roughly look like:

  1. User selects COM port and clicks Connect button.
  2. Program tries to connect to Arduino. If it can’t, display an error message. If it can, display a success message and remove that Panel that was hiding our controls.
  3. User selects a pin number in a ComboBox, and moves around a TrackBar to select a position/speed for the servo. This value is displayed in a textbox.
  4. Regardless of any input from the user, the VB program sends the values to the Arduino every 100ms, which is the interval of the Timer.

The program also offers a way to set a zero position… The home position of a 180º servo, or the stop value of a continuous rotation servo motor. When the user clicks the Button, “btnZero”, change the variable, “Zero”, to the current value. When the user clicks another Button, “btnSetZero”, set the TrackBar and “txtSpeed” TextBox to the value of the variable “Zero”. The Arduino gets the zero value because it’s sent shortly after because of the Timer.

Hopefully you’re still following. Let’s take a look at the actual code.

Code Walkthrough

frmMain_Load  (When the program starts)

cboxCOMport.SelectedItem = “COM3”
cboxPin.SelectedItem = “11”
panelHide.Visible = True
txtSpeed.Text = Zero
txtZero.Text = Zero

These fives lines in frmMain_Load set some default values, as well as make sure the Panel to hide the controls is visible. The first two lines set the default values for the drop down ComboBoxes for the COM port and servo pin number. The third line makes sure the Panel is visible. The fourth and fifth line sets the speed and zero TextBoxes to the value of the Zero variable, which is declared globally.

My Arduino typically connects via COM port 3 and I was using pin 11 for the servo, so you can change these to fit your setup.

btnConnect_Click (When the Connect button, “btnConnect”, is clicked)

Dim ConnectSuccess As Boolean = True
SerialPort.PortName = cboxCOMport.Text
SerialPort.BaudRate = 9600
SerialPort.DataBits = 8
SerialPort.Parity = Parity.None
SerialPort.StopBits = StopBits.One
SerialPort.Handshake = Handshake.None
SerialPort.Encoding = System.Text.Encoding.Default
ConnectSuccess = False
End Try

Before the Try-Catch statement, we initialized the boolean variable, “ConnectSuccess” as true. If there is an error executing the code between Try and Catch, the code in Catch will change the variable to false and close the serial connection. If there is no problem, the serial connection remains open.

If ConnectSuccess = True Then
btnConnect.Enabled = False
btnConnect.Text = “Connected!”
MsgBox(“Connected to Arduino using ” & cboxCOMport.Text & “!” & vbNewLine, MsgBoxStyle.Information, “Success”)
panelHide.Visible = False
Timer1.Enabled = True

If the serial connection was a success, disable the Connect button and change the text in the button to read, “Connected!”. Display a message box that says the connection was successful. Make the Panel that was hiding the controls invisible. Finally, enable the Timer that is used to continuously send data to the Arduino.

MsgBox(“Could not connect using ” & cboxCOMport.Text & “!” & vbNewLine & “Please try again!”, MsgBoxStyle.Exclamation, “Error”)
End If

If the serial connection failed, display an error message.

Timer1_Tick (Whenever the Timer interval elapses)


For good practice, clear the serial buffers, before writing the pin number and speed TrackBar value to the serial port. PadLeft() is to make sure there is always 5 characters sent in total. Otherwise, any value under 100 would cause the Arduino to think there’s an error because it would only receive 3 or 4 characters.

trkSpeed_Scroll (Whenever the TrackBar is moved)

txtSpeed.Text = trkSpeed.Value

The TrackBar is the only way to change the servo speed/position value, but we have a TextBox to display exactly what value we have on the TrackBar. This line of code puts the value of the TrackBar into the TextBox.

btnZero_Click (Whenever the button labelled “Set current value as 0” is clicked)

Zero = trkSpeed.Value
txtZero.Text = Zero

The first line adjusts the variable “Zero” to whatever the current speed/position value is set. The second line changes the text in a TextBox that displays the Zero value, “txtZero”, to the new Zero value.

btnSetZero_Click (Whenever the button labelled “0” is clicked)

trkSpeed.Value = Zero
txtSpeed.Text = Zero

This code sets the TrackBar and the current speed TextBox, “txtSpeed”, to the value of the variable, “Zero”. Basically, it homes a 180º servo motor or stops a continuous rotation servo motor.

frmMain_FormClosing (When the user quits the program)

If (SerialPort.IsOpen) Then
End If

Just before the program closes completely, we want to send the Zero value so that it will home or stop the servo motor. We should only try sending it if there is an open serial connection. Close the serial connection.

That’s it!

I hope you enjoyed this tutorial! If you have any questions, please feel free to leave a comment in the appropriate post. I know all of this code can be overwhelming for some, so if there is any part that needs more clarification, I’ll be happy to help!

Thanks for visiting!

VB Interface for Arduino Tutorial Series Part 2: VB Form

This is part 2 of the Creating a Visual Basic Interface for Arduino Tutorial.

In the second part of the tutorial, we’re going to build our Windows form for project. If you haven’t already downloaded the code, you can grab it at GitHub.

Adding Controls

programThis is what the final program looks like. On the surface, there are two ComboBoxes, two TextBoxes, three Buttons, and one TrackBar. In the editor, you’ll have to add a couple more controls that aren’t visible controls. They are a SerialPort and a Timer.

visualstudioHere are the main controls in the form and some important settings:

“SerialPort” – SerialPort

“Timer1” – Timer, with interval of 100.

“cboxCOMport” – ComboBox

“btnConnect” – Button

“cboxPin” – ComboBox

“txtSpeed” – TextBox, ReadOnly  true.

“trkSpeed” – TrackBar, with min of 0, max of 180, and value (default on form load) 90.

“btnSetZero” – Button

txtZero” – TextBox, ReadOnly true.
“panelHide” – Panel, docked fill.

Hiding Controls

You may have noticed in the previous picture that there was nothing in the bottom section of the program. Until the program is connected to an Arduino, a Panel, “panelHide”, is used to hide the controls that set the parameters that will be sent to the Arduino.


The panel is above all of the other controls so that it covers them. When you’re designing the form, it will be in the way. To temporarily move it out of the way, open the Document Outline in View > Other Windows > Document Outline, which is shown on the left in the picture above. In the Document Outline, you can move around the layers, so you can move the Panel underneath all of the controls so you can get to them. Just remember to move the Panel back as the top layer when you’re done.

Coming up next: VB Code

In the next part of the tutorial, we’ll finish off the project by programming this Windows form we just designed! Stay tuned!

VB Interface for Arduino Tutorial Series Part 1: Arduino Code

This is part 1 of the Creating a Visual Basic Interface for Arduino Tutorial.

In the first part of the tutorial, we’re going to focus on the Arduino part of the project. If you haven’t already downloaded the code, you can grab it at GitHub.

Understanding the Process

First, let’s take a moment to understand what we’re trying to do in the Arduino code. The Arduino code in this project is only receiving data; We’re not bothering to send anything back. This makes it pretty simple as all we have to do is:

  1. Wait for serial buffer to fill up with data from the VB program, then put the data into a string variable.
  2. Check if we got the right amount of data from the VB program.
  3. Split up the data into two parts: One for selecting the pin of the servo motor, the other for setting the servo position/speed.
  4. Convert the two parts into integers so we can properly use the values with the servo library.

That’s it! Let’s take a look at the code.

Code Walkthrough

#include <Servo.h>
Servo userservo;

This code sets up the servo library. “userservo” is what we use to identify the servo pin we’re using.

String receivedData;
String pinSelect, servoSpeed;

These strings are our pieces of data. “receivedData” is the full string of data we get from the VB program. “pinSelect” and “servoSpeed” are the two pieces of data we end up when we split “receivedData”.

void setup(){

We start a serial connection at 9600 baud as soon as the Arduino is powered on.

void loop()
while(Serial.available()) {

char c = Serial.read();
receivedData += c;

We’re now into the main looping program. The code in the while loop executes when there is data in the buffer. We start reading one character at a time from the serial buffer and adding it to the end of the “receivedData” string. The code in the while loop loops until the serial buffer is empty, so by the time we’re out of the while loop, “receivedData” should have the full string that was sent from the VB program.

if(receivedData.length() != 5){
receivedData = “”;

The next step is to check if we have the correct amount of data from the VB program. We’re expecting 5 characters in the string, so if there isn’t 5 characters, we’ll just throw out whatever we did get.


pinSelect = receivedData.substring(0, 2);
int pinSelectInt = pinSelect.toInt();

If we did get the correct amount of data, we’ll split it up into two pieces of data. The first piece selects the pin the servo motor is connected to. The substring() function returns a piece of the string. The first parameter is the position of the first character we want, and the second parameter is the position after the last character we want. Note that the position of the first character in a string is 0. We want the first two characters of the string, so the arguments are 0 and 2.

The next line converts the new string into an integer, which is the data type that the servo library works with.

The following line attaches the selected pin to “userservo”. We need this to address our servo when we set the position/speed.

servoSpeed = receivedData.substring(2, 6);
int servoSpeedInt = servoSpeed.toInt();

receivedData = “”;

Repeat the process again for the second piece of data, which is the servo speed. The parameters for substring() this time is 2 and 6 because we want characters in positions 2 to 5 of the string.


The last line sets the servo position/speed to whatever the VB program sent.


And that’s the end of our program loop!

Coming up next: VB Form

In the next part of the tutorial, we’re going to design the Visual Basic form in Visual Studio. Stay tuned!

Visual Basic Interface for Arduino Tutorial Series: Meet the Servo Tester

I’m excited to announce my first blog tutorial series! It will be a 3-part tutorial that is based on making a Visual Basic interface for your Arduino. In this tutorial, we’ll be building a servo tester so you can find certain positions of a 180º servo, or certain speeds for a continuous servo motor. If you have an Arduino and servo motor lying around, dig it out and get ready! It’s actually a pretty practical program.


The full code files are already on GitHub. I don’t really feel it’s necessary to drag it out, so the entire tutorial will be released tomorrow throughout the day. I just wanted to separate posts so that people can leave comments in the appropriate place in the tutorial.

Anyways, thanks for visiting! Come back tomorrow and see what this project is made of!

Creating a VB Interface for Arduino Tutorial Website Update!

4When I put up the “Interfacing Arduino with Visual Basic Tutorial” website, there was a lot of pressure from people to put up information on this topic so I didn’t put a lot of effort into the website itself. After some thought, I decided to give the website a makeover that would make it both nicer looking and easier to learn from. You can check it out at mwhprojects.com/arduinovb. Let’s take a look at what’s new.

New Name. New Colors. New Layout.

2A lot of small changes have added up to a great redesign. The tutorial name has changed to “Creating a VB Interface for Arduino”. The new website feels a lot warmer with a blue and white color scheme, with a little gray too. I’m using Google Code Prettify to display the snippets of code on the website, instead of the screenshots of the IDE from before. The tutorial is now split up into pages so that there are less large walls of overwhelming text.

The Menu

1The menu gracefully slides out to display an index of all of the pages on the website. As you’ll see, it’s a lot easier to find certain concepts used in the program. The idea is that, if you’re not willing to go through the entire tutorial, you can easily find a certain concept that you’re looking for to implement in your project.


3I’m really excited to add a couple special features to the website. Some of the tutorial pages have quizzes at the end of them that ask questions that can be answered from what was talked about in the code descriptions. I hope you have fun with it.


The other special feature is Bookmark. Basically, if you plan on going through the entire tutorial but can’t do it in one sitting, with the Bookmark feature enabled, all you have to do is bookmark the homepage of the website instead of bookmarking the page you’re on. When you revisit the homepage next time, you’ll get a little notification letting you know where you left off.

I was thinking of other options to add to the website, but I think this one feature is enough for a site like this.


Please let me know what you think! Leave a comment or send me a message!

Thanks for visiting!