Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 5 ■ IOT PATTERNS: REALTIME CLIENTS

81

// Publish sensor data to MQTT broker
publishSensorData();


delay(50);


}


takeLowTime = true;
}


if(digitalRead(pirPin) == LOW)
{
if(takeLowTime)
{
lowIn = millis();
takeLowTime = false;
}


if(!lockLow && millis() - lowIn > pause)
{
lockLow = true;


Serial.print("[INFO] Activity Ended @ "); //output
Serial.print((millis() - pause)/1000);
Serial.print(" secs");
Serial.println("");


delay(50);
}
}
}


Data Subscribe


The fourth section of the code defines variables, constants, and functions that are going
to be used for publishing the data to an MQTT broker.
This is the same code that you saw in Chapter 3. You do not need to make any
changes for the code to work, but it is recommended that you customize some of the
messages so that they do not get mixed up with someone else using the same values.
All values that can be changed have been highlighted in bold in Listing 5-5. If you are
using your own MQTT server, make sure to change the server and port values. The two
recommended changes include value of the topic variable and the name of the client
that you need to pass while connecting to the MQTT broker.


http://www.allitebooks.com

Free download pdf