#include <WiFi.h>
#include <PubSubClient.h>
#define WIFISSID "Krupa" // Put your WifiSSID here
#define PASSWORD "123456789" // Put your wifi password here
#define TOKEN "XXXXXXXXXXXXXXXXX" // Put your Ubidots' TOKEN
#define MQTT_CLIENT_NAME "XXXXXXXXXXXXXX" // MQTT client Name, please enter your own 8-12 alphanumeric character ASCII string;
const int ledPin = 2;
const int s1=36;
const int s2=39;
const int s3=34;
const int s4=35;
const int s5=32;
const int s6=33;
float s1value=0;
float s2value=0;
float s3value=0;
float s4value=0;
float s5value=0;
float s6value=0;
#define DEVICE_LABEL "ew_wings"
#define VARIABLE_LABEL1 "ew_s1" // Assing the variable label
#define VARIABLE_LABEL2 "ew_s2"
#define VARIABLE_LABEL3 "ew_s3" // Assing the variable label
#define VARIABLE_LABEL4 "ew_s4"
#define VARIABLE_LABEL5 "ew_s5" // Assing the variable label
#define VARIABLE_LABEL6 "ew_s6"
#define DEVICE_LABEL "ew_wings" // Assig the device label
char mqttBroker[] = "things.ubidots.com";
char payload[2000]; //The maximum payload size supported by Ubidots is of 10000 bytes.
char topic[150];
char topic1[150];
char topic2[150];
char topic3[150];
char topic4[150];
char topic5[150];
// Space to store values to send
char str_sensor1[60];
char str_sensor2[60];
char str_sensor3[60];
char str_sensor4[60];
char str_sensor5[60];
char str_sensor6[60];
/****************************************
* 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() {
pinMode(s1,INPUT);
pinMode(s2,INPUT);
pinMode(s3,INPUT);
pinMode(s4,INPUT);
pinMode(s5,INPUT);
pinMode(s6,INPUT);
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) {
pinMode(ledPin, OUTPUT);
delay(10);
digitalWrite(ledPin, HIGH);
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();
}
s1value=analogRead(s1);
s2value=analogRead(s2);
s3value=analogRead(s3);
s4value=analogRead(s4);
s5value=analogRead(s5);
s6value=analogRead(s6);
/* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
dtostrf(s1value, 4, 2, str_sensor1);
dtostrf(s2value, 4, 2, str_sensor2);
dtostrf(s3value, 4, 2, str_sensor3);
dtostrf(s4value, 4, 2, str_sensor4);
dtostrf(s5value, 4, 2, str_sensor5);
dtostrf(s6value, 4, 2, str_sensor6);
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); // Adds the variable label
sprintf(payload, "%s {\"value\": %s", payload, str_sensor1); // Adds the value
sprintf(payload, "%s } }", payload); // Closes the dictionary brackets
Serial.println("Publishing data to Ubidots Cloud");
Serial.println(payload);
client.publish(topic, payload);
delay(500);
sprintf(topic2, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL2); // Adds the variable label
sprintf(payload, "%s {\"value\": %s", payload, str_sensor2); // Adds the value
sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
Serial.println("Publishing data to Ubidots Cloud");
Serial.println(payload);
client.publish(topic1, payload);
delay(500);
sprintf(topic2, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL3); // Adds the variable label
sprintf(payload, "%s {\"value\": %s", payload, str_sensor3); // Adds the value
sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
Serial.println("Publishing data to Ubidots Cloud");
Serial.println(payload);
client.publish(topic2, payload);
delay(500);
sprintf(topic2, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL4); // Adds the variable label
sprintf(payload, "%s {\"value\": %s", payload, str_sensor4); // Adds the value
sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
Serial.println("Publishing data to Ubidots Cloud");
Serial.println(payload);
client.publish(topic3, payload);
delay(500);
sprintf(topic2, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL5); // Adds the variable label
sprintf(payload, "%s {\"value\": %s", payload, str_sensor5); // Adds the value
sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
Serial.println("Publishing data to Ubidots Cloud");
Serial.println(payload);
client.publish(topic4, payload);
delay(500);
sprintf(topic2, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL6); // Adds the variable label
sprintf(payload, "%s {\"value\": %s", payload, str_sensor6); // Adds the value
sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
Serial.println("Publishing data to Ubidots Cloud");
Serial.println(payload);
client.publish(topic5, payload);
delay(500);
client.loop();
delay(2000);
}
Greetings, change your broker from things.ubidots.com
to industrial.api.ubidots.com
All the best
Thank you ,
But I am able to visualise only two data in the