Last active
October 20, 2019 23:51
-
-
Save jotathebest/4d479bec5679ba9e7bed9936f1f99232 to your computer and use it in GitHub Desktop.
Publish and Subscribe to Ubidots ESP8266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************************************************* | |
* This Example sends harcoded data to Ubidots and serves as example for users that have devices | |
* based on ESP8266 chips | |
* | |
* This example is given AS IT IS without any warranty | |
* | |
* Made by Jose García @https://github.com/jotathebest/ , | |
* adapted from the original WiFiClient ESP8266 example | |
*************************************************************************************************/ | |
/**************************************** | |
* Include Libraries | |
****************************************/ | |
#include <PubSubClient.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include <stdio.h> | |
/**************************************** | |
* Define Constants | |
****************************************/ | |
#define WIFISSID "..." // Put your WifiSSID here | |
#define PASSWORD "..." // Put your wifi password here | |
#define TOKEN "...." // Put your Ubidots' TOKEN | |
#define VARIABLE_LABEL "temperature" // Assing the variable label | |
#define VARIABLE_TO_SUBSCRIBE "humidity" | |
#define DEVICE_LABEL "pycon" // Assig the device label | |
#define MQTT_CLIENT_NAME "...." // MQTT client Name, put a Random ASCII | |
char mqttBroker[] = "industrial.api.ubidots.com"; | |
char payload[700]; | |
char topic[150]; | |
// Space to store values to send | |
char str_val[6]; | |
/**************************************** | |
* Initializate constructors for objects | |
****************************************/ | |
ESP8266WiFiMulti WiFiMulti; | |
WiFiClient ubidots; | |
PubSubClient client(ubidots); | |
/**************************************** | |
* Auxiliar Functions | |
****************************************/ | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i=0;i<length;i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.println("Attempting MQTT connection..."); | |
// Attempt 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); | |
pinMode(A0, INPUT); | |
WiFiMulti.addAP(WIFISSID, PASSWORD); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Wait for WiFi... "); | |
while(WiFiMulti.run() != 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(); | |
// Subscribes for getting the value of the control variable in the temperature-box device | |
char topicToSubscribe[200]; | |
sprintf(topicToSubscribe, "%s", ""); // Cleans the content of the char | |
sprintf(topicToSubscribe, "%s%s", "/v1.6/devices/", DEVICE_LABEL); | |
sprintf(topicToSubscribe, "%s/%s/lv", topicToSubscribe, VARIABLE_TO_SUBSCRIBE); | |
Serial.println("subscribing to topic:"); | |
Serial.println(topicToSubscribe); | |
client.subscribe(topicToSubscribe); | |
} | |
// Values to send | |
float temperature = random(0, 9); | |
/* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/ | |
dtostrf(temperature, 4, 2, str_val); | |
sprintf(topic, "%s", ""); // Cleans the topic content | |
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL); | |
sprintf(payload, "%s", ""); // Cleans the payload content | |
sprintf(payload, "{\"%s\":", VARIABLE_LABEL); // Adds the variable label | |
sprintf(payload, "%s {\"value\": %s", payload, str_val); // Adds the value | |
sprintf(payload, "%s } }", payload); // Closes the dictionary brackets | |
client.publish(topic, payload); | |
client.loop(); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment