模組化,中斷服務
Dependencies: EthernetInterface MQTT RHT03 mbed-rtos mbed
Fork of MyTempuratureMqtt by
main.cpp@1:8b5f77770753, 2015-05-21 (annotated)
- Committer:
- bruceyang
- Date:
- Thu May 21 07:40:43 2015 +0000
- Revision:
- 1:8b5f77770753
- Parent:
- 0:5939450e4bef
Finish V0.0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charliebsimms | 0:5939450e4bef | 1 | #include "mbed.h" |
charliebsimms | 0:5939450e4bef | 2 | #include "EthernetInterface.h" |
charliebsimms | 0:5939450e4bef | 3 | #include "PubSubClient.h" |
charliebsimms | 0:5939450e4bef | 4 | |
charliebsimms | 0:5939450e4bef | 5 | Serial pc(USBTX, USBRX); |
bruceyang | 1:8b5f77770753 | 6 | DigitalOut led(LED1); |
bruceyang | 1:8b5f77770753 | 7 | DigitalIn button(p21); |
charliebsimms | 0:5939450e4bef | 8 | |
bruceyang | 1:8b5f77770753 | 9 | char* serverIpAddr = "128.199.127.110"; /*Sever ip address*/ |
bruceyang | 1:8b5f77770753 | 10 | int port = 443; /*Sever Port*/ |
charliebsimms | 0:5939450e4bef | 11 | |
charliebsimms | 0:5939450e4bef | 12 | void callback(char* topic, char* payload, unsigned int len); /*Callback function prototype*/ |
charliebsimms | 0:5939450e4bef | 13 | PubSubClient mqtt(serverIpAddr, port, callback); |
charliebsimms | 0:5939450e4bef | 14 | EthernetInterface eth; |
charliebsimms | 0:5939450e4bef | 15 | |
charliebsimms | 0:5939450e4bef | 16 | void callback(char* topic, char* payload, unsigned int len) |
charliebsimms | 0:5939450e4bef | 17 | { |
charliebsimms | 0:5939450e4bef | 18 | //Send incoming payloads back to topic "/mbed". |
charliebsimms | 0:5939450e4bef | 19 | mqtt.publish("mbed", payload, len); |
charliebsimms | 0:5939450e4bef | 20 | } |
charliebsimms | 0:5939450e4bef | 21 | |
charliebsimms | 0:5939450e4bef | 22 | int main() { |
charliebsimms | 0:5939450e4bef | 23 | |
charliebsimms | 0:5939450e4bef | 24 | eth.init(); //Use DHCP |
charliebsimms | 0:5939450e4bef | 25 | eth.connect(); |
charliebsimms | 0:5939450e4bef | 26 | pc.printf("IP Address is %s\n", eth.getIPAddress()); |
charliebsimms | 0:5939450e4bef | 27 | |
charliebsimms | 0:5939450e4bef | 28 | pc.printf("MQTTClient Tester"); |
charliebsimms | 0:5939450e4bef | 29 | |
charliebsimms | 0:5939450e4bef | 30 | |
charliebsimms | 0:5939450e4bef | 31 | char clientID[] = "mbed"; /*Client nanme show for MQTT server*/ |
charliebsimms | 0:5939450e4bef | 32 | char pub_topic[] = "mbed"; /*Publish to topic : "/mbed" */ |
charliebsimms | 0:5939450e4bef | 33 | char sub_topic[] = "mirror"; /*Subscribe to topic : "/mirror" */ |
charliebsimms | 0:5939450e4bef | 34 | |
charliebsimms | 0:5939450e4bef | 35 | if(!mqtt.connect(clientID)){ |
charliebsimms | 0:5939450e4bef | 36 | pc.printf("\r\nConnect to server failed ..\r\n"); |
charliebsimms | 0:5939450e4bef | 37 | return -1; |
charliebsimms | 0:5939450e4bef | 38 | } |
charliebsimms | 0:5939450e4bef | 39 | |
charliebsimms | 0:5939450e4bef | 40 | pc.printf("\r\nConnect to server sucessed ..\r\n"); |
charliebsimms | 0:5939450e4bef | 41 | |
charliebsimms | 0:5939450e4bef | 42 | mqtt.publish(pub_topic, "Hello here is mbed..."); |
charliebsimms | 0:5939450e4bef | 43 | mqtt.subscribe(sub_topic); |
charliebsimms | 0:5939450e4bef | 44 | |
charliebsimms | 0:5939450e4bef | 45 | |
charliebsimms | 0:5939450e4bef | 46 | pc.printf("#### End of the test.. ####"); |
charliebsimms | 0:5939450e4bef | 47 | |
charliebsimms | 0:5939450e4bef | 48 | //eth.disconnect(); |
charliebsimms | 0:5939450e4bef | 49 | |
charliebsimms | 0:5939450e4bef | 50 | while(1) { |
charliebsimms | 0:5939450e4bef | 51 | mqtt.loop(); |
bruceyang | 1:8b5f77770753 | 52 | if (button) { |
bruceyang | 1:8b5f77770753 | 53 | mqtt.publish(pub_topic, "ON!!"); |
bruceyang | 1:8b5f77770753 | 54 | mqtt.publish("control", "ffffff"); |
bruceyang | 1:8b5f77770753 | 55 | |
bruceyang | 1:8b5f77770753 | 56 | led = 1; |
bruceyang | 1:8b5f77770753 | 57 | } else { |
bruceyang | 1:8b5f77770753 | 58 | mqtt.publish(pub_topic, "OFF"); |
bruceyang | 1:8b5f77770753 | 59 | mqtt.publish("control", "000000"); |
bruceyang | 1:8b5f77770753 | 60 | led = 0; |
bruceyang | 1:8b5f77770753 | 61 | } |
bruceyang | 1:8b5f77770753 | 62 | wait(0.4); // simple debouncing |
charliebsimms | 0:5939450e4bef | 63 | } |
charliebsimms | 0:5939450e4bef | 64 | } |