Giter Club home page Giter Club logo

ubidots-mqtt-esp's Introduction

ubidots-mqtt-esp

MQTT library for connecting to Ubidots using MQTT protocol and an ESP8266 chip.

Setup

  1. Go to the Arduino IDE, click on Files -> Preferences and enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas
  2. Open Boards Manager from Tools -> Board menu and install esp8266 platform (and don’t forget to select your ESP8266 board from Tools > Board menu after installation)
  3. Download this library as .zip
  4. Now, click on Sketch -> Include Library -> Add .ZIP Library
  5. Select the .ZIP file that you have just downloaded and then “Accept” or “Choose”
  6. Go to Sketch/Program -> Include Library -> Library Manager and install the PubSubClient library
  7. Close the Arduino IDE and open it again.
  8. If you are using windows, please install the appropiate driver for your ESP board (CH340G if you are using a LoLin board or CP2102 if you are using an AMICA board)

Documentation

Constructor

Ubidots

Ubidots(char* token) Ubidots(char* token, char* clientName) Creates an Ubidots instance, you must setup as input your Ubidots TOKEN, the MQTT client name is optional and must be unique so we recommend you to insert random ASCII characters if you decide to use it, if you don't pass the clientName as parameter to the constructor the library will try to get the MAC of the device as default client name.

  • @token, [Required]. Your Ubidots unique account TOKEN.
  • @clientName, [Optional] [default] = device unique MAC address. The MQTT unique client id to be identified by the broker.

Methods

Ubidots

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. You can add 5 variables maximum before of publish them. Important: As this library depends on a Pubsubclient client, the max length of the JSON dictionary to send by default is 128 bytes, if you want to publish more than 3 variables and they have context or long names you should set at PubSubclient.h the MQTT_MAX_PACKET_SIZE to 512, you can see on your serial console the dictionary to POST if you call the setDebug(bool debug) method and pass a true value to it. For more information, refer to the PubSubclient official library: https://github.com/knolleary/pubsubclient

  • @variable_label, [Required]. The label of the variable where the dot will be stored.
  • @value, [Required]. The value of the dot.
  • @context, [Optional]. The dot's context.
  • @timestamp, [Optional]. The dot's timestamp in milliseconds.

begin(void (*callback)(char*,uint8_t*,unsigned int)) Sets the callback function for subscribed topics

  • @callback [Mandatory] Pointer to the callback function that will process the incoming data

connected() Returns True if the device is connected to the MQTT broker

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

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

ubidotsSetBroker(char* broker) 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".

  • @broker, [Optional] [default] = industrial.api.ubidots.com. The server to send data url.

setDebug(bool debug) Make available debug messages through the serial port.

ubidotsPublish(char *deviceLabel) Publishes the variables added to the specified device label.

  • @deviceLabel [Mandatory] Device label that stores the the values to be ingested

ubidotsSubscribe(char* deviceLabel, char* variableLabel) Subscribe to the specified device label and variable label of your Ubidots account.

  • @deviceLabel [Mandatory] Device label that stores the variable to retrieve values from
  • @variableLabel [Mandatory] Variable label to retrieve values from

bool connect(const char* clientName, const char* username, const char* password) Connects to the broker using a custom client name, username and password

  • @clientName, [Optional] [default] = device unique MAC address. The MQTT unique client id to be identified by the broker.
  • @username, [optional] [default] = Ubidots account token used in the constructor. The username to be identified by the broker
  • @password, [optional] [default] = Ubidots account token used in the constructor. The password to be identified by the broker

void wifiConnection(char* ssid, char* pass) Connects to an available WiFi network using WAP2.

  • @ssid [Mandatory] SSID of the network to connect to
  • @pass [Mandatory] WiFi network password

void disconnect() Disconnects gracefully from the broker, closing the socket

Examples

Please refer to examples folder.

ubidots-mqtt-esp's People

Contributors

crisap94 avatar jotathebest avatar jotaubi avatar marcoetmx avatar mashal1996 avatar metavix avatar per1234 avatar randomgenericusername avatar woakas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ubidots-mqtt-esp's Issues

publish a value with double value

when i am publishing value 6.9336972 as
client.add("stuff", 6.9336972);
client.ubidotsPublish("source1");

value is received as 6.93 to ubidots variable. How can I publish a double value without this issue? I want to publish latitude and longitude to the dashboard map.

Get the subscribe

How to get the value of the variable that the Ubidots sent to the ESP8266? I need to use it in esp8266 code.

publish example throws LoadStoreAlignmentCause exception

Hi,

I've successfully got WiFi to work on an Feather Huzzah ESP8266, but am unable to get MQTT to work. The publish example consistently throws an LoadStoreAlignmentCause error.

The stacktrace is

Exception 9: LoadStoreAlignmentCause: Load or store to an unaligned address
PC: 0x4020269a: WiFiClient::WiFiClient() at ./Library/Arduino15/packages/esp8266/hardware/esp8266/3.0.1/cores/esp8266/Print.h line 46
EXCVADDR: 0x00000017

