Adafruit_MQTT_Library
Adafruit_MQTT_Library copied to clipboard
No AdafruitIO feed change using Arduino Ethernet Shield
/*With this code I connect to the server, but get no changes in the feed. *
- This is a test of the AdafruitIO scheme to work with IFTTT in sending me an SMS message when security triggers.
*/
#include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h"
#include <Ethernet.h> #include <Ethernetclient.h> #include <SPI.h> #include <Dns.h> #include <Dhcp.h>
byte sdPin = 4; //SD control pin byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x55, 0x2A}; //MAC address of the Ethernet Board
#define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "my username" #define AIO_KEY "my key" #define FEED_PATH AIO_USERNAME"/feeds/door/"
EthernetClient client; // Initialize the Ethernet client library Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); #define halt(s) { Serial.println(F( s )); while(1);
int repeat = 0; int n = 0;
void setup() { Serial.begin(115200); //Turn on serial monitor pinMode(sdPin ,OUTPUT); //To prevent SD card interaction digitalWrite(sdPin, HIGH); //Turn off SD card slot on ethernet shield Serial.println(F("Adafruit MQTT demo"));
// Initialise the Client Serial.print(F("\nInit the Client...")); Ethernet.begin(mac); delay(1000); //give the ethernet a second to initialize
}
void loop() { if (repeat < 3){ door_open(); repeat++; }else mqtt.disconnect(); delay(10000);
}
void door_open(){ // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. bool sent = false; MQTT_connect();
//Send info to AdafruitIO sent = mqtt.publish (FEED_PATH, n); if(sent == true){ Serial.print(n++); Serial.println(" got sent!!!"); } }
// Function to connect and reconnect as necessary to the MQTT server. // Should be called in the loop function and it will take care if connecting. void MQTT_connect() { int8_t ret;
// Stop if already connected. if (mqtt.connected()) { return; }
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(5000); // wait 5 seconds }
/*With this version of the code I get the error code "Not authorized to connect". *
- This is a test of the AdafruitIO scheme to work with IFTTT in sending me an SMS message when security triggers.
*/
#include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h"
#include <Ethernet.h> #include <Ethernetclient.h> #include <SPI.h> #include <Dns.h> #include <Dhcp.h>
byte sdPin = 4; //SD control pin byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0x55, 0x2A}; //MAC address of the Ethernet Board
#define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 #define AIO_USERNAME "my username" #define AIO_KEY "my key" #define FEED_PATH AIO_USERNAME"/feeds/door/"
// Store the MQTT server, username, and password in flash memory. // This is required for using the Adafruit MQTT library. const char MQTT_SERVER[] PROGMEM = AIO_SERVER; const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME; const char MQTT_PASSWORD[] PROGMEM = AIO_KEY; const char DOOR_FEED[] PROGMEM = FEED_PATH;
EthernetClient client; // Initialize the Ethernet client library Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); Adafruit_MQTT_Publish door = Adafruit_MQTT_Publish(&mqtt, DOOR_FEED); #define halt(s) { Serial.println(F( s )); while(1);
int repeat = 0; int n = 0;
void setup() { Serial.begin(115200); //Turn on serial monitor pinMode(sdPin ,OUTPUT); //To prevent SD card interaction digitalWrite(sdPin, HIGH); //Turn off SD card slot on ethernet shield Serial.println(F("Adafruit MQTT demo"));
// Initialise the Client Serial.print(F("\nInit the Client...")); Ethernet.begin(mac); delay(1000); //give the ethernet a second to initialize
}
void loop() { if (repeat < 3){ door_open(); repeat++; } delay(15000);
}
void door_open(){ /* // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. bool sent = false; MQTT_connect();
//Send info to AdafruitIO sent = mqtt.publish (FEED_PATH, n); if(sent = true){ Serial.print(n++); Serial.println(" got sent!!!"); }*/ MQTT_connect(); // Now we can publish stuff! if (! door.publish(FEED_PATH, n++)) { Serial.println(F("Failed")); } else { Serial.println(F("OK!")); Serial.print(n - 1); Serial.println(" was sent!"); } }
// Function to connect and reconnect as necessary to the MQTT server. // Should be called in the loop function and it will take care if connecting. void MQTT_connect() { int8_t ret;
// Stop if already connected. if (mqtt.connected()) { return; }
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); mqtt.disconnect(); delay(5000); // wait 5 seconds } Serial.println("MQTT Connected!"); } Serial.println("MQTT Connected!"); }Thank you for opening an issue on an Adafruit Arduino library repository. To improve the speed of resolution please review the following guidelines and common troubleshooting steps below before creating the issue:
-
Do not use GitHub issues for troubleshooting projects and issues. Instead use the forums at http://forums.adafruit.com to ask questions and troubleshoot why something isn't working as expected. In many cases the problem is a common issue that you will more quickly receive help from the forum community. GitHub issues are meant for known defects in the code. If you don't know if there is a defect in the code then start with troubleshooting on the forum first.
-
If following a tutorial or guide be sure you didn't miss a step. Carefully check all of the steps and commands to run have been followed. Consult the forum if you're unsure or have questions about steps in a guide/tutorial.
-
For Arduino projects check these very common issues to ensure they don't apply:
-
For uploading sketches or communicating with the board make sure you're using a USB data cable and not a USB charge-only cable. It is sometimes very hard to tell the difference between a data and charge cable! Try using the cable with other devices or swapping to another cable to confirm it is not the problem.
-
Be sure you are supplying adequate power to the board. Check the specs of your board and plug in an external power supply. In many cases just plugging a board into your computer is not enough to power it and other peripherals.
-
Double check all soldering joints and connections. Flakey connections cause many mysterious problems. See the guide to excellent soldering for examples of good solder joints.
-
Ensure you are using an official Arduino or Adafruit board. We can't guarantee a clone board will have the same functionality and work as expected with this code and don't support them.
-
If you're sure this issue is a defect in the code and checked the steps above please fill in the following fields to provide enough troubleshooting information. You may delete the guideline and text above to just leave the following details:
-
Arduino board: INSERT ARDUINO BOARD NAME/UNO
-
Arduino IDE version (found in Arduino -> About Arduino menu): ** 1.8.9 **
-
List the steps to reproduce the problem below (if possible attach a sketch or copy the sketch code in too): LIST REPRO STEPS BELOW