Nishanth S / Mbed OS cis541-hw-mqtt

Dependencies:   TextLCD MQTT IAP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #define MQTTCLIENT_QOS2 0
00002 //#define IAP_LOCATION 0x1FFF1FF1
00003 #define HTTP
00004 
00005 #include "ESP8266.h"
00006 #include "ESP8266Interface.h"
00007 #include "MQTTNetwork.h"
00008 #include "MQTTmbed.h"
00009 #include "MQTTClient.h"
00010 #include "TCPSocket.h"
00011 #include "IAP.h"
00012 #include "stdlib.h"
00013 #include "time.h"
00014 #include "TextLCD.h"
00015 
00016 TextLCD lcd(p6,p8,p25,p26,p27,p28, TextLCD::LCD16x2);
00017 //TextLCD lcd(p15,p16,p17,p18,p19,p20, TextLCD::LCD16x2);
00018 //ESP8266Interface wifi(p28, p27);
00019 ESP8266Interface wifi(p13, p14);
00020 char* salted_id;
00021 Timer timer;
00022 //unsigned int iap_out[10];
00023 
00024 void messageArrived(MQTT::MessageData& md) {
00025     timer.stop();
00026     MQTT::Message &message = md.message;
00027     char* number;
00028     lcd.cls();
00029     lcd.locate(0,0);
00030     printf("Message arrived: qos %d , packet id: %d\n", message.qos, message.id);
00031     printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
00032     salted_id = strtok((char *)message.payload, "-");
00033     number = strtok(NULL, "\0");
00034    // printf("number is %s\n", number);
00035     
00036 //    ++arrivedcount;
00037 //    lcd.printf((char*)message.payload);
00038    // lcd.puts((char*)message.payload);
00039     lcd.puts((char*)salted_id);
00040     lcd.locate(0,1);
00041     lcd.puts((char*)number);
00042     
00043     
00044 }
00045 
00046 void GET(NetworkInterface *net, const char * address, const char * argument, int portnum)
00047 {
00048     //TCPSocket socket;
00049     TCPSocket socket;
00050     printf("Sending HTTP request to %s:%d/\r\n", address, portnum);
00051 
00052     // Open a socket on the network interface, and create a TCP connection to www.arm.com
00053      int ret = socket.open(net);
00054     printf("socket open returns %d\n", ret);
00055     ret = socket.connect(address, portnum);
00056     printf("socket connect returns %d\n", ret);
00057     while (ret < 0) {
00058         ret = socket.connect(address, portnum);
00059     printf("socket connect returns %d\n", ret);
00060     }       
00061 
00062     // Send a simple http request
00063     char sbuffer[1024]; 
00064     sprintf(sbuffer, "GET / HTTP/1.1\r\nHost: %s\r\n\r\n", address);
00065 
00066 while(1) {
00067     
00068     int scount = socket.send(sbuffer, sizeof sbuffer);
00069     printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
00070     printf("-->%s\r\n", sbuffer);
00071     // Recieve a simple http response and print out the response line
00072     char rbuffer[1024];
00073     int rcount = socket.recv(rbuffer, sizeof rbuffer);
00074     printf("recv %d %s\r\n", rcount, rbuffer);
00075 }
00076     // Close the socket to return its memory and bring down the network interface
00077     socket.close();
00078 }
00079 
00080 int initConnection(const char * SSID, const char * password)
00081 {
00082     printf("WiFi example\r\n\r\n");
00083 
00084     // Scan for available access points 
00085   //  scan_demo(&wifi);
00086 
00087     printf("\r\nConnecting...\r\n");
00088     int ret = wifi.connect(SSID, password);
00089     if (ret != 0) {
00090         printf("\r\nConnection error\r\n");
00091         return -1;
00092     }
00093 
00094     printf("Success\r\n\r\n");
00095     printf("MAC: %s\r\n", wifi.get_mac_address());
00096     printf("IP: %s\r\n", wifi.get_ip_address());
00097     printf("Netmask: %s\r\n", wifi.get_netmask());
00098     printf("Gateway: %s\r\n", wifi.get_gateway());
00099     printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
00100 
00101     //GET(&wifi, "upenn.pennpacapp.com", "1");
00102     //GET(&wifi, "192.168.8.1", "Testing|testing|1|2|3");
00103 }
00104 
00105 
00106 int main(int argc, char* argv[]) {
00107     int ret;
00108     
00109     //Define host name/ identification
00110     //const char *hostname = "192.168.4.1";
00111         //const char *hostname = "35.196.225.7";
00112     const char *hostname = "192.168.43.138";
00113     char topic[100];
00114     char subscription_topic[100];
00115     char id[8];
00116     char deviceID[33];
00117     char uuid[45];
00118     int portnum = 8087;
00119 
00120     IAP iap;
00121     
00122     unsigned int iap_out;
00123     iap_out = iap.read_ID();
00124     
00125     int *iap_serial = (int *)malloc(sizeof(int) * 4);
00126     iap_serial = iap.read_serial();
00127     
00128  
00129    #ifdef DEBUG_PRINT
00130     printf("IAP output is %08x\n",iap_out);
00131     printf("IAP output is %08x\n", *(iap_serial));
00132     printf("IAP output is %08x\n", *(iap_serial+ 1));
00133     printf("IAP output is %08x\n", *(iap_serial+ 2));
00134     printf("IAP output is %08x\n", *(iap_serial+ 3));
00135     printf("size of int is %d\n", sizeof(int));
00136     #endif
00137     
00138     sprintf(id, "%08x", iap_out);
00139     sprintf(deviceID, "%08x%08x%08x%08x", *(iap_serial),*(iap_serial + 1),*(iap_serial+ 2),*(iap_serial+ 3));
00140     sprintf(uuid, "%8s-%32s", id, deviceID);
00141     printf("uuid: %s\n", uuid);
00142     sprintf(topic, "cis541/hw-mqtt/%s/data", uuid);
00143     printf("topic: %s\n", topic);
00144     sprintf(subscription_topic, "cis541/hw-mqtt/%s/echo", uuid);
00145     printf("subscribe topic: %s\n", subscription_topic);
00146     
00147  #ifdef MQTT   
00148     MQTTNetwork mqttnw(&wifi);
00149     MQTT::Client<MQTTNetwork, Countdown> client(mqttnw);
00150 #endif
00151 
00152     //initConnection("Aspire-E5-573G", "03fee30fde");
00153     //initConnection("Pi_Network", "warehouse");    
00154     initConnection("OnePlus3", "magnasift");
00155     //Specify network parameters
00156     
00157   /*
00158     wifi.set_credentials(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00159     ret = wifi.connect();
00160     if(ret < 0) {
00161         printf("Error in connecting to AP \n");    
00162     }
00163     
00164         printf("IP address is %s \n", wifi.get_ip_address());    
00165     
00166   */
00167   
00168   
00169   
00170  #ifdef MQTT   
00171     //Establish connection
00172     ret = mqttnw.connect(hostname, portnum);
00173     if(ret < 0) {
00174         printf("Error in connecting to mqttnw ret: %d\n", ret);    
00175     }
00176     
00177      MQTTPacket_connectData data = MQTTPacket_connectData_initializer;  
00178  //    connackData connData;
00179      data.clientID.cstring = uuid;
00180      data.username.cstring = "mbed";
00181      data.password.cstring = "homework";
00182     
00183     ret = client.connect(data);
00184     if(ret < 0) {
00185         printf("Error in connecting to MQTT server at %s, ret i %d\n", hostname, ret);    
00186     }
00187   
00188     ret = client.subscribe(subscription_topic, MQTT::QOS0, messageArrived);
00189     if(ret < 0) {
00190         printf("Client did not subscribe to topic %s, ret is %d\n", subscription_topic, ret);    
00191     }
00192     printf("client subscribe ret is %d\n", ret);
00193     
00194     int count = 0;
00195     //Create a message in buffer
00196     char msgbuf[20] = "Test Data";
00197     int rand_num;
00198     MQTT::Message message;
00199     message.qos = MQTT::QOS1;
00200     message.retained = false;
00201     message.dup = false;
00202     
00203     while(1) {
00204         
00205         //rand_num = 5 * (count % 20);
00206         rand_num = rand() % 100;
00207 //        printf("rand_num %02d\n", rand_num);
00208         sprintf(msgbuf, "%02d", rand_num);
00209         message.payload = (void*)msgbuf;
00210         message.payloadlen = strlen(msgbuf)+1;  
00211         ret = client.publish(topic, message);  
00212         timer.reset();  
00213         timer.start();
00214     
00215         if(ret != 0) {
00216             printf("Client publish failure %d\n", ret);    
00217         } else {
00218             printf("Client publish successful %d\n", ret);    
00219         }
00220         client.yield(1000);
00221      
00222         wait(4);
00223         printf("Timer observed value is %f\n", timer.read());   
00224     }
00225     
00226     client.unsubscribe(subscription_topic);
00227     client.disconnect();
00228 #endif
00229   
00230 #ifdef HTTP   
00231     GET(&wifi, hostname, "", portnum);
00232     printf("End of GET\n");
00233 #endif 
00234 
00235     return 0;
00236 }