this is a sample for mbed(LPC1768)
ennoTest2.cpp@0:3163adfd2cf1, 2015-11-19 (annotated)
- Committer:
- 1
- Date:
- Thu Nov 19 10:17:55 2015 +0800
- Revision:
- 0:3163adfd2cf1
????????????????????????
1.callback
2.thread
3.auto-connect,time-up
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
1 | 0:3163adfd2cf1 | 1 | // Do not remove the include below |
1 | 0:3163adfd2cf1 | 2 | #include "ennoTest2.h" |
1 | 0:3163adfd2cf1 | 3 | #include "MQTTEthernet.h" |
1 | 0:3163adfd2cf1 | 4 | #include "MQTTClient.h" |
1 | 0:3163adfd2cf1 | 5 | #include <pb_decode.h> |
1 | 0:3163adfd2cf1 | 6 | #include <sitewhere.h> |
1 | 0:3163adfd2cf1 | 7 | #include <sitewhere.pb.h> |
1 | 0:3163adfd2cf1 | 8 | |
1 | 0:3163adfd2cf1 | 9 | #define MQTTCLIENT_QOS2 1 |
1 | 0:3163adfd2cf1 | 10 | int arrivedcount = 0; |
1 | 0:3163adfd2cf1 | 11 | bool registered = false; |
1 | 0:3163adfd2cf1 | 12 | |
1 | 0:3163adfd2cf1 | 13 | DigitalOut led1(LED1); |
1 | 0:3163adfd2cf1 | 14 | DigitalOut led2(LED2); |
1 | 0:3163adfd2cf1 | 15 | DigitalOut led3(LED3); |
1 | 0:3163adfd2cf1 | 16 | DigitalOut led4(LED4); |
1 | 0:3163adfd2cf1 | 17 | |
1 | 0:3163adfd2cf1 | 18 | void messageArrived(MQTT::MessageData& md) |
1 | 0:3163adfd2cf1 | 19 | { |
1 | 0:3163adfd2cf1 | 20 | uint8_t buffer[512]; |
1 | 0:3163adfd2cf1 | 21 | MQTT::Message &message = md.message; |
1 | 0:3163adfd2cf1 | 22 | printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); |
1 | 0:3163adfd2cf1 | 23 | printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); |
1 | 0:3163adfd2cf1 | 24 | ++arrivedcount; |
1 | 0:3163adfd2cf1 | 25 | /* |
1 | 0:3163adfd2cf1 | 26 | buffer = message.payload; |
1 | 0:3163adfd2cf1 | 27 | Device_Header header; |
1 | 0:3163adfd2cf1 | 28 | pb_istream_t stream = pb_istream_from_buffer(buffer, message.payloadlen); |
1 | 0:3163adfd2cf1 | 29 | |
1 | 0:3163adfd2cf1 | 30 | // Read header to find what type of command follows. |
1 | 0:3163adfd2cf1 | 31 | if (pb_decode_delimited(&stream, Device_Header_fields, &header)) { |
1 | 0:3163adfd2cf1 | 32 | // Handle a registration acknowledgement. |
1 | 0:3163adfd2cf1 | 33 | if (header.command == Device_Command_REGISTER_ACK) { |
1 | 0:3163adfd2cf1 | 34 | Device_RegistrationAck ack; |
1 | 0:3163adfd2cf1 | 35 | if (pb_decode_delimited(&stream, Device_RegistrationAck_fields, &ack)) { |
1 | 0:3163adfd2cf1 | 36 | if (ack.state == Device_RegistrationAckState_NEW_REGISTRATION) { |
1 | 0:3163adfd2cf1 | 37 | printf("Registered new device."); |
1 | 0:3163adfd2cf1 | 38 | registered = true; |
1 | 0:3163adfd2cf1 | 39 | } else if (ack.state == Device_RegistrationAckState_ALREADY_REGISTERED) { |
1 | 0:3163adfd2cf1 | 40 | printf("Device was already registered."); |
1 | 0:3163adfd2cf1 | 41 | registered = true; |
1 | 0:3163adfd2cf1 | 42 | } else if (ack.state == Device_RegistrationAckState_REGISTRATION_ERROR) { |
1 | 0:3163adfd2cf1 | 43 | printf("Error registering device."); |
1 | 0:3163adfd2cf1 | 44 | } |
1 | 0:3163adfd2cf1 | 45 | } |
1 | 0:3163adfd2cf1 | 46 | } |
1 | 0:3163adfd2cf1 | 47 | } else { |
1 | 0:3163adfd2cf1 | 48 | printf("Unable to decode system command."); |
1 | 0:3163adfd2cf1 | 49 | } |
1 | 0:3163adfd2cf1 | 50 | */ |
1 | 0:3163adfd2cf1 | 51 | } |
1 | 0:3163adfd2cf1 | 52 | |
1 | 0:3163adfd2cf1 | 53 | |
1 | 0:3163adfd2cf1 | 54 | int main(int argc, char* argv[]) |
1 | 0:3163adfd2cf1 | 55 | { |
1 | 0:3163adfd2cf1 | 56 | MQTTEthernet ipstack = MQTTEthernet(); |
1 | 0:3163adfd2cf1 | 57 | float version = 0.5; |
1 | 0:3163adfd2cf1 | 58 | char* topic = "SiteWhere/input/protobuf"; |
1 | 0:3163adfd2cf1 | 59 | char* hardwareId = "enno-device-mbed-id-1"; |
1 | 0:3163adfd2cf1 | 60 | char* specificationToken = "9556b504-52cc-4194-89c0-f301a782b286"; |
1 | 0:3163adfd2cf1 | 61 | char* system = "SiteWhere/system/mbed-test-A"; |
1 | 0:3163adfd2cf1 | 62 | char* hostname = "139.217.10.99";//sitewhere.chinacloudapp.cn:1883 |
1 | 0:3163adfd2cf1 | 63 | int port = 1883; |
1 | 0:3163adfd2cf1 | 64 | |
1 | 0:3163adfd2cf1 | 65 | led1=1;led2=1;led3=1;led4=1; |
1 | 0:3163adfd2cf1 | 66 | wait(0.5); |
1 | 0:3163adfd2cf1 | 67 | led1=!led1;led2=!led2;led3=!led3;led4=!led4; |
1 | 0:3163adfd2cf1 | 68 | wait(0.5); |
1 | 0:3163adfd2cf1 | 69 | led1=!led1;led2=!led2;led3=!led3;led4=!led4; |
1 | 0:3163adfd2cf1 | 70 | |
1 | 0:3163adfd2cf1 | 71 | printf("HelloMQTT: version is %f\n", version); |
1 | 0:3163adfd2cf1 | 72 | MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); |
1 | 0:3163adfd2cf1 | 73 | printf("Connecting to %s:%d\n", hostname, port); |
1 | 0:3163adfd2cf1 | 74 | int rc = ipstack.connect(hostname, port); |
1 | 0:3163adfd2cf1 | 75 | |
1 | 0:3163adfd2cf1 | 76 | if (rc != 0){ //tcp connect error! |
1 | 0:3163adfd2cf1 | 77 | printf("rc from TCP connect is %d\n", rc); |
1 | 0:3163adfd2cf1 | 78 | } |
1 | 0:3163adfd2cf1 | 79 | if (rc == 0){ //tcp connected! |
1 | 0:3163adfd2cf1 | 80 | led1=!led1;wait(1);led1=!led1;wait(1);led1=!led1; |
1 | 0:3163adfd2cf1 | 81 | |
1 | 0:3163adfd2cf1 | 82 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
1 | 0:3163adfd2cf1 | 83 | data.MQTTVersion = 3; |
1 | 0:3163adfd2cf1 | 84 | data.clientID.cstring = "mbed-sample"; |
1 | 0:3163adfd2cf1 | 85 | data.username.cstring = NULL; |
1 | 0:3163adfd2cf1 | 86 | data.password.cstring = NULL; |
1 | 0:3163adfd2cf1 | 87 | if ((rc = client.connect(data)) != 0) |
1 | 0:3163adfd2cf1 | 88 | printf("rc from MQTT connect is %d\n", rc); |
1 | 0:3163adfd2cf1 | 89 | if (rc == 0){ //MQTT Client connected! |
1 | 0:3163adfd2cf1 | 90 | led2=!led2;wait(1);led2=!led2;wait(1);led2=!led2; |
1 | 0:3163adfd2cf1 | 91 | |
1 | 0:3163adfd2cf1 | 92 | if ((rc = client.subscribe(system, MQTT::QOS2, messageArrived)) != 0) |
1 | 0:3163adfd2cf1 | 93 | printf("rc from MQTT subscribe is %d\n", rc); |
1 | 0:3163adfd2cf1 | 94 | if(rc == 0){ //subscribe successed! |
1 | 0:3163adfd2cf1 | 95 | led3=!led3;wait(1);led3=!led3;wait(1);led3=!led3; |
1 | 0:3163adfd2cf1 | 96 | |
1 | 0:3163adfd2cf1 | 97 | MQTT::Message message; |
1 | 0:3163adfd2cf1 | 98 | |
1 | 0:3163adfd2cf1 | 99 | // QoS 0 |
1 | 0:3163adfd2cf1 | 100 | char buf[100]; |
1 | 0:3163adfd2cf1 | 101 | //unsigned char buffer[512]; |
1 | 0:3163adfd2cf1 | 102 | uint8_t buffer[512]; // Caution: unsigned char cause an error!!! |
1 | 0:3163adfd2cf1 | 103 | unsigned int len = 0; |
1 | 0:3163adfd2cf1 | 104 | sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); |
1 | 0:3163adfd2cf1 | 105 | message.qos = MQTT::QOS0; |
1 | 0:3163adfd2cf1 | 106 | message.retained = false; |
1 | 0:3163adfd2cf1 | 107 | message.dup = false; |
1 | 0:3163adfd2cf1 | 108 | // message.payload = (void*)buf; |
1 | 0:3163adfd2cf1 | 109 | // message.payloadlen = strlen(buf)+1; //?? |
1 | 0:3163adfd2cf1 | 110 | /*********** serialization ************/ |
1 | 0:3163adfd2cf1 | 111 | len = sw_register(hardwareId, specificationToken, buffer, 512,NULL); |
1 | 0:3163adfd2cf1 | 112 | message.payload = buffer; |
1 | 0:3163adfd2cf1 | 113 | message.payloadlen = len; |
1 | 0:3163adfd2cf1 | 114 | /*************************************/ |
1 | 0:3163adfd2cf1 | 115 | rc = client.publish(topic, message); |
1 | 0:3163adfd2cf1 | 116 | // while (arrivedcount < 1) |
1 | 0:3163adfd2cf1 | 117 | // client.yield(100); |
1 | 0:3163adfd2cf1 | 118 | |
1 | 0:3163adfd2cf1 | 119 | |
1 | 0:3163adfd2cf1 | 120 | |
1 | 0:3163adfd2cf1 | 121 | // QoS 1 |
1 | 0:3163adfd2cf1 | 122 | /*********** serialization ************/ |
1 | 0:3163adfd2cf1 | 123 | // len = sw_alert(hardwareId, "enno.info","mbed is alive", NULL, buffer, 512, NULL); |
1 | 0:3163adfd2cf1 | 124 | len = sw_location(hardwareId, 27.59, 86.55, 8844.43, 0,buffer, 512, NULL); |
1 | 0:3163adfd2cf1 | 125 | message.payload = buffer; |
1 | 0:3163adfd2cf1 | 126 | message.payloadlen = len; |
1 | 0:3163adfd2cf1 | 127 | sprintf(buf, "Hello World! QoS 1 message from app version %f\n", version); |
1 | 0:3163adfd2cf1 | 128 | message.qos = MQTT::QOS1; |
1 | 0:3163adfd2cf1 | 129 | |
1 | 0:3163adfd2cf1 | 130 | while (0 == rc) { |
1 | 0:3163adfd2cf1 | 131 | client.yield(1000); |
1 | 0:3163adfd2cf1 | 132 | //sleep(); |
1 | 0:3163adfd2cf1 | 133 | //wait(0.5); |
1 | 0:3163adfd2cf1 | 134 | client.publish(topic, message); |
1 | 0:3163adfd2cf1 | 135 | printf("Location updated.\n"); |
1 | 0:3163adfd2cf1 | 136 | led4=!led4; |
1 | 0:3163adfd2cf1 | 137 | |
1 | 0:3163adfd2cf1 | 138 | } |
1 | 0:3163adfd2cf1 | 139 | return 0; |
1 | 0:3163adfd2cf1 | 140 | } |
1 | 0:3163adfd2cf1 | 141 | } |
1 | 0:3163adfd2cf1 | 142 | } |
1 | 0:3163adfd2cf1 | 143 | } |
1 | 0:3163adfd2cf1 | 144 | /* |
1 | 0:3163adfd2cf1 | 145 | // QoS 2 |
1 | 0:3163adfd2cf1 | 146 | sprintf(buf, "Hello World! QoS 2 message from app version %f\n", version); |
1 | 0:3163adfd2cf1 | 147 | message.qos = MQTT::QOS2; |
1 | 0:3163adfd2cf1 | 148 | message.payloadlen = strlen(buf)+1; |
1 | 0:3163adfd2cf1 | 149 | rc = client.publish(topic, message); |
1 | 0:3163adfd2cf1 | 150 | while (arrivedcount < 3) |
1 | 0:3163adfd2cf1 | 151 | client.yield(100); |
1 | 0:3163adfd2cf1 | 152 | */ |
1 | 0:3163adfd2cf1 | 153 | /* |
1 | 0:3163adfd2cf1 | 154 | if ((rc = client.unsubscribe(topic)) != 0) |
1 | 0:3163adfd2cf1 | 155 | printf("rc from unsubscribe was %d\n", rc); |
1 | 0:3163adfd2cf1 | 156 | |
1 | 0:3163adfd2cf1 | 157 | if ((rc = client.disconnect()) != 0) |
1 | 0:3163adfd2cf1 | 158 | printf("rc from disconnect was %d\n", rc); |
1 | 0:3163adfd2cf1 | 159 | |
1 | 0:3163adfd2cf1 | 160 | ipstack.disconnect(); |
1 | 0:3163adfd2cf1 | 161 | |
1 | 0:3163adfd2cf1 | 162 | printf("Version %.2f: finish %d msgs\n", version, arrivedcount); */ |
1 | 0:3163adfd2cf1 | 163 | |
1 | 0:3163adfd2cf1 | 164 | |
1 | 0:3163adfd2cf1 | 165 | |
1 | 0:3163adfd2cf1 | 166 | // Auto Added by "SMeshLink SMeshStudio" |
1 | 0:3163adfd2cf1 | 167 |