CODE EXPLANATION Exp 1.2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

DR.

SUNITI DUTT DT-2 LAB

LINE-WISE CODE EXPLANATION OF EXP 1.2 (WEATHER MONITORING):

#include <Adafruit_BMP280.h>

Adafruit Industries is an open-source hardware company based in New York City, founded in 2005.
The company designs, manufactures and sells a number of electronics products, electronics
components, tools and accessories.

This is the adafruit library header.

#include <UbidotsESPMQTT.h>

MQTT (Message Queuing Telemetry Transport) library header

#define TOKEN "….." // Your Ubidots TOKEN

To interact with ubidots (known as ubidots broker) with the help of MQTT, you (client) will need a
TOKEN. It is available under API credentials on ubidots. (NOTE: API is Application Programming
Interface, which is a software intermediary that allows two applications to talk to each other. Each
time you use an app like Facebook, send an instant message, or check the weather on your phone,
you're using an API.)

#define WIFISSID "……." // Your SSID

SSID is Service Set Identifier. An SSID is a unique ID that consists of 32 characters and is used for
naming wireless networks. When multiple wireless networks overlap in a certain location, SSIDs
make sure that data gets sent to the correct destination.

Ubidots client(TOKEN);

Ubidots(char* token)

Ubidots(char* token, char* clientName)

Creates an Ubidots client, you must setup as input your Ubidots TOKEN, the MQTT client name is
optional. Basically, the client/our device is authenticated with ubidots by this statement.

void callback(char* topic, byte* payload, unsigned int length)

In computer programming, a callback, also known as a "call-after" function, is any executable code
that is passed as an argument to other code; that other code is expected to call back (execute) the
argument at a given time. Normally, a code runs sequentially in top-down order. However, there are
some cases that code runs (or must run) after something else happens and also not sequentially.
This is called asynchronous programming. Callbacks make sure that a function is not going to run
before a task is completed but will run right after the task has completed.
DR. SUNITI DUTT DT-2 LAB

We are going to publish messages/data to ubidots within the callback function. The callback function
header needs to be declared before the Client constructor and the actual callback defined
afterwards. This instruction is just declaring the callback function, allowing us to send data to
ubidots.

char* topic: The topic is the endpoint of your variable

byte* payload: Is the response obtained directly from the broker once a change in one of your
subscribed variables has taken place.

unsigned int length: The length of the payload.

WHAT THIS FUNCTION DOES: Once your variable has changed, it prints through the serial port which
topic has changed and concludes with printing the response from the broker.

Serial.println

Prints data to the serial port as human-readable ASCII text followed by a carriage return character
(ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').

Serial.print

Prints data to the serial port as human-readable ASCII text.

Serial.begin()

Sets the data rate in bits per second (baud) for serial data transmission.

Client: Client is the base class for all Ethernet client based calls. It is not called directly, but invoked
whenever you use a function that relies on it [eg: EthernetClient(), connected(), connect(), write(),
print(), println(), available(), read(), flush(), stop()]

client.wifiConnection(WIFISSID, WIFIPASS);

Sets the wifi connection for client (your device)

wifiConnection(char* ssid, char* pass);

Connect via Wifi to the specified SSID.

client.ubidotsSetBroker("things.ubidots.com");

Sets the broker properly for the educational account- industrial.api.ubidots.com;

ubidotsSetBroker(char* broker);
DR. SUNITI DUTT DT-2 LAB

Sets the broker properly for publish and subscribe to Ubidots accounts. If your account if a business
one, set "business.api.ubidots.com" or the endpoint provided by Ubidots as your broker, see
examples for more information. By default, broker will be set to publish and subscribe to free
educational version accounts with broker "things.ubidots.com".

client.setDebug(true);

Pass a true or false bool value to activate debug messages

setDebug(bool debug);

Make available debug messages through the serial port.

client.begin(callback);

begin(void (*callback)(char*,uint8_t*,unsigned int));

Sets the callback function for subscribed topics

float temp = bmp280.readTemperature();

float pressure = bmp280.readPressure();

float altitude = bmp280.readAltitude();

float water_boiling_point = bmp280.waterBoilingPoint(pressure);

Acquiring environmental data using “Adafruit_BMP280.h” library’s instructions

client.connected()

For class client of Arduino, connected() is a function that tells Whether or not the client is
connected. Note that a client is considered connected if the connection has been closed but there is
still unread data. Use syntax client.connected(). It Returns true if the client is connected, false if not.

reconnect();

For trying to make a reconnection every 5 seconds if the connection is lost.

client.add("temp", temp);

add(char* variableLabel, float value, char *context, char *timestamp);

Add a variable with a value, context and timestamp to be sent to a certain data source, once you use
add() you can publish your variable using the ubidotsPublish() method.
DR. SUNITI DUTT DT-2 LAB

client.ubidotsPublish("weather-monitoring-system"); // insert your device label here

ubidotsPublish(char *deviceLabel);

Publishes the variables added to the specified device label.

client.loop();

loop();

Infinite loop for MQTT connection, insert it at the end of your routine

You might also like