7 years, 7 months ago.

Query about SQL client

I was reading this page, https://mbed.org/cookbook/MySQL-Client, about setting up a SQL client and I was wondering if i understood it correctly. With this could I send a constant stream of data to a local host on a PC using software like Xampp which has MYSQL? Also would this be connected by RJ45 to a router and if so where in the code would I say where the data should go?

#include "mbed.h"
#include "EthernetNetIf.h"
#include "MySQLClient.h"

#define SQL_SERVER   "192.168.0.1"
#define SQL_USER     "root"
#define SQL_PASSWORD "ecg"
#define SQL_DB       "database"

EthernetNetIf eth; 
MySQLClient sql;
....

Any help would be appreciated.

1 Answer

7 years, 7 months ago.

Yes you can connect to a MySQL server on another computer with this. Yes it will require the use of RJ45 cables and what ever network equipment needed. Your board seems to be an LPC1768 which has on board Ethernet. You'll need something like SparkFun's RJ45 breakout kit.

Where should the data go? That is what the following #defines are for

  1. define SQL_SERVER "192.168.0.1"
  2. define SQL_USER "root" ng defines are #define SQL_PASSWORD "....."
  3. define SQL_DB "database"

connect to the MySQL database called "database" on the computer w. the ip address 192.168.0.1

You could change this to be any ip address on the internet. and hence in principle connect to any database anywhere.

Thanks for the quick reply. What I was wondering was would I always connect the RJ45 cable to a router or could I connect it directly to a PC? If I would want to connect to a ip address across the internet how would I go about that in the code? What I have so far is the following, however after a few seconds I get the lights of death, can anyone see the problem? My database is called database, table -> ecgvalues, column -> values

#include "mbed.h"
#include "EthernetNetIf.h"
#include "MySQLClient.h"

#define SQL_SERVER   "192.168.0.1"
#define SQL_USER     "root"
#define SQL_PASSWORD "ecg"
#define SQL_DB       "database"

EthernetNetIf eth; 
MySQLClient sql;

AnalogIn Ain(p20);
float data_in;

MySQLResult sqlLastResult;
void onMySQLResult(MySQLResult r)
{
  sqlLastResult = r;
}

int main()
{
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    return -1;
  }
  
  Host host(IpAddr(), 3306, SQL_SERVER);
  
  //Connect
  sqlLastResult = sql.open(host, SQL_USER, SQL_PASSWORD, SQL_DB, onMySQLResult);
  while(sqlLastResult == MYSQL_PROCESSING)
  {
    Net::poll();
  }
  if(sqlLastResult != MYSQL_OK)
  {
    printf("Error %d during connection\n", sqlLastResult);
  }
  
  //SQL Command
  //Make command
  char cmd[128] = {0};
  data_in = Ain;
  sprintf(cmd, "INSERT INTO ecgvalues (values) VALUES('%f')", data_in);
  
  //INSERT INTO DB
  string cmdStr = string(cmd); 
  sqlLastResult = sql.sql(cmdStr);
  while(sqlLastResult == MYSQL_PROCESSING)
  {
    Net::poll();
  }
  if(sqlLastResult != MYSQL_OK)
  {
    printf("Error %d during SQL Command\n", sqlLastResult);
  }
  
  sql.exit();
  
  while(1)
  {
  
  }
  
  return 0;
}
posted by Johann Hur 12 Jun 2014