Has anyone experienced example of code that how to use the library MQTT
that support enough for QoS 0 and 1
Regards
example code for Qos 0 and 1
Moderators: adafruit_support_bill, adafruit
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
- uniontownlabs
- Posts: 112
- Joined: Wed Dec 17, 2014 10:56 pm
Re: example code for Qos 0 and 1
Which language and platform are you planning on using? Raspberry Pi (with Node.js, Ruby, Python), Arduino, ESP8266, etc
- fapuc2
- Posts: 13
- Joined: Fri Oct 16, 2015 8:29 am
Re: example code for Qos 0 and 1
Thanks for the reply,
I successfully tested this example
I would add with quality of service 0 or 1.
with ESP8266 environment "arduino-1.6.5-R5" and Adafruit MQTT Library version=0.12
My application is control actuator skylight roof
through feed "lamp" and its effective closure with feed "button"
https://io.adafruit.com/fapuc2
I successfully tested this example
I would add with quality of service 0 or 1.
with ESP8266 environment "arduino-1.6.5-R5" and Adafruit MQTT Library version=0.12
My application is control actuator skylight roof
through feed "lamp" and its effective closure with feed "button"
https://io.adafruit.com/fapuc2
Code: Select all
/***************************************************
Adafruit MQTT Library ESP8266 Example
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board:
----> https://www.adafruit.com/product/2471
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
Adafruit IO example additions by Todd Treece.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
/****************************** Pins ******************************************/
#define LAMP 2 // Led o BANNED Relè
#define BUTTON 0 // switch fine corsa normalmente aperto //****************
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "............"
#define WLAN_PASS "......................."
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "........."
#define AIO_KEY "................................."
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Store the MQTT server, client ID, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);
/****************************** Feeds ***************************************/
// Setup a feed called 'lamp' for subscribing to changes.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname> //*****************************************************
const char LAMP_FEED[] PROGMEM = AIO_USERNAME "/feeds/lamp"; //***
Adafruit_MQTT_Subscribe lamp = Adafruit_MQTT_Subscribe(&mqtt, LAMP_FEED); //*** Subscribe (Riceve BANNED x relè motore )
const char BUTTON_FEED[] PROGMEM = AIO_USERNAME "/feeds/button"; //***
Adafruit_MQTT_Publish button = Adafruit_MQTT_Publish(&mqtt, BUTTON_FEED); //*** Publish (Trasmette lo stato del fine corsa)
//*******************************************************************************************************************************
/*************************** Sketch Code ************************************/
// button state
int current = 0;
int currentB = 0;
int last = -1;
int lastB = -1;
void setup() {
// set pin ingresso uscita ESP8266
pinMode(LAMP, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP); //**************** "INPU_PULLUP" se ardware manca resistenza fisica di pullup verso +vcc
Serial.begin(115200);
Serial.println(F("Adafruit IO Example"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
delay(10);
Serial.print(F("Connecting to "));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
// listen for events on the lamp feed
mqtt.subscribe(&lamp);
// mqtt.subscribe(&button); //**************** button è solo pubblicato
// connect to adafruit io
connect();
digitalWrite(LAMP,LOW); // stato iniziale al boot
digitalWrite(BUTTON,LOW); // stato iniziale al boot ***************
}
void loop() {
Adafruit_MQTT_Subscribe *subscription;
// ping adafruit io a few times to make sure we remain connected
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}
// feeds/button ****************************************************************************************************************
// grab the current state of the button
current = digitalRead(BUTTON);
// return if the value hasn't changed // return se ilvalore non cambia //**************************
// if(current == last) // abilita questa riga e quella sopra e sotto
// return; // per aspettare il cambio di stato prima di fare nuovo invio
// invece se queste righe commentate questo feeds invia ciclicamente il suo stato
int32_t value = (current == LOW ? 1 : 0);
// Now we can publish stuff!
Serial.print(F("\nSending button value: "));
Serial.print(value);
Serial.print("... ");
if (! button.publish(value))
Serial.println(F("Failed."));
else
Serial.println(F("Success!"));
delay(5000); // *************************************
// 5 secondi // intervallo x ogni invio
// save the button state
last = current;
// feeds/lamp ****************************************************************************************************************
// this is our 'wait for incoming subscription packets' busy subloop
while (subscription = mqtt.readSubscription(1000)) {
// we only care about the lamp events
if (subscription == &lamp) {
// convert mqtt ascii payload to int
char *value = (char *)lamp.lastread;
Serial.print(F("Received: "));
Serial.println(value);
int current = atoi(value);
// write the current state to the power switch tail
digitalWrite(LAMP, current == 1 ? HIGH : LOW);
}
}
}
// connect to adafruit io via MQTT
void connect() {
Serial.print(F("Connecting to Adafruit IO... "));
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
switch (ret) {
case 1: Serial.println(F("Wrong protocol")); break;
case 2: Serial.println(F("ID rejected")); break;
case 3: Serial.println(F("Server unavail")); break;
case 4: Serial.println(F("Bad user/pass")); break;
case 5: Serial.println(F("Not authed")); break;
case 6: Serial.println(F("Failed to subscribe")); break;
default: Serial.println(F("Connection failed")); break;
}
if(ret >= 0)
mqtt.disconnect();
Serial.println(F("Retrying connection..."));
delay(5000);
}
Serial.println(F("Adafruit IO Connected!"));
}
- adafruit2
- Posts: 22148
- Joined: Fri Mar 11, 2005 7:36 pm
Re: example code for Qos 0 and 1
right now we only support QoS 0, you can always subscribe to the same feed you're publishing to, and we have upgraded to 3.1.1 which makes QoS1 a little easier but yah - its not supported right now and no ETA
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.