Paho MQTT Client example with W7500
Dependencies: DHT MQTTforLecture WIZnetInterface mbed-src
Fork of w7500-paho-mqtt by
Code
WIZwiki W7500 with paho mqtt client.
Import programw7500-paho-mqtt
Paho MQTT Client example with W7500
You have to change
You have to use your ethernet(network) information.
MQTTEthernet.h
uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x00, 0x00}; // your mac address const char * ip_addr = "???.???.???.???"; // your ip const char * gw_addr = "???.???.???.???"; // your gateway const char * snmask = "???.???.???.???"; // your subnetmask
And you have to use your mqtt broker server information. Broker server ip address and port number listed bleow are available now. But I will close the mqtt broker server on July 15,2015.
main.cpp
char* hostname = "104.199.146.45"; int port = 1883;
Platform
WIZwiki W7500 [ https://developer.mbed.org/platforms/WIZwiki-W7500/ ]
Component
YWRobot Easy module shield [ where to buy ] (This is not mbed component)
Test software
PC
MQTT-FX - A JavaFX based MQTT Client [ Home page ]
Mobile(iPhone)
ZMQTT-UTILITY - MQTT Test utility|You can download from App store(iPhone).
main.cpp@7:a5ead1402704, 2015-05-04 (annotated)
- Committer:
- mbedAustin
- Date:
- Mon May 04 22:47:02 2015 +0000
- Revision:
- 7:a5ead1402704
- Parent:
- 3:36fd3cfad85a
- Child:
- 8:f837e0d255e8
updated example code to be more verbose
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:38cbb854d85f | 1 | #include "mbed.h" |
emilmont | 1:5cebe0e38cd2 | 2 | #include "EthernetInterface.h" |
emilmont | 1:5cebe0e38cd2 | 3 | |
emilmont | 3:36fd3cfad85a | 4 | #define ECHO_SERVER_PORT 7 |
emilmont | 3:36fd3cfad85a | 5 | |
emilmont | 1:5cebe0e38cd2 | 6 | int main (void) { |
emilmont | 1:5cebe0e38cd2 | 7 | EthernetInterface eth; |
emilmont | 1:5cebe0e38cd2 | 8 | eth.init(); //Use DHCP |
emilmont | 1:5cebe0e38cd2 | 9 | eth.connect(); |
mbedAustin | 7:a5ead1402704 | 10 | printf("\nServer IP Address is %s\n", eth.getIPAddress()); |
emilmont | 1:5cebe0e38cd2 | 11 | |
emilmont | 1:5cebe0e38cd2 | 12 | TCPSocketServer server; |
emilmont | 3:36fd3cfad85a | 13 | server.bind(ECHO_SERVER_PORT); |
emilmont | 3:36fd3cfad85a | 14 | server.listen(); |
emilmont | 1:5cebe0e38cd2 | 15 | |
emilmont | 1:5cebe0e38cd2 | 16 | while (true) { |
emilmont | 1:5cebe0e38cd2 | 17 | printf("\nWait for new connection...\n"); |
emilmont | 1:5cebe0e38cd2 | 18 | TCPSocketConnection client; |
emilmont | 1:5cebe0e38cd2 | 19 | server.accept(client); |
emilmont | 3:36fd3cfad85a | 20 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
emilmont | 1:5cebe0e38cd2 | 21 | |
emilmont | 1:5cebe0e38cd2 | 22 | printf("Connection from: %s\n", client.get_address()); |
emilmont | 1:5cebe0e38cd2 | 23 | char buffer[256]; |
emilmont | 1:5cebe0e38cd2 | 24 | while (true) { |
emilmont | 3:36fd3cfad85a | 25 | int n = client.receive(buffer, sizeof(buffer)); |
emilmont | 1:5cebe0e38cd2 | 26 | if (n <= 0) break; |
emilmont | 1:5cebe0e38cd2 | 27 | |
mbedAustin | 7:a5ead1402704 | 28 | // print received message to terminal |
mbedAustin | 7:a5ead1402704 | 29 | buffer[n] = '\0'; |
mbedAustin | 7:a5ead1402704 | 30 | printf("Received message from Client :'%s'\n",buffer); |
mbedAustin | 7:a5ead1402704 | 31 | |
mbedAustin | 7:a5ead1402704 | 32 | // reverse the message |
mbedAustin | 7:a5ead1402704 | 33 | char temp; |
mbedAustin | 7:a5ead1402704 | 34 | for(int f = 0, l = n-1; f<l; f++,l--){ |
mbedAustin | 7:a5ead1402704 | 35 | temp = buffer[f]; |
mbedAustin | 7:a5ead1402704 | 36 | buffer[f] = buffer[l]; |
mbedAustin | 7:a5ead1402704 | 37 | buffer[l] = temp; |
mbedAustin | 7:a5ead1402704 | 38 | } |
mbedAustin | 7:a5ead1402704 | 39 | |
mbedAustin | 7:a5ead1402704 | 40 | // print reversed message to terminal |
mbedAustin | 7:a5ead1402704 | 41 | printf("Sending message to Client: '%s'\n",buffer); |
mbedAustin | 7:a5ead1402704 | 42 | |
mbedAustin | 7:a5ead1402704 | 43 | // Echo received message back to client |
emilmont | 2:ec5ae99791da | 44 | client.send_all(buffer, n); |
emilmont | 1:5cebe0e38cd2 | 45 | if (n <= 0) break; |
emilmont | 1:5cebe0e38cd2 | 46 | } |
emilmont | 3:36fd3cfad85a | 47 | |
emilmont | 1:5cebe0e38cd2 | 48 | client.close(); |
emilmont | 1:5cebe0e38cd2 | 49 | } |
emilmont | 1:5cebe0e38cd2 | 50 | } |