Skip to main content
deleted 28 characters in body
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

I started a project with my Arduino MEGA2560Mega 2560 that getgets some values from sensors. I'm using MySQL Connector, https://launchpad.net/mysql-arduino, and this library allows me to connect to a server using the IP address, and it worked on localhost.

/* 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 address must be unique; this is the Ethernet shield address
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 degree 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 complement
  float TemperatureSum = tempRead / 16;
 
  return TemperatureSum;
}

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

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 library allows me to connect to a server using the IP address, and it worked on localhost.

/* 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 address must be unique; this is the Ethernet shield address
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 degree 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 complement
  float TemperatureSum = tempRead / 16;
 
  return TemperatureSum;
}

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

I started a project with my Arduino Mega 2560 that gets some values from sensors. I'm using MySQL Connector, https://launchpad.net/mysql-arduino, and this library allows me to connect to a server using the IP address, and it worked on localhost.

/* 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 address must be unique; this is the Ethernet shield address
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 degree 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 complement
  float TemperatureSum = tempRead / 16;
  return TemperatureSum;
}

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

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/MySQL> and <http://en.wikipedia.org/wiki/Uniform_resource_locator>).
Source Link

How todo I connect arduinoArduino directly to mysqlMySQL server using the urlURL?

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

            /* 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 MAC address must be unique,unique; this is the ethernetEthernet shield addraddress
            IPAddress server_addr( 177, 156, 122, 113 ); // serverServer 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);  // onOn digital pin 2


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


            /*Setup* Setup for connection and all variables of the database*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 DEGdegree 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++) { // weWe need 9 bytes
                data[i] = ds.read();
              }
              
              ds.reset_search();
              
              byte MSB = data[1];
              byte LSB = data[0];

              float tempRead = ((MSB << 8) | LSB); //using Using two's complimentcomplement
              float TemperatureSum = tempRead / 16;
              
              return TemperatureSum;
            }

Now, how can I connect to a server using the URL? Is there anya library like MysqlMySQL 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 connectorMySQL Connector, but using the URL of the server instead of the IP address.

How to connect arduino directly to mysql server using the url?

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.

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

How do I connect Arduino directly to MySQL server using the URL?

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 library allows me to connect to a server using the IP address, and it worked on localhost.

/* 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 address must be unique; this is the Ethernet shield address
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 degree 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 complement
  float TemperatureSum = tempRead / 16;

  return TemperatureSum;
}

Now, how can I connect to a server using the URL? Is there a 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 of the IP address.

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 1 character in body
Source Link
            /* 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);

              sprintfsnprintf(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;
            }
            /* 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);

              sprintf(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;
            }
            /* 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;
            }
Added tags, fixed grammar.
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126
Loading
added 56 characters in body
Source Link
Loading
Source Link
Loading