0% found this document useful (0 votes)
30 views14 pages

Lab 07

The document contains code for two tasks that publish sensor data from an Arduino to the Ubidots IoT platform using MQTT. Task 1 reads the value of a potentiometer sensor and publishes it under one variable label. Task 2 reads the same sensor, calculates two additional derived values by dividing the original, and publishes all three values under separate variable labels. Both tasks initialize WiFi and MQTT client connections, format payload strings, and enter a loop to continuously publish updated sensor readings at 1-second intervals.

Uploaded by

Junaid Akhtar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views14 pages

Lab 07

The document contains code for two tasks that publish sensor data from an Arduino to the Ubidots IoT platform using MQTT. Task 1 reads the value of a potentiometer sensor and publishes it under one variable label. Task 2 reads the same sensor, calculates two additional derived values by dividing the original, and publishes all three values under separate variable labels. Both tasks initialize WiFi and MQTT client connections, format payload strings, and enter a loop to continuously publish updated sensor readings at 1-second intervals.

Uploaded by

Junaid Akhtar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

TASK 01

/**************

* Include Libraries

**************/

#include <WiFi.h>

#include <PubSubClient.h>

#define WIFISSID "PTCL_BB" // Put your WifiSSID here

#define PASSWORD "35601840" // Put your wifi password here

#define TOKEN "BBFF-UP4uwJCVHT0UVbglk3dTF7cJCW2D2L" // Put your Ubidots' TOKEN

#define MQTT_CLIENT_NAME "lyZrQTeXwJ" // MQTT client Name, please enter your own 8-12
alphanumeric character ASCII string;

//it should be a random and unique ascii string and different from all other
devices

/**************

* Define Constants

**************/

#define VARIABLE_LABEL "potentiometer_1" // Assing the variable label

#define DEVICE_LABEL "lab_07_task_01" // Assig the device label

#define SENSOR 33 // Set the GPIO15 as SENSOR

char mqttBroker[] = "industrial.api.ubidots.com";

char payload[100];

char topic[150];
// Space to store values to send

char str_sensor[10];

/**************

* Auxiliar Functions

**************/

WiFiClient ubidots;

PubSubClient client(ubidots);

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

char p[length + 1];

memcpy(p, payload, length);

p[length] = NULL;

String message(p);

Serial.write(payload, length);

Serial.println(topic);

void reconnect() {

// Loop until we're reconnected

while (!client.connected()) {

Serial.println("Attempting MQTT connection...");

// Attemp to connect

if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {


Serial.println("Connected");

} else {

Serial.print("Failed, rc=");

Serial.print(client.state());

Serial.println(" try again in 2 seconds");

// Wait 2 seconds before retrying

delay(2000);

/**************

* Main Functions

**************/

void setup() {

Serial.begin(115200);

WiFi.begin(WIFISSID, PASSWORD);

// Assign the pin as INPUT

pinMode(SENSOR, INPUT);

Serial.println();

Serial.print("Wait for WiFi...");

while (WiFi.status() != WL_CONNECTED) {

Serial.print(".");
delay(500);

Serial.println("");

Serial.println("WiFi Connected");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

client.setServer(mqttBroker, 1883);

client.setCallback(callback);

void loop() {

if (!client.connected()) {

reconnect();

sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);

sprintf(payload, "%s", ""); // Cleans the payload

sprintf(payload, "{\"%s\":", VARIABLE_LABEL); // Adds the variable label

float sensor = analogRead(SENSOR);

/* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/

dtostrf(sensor, 4, 2, str_sensor);
sprintf(payload, "%s {\"value\": %s}}", payload, str_sensor); // Adds the value

Serial.println("Publishing data to Ubidots Cloud");

client.publish(topic, payload);

client.loop();

delay(1000);

}
TASK 02

#include <WiFi.h>

#include <PubSubClient.h>

#define WIFISSID "Honor Play" // Put your WifiSSID here

#define PASSWORD "12345678" // Put your wifi password here

#define TOKEN "BBFF-UP4uwJCVHT0UVbglk3dTF7cJCW2D2L" // Put your Ubidots' TOKEN

#define MQTT_CLIENT_NAME "PYAgp9EERR" // MQTT client Name, please enter your own 8-12
alphanumeric character ASCII string;

//it should be a random and unique ascii string and different from all other
devices

/**************

* Define Constants

**************/

#define VARIABLE_LABEL "pot_1" // Assing the variable label

#define VARIABLE_LABEL1 "pot_2" // Assing the variable label

#define VARIABLE_LABEL2 "pot_3" // Assing the variable label

#define DEVICE_LABEL "lab_07_task_02" // Assig the device label

#define SENSOR 33 // Set the GPIO12 as SENSOR

char mqttBroker[] = "industrial.api.ubidots.com";

char payload[100];

char topic[150];

// Space to store values to send


char str_sensorP_1[10];

char str_sensorP_2[10];

char str_sensorP_3[10];

/**************

* Auxiliar Functions

**************/

WiFiClient ubidots;

PubSubClient client(ubidots);

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

char p[length + 1];

memcpy(p, payload, length);

p[length] = NULL;

String message(p);

Serial.write(payload, length);

Serial.println(topic);

void reconnect() {

// Loop until we're reconnected

while (!client.connected()) {

Serial.println("Attempting MQTT connection...");

// Attemp to connect

if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {


Serial.println("Connected");

} else {

Serial.print("Failed, rc=");

Serial.print(client.state());

Serial.println(" try again in 2 seconds");

// Wait 2 seconds before retrying

delay(2000);

/**************

* Main Functions

**************/

void setup() {

Serial.begin(115200);

WiFi.begin(WIFISSID, PASSWORD);

// Assign the pin as INPUT

pinMode(SENSOR, INPUT);

Serial.println();

Serial.print("Wait for WiFi...");

while (WiFi.status() != WL_CONNECTED) {

Serial.print(".");
delay(500);

Serial.println("");

Serial.println("WiFi Connected");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

client.setServer(mqttBroker, 1883);

client.setCallback(callback);

void loop() {

if (!client.connected()) {

reconnect();

sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);

sprintf(payload, "%s", ""); // Cleans the payload

sprintf(payload, "{\"%s\":", VARIABLE_LABEL); // Adds the variable label

float sensorP_1 = analogRead(SENSOR);

Serial.println(sensorP_1);

float sensorP_2 = sensorP_1/2;

Serial.println(sensorP_2);
float sensorP_3 = sensorP_1/4;

Serial.println(sensorP_3);

/* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/

dtostrf(sensorP_1, 4, 2, str_sensorP_1);

sprintf(payload, "%s {\"value\": %s}}", payload, str_sensorP_1); // Adds the value

client.publish(topic, payload);

client.loop();

delay(1000);

sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); // Adds the variable label

dtostrf(sensorP_2, 4, 2, str_sensorP_2);

sprintf(payload, "%s {\"value\": %s}}", payload, str_sensorP_2); // Adds the value

client.publish(topic, payload);

client.loop();

delay(1000);

sprintf(payload, "{\"%s\":", VARIABLE_LABEL2); // Adds the variable label

dtostrf(sensorP_3, 4, 2, str_sensorP_3);

sprintf(payload, "%s {\"value\": %s}}", payload, str_sensorP_3); // Adds the value

Serial.println("Publishing data to Ubidots Cloud");

client.publish(topic, payload);

client.loop();

delay(1000);
}

You might also like