Skip to content

Code updated for Arduino 1.0.1

2013 Mai 16
by Jan

Hey all, just wanted to say that i updated the code so it compiles and uploads in the 1.0.1 IDE. The Arduino guys changed the function name “Client” to “EthernetClient” in the ethernet libraries, and i had to get the updates library for the Dallas Temperature sensors. Miles Burton is doing a good job with this: http://milesburton.com/Dallas_Temperature_Control_Library

Here’s the Code. It reads the sensor data, connects to the server and sends the data nicely.


#include //library for ethernet functions
#include
#include //library for temperature sensors
#include //library for the onewire bus

#define Client EthernetClient
#define ONE_WIRE_BUS 7
#define TEMPERATURE_PRECISION 9

// Ethernet settings
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // mac-adress of arduino
byte ip[] = {192, 168, 0, 177}; // IP-adress of arduino
//byte gwaddr[] = {192, 168, 0, 1}; // IP-adress of gateway for later DNS implementation
//byte subnet[] = {255, 255, 255, 0}; // subnetmask for later DNS implementation
byte server[] = {192, 168, 0, 105}; // IP-adress of server arduino sends data to

EthernetClient client; // make a new instance from type "Client"

OneWire oneWire(ONE_WIRE_BUS); //the onewire bus is connected to pin 7 on arduino
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature

int numSensors; // variable to store the number of sensors

bool connectedn = false; // yes-no variable (boolean) to store if the arduino is connected to the server
int i = 0; // variable to count the sendings to the server

void setup(void) // setup-function runs at the startup of the arduino
{

Serial.begin(9600); // start the serial port service
Serial.println("1-Wire-to-Ethernet Bridge.");
Serial.println("Initializing Ethernet.");
Ethernet. begin(mac, ip); // start up ethernet with defined mac and ip
sensors.begin(); // start up the library

Serial.println("Scanning for 1-Wire sensors.");

int numSensors = sensors.getDeviceCount(); // store the number of sensors to numSensors

if(numSensors > 0) // if there is at least one sensor:
{
Serial.print("Enumerated "); //print the number of sensors to serial port
Serial.print(numSensors);
Serial.println( " sensors.");

}
else //if there is no sensor:
{
Serial.println("No sensors enumerated."); // tell the serial port
}

}

void loop(void) // loop function runs over and over again
{
int numSensors = sensors.getDeviceCount();
delay(500);
int temparray[numSensors]; // array with "numSensors" storage places for the temperature of each sensor

if(!connectedn) { // if "not" connected print: not connected ;)
Serial.println("Not connected");
Serial.print("Connecting");
delay(300);
Serial.print(".");
delay(300);
Serial.print(".");
delay(300);
Serial.println(".");

if (client.connect(server, 80)){ // if connected, set variable connected to "true"
connectedn = true;
sensors.requestTemperatures(); // send the request for temperature to sensors (all sensors)
delay(200);
Serial.println("Connected to Server");
for(i=0; i {
int temp = sensors.getTempCByIndex(i); /*take temperature reading from sensor "i" and store it to the variable "temp"*/
temparray[i] = temp; // store the temperature from sensor i to storage place i in the array
}

client.print("GET /input.php?");
Serial.print("GET /input.php?");
for (i=0; i {
client.print("t");
Serial.print("t");
client.print(i);
Serial.print(i);
client.print("=");
Serial.print("=");
client.print(temparray[i]);
Serial.print(temparray[i]);
if (i < numSensors-1)
{
client.print("&&");
Serial.print("&&");
}
else
{
}
}

client.println(" HTTP/1.1");
Serial.println(" HTTP/1.1");
client.println("Host: blog.jfkreuter.com");
Serial.println("Host: blog.jfkreuter.com");
client.println("User-Agent: Arduino");
Serial.println("User-Agent: Arduino");
client.println("Accept: text/html");
Serial.println("Accept: text/html");
client.println("Connection: close");
Serial.println("Connection: close");
client.println();
Serial.println();

}
else{
Serial.println("Cannot connect to Server"); // else block if the server connection fails

}
}
else {

delay(500);
while (client.connected() && client.available()) { // when connected and availabe:
char c = client.read(); // read the answer of the server and
Serial.print(c); // print it to serial port
}
Serial.println();
client.stop(); // stop the connection and set
connectedn = false; // "connected" to false

}
delay(300000); //somebody told me i can do the delay with interrupts...
}

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS