PubSubCient Arduino

Tenferenzu

Banned
Registriert
Okt. 2017
Beiträge
6.473
Guten Abend allerseits. Ich probiere mich gerade an einem kleinen IOT-Projekt und stehe grade ziemlich auf dem Schlauch. Ich möchte über MQTT Sensordaten an einen Empfänger schicken allerdings bekomme ich momentan entweder arabische Buchstaben/Zeichen oder eine leere Nachricht. Konkret möchte ich die Sensordaten die in dem Webclient ausgegeben werden über MQTT schicken.

Vielleicht kann jemand mit etwas Programmiererfahrung mit dem Code etwas anfangen?

C:
void loop() {
  // This is needed at the top of the loop!
  mqttClient.loop();
  
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          client.print("MQTT WEB Server <br />");
          
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            sensorReading[analogChannel] = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading[analogChannel]);
            client.println("<br />");
            Serial.println(sensorReading[analogChannel]);

            String thisString = String(sensorReading[analogChannel]);
            mqttClient.publish("webmqtt", thisString);
          
            //mqttClient.publish("webmqtt", String thisString = String( client.println(sensorReading[analogChannel])));
          
          }
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(100);
    // close the connection:
    // Ensure that we are subscribed to the topic
    mqttClient.subscribe("webmqtt");
 
    // Attempt to publish a value to the topic
    
 //mqttClient.publish("webmqtt", "Hello NEW World");
    
    client.stop();
    Serial.println("client disconnected");
    
  }
  delay(500);
}
 
Tenferenzu schrieb:
String thisString = String(sensorReading[analogChannel]); mqttClient.publish("webmqtt", thisString);

funktioniert "hello world" anstatt thisString ?
kurze google suche wird eher ein char[] / char* statt String empfohlen

vlt gibt die String Umwandlung an der Stelle den Zeichensalat aus


Bei der Anzeige in HTML usw. bietet es sich evtl. an die Encoding oder Sprachparameter von HTTP/HTML zu nutzen / zu beachten
w3c encoding w3c sprache encoding

  • als "debug" die Daten über Serial ausgeben
  • mit tcpdump den Datenverkehr mitschneiden und HTTP/HTML mit wireshark analysieren (oder Entwicklerkonsole im Browser)
 
Zurück
Oben