Connect NodeMCU and send DHT Sensor data to MYSQL Directly

#include <dht.h>


#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

#define dht_apin 0 
dht DHT;
float get_temperature,get_humidity;
//192,168,1,34
IPAddress server_addr(127,0,0,1); // IP of the MySQL server here
char user[] = "root"; // MySQL user login username
char password[] = ""; // MySQL user login password

// WiFi card example
//char ssid[] = "Adi Wifi"; // your SSID
//char pass[] = "adithya123"; // your SSID Password
const char* ssid        = "Adi Wifi";
const char* pwd         = "adithya123";
// Sample query
char INSERT_SQL[] = "INSERT INTO temps(`temp`, `humidity`) VALUES(get_temperature,get_humidity)";

WiFiClient client;
MySQL_Connection conn((Client *)&client);

void setup() {
  /*  
Serial.begin(115200);
// Begin WiFi section
WiFi.begin(ssid, pass);
// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );



*/

  Serial.begin(115200); //initialise the serial communication
  delay(20);
  WiFi.begin(ssid, pwd);

    while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  
    //starting the server
    //server.begin();


}
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
// End WiFi section
Serial.println("DB - Connecting...");
while (conn.connect(server_addr, 3306, user, password) != true) {
delay(500);
Serial.println( "not connected" );
}
}

void loop() {
delay(2000);

Serial.println("Recording data.");
DHT.read11(dht_apin);
    get_temperature =   DHT.temperature;
    get_humidity    =   DHT.humidity;
    Serial.print("Temperature");
    Serial.print(get_temperature);
    Serial.print("Humidity");
    Serial.print(get_humidity );

    
    


// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(INSERT_SQL);
// Note: since there are no results, we do not need to read any data
// Deleting the cursor also frees up memory used
delete cur_mem;
}

Hi, This is my code to connect mysql directly using NODEMCU. but i cannot to the database please help!!!

In my serial monitor i get the error not connected according to this piece of code
{
Serial.println(“DB - Connecting…”);
while (conn.connect(server_addr, 3306, user, password) != true) {
delay(500);
Serial.println( “not connected” );
}
}

IPAddress server_addr(127,0,0,1); // IP of the MySQL server here

You clearly have no clue what this address means. It is NOT the address where your MySQL server is running, unless you have ported MySQL to the NodeMCU. Which I seriously doubt.

Hi,

Thank you for replying i am a newbie. um just started learning arduino and can please clarify reply little more can you please send me a link to study this more sir.

Thanks in advance.

IP 127.0.0.1 is called a loopback address. It is the address that represents the computer itself. Using it in this manner suggests that the database is on the same computer that your code is running on...the arduino, which it's not. You'll need to start with the FQDN address of the computer your database is on.

hi @jasonwnc ,

I got the ip using cmd command "nslookup http://localhost:8081/phpmyadmin/" this gave me two ips and i entered them also but its still not connecting to the database !!

Please help me

Assuming you've now got your IP address correct, you may need to change config on the mysql database: by default, it will only accept connections from the localhost. Google mysql bind_address for more details.

Alternatively, you can create a web page on the server that is provided the sensor data via a get request and it can take care of pushing data to the database. This is a common solution if you can't (or don't want to) change the configuration.

Hi Posted by wildbill,

how do i change the config did you mean that i need to change MySQL database configuration right. but can you tell me that what are the features that i need to correct please...

Thank you in advance..

Your MySQL database is running on some machine. Log onto that machine, and use ipconfig to get the address. ANYTHING you do with localhost in the name is a waste of time.