0

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

MQTT

by sobellinni on Thu Jul 29, 2021 2:33 pm

MQTT library disables writing to D0 and D5

this program write to the pins to check operation
/*
Wemos D1 pin finder
Aduino pin number vs Wemos D1 mini printed number on board
0=RX
1=TX
2=D0
3=D1
4=D2
5=D5
6=D6
7=D7
8=D3
9=D4
10=D8
11=D7

*/

#include <ESP8266WiFi.h>


void setup() {
Serial.begin(115200);
pinMode(D0,OUTPUT);
pinMode(D1,OUTPUT);
pinMode(D2,OUTPUT);
pinMode(D3,OUTPUT);
pinMode(D4,OUTPUT);
pinMode(D5,OUTPUT);
pinMode(D6,OUTPUT);
pinMode(D7,OUTPUT);
pinMode(D8,OUTPUT);

pinMode(RX,OUTPUT);
pinMode(TX,OUTPUT);

}
void loop() {
digitalWrite(D0,HIGH);
digitalWrite(D1,HIGH);
delay(10);
digitalWrite(D1,LOW);
digitalWrite(D2,HIGH);
delay(10);
digitalWrite(D2,LOW);
digitalWrite(D3,HIGH);
delay(10);
digitalWrite(D3,LOW);
digitalWrite(D4,HIGH);
delay(10);
digitalWrite(D4,LOW);
digitalWrite(D5,HIGH);
delay(10);
digitalWrite(D5,LOW);
digitalWrite(D6,HIGH);
delay(10);
digitalWrite(D6,LOW);
digitalWrite(D7,HIGH);
delay(10);
digitalWrite(D7,LOW);
digitalWrite(D8,HIGH);
delay(10);
digitalWrite(D8,LOW);
digitalWrite(RX,HIGH);
delay(10);
digitalWrite(RX,LOW);

digitalWrite(TX,HIGH);
delay(10);
digitalWrite(TX,LOW);





digitalWrite(D0,LOW);
delay(100);
}


This program shouls have operated all the pins

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define WIFI_SSID "SSID"
#define WIFI_PASS "PASS"

#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME "account"
#define MQTT_PASS "Big_Number"



WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);

Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/onoff");
Adafruit_MQTT_Publish LightsStatus = Adafruit_MQTT_Publish(&mqtt, MQTT_NAME "/f/LightsStatus");


void setup()
{
Serial.begin(9600);

pinMode(D0, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
pinMode(RX, OUTPUT);
pinMode(TX, OUTPUT);

digitalWrite(D0, LOW);
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
digitalWrite(D6, LOW);
digitalWrite(D7, LOW);
digitalWrite(D8, LOW);
digitalWrite(RX, LOW);
digitalWrite(TX, LOW);





//Connect to WiFi
Serial.print("\n\nConnecting Wifi>");
WiFi.begin(WIFI_SSID, WIFI_PASS);
digitalWrite(LED_BUILTIN, LOW);

while (WiFi.status() != WL_CONNECTED)
{
Serial.print(">");
delay(50);
}

Serial.println("OK!");

//Subscribe to the onoff topic
mqtt.subscribe(&onoff);



}

void loop()
{
//Connect/Reconnect to MQTT
MQTT_connect();

//Read from our subscription queue until we run out, or
//wait up to 5 seconds for subscription to update
Adafruit_MQTT_Subscribe * subscription;
while ((subscription = mqtt.readSubscription(5000)))
{
//If we're in here, a subscription updated...
if (subscription == &onoff)
{
//Print the new value to the serial monitor
Serial.print("onoff: ");
Serial.println((char*) onoff.lastread);

//If the new value is "ON", turn the light on.
//Otherwise, turn it off.
if (!strcmp((char*) onoff.lastread, "ON"))
{
//active low logic
digitalWrite(D0, LOW);
digitalWrite(D1, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, HIGH);
digitalWrite(D6, HIGH);
digitalWrite(D7, HIGH);
digitalWrite(D8, HIGH);
digitalWrite(RX, HIGH);
digitalWrite(TX, HIGH);
LightsStatus.publish("ON");
}
else if (!strcmp((char*) onoff.lastread, "OFF"))
{


digitalWrite(D0, LOW);
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
digitalWrite(D6, LOW);
digitalWrite(D7, LOW);
digitalWrite(D8, LOW);
digitalWrite(RX, LOW);
digitalWrite(TX, LOW);
LightsStatus.publish("OFF");

}
else
{
LightsStatus.publish("ERROR");
}
}
else
{
//LightsStatus.publish("ERROR");
}
}
// if (!mqtt.ping())
// {
// mqtt.disconnect();
// }
}


void MQTT_connect()
{

// // Stop if already connected
if (mqtt.connected() && mqtt.ping())
{
// mqtt.disconnect();
return;
}

int8_t ret;

mqtt.disconnect();

Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
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
retries--;
if (retries == 0)
{
ESP.reset();
}
}
Serial.println("MQTT Connected!");
}

sobellinni
 
Posts: 3
Joined: Thu Feb 14, 2013 8:48 pm

Re: MQTT

by brubell on Fri Jul 30, 2021 9:45 am

Could you provide a link to the MQTT library you're using?

brubell
 
Posts: 1512
Joined: Fri Jul 17, 2015 10:33 pm

Re: MQTT

by sobellinni on Sun Aug 01, 2021 11:40 pm

now that I posted this I found it was my programming error

sobellinni
 
Posts: 3
Joined: Thu Feb 14, 2013 8:48 pm

Please be positive and constructive with your questions and comments.