This is an simple example to send the data to the MQTT broker using W7500 ECO module
Dependencies: MQTT WIZnetInterface mbed
Fork of W7500_ECO_Sending_String_to_MQTT_Broker by
main.cpp@0:5db3b5b4ae2e, 2018-08-06 (annotated)
- Committer:
- HarshaDRAGNEEL
- Date:
- Mon Aug 06 11:28:02 2018 +0000
- Revision:
- 0:5db3b5b4ae2e
- Child:
- 1:9c13b9f1f2b3
Used older mbed libraries to restrict the timer.h warning in Serial PC display
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 1 | #include "mbed.h" |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 2 | #include "SPI.h" |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 3 | #include "MQTTEthernet.h" |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 4 | #include "MQTTClient.h" |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 5 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 6 | #define ECHO_SERVER_PORT 7 |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 7 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 8 | Serial pc(USBTX, USBRX); //Enabling the Serial transmission between WIZ750SR and PC. |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 9 | char c[100]="Hello testing"; |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 10 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 11 | int main(void) { |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 12 | printf("Wait a second...\r\n"); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 13 | char* topic = "ack"; //if we are subscribing the acknowledgement. |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 14 | MQTTEthernet ipstack = MQTTEthernet(); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 15 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 16 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 17 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 18 | char* hostname = "iot.eclipse.org"; //Give the IP Address of the MQTT Broker. |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 19 | int port = 1883; // Port number of the MQTT broker. |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 20 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 21 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 22 | int rc = ipstack.connect(hostname, port); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 23 | if (rc != 0) |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 24 | printf("rc from TCP connect is %d\n", rc); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 25 | printf("Topic: %s\r\n",topic); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 26 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 27 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 28 | data.MQTTVersion = 3; |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 29 | data.clientID.cstring = "Gintama"; |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 30 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 31 | if ((rc = client.connect(data)) == 0) |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 32 | printf("rc from MQTT connect is %d\n", rc); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 33 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 34 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 35 | while (true) { |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 36 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 37 | pc.printf("The value Sent to MQTT is: %s \n ",c); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 38 | MQTT::Message message; |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 39 | char buf[100]; |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 40 | sprintf(buf, "%s", c); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 41 | message.qos = MQTT::QOS0; |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 42 | message.retained = false; |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 43 | message.dup = false; |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 44 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 45 | message.payload = (void*)c; |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 46 | message.payloadlen = strlen(c); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 47 | |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 48 | rc = client.publish("hellooo", message); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 49 | // pc.printf("Rc result: %c \n ",rc); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 50 | client.yield(60); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 51 | wait(60); |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 52 | } |
HarshaDRAGNEEL | 0:5db3b5b4ae2e | 53 | } |