Issue:connecting arduino uno to mysql database using esp8266

Issue :connecting arduino uno to mysql database using esp8266 via the arduino ide sketch program.

Hardwares we are using :

Arduino uno connected to local system via cable
ESP8266 wifi module connected to arduino uno as per the attached diagram.

Arduino ide 1.8.7 sketch program
mysql 8.0

our objective is to insert the data from arduino uno sensor data into local mysql database .

arduino ide board we are using is “generic esp8266 module”.

We are using following code for inserting the data into database:

#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <MySQL_Encrypt_Sha1.h>
#include <MySQL_Packet.h>
#include <mysql.h>

char *host, *user, *pass, *db;

void setup()
host = “localhost”;
user = “root”;
pass = “sriram135$”;
db = “arduino”;

Serial.print("Connected to ");
// int y=is_mysql();
char INSERT_SQL = “INSERT INTO arduino_table(weight) VALUES(‘0001’)”;
//int result = mysql_query(“INSERT INTO arduino_table(weight) VALUES(‘0001’)”);
Serial.println(“Query exexcuted.”);
Serial.println(“Query exexcution failed.”);
Serial.println(“Connection failed.”);

void loop(){}

We are getting following error for the following when arduino ide while uploading:

Arduino: 1.8.7 (Windows 8.1), Board: “Generic ESP8266 Module, 80 MHz, Flash, ck, 26 MHz, 40MHz, QIO, 512K (no SPIFFS), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200”

Sketch uses 247964 bytes (49%) of program storage space. Maximum is 499696 bytes.
Global variables use 28044 bytes (34%) of dynamic memory, leaving 53876 bytes for local variables. Maximum is 81920 bytes.
warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed
error: espcomm_upload_mem failed

kindly help…


VeeraSekhar: Issue :connecting arduino uno to mysql database using esp8266 via the arduino ide sketch program.

That is useless information, without knowing what the issue is, and what your code looks like.

Please check now and help us to resolve

Is it an ESP-01 or ESP-12? Have a look at the "I am getting “espcomm_sync failed” error when trying to upload my ESP. How to resolve this issue?" article at ESP8266 docs

wifi module which we are using is .....ESP8266 Serial Port To Wi-Fi Module, FL-M1S

If it looks like this
It's an ESP-01 You need to put it into flashing mode to upload your sketch.

See this article.
It will give you a circuit you need to build (its really just a couple of buttons) and when to press the buttons to upload your software.


Thank you for providing the details,we will go through that article and will work accordingly.

Still getting the error as per attached,

error.txt (166 KB)

Okay. 1) You may have already burned out the ESP-01 if you connected according to your diagram. While you have resistors to drop the voltage to the RX pin on the ESP-01, you have no resistor on the CH-PD pin. If you drive pin 4 on the UNO high, you are supplying 5V to a 3.3V device.

2) The sketch you have, will do nothing. If you manage to upload it to the ESP, you will no longer have access to wifi as you will wipe out the existing firmware. To use that sketch on the ESP-01 you also need to include ESP8266WiFi.h and set up the device as a client or server.

Best to leave the firmware as is, and do everything on the UNO.

Load the WiFiEsp library on the UNO to treat the ESP as a wifi/ethernet shield, then run the MYSQL code on the UNO to connect to your server. The ESP device will not need to be re-programmed.

Also make sure that you can talk to the ESP-01. Use the instructions in the link I posted earlier to see if you can send AT commands via a terminal.

We are getting below error in serial monitor,

