CODE EXPLANATION Exp 1.2
CODE EXPLANATION Exp 1.2
CODE EXPLANATION Exp 1.2
#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.
#include <UbidotsESPMQTT.h>
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.)
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)
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.
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.
byte* payload: Is the response obtained directly from the broker once a change in one of your
subscribed variables has taken place.
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
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);
client.ubidotsSetBroker("things.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);
setDebug(bool debug);
client.begin(callback);
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();
client.add("temp", temp);
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
ubidotsPublish(char *deviceLabel);
client.loop();
loop();
Infinite loop for MQTT connection, insert it at the end of your routine