My whole Day i tried to get my code run.
My plan is to redirect Serial Values with an Arduino Uno and an W5500 Shield to my Openhab Mosquitto Broker.
I guess my Code seems like a mess, but its the first time i try to create my own Code.
It is written in Arduino IDE 2.0.3
I Guess you will see foults as well, which i have not seen, than please let me know.
The Problem i have
I could connect to my local Wifi, and could receive my serial Data, but can not connect to my Mosquitto Broker.
i tried everything i could find.
The foult is:
Initialize Ethernet with DHCP:
DHCP assigned IP 192.168.178.31
Could not send message :(
Attempting MQTT connection...failed, rc=5 try again in 5 seconds
Please, i hope you can help me.
`
/*
Web client
This sketch connects to a website (http://www.google.com)
using an Arduino WIZnet Ethernet shield.
Circuit:
Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
include
include
include
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 178, 253);
IPAddress myDns(192, 168, 178, 1);
IPAddress server(192, 168, 178, 10);
char* clientId = "ESPNOW";
define MQTTCONNECT
void callback(char topic, byte payload, unsigned int length);
void subscribeReceive(char topic, byte payload, unsigned int length);
const char mqttServer = "192.168.178.10"; //real server cannot be given due to privacy reasons
const int mqttPort = 1883;
const char mqttUser = "openhabian";
const char* mqttPass = "o\qLG3brUr9g=pQP75";
//#define MQTT_VERSION MQTT_VERSION_3_1
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
void subscribeReceive(char topic, byte payload, unsigned int length)
{
// Print the topic
Serial.print("Topic: ");
Serial.println(topic);
// Print the message
Serial.print("Message: ");
for(int i = 0; i < length; i ++)
{
Serial.print(char(payload[i]));
}
// Print a newline
Serial.println("");
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("openhabian")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic","hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH Shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet
// Open serial communications and wait for port to open:
Serial.begin(115200);
{
// Set the MQTT server to the server stated above
client.setServer(server, 1883);
client.setCallback(callback);
// Attempt to connect to the server with the ID "myClientID"
{
if (client.connect(clientId, mqttUser, mqttPass));
Serial.println("Connection has been established, well done");
client.setCallback(subscribeReceive);
}
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to configure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
// Note - the default maximum packet size is 128 bytes. If the
// combined length of clientId, username and password exceed this use the
// following to increase the buffer size:
client.setBufferSize(255);
void loop() {
client.loop();
client.subscribe("ESPNOW");
if(client.publish("ESPNOW", "Hello World"))
{
Serial.println("Publish message success");
}
else
{
Serial.println("Could not send message :(");
}
if (!client.connected()) {
reconnect();
}
client.loop();
if(Serial.available()) // Chek for availablity of data at Serial Port
{
char data = Serial.read(); // Reading Serial Data and saving in data variable
Serial.print(data); // Printing the Serial data
}
}
`
Happy 2023 everyone,
My whole Day i tried to get my code run. My plan is to redirect Serial Values with an Arduino Uno and an W5500 Shield to my Openhab Mosquitto Broker. I guess my Code seems like a mess, but its the first time i try to create my own Code. It is written in Arduino IDE 2.0.3 I Guess you will see foults as well, which i have not seen, than please let me know.
The Problem i have I could connect to my local Wifi, and could receive my serial Data, but can not connect to my Mosquitto Broker.
i tried everything i could find.
The foult is: Initialize Ethernet with DHCP: DHCP assigned IP 192.168.178.31 Could not send message :( Attempting MQTT connection...failed, rc=5 try again in 5 seconds
Please, i hope you can help me.
` /* Web client
This sketch connects to a website (http://www.google.com) using an Arduino WIZnet Ethernet shield.
Circuit:
Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009 by David A. Mellis modified 9 Apr 2012 by Tom Igoe, based on work by Adrian McEwen
*/
include
include
include
// Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Set the static IP address to use if the DHCP fails to assign IPAddress ip(192, 168, 178, 253); IPAddress myDns(192, 168, 178, 1); IPAddress server(192, 168, 178, 10);
char* clientId = "ESPNOW";
define MQTTCONNECT
void callback(char topic, byte payload, unsigned int length); void subscribeReceive(char topic, byte payload, unsigned int length);
const char mqttServer = "192.168.178.10"; //real server cannot be given due to privacy reasons const int mqttPort = 1883; const char mqttUser = "openhabian"; const char* mqttPass = "o\qLG3brUr9g=pQP75";
//#define MQTT_VERSION MQTT_VERSION_3_1
// Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): EthernetClient ethClient; PubSubClient client(server, 1883, callback, ethClient);
void callback(char topic, byte payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i=0;i<length;i++) { Serial.print((char)payload[i]); } Serial.println(); }
void subscribeReceive(char topic, byte payload, unsigned int length) { // Print the topic Serial.print("Topic: "); Serial.println(topic);
// Print the message Serial.print("Message: "); for(int i = 0; i < length; i ++) { Serial.print(char(payload[i])); }
// Print a newline Serial.println(""); }
void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("openhabian")) { Serial.println("connected"); // Once connected, publish an announcement... client.publish("outTopic","hello world"); // ... and resubscribe client.subscribe("inTopic"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } }
// Variables to measure the speed unsigned long beginMicros, endMicros; unsigned long byteCount = 0; bool printWebData = true; // set to false for better speed measurement
void setup() { // You can use Ethernet.init(pin) to configure the CS pin Ethernet.init(10); // Most Arduino shields //Ethernet.init(5); // MKR ETH Shield //Ethernet.init(0); // Teensy 2.0 //Ethernet.init(20); // Teensy++ 2.0 //Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet //Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet // Open serial communications and wait for port to open: Serial.begin(115200); { // Set the MQTT server to the server stated above client.setServer(server, 1883); client.setCallback(callback);
// Attempt to connect to the server with the ID "myClientID" { if (client.connect(clientId, mqttUser, mqttPass));
} // start the Ethernet connection: Serial.println("Initialize Ethernet with DHCP:"); if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP");
} else { Serial.print(" DHCP assigned IP "); Serial.println(Ethernet.localIP()); } // give the Ethernet shield a second to initialize: delay(1000);
// Note - the default maximum packet size is 128 bytes. If the // combined length of clientId, username and password exceed this use the // following to increase the buffer size: client.setBufferSize(255);
if (client.connect("openhabian", "openhabian", "Admin12345")) { client.publish("outTopic","hello world"); client.subscribe("inTopic"); }
beginMicros = micros(); }
void loop() { client.loop(); client.subscribe("ESPNOW"); if(client.publish("ESPNOW", "Hello World")) { Serial.println("Publish message success"); } else { Serial.println("Could not send message :("); } if (!client.connected()) { reconnect(); } client.loop();
if(Serial.available()) // Chek for availablity of data at Serial Port { char data = Serial.read(); // Reading Serial Data and saving in data variable Serial.print(data); // Printing the Serial data }
} `
Kind regards,
Mordi