Decoding stack results
0x40203424: HardwareSerial::write(unsigned char const*, unsigned int) at ./Library/Arduino15/packages/esp8266/hardware/esp8266/3.0.1/cores/esp8266/HardwareSerial.h line 193
0x40203a3c: Print::write(char const*) at ./Library/Arduino15/packages/esp8266/hardware/esp8266/3.0.1/cores/esp8266/Print.h line 59
0x40201634: Ubidots::Ubidots(char*) at ./Documents/Arduino/libraries/ubidots-mqtt-esp-master/src/UbidotsESPMQTT.cpp line 39
0x402010c0: setup() at ./Documents/Arduino/sketch_wifi/sketch_wifi.ino line 48
0x40204d54: loop_wrapper() at ./Library/Arduino15/packages/esp8266/hardware/esp8266/3.0.1/cores/esp8266/core_esp8266_main.cpp line 198

Here's the console output, with debug for WIFI enabled. Any thoughts much appreciated!!!

.....scandone
state: 0 -> 2 (b0)
.state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 2
cnt 

connected with paustian-huey, channel 11
dhcp client start...
wifi evt: 0
....ip:192.168.86.20,mask:255.255.255.0,gw:192.168.86.1
wifi evt: 3
.WiFi connected
IP address: 
192.168.86.20
Fatal exception 9(LoadStoreAlignmentCause):
epc1=0x4020269a, epc2=0x00000000, epc3=0x00000000, excvaddr=0x00000017, depc=0x00000000

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Exception (9):
epc1=0x4020269a epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000017 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffdb0 end: 3fffffc0 offset: 0190
3fffff40:  40203424 3ffe8985 3ffee7f0 40203a3c  
3fffff50:  3fffdad0 0000000d 3ffee7f0 1c4466df  
3fffff60:  3fffdad0 1456a8c0 00ffffff 3ffee880  
3fffff70:  3fffdad0 3ffee7b4 3ffee7f0 40201634  
3fffff80:  feefeffe feefeffe 402081cc 1456a8c0  
3fffff90:  3fffdad0 00000000 3ffee728 402010c0  
3fffffa0:  feefeffe feefeffe 3ffee86c 40204d54  
3fffffb0:  feefeffe feefeffe 3ffe85dc 40100ef9  
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3460, room 16 
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4 
tail 4
chksum 0xc9
csum 0xc9
v000526a0
~ld

SDK:2.2.2-dev(38a443e)/Core:3.0.1=30001000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:c0b69df

subscription not reliable

I have the issue that following code will not return a broken subscription:

if (!client.connected()) { client.reconnect(); subscribe(); }

After some hours the subscription is not present anymore although client.connected() returns true!
The publishing though keeps on working.

This may be related to wifi based ip address changes happening.

Is there a better way to test subscription than the above from the examples?

Static IP

Hi @woakas, and @jotathebest
Thank you for this wonderful work.
I like to add static IP to ubidots publish code in order to connect with router by this IP. I tried the following modification but it lead to appear the error in the attached screenshot.
Any Suggestion is appreciated.

#include "UbidotsESPMQTT.h"

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

  • Define Constants
    ****************************************/
    #define TOKEN "---" // Your Ubidots TOKEN
    #define WIFINAME "----" //Your SSID
    #define WIFIPASS "-----" // Your Wifi Pass

Ubidots client(TOKEN);

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

  • 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();
}
/****************************************

  • Main Functions
    ****************************************/
    void setup() {
    IPAddress staticIP224_60(192,168,1,70);
    IPAddress gateway224_60(192,168,1,1);
    IPAddress subnet224_60(255,255,255,0);
    IPAddress dns224_60(192,168,1,1);// DNS server IP
    Serial.begin(115200);
    client.setDebug(true); // Pass a true or false bool value to activate debug messages
    WiFi.begin(WIFINAME, WIFIPASS);
    WiFi.config(staticIP224_60, gateway224_60, subnet224_60, dns224_60);
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
    }
    Serial.println(F("WiFi connected"));
    Serial.println(F("IP address: "));
    Serial.println(WiFi.localIP());
    client.begin(callback);
    }
    void loop() {
    // put your main code here, to run repeatedly:
    if(!client.connected()){
    client.reconnect();
    }

// Publish values to 2 different data sources
client.add("stuff", 10.2); //Insert your variable Labels and the value to be sent
client.ubidotsPublish("source1");
client.add("stuff", 10.2);
client.add("more-stuff", 120.2);
client.ubidotsPublish("source2");
client.loop();
delay(5000);
}
image

Expose espClient publicly

Requires a very small change in Source Code.
Currently, espClient is a private member, which prevents users from using this already connected Client for other functions. Is there any specific reason to make this private member?

request subscription update

Don't know how close this is related to this library, but in practical use it turns out to be a design issue if I cant request an update of the subscription on bootup.

Given that my IoT device might reboot or sleep I found no way to request or trigger an update of the subscribed topic. Also, if the device might have been disconnected how can I retrieve the latest value?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.