Probleme bei Datenübergabe HTTP

Heeeey

Ich habe (wider mal ;D) eine Frage zu meinem Arduino.

Ich habe es (dank diesem Forum) geschafft, zwei DS18B20 Sensorwerte über einen Webserver auf meiner MYSQL Datenbank abzuspeichern. So weit so gut.

Nun wollte ich zwei weiter DS18B20 hinzufügen, deshalb habe ich die DB um zwei Spalten vergrössert und den php Skript auf dem Webserver richtig angepasst (in meinem Fall heisst die php Datei data2mysqlds18204).

Wenn ich im Browser manuell eingebe /data2mysqlds18204.php?S1=111&S2=222&S3=333&S4=444 werden die Daten erfolgreich in den Spalten S1 bis S4 abgespeichert

Nun zum Problem:

Dieser Sketch funktioniert mit 2 Sensoren:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>
 OneWire  ds(2); //pin für ds1820
 

// assign a MAC address for the ethernet controller.
// fill in your address here:
DeviceAddress sensor1 = { 0x28, 0x24, 0xE2, 0x7, 0x0, 0x0, 0x80, 0x27 };
DeviceAddress sensor2 = { 0x28, 0xE3, 0xDB, 0x7, 0x0, 0x0, 0x80, 0x4A };


byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192,168,1,177);      // <- hier die IP des Ethernet Shield eintragen
 
// initialize the library instance:
EthernetClient client;
 
char server[] = "Domain.bplaced.netr Name des Webspaces eintragen
 
unsigned long lastConnectionTime = 0;          // 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 = 60000;  // das ist ein Bug im Beispiel -> http://forum.arduino.cc/index.php/topic,125510.0.html
 
void setup() {
// start serial port:
Serial.begin(115200);
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection using a fixed IP address and DNS server:
Ethernet.begin(mac, ip);
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
 void writeTimeToScratchpad(byte* address)
{
  //reset the bus
  ds.reset();
  //select our sensor
  ds.select(address);
  //CONVERT T function call (44h) which puts the temperature into the scratchpad
  ds.write(0x44,1);
  //sleep a second for the write to take place
  delay(1000);
}
 
void readTimeFromScratchpad(byte* address, byte* data)
{
  //reset the bus
  ds.reset();
  //select our sensor
  ds.select(address);
  //read the scratchpad (BEh)
  ds.write(0xBE);
  for (byte i=0;i<9;i++){
    data[i] = ds.read();
  }
}
 
float getTemperature(byte* address)
 {
 int tr;
 byte data[12];

writeTimeToScratchpad(address);

readTimeFromScratchpad(address,data);

//put in temp all the 8 bits of LSB (least significant byte)
 tr = data[0];

if (address[0] == 0x10) // DS18S20
 {
 //check for negative temperature
 if (data[1] > 0x80)
 {
 tr = !tr + 1; //two’s complement adjustment
 tr = tr * -1; //flip value negative.
 }

//drop bit 0
 tr = tr >> 1;

//COUNT PER Celsius degree (10h)
 int cpc = data[7];
 //COUNT REMAIN (0Ch)
 int cr = data[6];

return tr - (float)0.25 + (cpc - cr)/(float)cpc;
 }
 else // DS18B20
 {
 return ((data[1] << 8) + tr) * (float)0.0625;
 }
 } 

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() {
// if there's a successful connection:
Serial.println("try connecting...");
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.print("GET /data2mysqlds1820.php?S1=");
client.print(getTemperature(sensor1));
client.print("&S2=");
client.print(getTemperature(sensor2));
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
 
// 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();
}
}

Folgende Dinge habe ich im Arduino Sketch geändert um 4 Sensoren auszulesen:

  1. 2 Neue Sensor Adressen hinzugefügt
  2. Name der php Datei abgändert
  3. Die Befehle zum Auslesen hinzugefügt (Client.print(getTemperature(sensor3))usw...)

Hier der abgeänderte Sketch der noch nicht funktioniert

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>
 
 OneWire  ds(2); //pin für ds1820
 

// assign a MAC address for the ethernet controller.
// fill in your address here:
DeviceAddress sensor1 = { 0x28, 0x24, 0xE2, 0x7, 0x0, 0x0, 0x80, 0x27 };
DeviceAddress sensor2 = { 0x28, 0xE3, 0xDB, 0x7, 0x0, 0x0, 0x80, 0x4A };
DeviceAddress sensor3 = { 0x28, 0xFF, 0xAE, 0x39, 0x57, 0x16, 0x04, 0x24 };
DeviceAddress sensor4 = { 0x28, 0xFF, 0x67, 0xC7, 0x60, 0x16, 0x03, 0xB6 };

byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// fill in an available IP address on your network here,
// for manual configuration:
IPAddress ip(192,168,1,177);      // <- hier die IP des Ethernet Shield eintragen
 
// initialize the library instance:
EthernetClient client;
 
char server[] = "domain.bplaced.net";//  <- hier Name des Webspaces eintragen
 
unsigned long lastConnectionTime = 0;          // 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 = 60000;  // das ist ein Bug im Beispiel -> http://forum.arduino.cc/index.php/topic,125510.0.html
 
void setup() {


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() {
// if there's a successful connection:
Serial.println("try connecting...");
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.print("GET /data2mysqlds18204.php?S1=");
client.print(getTemperature(sensor1));
client.print("&S2=");
client.print(getTemperature(sensor2));
client.print("&S3=");
client.print(getTemperature(sensor3));
client.print("&S4=");
client.print(getTemperature(sensor4));
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
 
// 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();
}
}

Jemand eine Idee? :smiley:
Ps. Setup im 2te Code ist dasselbe wie im ersten (musste es löschen wegen zu langem Post)
LG T

Wenn ich im Browser manuell eingebe /data2mysqlds18204.php?S1=111&S2=222&S3=333&S4=444 werden die Daten erfolgreich in den Spalten S1 bis S4 abgespeichert

Und wenn du dies per Arduino machst? (einfach einen festen Text, oder deinen bestehenden Sketch mit den 2 Werten um  "&S3=333&S4=444"  ergänzen) Das halbiert das Problem, denke ich mal.

Alternativ kannst du auch die client.print Ausgaben parallel auf Serial.print mitschreiben, und sehen was in beiden Fällen passiert. (2 Werte / 4 Werte)

Irgendwo muss der Unterschied zu sehen sein.

Ich habe nun feste Werte eingesetzt und siehe da, die Werte werden auf der Datenbank abgespeichert.
Also habe ich irgendwo vermutlich vergessen etwas von 2 auf 4 Sensoren anzupassen.
Die Frage ist nun nur noch wo? :o :sweat_smile:

Vielen Dank Michael für deinen Tipp, nun bin ich schon ein Stück näher an der Lösung.

LG Toby

Das halbiert das Problem

Gibst du eventuell ein Leerzeichen aus ?

Evtl stimmen die Adressen der beiden neuen Sensoren nicht?

Gruß

Hast du mal am Seriellen Monitor geschaut ob deine Werte ankommen also in der Loop mal

Serial.print("S1: ");
Serial.print(getTemperature(sensor1));
Serial.print(" S2: ");
Serial.print(getTemperature(sensor2));
Serial.print(" S3: ");
Serial.print(getTemperature(sensor3));
Serial.print(" S4: ");
Serial.print(getTemperature(sensor4));

ausprobiert?
Gruß
DerDani