mqtt_sample_callback
/**
 * MQTT callback to process received messages
 */
void callback(char* topic, byte* payload, unsigned int length) {

  int strcomparison = strcmp(topic, mqttTopicTemperature);
  if (strcomparison == 0) {
    Serial.println("Matched Temperature Topic");
        if (payload[2] == '.') {
            length = 4;
            }
        for (int i=0;i<length;i++) {
            outTemp[i] = payload[i];
            }

        } //end If strcomparison
  strcomparison = strcmp(topic, mqttTopicHumidity);
  if (strcomparison == 0) {
    Serial.println("Matched Humidity Topic");
    if (payload[2] == '.') {
            length = 3;
            }
    for (int i=0;i<length;i++) {
            outHum[i] = payload[i];
                             }
  }
  strcomparison = strcmp(topic, mqttTopicDewpoint);
  if (strcomparison == 0) {
    Serial.println("Matched Dewpoint Topic");
    newdata = true;
                             }
}

// some useful string conversions to use above when going from char payload to an int

/*
  String to Integer conversion

 Reads a serial input string until it sees a newline, then converts
 the string to a number if the characters are digits.

 The circuit:
 No external components needed.

 created 29 Nov 2010
 by Tom Igoe

 This example code is in the public domain.
 */

String inString = "";    // string to hold input

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // send an intro:
  Serial.println("\n\nString toInt():");
  Serial.println();
}

void loop() {
  // Read serial input:
  while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char
      // and add it to the string:
      inString += (char)inChar;
    }
    // if you get a newline, print the string,
    // then the string's value:
    if (inChar == '\n') {
      Serial.print("Value:");
      Serial.println(inString.toInt());
      Serial.print("String: ");
      Serial.println(inString);
      // clear the string for new input:
      inString = "";
    }
  }
}
mqtt_sample_callback.txt · Last modified: 2017/07/09 08:10 by tomgle