Go Down

Topic: Temperature Data using DS18B20 sensor (Read 1 time) previous topic - next topic


Sep 27, 2012, 07:17 pm Last Edit: Sep 27, 2012, 07:19 pm by chris87 Reason: 1
Hi guys. I am trying to adjust an arduino code that reads temperature data from a DS18B20 sensor and a code that can store things to a phpMyadmin database and i am a little confused. I hope i ask in the right part of the forum

Arduino Code that measures Temperature

Code: [Select]
#include <SPI.h>

#include <Ethernet.h>           //library for ethernet functions
#include <HTTPClient.h>             //library for client functions
[u]#include <DallasTemperature.h>[/u]  //library for temperature sensors

[u]#include <OneWire.h>[/u]          //library for the onewire bus

[u]#define ONE_WIRE_BUS     2[/u]    //the onewire bus is connected to pin 2 on arduino
[u]#define TEMPERATURE_PRECISION  10[/u] //resolution of the sensors is set to 10bit

// Ethernet settings
uint8_t hwaddr[6] = {
 xxxx, xxxx, xxxx, xxxx, xxxx, xxxx};
uint8_t ipaddr[4] = {
 xxx, xxx, x, xx };                    
uint8_t gwaddr[4] = {
 xxx, xxx, x, xx};                    
uint8_t subnet[4] = {
 xxx, xxx, x, xx};                  
uint8_t serverip[4] = {
 xxx, xxx, x, xx};                  

uint8_t serverport = 80;                                

EthernetClient client;                      

[u]OneWire oneWire(ONE_WIRE_BUS);      [/u]                      
[u]DallasTemperature sensors(&oneWire);   [/u]              

int numSensors;                                          

bool connected = false;                                  
int i = 0;                                                

void setup(void)                                          

 Serial.println("I2C-to-Ethernet Bridge.");
 Serial.println("Initializing Ethernet.");

 Ethernet. begin(hwaddr, ipaddr);                          
 [u]sensors.begin(); [/u]                                      

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

 [u]numSensors = sensors.getDeviceCount(); [/u]                
 // "sensors.getDeviceCount" is a function in the library

 if(numSensors > 0)                                        
   Serial.print("Enumerated ");                              
   Serial.println( " sensors.");

   Serial.println("No sensors enumerated.");                


void loop(void)                                          
 if(!connected)   {                                        
   //Serial.println("Not connected");

   if (client.connect(serverip,80)){                                    
     connected = true;
     [u]sensors.requestTemperatures(); [/u]                          
     [u]float temp1 = sensors.getTempCByIndex(0); [/u]              
     Serial.print("Temp is ");                                
     Serial.println("Sending to Server: ");                    
     client.print("GET /test_database.php?t");     // the database based on your post about php before        
   [u] client.println(temp1);[/u]
     client.println(" HTTP/1.0");                
     Serial.println(" HTTP/1.0");                
     //client.println("Host: 192.168.x.x");    
     //Serial.println("Host: 192.168.x.x");    
     Serial.println("Cannot connect to Server");              

 else {

   while (client.connected() && client.available()) {        
     char c = client.read();                                  
   connected = false;                                        


Arduino Code that sends data to Database

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  xxxx,xxxx,xxxx,xxxx };
IPAddress ip(x,x,x,x);
IPAddress gateway(x,x,x,x);
IPAddress subnet(255, 255, 255, 0);

IPAddress server(1,2,3,4); // Change to your server ip

EthernetClient client;
int totalCount = 0;
int loopCount = 0;

void setup() {


 Ethernet.begin(mac, ip, gateway, gateway, subnet);

char pageAdd[32];
void loop()
 if(loopCount < 30)
   loopCount = 0;
   if(!getPage(server,pageAdd)) Serial.print("Fail ");
   else Serial.print("Pass ");


byte getPage(IPAddress ipBuf,char *page)
 int inChar;
 char outBuf[128];



   sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
   return 0;

 int connectLoop = 0;
     inChar = client.read();
     connectLoop = 0;

   if(connectLoop > 1000)



 return 1;

My point is to combine somehow these 2 codes in order to send my data to a database. Thank you very much

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131