Sign up ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I started a project with my arduino MEGA2560 that get some values from sensors. I'm using Mysql Connector, https://launchpad.net/mysql-arduino , and this lib allow me to connect to a server using the IPAddress and it worked on localhost.

This is the code:

            /* Setup for all libraries */
            #include <SPI.h>
            #include <Ethernet.h>
            #include <sha1.h>
            #include <mysql.h>
            #include <OneWire.h>


            /* Setup for Ethernet Library */
            byte mac_addr[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xB3, 0x05 }; //mac must be unique, this is the ethernet shield addr
            IPAddress server_addr( 177, 156, 122, 113 ); // server addr


            /* Setup for all others variables */
            int DS18S20_Pin = 2;//Temperature sensor
            float temperature;
            float placasTensao;
            float placasCorrente;
            float geradorEolicoTensao;
            float geradorEolicoCorrente;
            float bateriaTensao;
            float bateriaTemp;
            float correnteSaidaControlador;
            float correnteSaidaConsumo;

            OneWire ds(DS18S20_Pin);  // on digital pin 2


            /* Setup for the Connector/Arduino */
            Connector my_conn; // The Connector/Arduino reference


            /*Setup for connection and all variables of the database*/
            char user[] = "";
            char password[] = "";
            char INSERT_SQL[1000];
            char temperature_convt[10], placasTensao_convt[10], placasCorrente_convt[10], geradorEolicoTensao_convt[10],
                 geradorEolicoCorrente_convt[10], bateriaTensao_convt[10], bateriaTemp_convt[10], correnteSaidaControlador_convt[10],
                 correnteSaidaConsumo_convt[10];


            void setup(){
              Ethernet.begin(mac_addr);
              Serial.begin(9600);
              delay(1000);
            }

            void loop(){
              temperature = getTemp();
              convert(temperature, temperature_convt);

              snprintf(INSERT_SQL, "INSERT INTO data.data VALUES (%s, 1, 1, 1, 1, 1, 1, 1, NULL)", temperature_convt);

              sendData();
              delay(20000);
            }


            //Convert a float variable to string
            void convert(float data, char *value){
              dtostrf(data, 7, 3, value);
            }


            //Saves all data on database
            void sendData(){
              Serial.println("Connecting...");
              if (my_conn.mysql_connect(server_addr, 3306, user, password)){
                delay(500);
                Serial.println("Starting SQL!");
                Serial.println(INSERT_SQL);
                my_conn.cmd_query(INSERT_SQL);
                Serial.println("Query Success!");
                my_conn.disconnect();
                Serial.println("\n");
              } else {
                Serial.println("Connection failed!");
                Serial.println("Make sure all cables are properly connected!\nIf they are, restart the Arduino!\n");
              }
            }


            //Returns the temperature from one DS18S20 in DEG Celsius
            float getTemp(){
              byte data[12];
              byte addr[8];

              if ( !ds.search(addr)) {
                  //No more sensors on chain, reset search
                  ds.reset_search();
                  return -1000;
              }

              if ( OneWire::crc8( addr, 7) != addr[7]) {
                  Serial.println("CRC is not valid!");
                  return -1000;
              }

              if ( addr[0] != 0x10 && addr[0] != 0x28) {
                  Serial.print("Device is not recognized");
                  return -1000;
              }

              ds.reset();
              ds.select(addr);
              ds.write(0x44,1); // Start conversion, with parasite power on at the end

              byte present = ds.reset();
              ds.select(addr);    
              ds.write(0xBE); // Read Scratchpad


              for (int i = 0; i < 9; i++) { // we need 9 bytes
                data[i] = ds.read();
              }

              ds.reset_search();

              byte MSB = data[1];
              byte LSB = data[0];

              float tempRead = ((MSB << 8) | LSB); //using two's compliment
              float TemperatureSum = tempRead / 16;

              return TemperatureSum;
            }

Now, how can I connect to a server using the URL? Is there any library like Mysql Connector that allow me to use the URL instead the IP address?

All I want is to do exactly the same thing that I'm doing with Mysql connector, but using the URL of the server instead the IP address.

share|improve this question

1 Answer 1

You are using DHCP, so this step might be optional. Specify your network addresses (actually we want to specify DNS only but have to specify IP in 2nd argument, so likely have to specify gateway and subnet too):

IPAddress dns_addr( 8, 8, 8, 8); // GOOGLE DNS server, could be any DNS
IPAddress gateway_addr( , , , );   // put your gateway addr
IPAddress ip_addr( , , , );   // put your IP addr
IPAddress subnet( , , , );   // put your subnet mask

void setup(){
                  Ethernet.begin(mac_addr, ip_addr, dns_addr, gateway_addr, subnet);...

Now you could modify Connector::mysql_connect method (or create new method) as follows (only function declaration is changed, body is not):

boolean Connector::mysql_connect(/*IPAddress server*/const char *server, int port, char *user, char *password)
{
  int connected = 0;
  int i = -1;

  // Retry up to MAX_CONNECT_ATTEMPTS times 1 second apart.
  do {
    delay(1000);
    connected = client.connect(server, port);
    i++;
  } while (i < MAX_CONNECT_ATTEMPTS && !connected);

  if (connected) {
    read_packet();
    parse_handshake_packet();
    send_authentication_packet(user, password);
    read_packet();
    if (check_ok_packet() != 0) {
      parse_error_packet();
      return false;
    }
    print_message(CONNECTED);
    Serial.println(server_version);
    free(server_version); // don't need it anymore
    return true;
  }
  return false;
} 

Modify the header file also:

boolean mysql_connect(/*IPAddress server*/const char *server, int port,
                                 char *user, char *password)

Now you can supply URL:

if (my_conn.mysql_connect("http:\\www.mysqlserver.com", 3306, user, password))...
share|improve this answer
    
I did everything. Modified the Connector::mysql_connect method and also the header. But it doesn't work @Flanker. Do you know what could be causing this? When the program call the my_conn.mysql_connect(), it just stop there, without any answer, appear just my message "Connecting...". Thank you for the answer. – leraningLearning Sep 29 at 19:15
    
Do you have "http://" protocol part in your URL? Please try without it, like "www.domain...". The Ethernet client have same connect function with URL parameter instead IP (and port), the idea is to use it. Also maybe DNS is not working, maybe test the network address configuration with another DNS sample sketch? – Flanker Sep 30 at 22:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.