Vielen Dank Thomas! Das ist perfekt!
Inzwischen hänge ich wieder beim Arduino. Kurz zu meinem Script: Ich habe 6 Sensoren und pro Loop lese ich die Daten eines Sensors und schicke diese zum Emoncms. Das mache ich mit einem Case-Konstrukt.
Das komische: Wenn ich 3 Sensoren schicke, also z.B. Case 4/5/6 auskommentiere, dann geht alles tadellos. Auch wenn ich Case 1/2/3 auskommentiere.
Sobald ich aber 4 Sensoren, also 4 Cases, laufen lasse, dann geht es manchmal, manchmal nicht.
Und wenn ich 5 oder alle 6 Sensoren laufen lasse, dann geht gar nichts mehr.
Das Programm loopt zwar durch und ich sehe auch ganz normal alle meine println damit ich das im Serial Ouput prüfen kann, aber im emoncms kommt nichts an...
Wenn ich die Cases umschreibe und 6 mal einfach hardcodierte Testwerte sende, dann funktioniert es!
Ich habe dann allerdings auch nie einen textfinder im Einsatz. Kann das "zuviel" für den Arduino sein?
Hier noch mein Code:
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <TextFinder.h>
// MAC address for your Ethernet shield
byte mac[] = {
0x90, 0x2, 0xDA, 0x0E, 0xB2, 0x12 };
EthernetClient client;
IPAddress Emoncms(80, 74, 158, 130);
//Arexx IP
IPAddress server(192,168,0,30);
EthernetClient sensorclient;
TextFinder finder( sensorclient );
long value; // jeweiliger gemessener Wert
String stringValue; //Wert umgewandelt in String
int sensorCount = 1; // Welcher Sensor an der Reihe ist
String json1;
String json2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("void setup...");
Serial.println();
while (Ethernet.begin(mac) != 1)
{
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
}
void loop() {
//Da nicht alle Sensordaten mit einem Request geschickt werden, sende Daten
//pro Case (Sensor 1-6)
Serial.print("sensorcount vor Programmloop: ");
Serial.println(sensorCount);
if (sensorCount > 6) {
sensorCount = 1;
}
Serial.print("sensorcount nach korrektur:");
Serial.println(sensorCount);
if (sensorclient.connect(server, 80))
{
Serial.println("Connect to Arexx...");
sensorclient.println("GET /cdata.xml HTTP/1.0");
sensorclient.println();
Serial.println("Connected...");
}
else
{
Serial.println(" connection to Arexx failed");
}
switch (sensorCount) {
case 1:
Serial.println("Case 1:");
//Get Data from RH17196
if (sensorclient.connected())
{
finder.find("<value id=\"17196\" type=\"3\" uid=\"171963\" t=\""); // seek to the Results field
finder.find("\">"); // skip past this
value = finder.getValue('.'); // get numeric value, ignore dot
//Serial.print(value);
Serial.println(" cdata.xml resultat");
stringValue = String(value);
Serial.println(stringValue);
json1 = "GET /input/post.json?json={Divider:1000,Humidity_OG:";
json2 = "}&apikey=xxx HTTP/1.0";
}
else
{
Serial.print("cdata.xml nicht gelesen");
}
sensorclient.stop();
sensorclient.flush();
if (client.connect(Emoncms, 80))
{
Serial.println("Connect to Emoncms...");
Serial.println(json1+stringValue+json2);
client.println(json1+stringValue+json2);
client.println("HOST: emoncms.rossiworld.com");
client.println();
Serial.println("Connected...");
}
else
{
Serial.println(" connection to Emoncms failed");
}
client.stop();
client.flush();
break;
case 2:
Serial.println("Case 2:");
//Get Data from T17196
if (sensorclient.connected())
{
finder.find("<value id=\"17196\" type=\"1\" uid=\"171961\" t=\""); // seek to the Results field
finder.find("\">"); // skip past this
value = finder.getValue('.'); // get numeric value
//Serial.print(value);
Serial.println(" cdata.xml resultat");
stringValue = String(value);
Serial.println(stringValue);
json1 = "GET /input/post.json?json={Divider:1000,Temp_OG:";
json2 = "}&apikey=xxx HTTP/1.0";
}
else
{
Serial.print("cdata.xml nicht gelesen");
}
sensorclient.stop();
sensorclient.flush();
if (client.connect(Emoncms, 80))
{
Serial.println("Connect to Emoncms...");
Serial.println(json1+stringValue+json2);
client.println(json1+stringValue+json2);
client.println("HOST: emoncms.rossiworld.com");
client.println();
Serial.println("Connected...");
}
else
{
Serial.println(" connection to Emoncms failed");
}
client.stop();
client.flush();
break;
case 3:
Serial.println("Case 3:");
//Get Data from RH17304
if (sensorclient.connected())
{
finder.find("<value id=\"17304\" type=\"3\" uid=\"173043\" t=\""); // seek to the Results field
finder.find("\">"); // skip past this
value = finder.getValue('.'); // get numeric value
//Serial.print(value);
Serial.println(" cdata.xml resultat");
stringValue = String(value);
Serial.println(stringValue);
json1 = "GET /input/post.json?json={Divider:1000,Humidity_EG:";
json2 = "}&apikey=xxx HTTP/1.0";
}
else
{
Serial.print("cdata.xml nicht gelesen");
}
sensorclient.stop();
sensorclient.flush();
if (client.connect(Emoncms, 80))
{
Serial.println("Connect to Emoncms...");
Serial.println(json1+stringValue+json2);
client.println(json1+stringValue+json2);
client.println("HOST: emoncms.rossiworld.com");
client.println();
Serial.println("Connected...");
}
else
{
Serial.println(" connection to Emoncms failed");
}
client.stop();
client.flush();
break;
case 4:
Serial.println("Case 4:");
//Get Data from T17304
if (sensorclient.connected())
{
finder.find("<value id=\"17304\" type=\"1\" uid=\"173041\" t=\""); // seek to the Results field
finder.find("\">"); // skip past this
value = finder.getValue('.'); // get numeric value
//Serial.print(value);
Serial.println(" cdata.xml resultat");
stringValue = String(value);
Serial.println(stringValue);
json1 = "GET /input/post.json?json={Divider:1000,Temp_EG:";
json2 = "}&apikey=xxx HTTP/1.0";
}
else
{
Serial.print("cdata.xml nicht gelesen");
}
sensorclient.stop();
sensorclient.flush();
if (client.connect(Emoncms, 80))
{
Serial.println("Connect to Emoncms...");
Serial.println(json1+stringValue+json2);
client.println(json1+stringValue+json2);
client.println("HOST: emoncms.rossiworld.com");
client.println();
Serial.println("Connected...");
}
else
{
Serial.println(" connection to Emoncms failed");
}
client.stop();
client.flush();
break;
case 5:
Serial.println("Case 5:");
//Get Data from RH17228
if (sensorclient.connected())
{
finder.find("<value id=\"17228\" type=\"3\" uid=\"172283\" t=\""); // seek to the Results field
finder.find("\">"); // skip past this
value = finder.getValue('.'); // get numeric value
//Serial.print(value);
Serial.println(" cdata.xml resultat");
stringValue = String(value);
Serial.println(stringValue);
json1 = "GET /input/post.json?json={Divider:1000,Humidity_OUT:";
json2 = "}&apikey=xxx HTTP/1.0";
}
else
{
Serial.print("cdata.xml nicht gelesen");
}
sensorclient.stop();
sensorclient.flush();
if (client.connect(Emoncms, 80))
{
Serial.println("Connect to Emoncms...");
Serial.println(json1+stringValue+json2);
client.println(json1+stringValue+json2);
client.println("HOST: emoncms.rossiworld.com");
client.println();
Serial.println("Connected...");
}
else
{
Serial.println(" connection to Emoncms failed");
}
client.stop();
client.flush();
break;
case 6:
Serial.println("Case 6:");
//Get Data from T17228
if (sensorclient.connected())
{
finder.find("<value id=\"17228\" type=\"1\" uid=\"172281\" t=\""); // seek to the Results field
finder.find("\">"); // skip past this
value = finder.getValue('.'); // get numeric value
//Serial.print(value);
Serial.println(" cdata.xml resultat");
stringValue = String(value);
Serial.println(stringValue);
json1 = "GET /input/post.json?json={Divider:1000,Temp_OUT:";
json2 = "}&apikey=xxx HTTP/1.0";
}
else
{
Serial.print("cdata.xml nicht gelesen");
}
sensorclient.stop();
sensorclient.flush();
if (client.connect(Emoncms, 80))
{
Serial.println("Connect to Emoncms...");
Serial.println(json1+stringValue+json2);
client.println(json1+stringValue+json2);
client.println("HOST: emoncms.rossiworld.com");
client.println();
Serial.println("Connected...");
}
else
{
Serial.println(" connection to Emoncms failed");
}
client.stop();
client.flush();
break;
}
Serial.println("Starte Loop mit nächstem Sensor nach 5s...");
Serial.println();
sensorCount++;
delay(5000);
}