SOME QUESTIONS REGARDING MQTT & UART

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.
Locked
User avatar
Conduct3r
 
Posts: 2
Joined: Thu Jul 14, 2022 3:43 pm

SOME QUESTIONS REGARDING MQTT & UART

Post by Conduct3r »

Hello there,
I am currently doing research for a project and I am trying to understand exactly what is going on as I have been trying to understand and use the MQTT protocol on the ESP32 QT-PY. It's purpose would be to gather the information to from the Arduino Uno (via UART) and defining two variables from there ( temperature & moisture percentage of soil) to send to Adafruit.io . My questions are quite a lot, but nonetheless I will leave it all here and try to see if someone can response to any while continuing research on the current matters at hand. The following are some questions based on issues I've ran into:

1. The research project's location would ultimately be at my school, which has a public Wi-Fi meaning there is no password, but as soon as you connect a pop up shows up asking for student ID & password to confirm it is a student connecting to it. With MQTT, is there a way to bypass this?

2. Is it possible to access the data without using serial0.readString() using UART? I have tried parsing the int and float I need from the string being printed in the serial monitor on the Uno, but the ESP32 always disconnects (red light) whenever I compile and forces a reset. Just attempting to see if there is another way I haven't figured out to get those variables from the Arduino Uno to the ESP32 QT-PY and then have it send that information to Adafruit.io.

It is a school project, so I have tried asking the IT dept. if they could help me resolve the Wi-Fi issue but they don't have a solution. The main program is functioning well and is great, so I am just trying to see if I could send that information somewhere we could graph these numbers being recorded. I will note that I have researched MQTT as a protocol and seen examples of it used with other devices to see if I am missing something, but maybe I'm missing something or misusing a function in the program.

Any suggestions/feedback is greatly appreciated!

User avatar
Conduct3r
 
Posts: 2
Joined: Thu Jul 14, 2022 3:43 pm

Re: SOME QUESTIONS REGARDING MQTT & UART

Post by Conduct3r »

//This is the program written for the ESP32 USING MQTT & UART





#include <WiFi.h>
#include "WiFiClientSecure.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// WiFi parameters
#define WLAN_SSID
#define WLAN_PASS
// Adafruit IO
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define IO_USERNAME
#define IO_KEY
WiFiClientSecure client;

//UART PINS
#define RX 16
#define TX 5

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, IO_USERNAME, IO_KEY);
Adafruit_MQTT_Publish moisturePercentage = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/moisturePercentage");
Adafruit_MQTT_Publish TemperatureF = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/TemperatureF");

int moistureP;
float TempF;
String stringReceived;


void setup() {
Serial.begin(115200);
// parameters : baud rate , protocol, receiver, transmitter
Serial0.begin(115200, SERIAL_8N1, RX, TX);

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());


// connect to adafruit io
connect();

}
// 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(10000);
}
Serial.println(F("Adafruit IO Connected!"));
}


void loop() {

stringReceived = Serial0.readString();
//Serial.print("Data received: ");
//Serial.println(stringReceived);
moistureP = Serial0.print(MoisturePercentage);
TempF = Serial0.print(tempF);
// ping adafruit io a few times to make sure we remain connected
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}
Serial.print(TempF); Serial.print(" *F, ");
Serial.print(moistureP); Serial.println(" %");
delay(5000);

if (! TemperatureF.publish(TempF)) { //Publish to Adafruit
Serial.println(F("Failed"));
}
if (! moisturePercentage.publish(moistureP)) { //Publish to Adafruit
Serial.println(F("Failed"));
}
else {
Serial.println(F("Sent!"));
}
delay(2000);

}

User avatar
brubell
Learn User Page
 
Posts: 2010
Joined: Fri Jul 17, 2015 10:33 pm

Re: SOME QUESTIONS REGARDING MQTT & UART

Post by brubell »

1. The research project's location would ultimately be at my school, which has a public Wi-Fi meaning there is no password, but as soon as you connect a pop up shows up asking for student ID & password to confirm it is a student connecting to it. With MQTT, is there a way to bypass this?
Try asking IT to allow-list the MAC address of your QT Py ESP32.

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”