Skip to content

New Arduino Code for DS18B20 and MySQL

2010 Juli 20
by Jan


I just cleaned the code a little bit. Here´s the new code which will work better. It stops somehow after ~18h, and i think it has something to do with an overflow in the variable which stores the “delay”…will work this out later. Here´s the code:

#include <Ethernet.h>           //library for ethernet functions
#include <Client.h>             //library for client functions
#include <DallasTemperature.h>  //library for temperature sensors
#include <OneWire.h>            //library for the onewire bus

#define ONE_WIRE_BUS     7        //the onewire bus is connected to pin 7 on arduino
#define TEMPERATURE_PRECISION  10 //resolution of the sensors is set to 10bit(somehow won´t work)

// Ethernet settings
uint8_t hwaddr[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xBE}; // mac-adress of arduino
uint8_t ipaddr[4] = {192, 168, 0, 17};                    // IP-adress of arduino
uint8_t gwaddr[4] = {192, 168, 0, 1};                     // IP-adress of gateway ( for later DNS implementation)
uint8_t subnet[4] = {255, 255, 255, 0};                   // subnetmask           ( for later DNS implementation)
uint8_t serverip[4] = {192, 168, 0, 4};                   // IP-adress of server arduino sends data to
uint8_t serverport = 80;                                  // the port the arduino talks to

Client client(serverip, serverport);         // make a new instance from type "Client" named "client", giving it
                                             // serverip and serverport

OneWire oneWire(ONE_WIRE_BUS);               // setup a oneWire instance to communicate with any OneWire devices
                                             // (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire);         // Pass our oneWire reference to Dallas Temperature
                                             // variable to store the number of sensors
bool connected = 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 only at the startup of the arduino)
Serial.begin(9600);                               // start the serial port
Serial.println("I2C-to-Ethernet Bridge.");
Serial.println("Initializing Ethernet.");

Ethernet. begin(hwaddr, ipaddr);                  // start up ethernet
sensors.begin();                                  // start up the library
int numSensors = sensors.getDeviceCount();        // store the number of sensors to the variable numSensors,
int temparray[numSensors];            // array with "numSensors" storage places for the temperature of each sensor
                                      // "sensors.getDeviceCount" is a function in the library

Serial.println("Enumerating and scanning for I2C sensors.");

if(numSensors > 0)                      // if there is at least one sensor:
Serial.print("Enumerated ");            //print the number of sensors to serial port
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();
int temparray[numSensors];          // array with "numSensors" storage places for the temperature of each sensor
                                    // "sensors.getDeviceCount" is a function in the dallas temperature library

if (client.connect())
{                                   // if connected, set variable connected to "true" and
connected = true;
sensors.requestTemperatures();      // send the request for temperature to sensors (all sensors)
for(i=0; i<numSensors; i++)         // as long as "i" ( chosen number, starts at 0) is smaller than
                                    //numSensors" (number of sensors) do the "for" cycle
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 /writetemplocalserv.php?");
Serial.print("GET /writetemplocalserv.php?");
for (i=0; i<numSensors; i++)
if (i < numSensors-1)

client.println(" HTTP/1.1");
Serial.println(" HTTP/1.1");
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");

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

client.stop();                                              //  stop the connection and set
connected = false;                                          //  "connected" to false

Ant this is how one possible Output should look like (not actual data yet) : is the adress of my server which generated this graphic. I now have to pull the data from the database into the script so that it actually shows me the status of my sensors and not some fictional data…

I´m working on it ;)

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