ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
331 stars 132 forks source link

MySQL Connector is half or full-duplex communication #92

Closed 3rc0 closed 2 years ago

3rc0 commented 5 years ago

In MySQL_Connector_Arduino_Reference_Manual.pdf is mentioned that possible to assign an IP address for the Ethernet Module too by following the Ethernet.h library, so I tried to do it by W5500 and just adding ip to initialize Ethernet at Ethernet.begain() the MySQL connecting will fail.

Example,


#include  <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3f, 20, 4);

const byte rowsKeypad = 4;
const byte columnsKeypad = 4;
char keys[rowsKeypad][columnsKeypad] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowKeypadPins[rowsKeypad] = {25, 24, 23, 22};
byte columnKeypadPins[columnsKeypad] = {29, 28, 27, 26};
Keypad customKeypad(makeKeymap(keys), rowKeypadPins, columnKeypadPins, rowsKeypad, columnsKeypad);
char customKey;

#define passLength 3
char Data[passLength];
byte data_count = 0;

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
IPAddress server_addr(192, 168, 0, 16); // IP of the MySQL *server* here
char user[] = "root";              // MySQL user login username
char password[] = "123456";        // MySQL user login password

EthernetServer server(80);
EthernetClient clientWebServer;

EthernetClient client;
MySQL_Connection conn((Client *)&client);
// Create an instance of the cursor passing in the connection
MySQL_Cursor cur = MySQL_Cursor(&conn);

// Sample query
char querySelect[] = "SELECT num2 FROM dbName.tableName WHERE num2 =%d";
char query[128];

//Database Name: dbName
//Table Name:  tableName
//  (num1, num2)
//    | 1 | 44 |
//    | 2 | 55 |

void setup() {
  Serial.begin(115200);
  while (!Serial); // wait for serial port to connect
  Ethernet.begin(mac_addr,ip);
  Serial.println("Connecting...");
  if (conn.connect(server_addr, 3306, user, password)) {
    delay(1000);
  }
  else
    Serial.println("Connection failed.");
 server.begin();
 Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  lcd.init();  // Initialize the LCD screen.
  lcd.backlight();
}

void loop() {
  lcd.setCursor(0, 0);
  lcd.print("Enter Password:");

  customKey = customKeypad.getKey();
  userInputFunction();
  webServerFunction();
}

void webServerFunction(){

  if (clientWebServer)
  {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (clientWebServer.connected()) {
      if (clientWebServer.available()) {
        char c = clientWebServer.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          clientWebServer.println("HTTP/1.1 200 OK");
          clientWebServer.println("Content-Type: text/html");
          clientWebServer.println("Connection: close");  // the connection will be closed after completion of the response
          clientWebServer.println("Refresh: 5");  // refresh the page automatically every 5 sec
          clientWebServer.println();
          clientWebServer.println("<!DOCTYPE HTML>");
          clientWebServer.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            clientWebServer.print("analog input ");
            clientWebServer.print(analogChannel);
            clientWebServer.print(" is ");
            clientWebServer.print(sensorReading);
            clientWebServer.println("<br />");
          }
          clientWebServer.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    clientWebServer.stop();
    Serial.println("client disconnected");
  }
}

void userInputFunction()
{
  if (customKey) {
    Data[data_count] = customKey;
    lcd.setCursor(data_count, 1);
    lcd.print(Data[data_count]);
    data_count++;
  }

  if (data_count == passLength - 1) {

    row_values *row = NULL;
    long head_count = 0;

    delay(1000);

    MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
    // Execute the query

    int storedPass = atol(Data);
    Serial.println(storedPass);
    sprintf(query, querySelect, storedPass);
    cur_mem->execute(query);
    column_names *columns = cur_mem->get_columns();
    do {
      row = cur_mem->get_next_row();
      if (row != NULL) {
        head_count = atol(row->values[0]);
      }
    } while (row != NULL);
    delete cur_mem;

    // Show the result
    Serial.print("  num2= ");
    Serial.println(head_count);

    lcd.clear();
    if (!strcmp(storedPass, head_count)) {
      lcd.print("Correct");
      delay(5000);
    }
    else {
      lcd.print("Incorrect");
      delay(1000);
    }

    lcd.clear();
    clearData();
  }
}

void clearData() {
  while (data_count != 0) {
    Data[data_count--] = 0;
  }
  return;
}
ChuckBell commented 5 years ago

Ok, a couple of things...

First, are you sure the 0 in the server address is correct? Check that again.

Next, to answer your questions...

Q) Does MySQL Connector (Ethernet.begin()) initialize only for the physical address and pass it to the MySQL? because a web server needs to initialize physical and logical addresses Ethernet.begin(mac_addr,ip); !! https://www.arduino.cc/en/Reference/EthernetIPAddress

A) The connector does nothing of the sort. All of that takes place in the Ethernet class. Check the documentation for the Ethernet class you are using.

Q) Does MySQL Connector half or full-duplex connection?

A) Again, the connector isn't responsible for this. NAICT, it occurs at either the hardware layer or deep into the Ethernet class(es). Also NAICT, most networking Arduino boards support both.

Q) Does MySQL have own Router Table?

A) Nope. Relies completely on the underlying platform.