OK, that makes sense.
Now I use this code to send the data.
I cannot make it faster bij decrementing the interval between 2 posts, otherwise the program stucks.
Is there a better way to send the data?
#include <DallasTemperature.h>
#include <OneWire.h>
#include <SPI.h>
#include <Ethernet.h>
// Data wire temp sensor is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
DeviceAddress temp1 = { 0x28, 0xD2, 0x56, 0xF3, 0x03, 0x00, 0x00, 0xA0 };
DeviceAddress temp2 = { 0x28, 0xE1, 0x76, 0xF3, 0x03, 0x00, 0x00, 0xEC };
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = { 0xEF, 0x12, 0xDA, 0xAB, 0x17, 0x24};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress server(78,22,214,56); //IP of mysite
// initialize the library instance:
EthernetClient client;
unsigned long lastConnectionTime = 10; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 20*1000; // delay between updates, in milliseconds
void setup() {
// start serial port:
Serial.begin(9600);
//instellen temperatuursensors
sensors.begin();
sensors.setResolution(temp1, 10);
sensors.setResolution(temp2, 10);
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
httpRequest();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// temperaturen lezen
sensors.requestTemperatures();
float temp11 = sensors.getTempC(temp1);
float temp12 = sensors.getTempC(temp2);
delay(1000);
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
client.print("GET http://www.mysite.be/fillDB.php?t1=");
//fillDB.php = code to put the values in database
client.print(temp11);
client.print("&t2=");
client.print(temp12);
client.println(" HTTP/1.0");
client.println();
Serial.print(temp11);
Serial.print(" ");
Serial.println(temp12);
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.