Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 6 ■ IOT PATTERNS: REMOTE CONTROL

Listing 6-15. Code for Standard Arduino Functions

void setup()
{
// Initialize serial port
Serial.begin(9600);

// Connect Arduino to internet
connectToInternet();

// Set LED pin mode
pinMode(ledPin, OUTPUT);

//Connect MQTT Broker
Serial.println("[INFO] Connecting to MQTT Broker");
if (pubSubClient.connect("arduinoClient"))
{
Serial.println("[INFO] Connection to MQTT Broker Successful");
pubSubClient.subscribe(topic);
}
else
{
Serial.println("[INFO] Connection to MQTT Broker Failed");
}
}

void loop()
{
// Wait for messages from MQTT broker
pubSubClient.loop();
}

Your Arduino code is now complete.

The Final Product


To test the application, verify and upload the Arduino code as discussed in Chapter 1.
Once the code has been uploaded, open the Serial Monitor window. You will start seeing
log messages similar to ones shown in Figure  6-22.
Free download pdf