17:44:01.963 → [WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.0.0
[WiFiEsp] Connected to MAKARAND SYSTEMS
17:44:14.799 → Connected to network
My IP address is:
17:44:14.892 → Connecting…
[WiFiEsp] Connecting to
[WiFiEsp] TIMEOUT: 25
ERROR: Timeout waiting for client.
[WiFiEsp] >>> TIMEOUT >>>
17:44:22.148 → [WiFiEsp] Data packet send error (1)
17:44:22.148 → [WiFiEsp] Failed to write to socket 3
[WiFiEsp] Disconnecting 3

below is the code which we are working,

#include <WiFiEsp.h> //use for ESP8266
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

// Emulate Serial1 on pins 2/3 if not present
#include “SoftwareSerial.h”
SoftwareSerial Serial1(2, 3); // RX, TX

byte mac_addr = { 0x30, 0x3A, 0x64, 0xBB, 0x29, 0x0C };
IPAddress server_addr(192,168,1,5); // IP of the MySQL server here
char user = “karala”;
char password = “filmss21586”;

// WiFi card example
char ssid = “MAKARAND SYSTEMS”; // your network SSID (name)
char pass = “123456789”; // your network password

//WiFiClient client; // Use this for WiFi instead of EthernetClient
WiFiEspClient client; //Use this for ESP8266
MySQL_Connection conn((Client *)&client);

void setup() {
while (!Serial); // wait for serial port to connect. Needed for Leonardo only
// initialize serial for ESP module
// initialize ESP module

//Serial1.println(“Connecting to WiFi”);

// Begin WiFi section
int status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println(“Couldn’t get a wifi connection”);
// print out info about the connection:
else {
Serial.println(“Connected to network”);
IPAddress ip = WiFi.localIP();
Serial.print("My IP address is: ");
// End WiFi section

if(conn.connect(server_addr,3306,user,password)) {
Serial1.println(“Connected to mysql”);

Serial.println(“Connection failed.”);

void loop() {


It's good that you've managed to get the ESP working with the Arduino.

I cannot tell you why you can't connect to your server, and I have no experience in setting up such a server. All I can suggest is that you look at the MySQL connector user manual. On page 4 it talks about setting up user accounts. Make sure that you have a user account for the IP address of you ESP. eg

CREATE USER karala@'' IDENTIFIED BY 'filmss21586'

I also see that there is a note about the version of MySQL Server to use. It does say that if you're using a newer version of MySQL, you need to use the correct Authentication protocol.


Thank you for the inputs given...Now able to connect to mysql and able to insert the data :)

VeeraSekhar: Hi,

Thank you for the inputs given...Now able to connect to mysql and able to insert the data :)

Now, you should change your process, and create a PHP page on the server, and POST data to the PHP page, and have the PHP script insert the data into the database. More secure that way.

Thank you for help and suggestions,we are able to insert sensor data into our mysql database,

But Problem is load sensor weight is not accurate and it is showing some junk vaules…

Please help us in rectifying the issue…

Connections of arduino uno,load cell sensor and HX711 are as per attached

Instructions on how to use a loadcell.

Be sure to calibrate.

You have not posted your code or what errors you get, so I cannot offer any other help at this time.

PS Just noticed... Double check the wiring from loadCell to board. Find the datasheet for the LoadCell you have. That will have the color code of each wire.

Connections we did as per attached jpg file.

calibration factor Code is as per below.

Please suggest us if anything wrong in circuit connections or code.

#include “HX711.h”
#define DOUT 3
#define CLK 2
HX711 scale(DOUT, CLK);
float calibration_factor = -96650; //-7050 worked for my 440lb max scale setup
void setup() {
Serial.println(“HX711 calibration sketch”);
Serial.println(“Remove all weight from scale”);
Serial.println(“After readings begin, place known weight on scale”);
Serial.println(“Press + or a to increase calibration factor”);
Serial.println(“Press - or z to decrease calibration factor”);
scale.tare(); //Reset the scale to 0
long zero_factor = scale.read_average(); //Get a baseline reading
Serial.print("Zero factor: “); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
void loop() {
scale.set_scale(calibration_factor); //Adjust to this calibration factor
Serial.print(“Reading: “);
Serial.print(scale.get_units()*0.453592, 3);
Serial.print(” kg”); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
Serial.print(” calibration_factor: ");
char temp =;
if(temp == ‘+’ || temp == ‘a’)
calibration_factor += 10;
else if(temp == ‘-’ || temp == ‘z’)
calibration_factor -= 10;}}

What is the error that you are getting?

What reference weight are you using? The line Serial.print(scale.get_units()*0.453592, 3); is confusing to me why are you multiplying by 0.4? If it to convert between Imperial and SI, there is no need at all.

Do you have a datasheet or model number for the loadcell that you are using?

Model Number is YZC-131.

0.4 is the known weight on which we are trying to calculate calibration factor.

Please suggest to get exact calibration factor.

Try using a weight which is close to half of the loadcell capacity. So for a 1Kg loadcell, use 500g. For 5Kg use 2Kg or 2.5Kg etc.

Also try changing the line Serial.print(scale.get_units()*0.453592, 3); to Serial.print(scale.get_units(), 3);

Finally, the wiring of the loadcell depends on who made the loadcell in the first place. If the manufacturer did not include wire labels try swapping the white and green wires. (Test the code change above first!). I have found two different versions, using the same wire colours but with the green and white wires going to different terminals.

hi @VeeraSekhar

Can i get your source code please. i have the same issue and i cannot connect to the database by using esp8266..