๐ŸŽ„โ›„โ„๏ธ๐ŸŽ It's beginning to look a lot like ADABOX-mas! The next ADABOX from Adafruit ships soon! Get yours now, or give as a gift! ๐ŸŽ„โ›„โ„๏ธ๐ŸŽ
0

Arduino MKR 4000 Board MQTT Connection Fails
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.
Please be positive and constructive with your questions and comments.

Arduino MKR 4000 Board MQTT Connection Fails

by stlentz on Wed Oct 23, 2019 4:55 pm

I'm trying to send a feed from an Arduino MKR 4000 board with built in NINA Wifi. I have modified the WINC1500 sketch for testing. The sketch compiles and runs. Note that the Wifi connection is moved from MQTT_connect to setup, otherwise I have changed as little as possible. The wifi connection is good; I can ping the Arduino from my router. However, the MQTT connection fails. I have checked the ID and KEY repeatedly. I have a 50K pot in place of the photocell; eventually I plan to send frequency data collected by the FPGA.

Has anyone used the MKR 4000 board or any board with the WiFi NINA library?
Am I missing something obvious?
Any way to troubleshoot this other than Wireshark?

Code is below, thanks in advance.

/***************************************************
Adafruit MQTT Library WINC1500 Example
Modified for Arduino MKR4000

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <SPI.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <WiFiNINA.h>

/************************* WiFI Setup *****************************/
char ssid[] = "xxxx"; // your network SSID (name)
char pass[] = "xxxx"; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS; // the Wifi radio's status
IPAddress ip(192,168,1,12);
IPAddress dns(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);


/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "xxxx"
#define AIO_KEY "xxxx"

/************ Global State (you don't need to change this!) ******************/

//Set up the wifi client
WiFiClient client;

Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1); }

/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");

// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");

/*************************** Sketch Code ************************************/

#define LEDPIN 13


void setup() {

while (!Serial);
Serial.begin(115200);

Serial.println(F("Adafruit MQTT demo for MKR4000"));

WiFi.config(ip, dns, gateway, subnet);
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
}

// you're connected now, so print out the data:
Serial.println("You're connected to the network");

pinMode(LEDPIN, OUTPUT);
mqtt.subscribe(&onoffbutton);
}

uint32_t x=0;

void loop() {
// 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.
MQTT_connect();

// this is our 'wait for incoming subscription packets' busy subloop
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);

if (0 == strcmp((char *)onoffbutton.lastread, "OFF")) {
digitalWrite(LEDPIN, LOW);
}
if (0 == strcmp((char *)onoffbutton.lastread, "ON")) {
digitalWrite(LEDPIN, HIGH);
}
}
}

// Now we can publish stuff!
Serial.print(F("\nSending photocell val "));
Serial.print(x);
Serial.print("...");
if (! photocell.publish(x++)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}

}

// 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!");
}

stlentz
 
Posts: 2
Joined: Wed Oct 23, 2019 3:09 pm

Re: Arduino MKR 4000 Board MQTT Connection Fails

by stlentz on Fri Oct 25, 2019 11:24 am

Hey everyone - I got a working sketch by modifying Brubell's sketch found here:

https://github.com/adafruit/Adafruit_MQ ... sp8266.ino

I converted this back to normal MQTT on port 1883, used WiFiClient instead of WiFiClientSecure, and deleted the lines dealing with the fingerprint and all is good.

Not sure what I did wrong before, maybe as simple as DHCP works better than static IP config.

Thanks for checking.

stlentz
 
Posts: 2
Joined: Wed Oct 23, 2019 3:09 pm

Please be positive and constructive with your questions and comments.