Network with 2 MQTT servers (one on a Mac, one on a pi). Client on iphone can pub/sub to both; each can pub and sub to the other so doesn't seem to be MQTT side. Everything MQTT_VERSION_3_1_1
Can ping Heltec_WiFi_Lora32 from MQTT servers connection established.
DHCP issues same IP each time.
Note: On Arduino/PlatformIO Ethernet.h conflicts with SPI.h, so used Internet2 package (is this ok?)
Any clue how to overcome problem?
Thanks
#include <WiFi.h>
#include <PubSubClient.h>
#include "SPI.h"
#include <Ethernet2.h>
#include "SSD1306Wire.h"
#include <Wire.h>
const char* ssid = "HUAWEI_T222";
const char* password = "ABCDEF10293222";
const char* device_id = "dev01";
IPAddress server(192,168,8,102); // MQTT server (Mac)
WiFiClient espClient;
SSD1306Wire display(0x3c, 4, 15);
String rssi = "RSSI --";
String packSize = "--";
String packet ;
IPAddress ipIP; // dhcp ip ends 101
EthernetClient ethClient;
PubSubClient client(ethClient);
void connectToNetwork() {
WiFi.begin(ssid, password);
delay(1500);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connected to WiFi.."); // works
delay(500);
}
Serial.println("Connected to network");
ipIP = WiFi.localIP() ;
Serial.println(ipIP);
}
void connectToMQTTServer() {
Serial.println("Connecting to MQTT Server");
client.setServer(server, 1883);
client.setClient(ethClient);
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(device_id)) { // fails always
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("garden/light","works");
} else {
Serial.print("failed, rc= "); // always fails with -2
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
while (!Serial);
// WiFi.mode(WIFI_STA); // doesn't help
connectToNetwork();
connectToMQTTServer();
Serial.println("init ok");
}
void loop() {
Serial.println("In publishing loop");
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
int p = client.publish ("garden/light", "On");
if (p == true)
// display.drawString(40, 30, "Published");
else
// display.drawString(40, 30, "Pub Failed");
// display.display();
}
Because you are using WiFi and for PubSubClient client(ethClient) you are using EthernetClient ethClient so change it with WiFiClient espClient so your PubSubClient client(espClient) is now it will work.
Network with 2 MQTT servers (one on a Mac, one on a pi). Client on iphone can pub/sub to both; each can pub and sub to the other so doesn't seem to be MQTT side. Everything MQTT_VERSION_3_1_1
Can ping Heltec_WiFi_Lora32 from MQTT servers connection established. DHCP issues same IP each time.
Note: On Arduino/PlatformIO Ethernet.h conflicts with SPI.h, so used Internet2 package (is this ok?)
Any clue how to overcome problem? Thanks