adafruit / Adafruit_MQTT_Library

Arduino library for MQTT support
MIT License
571 stars 292 forks source link

No AdafruitIO feed change using Arduino Ethernet Shield #150

Open 2wheelD opened 5 years ago

2wheelD commented 5 years ago

/*With this code I connect to the server, but get no changes in the feed.

include "Adafruit_MQTT.h"

include "Adafruit_MQTT_Client.h"

include

include

include

include

include

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".

include "Adafruit_MQTT.h"

include "Adafruit_MQTT_Client.h"

include

include

include

include

include

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:

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: