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.