mbed mqtt
Dependencies: TextLCD MQTT IAP
main.cpp@2:562744909841, 2018-11-29 (annotated)
- Committer:
- ncshy
- Date:
- Thu Nov 29 03:28:14 2018 +0000
- Revision:
- 2:562744909841
- Parent:
- 1:91e33a7fe0b5
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ncshy | 2:562744909841 | 1 | #define MQTTCLIENT_QOS2 0 |
ncshy | 2:562744909841 | 2 | //#define IAP_LOCATION 0x1FFF1FF1 |
ncshy | 2:562744909841 | 3 | #define HTTP |
hungnguyenm | 1:91e33a7fe0b5 | 4 | |
ncshy | 2:562744909841 | 5 | #include "ESP8266.h" |
hungnguyenm | 1:91e33a7fe0b5 | 6 | #include "ESP8266Interface.h" |
hungnguyenm | 1:91e33a7fe0b5 | 7 | #include "MQTTNetwork.h" |
hungnguyenm | 1:91e33a7fe0b5 | 8 | #include "MQTTmbed.h" |
hungnguyenm | 1:91e33a7fe0b5 | 9 | #include "MQTTClient.h" |
ncshy | 2:562744909841 | 10 | #include "TCPSocket.h" |
ncshy | 2:562744909841 | 11 | #include "IAP.h" |
ncshy | 2:562744909841 | 12 | #include "stdlib.h" |
ncshy | 2:562744909841 | 13 | #include "time.h" |
ncshy | 2:562744909841 | 14 | #include "TextLCD.h" |
hungnguyenm | 1:91e33a7fe0b5 | 15 | |
ncshy | 2:562744909841 | 16 | TextLCD lcd(p6,p8,p25,p26,p27,p28, TextLCD::LCD16x2); |
ncshy | 2:562744909841 | 17 | //TextLCD lcd(p15,p16,p17,p18,p19,p20, TextLCD::LCD16x2); |
ncshy | 2:562744909841 | 18 | //ESP8266Interface wifi(p28, p27); |
ncshy | 2:562744909841 | 19 | ESP8266Interface wifi(p13, p14); |
ncshy | 2:562744909841 | 20 | char* salted_id; |
ncshy | 2:562744909841 | 21 | Timer timer; |
ncshy | 2:562744909841 | 22 | //unsigned int iap_out[10]; |
ncshy | 2:562744909841 | 23 | |
ncshy | 2:562744909841 | 24 | void messageArrived(MQTT::MessageData& md) { |
ncshy | 2:562744909841 | 25 | timer.stop(); |
ncshy | 2:562744909841 | 26 | MQTT::Message &message = md.message; |
ncshy | 2:562744909841 | 27 | char* number; |
ncshy | 2:562744909841 | 28 | lcd.cls(); |
ncshy | 2:562744909841 | 29 | lcd.locate(0,0); |
ncshy | 2:562744909841 | 30 | printf("Message arrived: qos %d , packet id: %d\n", message.qos, message.id); |
ncshy | 2:562744909841 | 31 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); |
ncshy | 2:562744909841 | 32 | salted_id = strtok((char *)message.payload, "-"); |
ncshy | 2:562744909841 | 33 | number = strtok(NULL, "\0"); |
ncshy | 2:562744909841 | 34 | // printf("number is %s\n", number); |
ncshy | 2:562744909841 | 35 | |
ncshy | 2:562744909841 | 36 | // ++arrivedcount; |
ncshy | 2:562744909841 | 37 | // lcd.printf((char*)message.payload); |
ncshy | 2:562744909841 | 38 | // lcd.puts((char*)message.payload); |
ncshy | 2:562744909841 | 39 | lcd.puts((char*)salted_id); |
ncshy | 2:562744909841 | 40 | lcd.locate(0,1); |
ncshy | 2:562744909841 | 41 | lcd.puts((char*)number); |
ncshy | 2:562744909841 | 42 | |
ncshy | 2:562744909841 | 43 | |
ncshy | 2:562744909841 | 44 | } |
ncshy | 2:562744909841 | 45 | |
ncshy | 2:562744909841 | 46 | void GET(NetworkInterface *net, const char * address, const char * argument, int portnum) |
ncshy | 2:562744909841 | 47 | { |
ncshy | 2:562744909841 | 48 | //TCPSocket socket; |
ncshy | 2:562744909841 | 49 | TCPSocket socket; |
ncshy | 2:562744909841 | 50 | printf("Sending HTTP request to %s:%d/\r\n", address, portnum); |
ncshy | 2:562744909841 | 51 | |
ncshy | 2:562744909841 | 52 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
ncshy | 2:562744909841 | 53 | int ret = socket.open(net); |
ncshy | 2:562744909841 | 54 | printf("socket open returns %d\n", ret); |
ncshy | 2:562744909841 | 55 | ret = socket.connect(address, portnum); |
ncshy | 2:562744909841 | 56 | printf("socket connect returns %d\n", ret); |
ncshy | 2:562744909841 | 57 | while (ret < 0) { |
ncshy | 2:562744909841 | 58 | ret = socket.connect(address, portnum); |
ncshy | 2:562744909841 | 59 | printf("socket connect returns %d\n", ret); |
ncshy | 2:562744909841 | 60 | } |
ncshy | 2:562744909841 | 61 | |
ncshy | 2:562744909841 | 62 | // Send a simple http request |
ncshy | 2:562744909841 | 63 | char sbuffer[1024]; |
ncshy | 2:562744909841 | 64 | sprintf(sbuffer, "GET / HTTP/1.1\r\nHost: %s\r\n\r\n", address); |
ncshy | 2:562744909841 | 65 | |
ncshy | 2:562744909841 | 66 | while(1) { |
ncshy | 2:562744909841 | 67 | |
ncshy | 2:562744909841 | 68 | int scount = socket.send(sbuffer, sizeof sbuffer); |
ncshy | 2:562744909841 | 69 | printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
ncshy | 2:562744909841 | 70 | printf("-->%s\r\n", sbuffer); |
ncshy | 2:562744909841 | 71 | // Recieve a simple http response and print out the response line |
ncshy | 2:562744909841 | 72 | char rbuffer[1024]; |
ncshy | 2:562744909841 | 73 | int rcount = socket.recv(rbuffer, sizeof rbuffer); |
ncshy | 2:562744909841 | 74 | printf("recv %d %s\r\n", rcount, rbuffer); |
ncshy | 2:562744909841 | 75 | } |
ncshy | 2:562744909841 | 76 | // Close the socket to return its memory and bring down the network interface |
ncshy | 2:562744909841 | 77 | socket.close(); |
ncshy | 2:562744909841 | 78 | } |
ncshy | 2:562744909841 | 79 | |
ncshy | 2:562744909841 | 80 | int initConnection(const char * SSID, const char * password) |
ncshy | 2:562744909841 | 81 | { |
ncshy | 2:562744909841 | 82 | printf("WiFi example\r\n\r\n"); |
ncshy | 2:562744909841 | 83 | |
ncshy | 2:562744909841 | 84 | // Scan for available access points |
ncshy | 2:562744909841 | 85 | // scan_demo(&wifi); |
ncshy | 2:562744909841 | 86 | |
ncshy | 2:562744909841 | 87 | printf("\r\nConnecting...\r\n"); |
ncshy | 2:562744909841 | 88 | int ret = wifi.connect(SSID, password); |
ncshy | 2:562744909841 | 89 | if (ret != 0) { |
ncshy | 2:562744909841 | 90 | printf("\r\nConnection error\r\n"); |
ncshy | 2:562744909841 | 91 | return -1; |
ncshy | 2:562744909841 | 92 | } |
ncshy | 2:562744909841 | 93 | |
ncshy | 2:562744909841 | 94 | printf("Success\r\n\r\n"); |
ncshy | 2:562744909841 | 95 | printf("MAC: %s\r\n", wifi.get_mac_address()); |
ncshy | 2:562744909841 | 96 | printf("IP: %s\r\n", wifi.get_ip_address()); |
ncshy | 2:562744909841 | 97 | printf("Netmask: %s\r\n", wifi.get_netmask()); |
ncshy | 2:562744909841 | 98 | printf("Gateway: %s\r\n", wifi.get_gateway()); |
ncshy | 2:562744909841 | 99 | printf("RSSI: %d\r\n\r\n", wifi.get_rssi()); |
ncshy | 2:562744909841 | 100 | |
ncshy | 2:562744909841 | 101 | //GET(&wifi, "upenn.pennpacapp.com", "1"); |
ncshy | 2:562744909841 | 102 | //GET(&wifi, "192.168.8.1", "Testing|testing|1|2|3"); |
ncshy | 2:562744909841 | 103 | } |
ncshy | 2:562744909841 | 104 | |
hungnguyenm | 1:91e33a7fe0b5 | 105 | |
hungnguyenm | 1:91e33a7fe0b5 | 106 | int main(int argc, char* argv[]) { |
ncshy | 2:562744909841 | 107 | int ret; |
ncshy | 2:562744909841 | 108 | |
ncshy | 2:562744909841 | 109 | //Define host name/ identification |
ncshy | 2:562744909841 | 110 | //const char *hostname = "192.168.4.1"; |
ncshy | 2:562744909841 | 111 | //const char *hostname = "35.196.225.7"; |
ncshy | 2:562744909841 | 112 | const char *hostname = "192.168.43.138"; |
ncshy | 2:562744909841 | 113 | char topic[100]; |
ncshy | 2:562744909841 | 114 | char subscription_topic[100]; |
ncshy | 2:562744909841 | 115 | char id[8]; |
ncshy | 2:562744909841 | 116 | char deviceID[33]; |
ncshy | 2:562744909841 | 117 | char uuid[45]; |
ncshy | 2:562744909841 | 118 | int portnum = 8087; |
ncshy | 2:562744909841 | 119 | |
ncshy | 2:562744909841 | 120 | IAP iap; |
ncshy | 2:562744909841 | 121 | |
ncshy | 2:562744909841 | 122 | unsigned int iap_out; |
ncshy | 2:562744909841 | 123 | iap_out = iap.read_ID(); |
ncshy | 2:562744909841 | 124 | |
ncshy | 2:562744909841 | 125 | int *iap_serial = (int *)malloc(sizeof(int) * 4); |
ncshy | 2:562744909841 | 126 | iap_serial = iap.read_serial(); |
ncshy | 2:562744909841 | 127 | |
ncshy | 2:562744909841 | 128 | |
ncshy | 2:562744909841 | 129 | #ifdef DEBUG_PRINT |
ncshy | 2:562744909841 | 130 | printf("IAP output is %08x\n",iap_out); |
ncshy | 2:562744909841 | 131 | printf("IAP output is %08x\n", *(iap_serial)); |
ncshy | 2:562744909841 | 132 | printf("IAP output is %08x\n", *(iap_serial+ 1)); |
ncshy | 2:562744909841 | 133 | printf("IAP output is %08x\n", *(iap_serial+ 2)); |
ncshy | 2:562744909841 | 134 | printf("IAP output is %08x\n", *(iap_serial+ 3)); |
ncshy | 2:562744909841 | 135 | printf("size of int is %d\n", sizeof(int)); |
ncshy | 2:562744909841 | 136 | #endif |
ncshy | 2:562744909841 | 137 | |
ncshy | 2:562744909841 | 138 | sprintf(id, "%08x", iap_out); |
ncshy | 2:562744909841 | 139 | sprintf(deviceID, "%08x%08x%08x%08x", *(iap_serial),*(iap_serial + 1),*(iap_serial+ 2),*(iap_serial+ 3)); |
ncshy | 2:562744909841 | 140 | sprintf(uuid, "%8s-%32s", id, deviceID); |
ncshy | 2:562744909841 | 141 | printf("uuid: %s\n", uuid); |
ncshy | 2:562744909841 | 142 | sprintf(topic, "cis541/hw-mqtt/%s/data", uuid); |
ncshy | 2:562744909841 | 143 | printf("topic: %s\n", topic); |
ncshy | 2:562744909841 | 144 | sprintf(subscription_topic, "cis541/hw-mqtt/%s/echo", uuid); |
ncshy | 2:562744909841 | 145 | printf("subscribe topic: %s\n", subscription_topic); |
ncshy | 2:562744909841 | 146 | |
ncshy | 2:562744909841 | 147 | #ifdef MQTT |
ncshy | 2:562744909841 | 148 | MQTTNetwork mqttnw(&wifi); |
ncshy | 2:562744909841 | 149 | MQTT::Client<MQTTNetwork, Countdown> client(mqttnw); |
ncshy | 2:562744909841 | 150 | #endif |
ncshy | 2:562744909841 | 151 | |
ncshy | 2:562744909841 | 152 | //initConnection("Aspire-E5-573G", "03fee30fde"); |
ncshy | 2:562744909841 | 153 | //initConnection("Pi_Network", "warehouse"); |
ncshy | 2:562744909841 | 154 | initConnection("OnePlus3", "magnasift"); |
ncshy | 2:562744909841 | 155 | //Specify network parameters |
ncshy | 2:562744909841 | 156 | |
ncshy | 2:562744909841 | 157 | /* |
ncshy | 2:562744909841 | 158 | wifi.set_credentials(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
ncshy | 2:562744909841 | 159 | ret = wifi.connect(); |
ncshy | 2:562744909841 | 160 | if(ret < 0) { |
ncshy | 2:562744909841 | 161 | printf("Error in connecting to AP \n"); |
ncshy | 2:562744909841 | 162 | } |
ncshy | 2:562744909841 | 163 | |
ncshy | 2:562744909841 | 164 | printf("IP address is %s \n", wifi.get_ip_address()); |
ncshy | 2:562744909841 | 165 | |
ncshy | 2:562744909841 | 166 | */ |
ncshy | 2:562744909841 | 167 | |
ncshy | 2:562744909841 | 168 | |
ncshy | 2:562744909841 | 169 | |
ncshy | 2:562744909841 | 170 | #ifdef MQTT |
ncshy | 2:562744909841 | 171 | //Establish connection |
ncshy | 2:562744909841 | 172 | ret = mqttnw.connect(hostname, portnum); |
ncshy | 2:562744909841 | 173 | if(ret < 0) { |
ncshy | 2:562744909841 | 174 | printf("Error in connecting to mqttnw ret: %d\n", ret); |
ncshy | 2:562744909841 | 175 | } |
ncshy | 2:562744909841 | 176 | |
ncshy | 2:562744909841 | 177 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
ncshy | 2:562744909841 | 178 | // connackData connData; |
ncshy | 2:562744909841 | 179 | data.clientID.cstring = uuid; |
ncshy | 2:562744909841 | 180 | data.username.cstring = "mbed"; |
ncshy | 2:562744909841 | 181 | data.password.cstring = "homework"; |
ncshy | 2:562744909841 | 182 | |
ncshy | 2:562744909841 | 183 | ret = client.connect(data); |
ncshy | 2:562744909841 | 184 | if(ret < 0) { |
ncshy | 2:562744909841 | 185 | printf("Error in connecting to MQTT server at %s, ret i %d\n", hostname, ret); |
ncshy | 2:562744909841 | 186 | } |
ncshy | 2:562744909841 | 187 | |
ncshy | 2:562744909841 | 188 | ret = client.subscribe(subscription_topic, MQTT::QOS0, messageArrived); |
ncshy | 2:562744909841 | 189 | if(ret < 0) { |
ncshy | 2:562744909841 | 190 | printf("Client did not subscribe to topic %s, ret is %d\n", subscription_topic, ret); |
ncshy | 2:562744909841 | 191 | } |
ncshy | 2:562744909841 | 192 | printf("client subscribe ret is %d\n", ret); |
ncshy | 2:562744909841 | 193 | |
ncshy | 2:562744909841 | 194 | int count = 0; |
ncshy | 2:562744909841 | 195 | //Create a message in buffer |
ncshy | 2:562744909841 | 196 | char msgbuf[20] = "Test Data"; |
ncshy | 2:562744909841 | 197 | int rand_num; |
ncshy | 2:562744909841 | 198 | MQTT::Message message; |
ncshy | 2:562744909841 | 199 | message.qos = MQTT::QOS1; |
ncshy | 2:562744909841 | 200 | message.retained = false; |
ncshy | 2:562744909841 | 201 | message.dup = false; |
ncshy | 2:562744909841 | 202 | |
ncshy | 2:562744909841 | 203 | while(1) { |
ncshy | 2:562744909841 | 204 | |
ncshy | 2:562744909841 | 205 | //rand_num = 5 * (count % 20); |
ncshy | 2:562744909841 | 206 | rand_num = rand() % 100; |
ncshy | 2:562744909841 | 207 | // printf("rand_num %02d\n", rand_num); |
ncshy | 2:562744909841 | 208 | sprintf(msgbuf, "%02d", rand_num); |
ncshy | 2:562744909841 | 209 | message.payload = (void*)msgbuf; |
ncshy | 2:562744909841 | 210 | message.payloadlen = strlen(msgbuf)+1; |
ncshy | 2:562744909841 | 211 | ret = client.publish(topic, message); |
ncshy | 2:562744909841 | 212 | timer.reset(); |
ncshy | 2:562744909841 | 213 | timer.start(); |
ncshy | 2:562744909841 | 214 | |
ncshy | 2:562744909841 | 215 | if(ret != 0) { |
ncshy | 2:562744909841 | 216 | printf("Client publish failure %d\n", ret); |
ncshy | 2:562744909841 | 217 | } else { |
ncshy | 2:562744909841 | 218 | printf("Client publish successful %d\n", ret); |
ncshy | 2:562744909841 | 219 | } |
ncshy | 2:562744909841 | 220 | client.yield(1000); |
ncshy | 2:562744909841 | 221 | |
ncshy | 2:562744909841 | 222 | wait(4); |
ncshy | 2:562744909841 | 223 | printf("Timer observed value is %f\n", timer.read()); |
ncshy | 2:562744909841 | 224 | } |
ncshy | 2:562744909841 | 225 | |
ncshy | 2:562744909841 | 226 | client.unsubscribe(subscription_topic); |
ncshy | 2:562744909841 | 227 | client.disconnect(); |
ncshy | 2:562744909841 | 228 | #endif |
ncshy | 2:562744909841 | 229 | |
ncshy | 2:562744909841 | 230 | #ifdef HTTP |
ncshy | 2:562744909841 | 231 | GET(&wifi, hostname, "", portnum); |
ncshy | 2:562744909841 | 232 | printf("End of GET\n"); |
ncshy | 2:562744909841 | 233 | #endif |
ncshy | 2:562744909841 | 234 | |
hungnguyenm | 1:91e33a7fe0b5 | 235 | return 0; |
hungnguyenm | 1:91e33a7fe0b5 | 236 | } |