# Issue:connecting arduino uno to mysql database using esp8266

**URL:** <https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980>\
**Category:** Programming\
**Created:** [October 25, 2018, 11:01am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980 "2018-10-25T11:01:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![VeeraSekhar](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@VeeraSekhar](https://forum.arduino.cc/u/VeeraSekhar)\
**Post date:** [October 25, 2018, 11:01am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/1 "2018-10-25T11:01:53Z")

</div>

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.

Software:  
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()  
{  
Serial.begin(9600);  
host = "localhost";  
user = "root";  
pass = "sriram135$";  
db = "arduino";  
if(is\_mysql(host,user,pass,db)){

Serial.print("Connected to ");  
Serial.println(host);  
// 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')");  
if(INSERT\_SQL){  
Serial.println("Query exexcuted.");  
}else{  
Serial.println("Query exexcution failed.");  
}  
}else{  
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...

![diagram.png](https://europe1.discourse-cdn.com/arduino/original/4X/2/f/b/2fbe17c0aec75d13870d9fd17158c3e88e6059dc.png)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 25, 2018, 11:11am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/2 "2018-10-25T11:11:30Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![VeeraSekhar](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@VeeraSekhar](https://forum.arduino.cc/u/VeeraSekhar)\
**Post date:** [October 25, 2018, 11:43am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/3 "2018-10-25T11:43:45Z")

</div>

Please check now and help us to resolve

---

<div class="post-metadata">

**Author:** ![darrob](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@darrob](https://forum.arduino.cc/u/darrob)\
**Post date:** [October 25, 2018, 11:53am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/4 "2018-10-25T11:53:41Z")

</div>

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?](https://arduino-esp8266.readthedocs.io/en/latest/faq/a01-espcomm_sync-failed.html)" article at ESP8266 docs

---

<div class="post-metadata">

**Author:** ![VeeraSekhar](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@VeeraSekhar](https://forum.arduino.cc/u/VeeraSekhar)\
**Post date:** [October 25, 2018, 12:20pm UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/5 "2018-10-25T12:20:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![darrob](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@darrob](https://forum.arduino.cc/u/darrob)\
**Post date:** [October 25, 2018, 12:33pm UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/6 "2018-10-25T12:33:46Z")

</div>

If it looks like this  
 ![esp8266-serial-esp-01-wifi-wireless-transceiver-module-500x500.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/4/9/d/49dd3d403744d16fdfdcc4dc20e2e8c53c71fb35.jpeg)  
It's an ESP-01 You need to put it into flashing mode to upload your sketch.

See [this](https://www.allaboutcircuits.com/projects/flashing-the-ESP-01-firmware-to-SDK-v2.0.0-is-easier-now/) 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.

![esp8266-serial-esp-01-wifi-wireless-transceiver-module-500x500.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/4/9/d/49dd3d403744d16fdfdcc4dc20e2e8c53c71fb35.jpeg)

---

<div class="post-metadata">

**Author:** ![VeeraSekhar](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@VeeraSekhar](https://forum.arduino.cc/u/VeeraSekhar)\
**Post date:** [October 25, 2018, 12:48pm UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/7 "2018-10-25T12:48:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![VeeraSekhar](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@VeeraSekhar](https://forum.arduino.cc/u/VeeraSekhar)\
**Post date:** [October 26, 2018, 5:22am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/8 "2018-10-26T05:22:34Z")

</div>

Still getting the error as per attached,

[error.txt](https://forum.arduino.cc/uploads/short-url/8rAres3INpc7cNnjdh9f4vHRgil.txt) (166 KB)

---

<div class="post-metadata">

**Author:** ![darrob](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@darrob](https://forum.arduino.cc/u/darrob)\
**Post date:** [October 26, 2018, 6:04am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/9 "2018-10-26T06:04:11Z")

</div>

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](https://github.com/bportaluri/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.

---

<div class="post-metadata">

**Author:** ![VeeraSekhar](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@VeeraSekhar](https://forum.arduino.cc/u/VeeraSekhar)\
**Post date:** [October 31, 2018, 12:19pm UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/10 "2018-10-31T12:19:17Z")

</div>

We are getting below error in serial monitor,

Starting....  
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: 192.168.1.2  
17:44:14.892 -\> Connecting...  
[WiFiEsp] Connecting to 192.168.1.5  
[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  
#ifndef HAVE\_HWSERIAL1  
#include "SoftwareSerial.h"  
SoftwareSerial Serial1(2, 3); // RX, TX  
#endif

byte mac\_addr[] = { 0x30, 0x3A, 0x64, 0xBB, 0x29, 0x0C };  
[//117.193.105.210](https://117.193.105.210)  
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() {  
Serial.begin(115200);  
while (!Serial); // wait for serial port to connect. Needed for Leonardo only  
// initialize serial for ESP module  
Serial.println("Starting....");  
Serial1.begin(9600);  
// initialize ESP module  
WiFi.init(&Serial1);

[//Serial1.println](https://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");  
while(true);  
}  
// print out info about the connection:  
else {  
Serial.println("Connected to network");  
IPAddress ip = WiFi.localIP();  
Serial.print("My IP address is: ");  
Serial.println(ip);  
}  
// End WiFi section

Serial.println("Connecting...");  
if(conn.connect(server\_addr,3306,user,password)) {  
delay(1000);  
Serial1.println("Connected to mysql");

}  
else  
Serial.println("Connection failed.");  
conn.close();  
}

void loop() {  
}

---

<div class="post-metadata">

**Author:** ![darrob](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@darrob](https://forum.arduino.cc/u/darrob)\
**Post date:** [October 31, 2018, 4:24pm UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/11 "2018-10-31T16:24:51Z")

</div>

Hi

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](https://github.com/ChuckBell/MySQL_Connector_Arduino/blob/master/extras/MySQL_Connector_Arduino_Reference_Manual.pdf). 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

```cpp
CREATE USER karala@'92.168.1.2' 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.

---

<div class="post-metadata">

**Author:** ![VeeraSekhar](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@VeeraSekhar](https://forum.arduino.cc/u/VeeraSekhar)\
**Post date:** [November 1, 2018, 11:51am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/12 "2018-11-01T11:51:05Z")

</div>

Hi,

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 1, 2018, 12:02pm UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/13 "2018-11-01T12:02:11Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![VeeraSekhar](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@VeeraSekhar](https://forum.arduino.cc/u/VeeraSekhar)\
**Post date:** [November 12, 2018, 11:48am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/14 "2018-11-12T11:48:04Z")

</div>

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

 ![Load_Cell_Interface_Arduino.jpg](https://europe1.discourse-cdn.com/arduino/original/3X/6/5/6512c57ae3109b6ce5f041e2033ba2881aab02dd.jpg)

---

<div class="post-metadata">

**Author:** ![darrob](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@darrob](https://forum.arduino.cc/u/darrob)\
**Post date:** [November 12, 2018, 12:45pm UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/15 "2018-11-12T12:45:48Z")

</div>

[Instructions](https://www.instructables.com/id/Arduino-Scale-With-5kg-Load-Cell-and-HX711-Amplifi/) 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.

---

<div class="post-metadata">

**Author:** ![VeeraSekhar](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@VeeraSekhar](https://forum.arduino.cc/u/VeeraSekhar)\
**Post date:** [November 22, 2018, 4:59am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/16 "2018-11-22T04:59:42Z")

</div>

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.begin(38400);  
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.set\_scale();  
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.  
Serial.println(zero\_factor);}  
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: ");  
Serial.print(calibration\_factor);  
Serial.println();  
if(Serial.available()){  
char temp = Serial.read();  
if(temp == '+' || temp == 'a')  
calibration\_factor += 10;  
else if(temp == '-' || temp == 'z')  
calibration\_factor -= 10;}}

 ![Load_Cell_Interface_Arduino.jpg](https://europe1.discourse-cdn.com/arduino/original/3X/6/5/6512c57ae3109b6ce5f041e2033ba2881aab02dd.jpg)

---

<div class="post-metadata">

**Author:** ![darrob](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@darrob](https://forum.arduino.cc/u/darrob)\
**Post date:** [November 23, 2018, 12:00am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/17 "2018-11-23T00:00:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![VeeraSekhar](https://avatars.discourse-cdn.com/v4/letter/v/c0e974/32.png) [@VeeraSekhar](https://forum.arduino.cc/u/VeeraSekhar)\
**Post date:** [November 23, 2018, 9:08am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/18 "2018-11-23T09:08:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![darrob](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@darrob](https://forum.arduino.cc/u/darrob)\
**Post date:** [November 23, 2018, 9:57am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/19 "2018-11-23T09:57:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Adithyawije](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Adithyawije](https://forum.arduino.cc/u/Adithyawije)\
**Post date:** [April 1, 2019, 5:51am UTC](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980/20 "2019-04-01T05:51:16Z")

</div>

hi @VeeraSekhar

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

[Next page](https://forum.arduino.cc/t/issue-connecting-arduino-uno-to-mysql-database-using-esp8266/552980.md?page=2)
