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 programmed is dated. Development for the ESP8266 has evolved so much and has made it easier. You no longer have to deal with AT commands with Arduino libraries out there that handle it for you.
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.
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):
- The ESP8266 connects to the webhost.
- The switch values are read.
- These switch values are then inserted into a URL which the ESP8266 tries to load.
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: example.com/page.php?val1=1&val2=2, we could use PHP to get the values of val1 and val2. In PHP, we’d use $_GET[‘val1’]; to use in our logic, and again for val2.
Once we have the values, we can then use PHP to connect up to a MySQL database and insert them (and whatever else you’d like) into the database 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!