Closed drewsed closed 4 years ago
Sorry, forgot to mention, that i need example especially for wiznet w7500p. There are several examples for other chips, I can't get running on w7500p
An MQTT example will be really helpful. I've just managed to use TCPSocket so maybe after a bit of tinkering, I'll handle MQTT too, though it'll be easier with the example.
Ok, I got a TCP Socket working, and also a MQTT connection. But after about 10 minutes publising is returning NSAPI_ERROR_NO_CONNECTION. A reconnect attempt most of the time fails. This behaviour happens with both, this library and the mbed version (https://os.mbed.com/teams/WIZnet/code/WIZnetInterface-OS5/).
I have no idea how to debug this. any help is really appreciated. Other aproaches with TCPSocketConnection sockets failed also after few minutes. Also the MQTTClient::yield() funktion is not working probably with the WizNet sockets. I do not think that this is a problem of the MQTT Lib, because all MQTTClient::yield regarding posts I've read were resolved and should work at this time.
I am using mbed 5.15.0 and testet all versions from 5.12.0 on up to the latest. Mbed studio 0.8.0 WizWiki W7500P Eval Board
Which is the latest supportet combination of WiZnet components on mbed? Mbed Os 5.15.0 with this github Wiznetinterface library or better with the Wiznetinterface from mbed website? It would be great if someone could clarify this.
This is my code with mqtt lib and github Wiznetinterface lib with minor modificatiions to make it compile. (Rename CONNECT enum to avoid conflict with MQTT Lib and comment out TLS Constructors of MQTTClient (I can upload these changes on request))
#include "mbed.h"
#include "WIZnetInterface.h"
#include <MQTTClientMbedOs.h>
DigitalOut relais11(LED_BLUE);//D11
void relais11_messageArrived(MQTT::MessageData& md)
{
MQTT::Message &message = md.message;
#ifdef SERIAL
//printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\r\n", message.qos, message.retained, message.dup, message.id);
#endif
if( strncmp((char*)message.payload,"ON",message.payloadlen) == 0 ){
relais11 = 0;
}
else if( strncmp((char*)message.payload,"OFF",message.payloadlen) == 0 ){
relais11 = 1;
}
}
// main() runs in its own thread in the OS
int main()
{
// ### Connection details ###
uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x1E, 0x72, 0x1F};
uint8_t host_ip[4] = {192,168,178,42};
const char* hostname = (char *)"192.168.178.42";
//const char* hostname = (const char *)"NUC.fritz.box";
int port = 1883;
const char* DnsServerIP = (const char *)"192.168.178.1";
const char* wiz_ip = (char *)"192.168.178.43";
const char* wiz_subnet = (char *)"255.255.255.0";
const char* wiz_gateway = (char *)"192.168.178.1";
WIZnetInterface eth;
//int rc = eth.init(mac_addr); // with DHCP
int rc = eth.init(mac_addr,wiz_ip,wiz_subnet,wiz_gateway); // no DHCP
printf("[WIZNET] init returned: %d \n", rc);
rc = eth.connect();
if (rc != 0){ printf("[WIZNET] connect returned: %d\n", rc); }
else printf("[WIZNET] Connection was successful \r\n[WIZNET] IP: %s \n",eth.get_ip_address());
TCPSocket socket;
//socket.set_blocking(false);
rc= socket.open(ð);
if (rc != 0){ printf("[TCP Socket] Connect returned: %d \r\n",rc); }
else printf("[TCP Socket] Init was successful\r\n");
rc = socket.connect(hostname, port);
if (rc != 0){ printf("[TCP Socket] Connect returned: %d \r\n",rc); }
else printf("[TCP Socket] Connection was successful\r\n");
MQTTClient mqttclient(&socket);
char MQTTClientID[30];
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
sprintf(MQTTClientID,"WIZwiki-W7500P");
data.clientID.cstring = MQTTClientID;
data.username.cstring = (char *)"wiznet";
data.password.cstring = (char *)"";
if ((rc = mqttclient.connect(data)) != 0){ printf("[MQTT] Connect returned: %d \r\n",rc); }
else printf("[MQTT] Connection was successful\r\n");
const char* relais11_topic = (char *)"/wiznet/relais11";
if ((rc = mqttclient.subscribe(relais11_topic, MQTT::QOS1, relais11_messageArrived)) != 0)
{
printf("[MQTT] Subscribe returned %d \r\n", rc); return 0;
}
else printf("[MQTT] Added subscription for relais11_topic \r\n");
MQTT::Message message;
char buf[100];
message.qos = MQTT::QOS1;
message.retained = false;
message.dup = false;
sprintf(buf, "Just Started");
message.payload = (void*)buf;
message.payloadlen = strlen(buf);
rc = mqttclient.publish("/wiznet",message);
while (true) {
//if (eth.){
// printf("[WIZNET] link down\n");
// rc = eth.init(mac_addr); // with DHCP
// rc = eth.connect();
//}
//else
printf("[LOOP] running... \n");
if ( (rc = socket.send(mac_addr, 0)) < 0 ) printf("[TCP Socket] Socket disconnected %d\n", rc);
else printf("[TCP Socket] Socket connected %d\n", rc);
rc = mqttclient.publish("/wiznet",message);
if ( rc != 0 ){
printf("[MQTT] Publish failed %d\n", rc);
if ( rc == NSAPI_ERROR_NO_CONNECTION){
//reconnect mqttclient
if ((rc = mqttclient.connect(data)) != 0){ printf("[MQTT] Reconnect returned: %d \r\n",rc); }
else printf("[MQTT] Reconnect was successful\r\n");
}
}
else printf("[MQTT] Publish success %d \r\n", rc);
//mqttclient.yield();
ThisThread::sleep_for(1000);
}
}
Hi, I would really appreciate, if you could provide example code to get at least a mbed os5 TCPSocket running. Or even Better a example for running a mqtt client in mbed os 5. (https://github.com/ARMmbed/mbed-mqtt)
Thanks a lot.
drewsed