HARSHA B.M / MQTT_publish_W750SR

Dependencies:   MQTT WIZnetInterface mbed-src

Fork of RFID_copy by Rajib Kumer Dey

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SPI.h"
00003 #include "MQTTEthernet.h"
00004 #include "MQTTClient.h"
00005 
00006 #define ECHO_SERVER_PORT   7
00007  
00008 Serial pc(USBTX, USBRX);   //Enabling the Serial transmission between WIZ750SR and PC.
00009 Serial serial(D1,D0);      // Enabling Serial transmission betwenn Wiz750SR and W7500. 
00010 char c[100]="";
00011 
00012  
00013 int main(void) {
00014     printf("Wait a second...\r\n");
00015     char* topic = "ack";                    //if we are subscribing the acknowledgement.
00016     MQTTEthernet ipstack = MQTTEthernet();
00017     
00018     MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
00019     
00020     char* hostname = "172.16.73.4";   //Give the IP Address of the MQTT Broker.
00021     int port = 1883;                  // Port number of the MQTT broker.  
00022     
00023  
00024     int rc = ipstack.connect(hostname, port);
00025     if (rc != 0)
00026     printf("rc from TCP connect is %d\n", rc);
00027     printf("Topic: %s\r\n",topic);
00028     
00029     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
00030     data.MQTTVersion = 3;
00031     data.clientID.cstring = "parents";
00032 
00033     if ((rc = client.connect(data)) == 0)
00034     printf("rc from MQTT connect is %d\n", rc);
00035    
00036                               
00037    while (true) {
00038     if (serial.readable()) // if anything available/ readable in Serial port 
00039     {
00040         int i;
00041         c[i]=0;
00042         for(i=0;i<=10;i++){
00043         char c1=serial.getc();
00044         c[i] = c1;
00045     } 
00046      pc.printf("The value returned is %s ",c);
00047      MQTT::Message message;
00048      char buf[100];
00049      sprintf(buf, "%s", c);
00050      message.qos = MQTT::QOS0;
00051          message.retained = false;
00052          message.dup = false;
00053 
00054          message.payload = (void*)c;
00055          message.payloadlen = strlen(c);
00056 
00057          rc = client.publish("balance", message);
00058          pc.printf("Rc result: %c \n ",rc);
00059          client.yield(60);
00060     }
00061 }
00062 }