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
#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.begin(9600);
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.print(numSensors);
Serial.println( " sensors.");
}
else
{
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(temp1);
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]
Serial.print(temp1);
client.println(" HTTP/1.0");
Serial.println(" HTTP/1.0");
//client.println("Host: 192.168.x.x");
//Serial.println("Host: 192.168.x.x");
client.println();
Serial.println();
delay(20000);
//}
}
else{
Serial.println("Cannot connect to Server");
}
}
else {
delay(500);
while (client.connected() && client.available()) {
char c = client.read();
Serial.print(c);
}
Serial.println();
client.stop();
connected = false;
}
}
Arduino Code that sends data to Database
#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() {
Serial.begin(9600);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
delay(2000);
Serial.println("Ready");
}
char pageAdd[32];
void loop()
{
if(loopCount < 30)
{
delay(1000);
}
else
{
loopCount = 0;
sprintf(pageAdd,"/test_database.php?temp1=%d",totalCount);
if(!getPage(server,pageAdd)) Serial.print("Fail ");
else Serial.print("Pass ");
totalCount++;
Serial.println(totalCount,DEC);
}
loopCount++;
}
byte getPage(IPAddress ipBuf,char *page)
{
int inChar;
char outBuf[128];
Serial.print("connecting...");
if(client.connect(ipBuf,80))
{
Serial.println("connected");
sprintf(outBuf,"GET %s HTTP/1.0\r\n\r\n",page);
client.write(outBuf);
}
else
{
Serial.println("failed");
return 0;
}
int connectLoop = 0;
while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
connectLoop = 0;
}
delay(10);
connectLoop++;
if(connectLoop > 1000)
{
Serial.println();
Serial.println("Timeout");
client.stop();
}
}
Serial.println();
Serial.println("disconnecting.");
client.stop();
return 1;
}
My point is to combine somehow these 2 codes in order to send my data to a database. Thank you very much