Arch GPRS MQTT demo
Dependencies: GPRSInterface USBDevice mbed
Fork of Seeed_HTTPClient_GPRSInterface_HelloWorld by
main.cpp@4:14a9621ec99c, 2015-04-03 (annotated)
- Committer:
- yihui
- Date:
- Fri Apr 03 09:51:32 2015 +0000
- Revision:
- 4:14a9621ec99c
- Parent:
- 3:9dc67659d945
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 3:9dc67659d945 | 1 | |
yihui | 3:9dc67659d945 | 2 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
yihui | 3:9dc67659d945 | 3 | * |
yihui | 3:9dc67659d945 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
yihui | 3:9dc67659d945 | 5 | * and associated documentation files (the "Software"), to deal in the Software without |
yihui | 3:9dc67659d945 | 6 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
yihui | 3:9dc67659d945 | 7 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
yihui | 3:9dc67659d945 | 8 | * Software is furnished to do so, subject to the following conditions: |
yihui | 3:9dc67659d945 | 9 | * |
yihui | 3:9dc67659d945 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
yihui | 3:9dc67659d945 | 11 | * substantial portions of the Software. |
yihui | 3:9dc67659d945 | 12 | * |
yihui | 3:9dc67659d945 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
yihui | 3:9dc67659d945 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
yihui | 3:9dc67659d945 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
yihui | 3:9dc67659d945 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yihui | 3:9dc67659d945 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
yihui | 3:9dc67659d945 | 18 | */ |
yihui | 3:9dc67659d945 | 19 | |
lawliet | 0:cc0bec77f305 | 20 | #include "mbed.h" |
lawliet | 0:cc0bec77f305 | 21 | #include "GPRSInterface.h" |
yihui | 3:9dc67659d945 | 22 | #include "PubSubClient.h" |
yihui | 1:16498811e319 | 23 | #include "USBSerial.h" |
yihui | 3:9dc67659d945 | 24 | |
yihui | 3:9dc67659d945 | 25 | #define NETWORK_APN "" |
yihui | 1:16498811e319 | 26 | |
yihui | 3:9dc67659d945 | 27 | #define PIN_PWR P1_2 //power up gprs module |
yihui | 3:9dc67659d945 | 28 | #define PIN_PWR_KEY P1_7 |
yihui | 3:9dc67659d945 | 29 | #define PIN_TX P1_27 //Serial tx pin |
yihui | 3:9dc67659d945 | 30 | #define PIN_RX P1_26 //Serial rx pin |
lawliet | 0:cc0bec77f305 | 31 | |
yihui | 1:16498811e319 | 32 | |
yihui | 3:9dc67659d945 | 33 | DigitalOut power(PIN_PWR); |
yihui | 3:9dc67659d945 | 34 | DigitalOut powerKey(PIN_PWR_KEY); |
lawliet | 0:cc0bec77f305 | 35 | |
yihui | 3:9dc67659d945 | 36 | USBSerial pc; |
yihui | 3:9dc67659d945 | 37 | |
yihui | 3:9dc67659d945 | 38 | //Serial uart(USBTX, USBRX); |
yihui | 3:9dc67659d945 | 39 | GPRSInterface eth(PIN_TX, PIN_RX, NETWORK_APN, "", ""); |
yihui | 1:16498811e319 | 40 | |
yihui | 3:9dc67659d945 | 41 | char* serverIpAddr = "opensensors.io"; /*Sever ip address*/ |
yihui | 3:9dc67659d945 | 42 | int port = 1883; /*Sever Port*/ |
yihui | 3:9dc67659d945 | 43 | void callback(char* topic, char* payload, unsigned int len); /*Callback function prototype*/ |
yihui | 3:9dc67659d945 | 44 | PubSubClient mqtt(serverIpAddr, port, callback); |
yihui | 1:16498811e319 | 45 | |
yihui | 3:9dc67659d945 | 46 | void callback(char* topic, char* payload, unsigned int len) |
yihui | 3:9dc67659d945 | 47 | { |
yihui | 3:9dc67659d945 | 48 | pc.printf("Topic:%s\r\n", topic); |
yihui | 3:9dc67659d945 | 49 | pc.printf("Payload:%s\r\n\r\n", payload); |
yihui | 1:16498811e319 | 50 | |
yihui | 3:9dc67659d945 | 51 | //Send incoming payloads back to topic "/mbed". |
yihui | 3:9dc67659d945 | 52 | mqtt.publish("/users/yihui/message", payload, len); |
yihui | 3:9dc67659d945 | 53 | } |
yihui | 1:16498811e319 | 54 | |
yihui | 1:16498811e319 | 55 | void gprsPowerUp(void) |
yihui | 1:16498811e319 | 56 | { |
yihui | 1:16498811e319 | 57 | power = 1; |
yihui | 1:16498811e319 | 58 | wait(2); |
yihui | 1:16498811e319 | 59 | power = 0; |
yihui | 1:16498811e319 | 60 | wait(2); |
yihui | 1:16498811e319 | 61 | |
yihui | 1:16498811e319 | 62 | powerKey = 0; |
yihui | 1:16498811e319 | 63 | wait(1); |
yihui | 1:16498811e319 | 64 | powerKey = 1; |
yihui | 1:16498811e319 | 65 | wait(2); |
yihui | 1:16498811e319 | 66 | powerKey = 0; |
yihui | 1:16498811e319 | 67 | wait(3); |
yihui | 1:16498811e319 | 68 | } |
yihui | 1:16498811e319 | 69 | |
lawliet | 0:cc0bec77f305 | 70 | int main() |
lawliet | 0:cc0bec77f305 | 71 | { |
yihui | 3:9dc67659d945 | 72 | pc.printf("Powering up!"); |
yihui | 1:16498811e319 | 73 | gprsPowerUp(); |
yihui | 3:9dc67659d945 | 74 | wait(10); |
yihui | 3:9dc67659d945 | 75 | |
yihui | 3:9dc67659d945 | 76 | // Initialize the interface. |
yihui | 3:9dc67659d945 | 77 | int s = eth.init(); |
yihui | 3:9dc67659d945 | 78 | if (s != NULL) { |
yihui | 3:9dc67659d945 | 79 | pc.printf(">>> Could not initialise. Halting!\n"); |
yihui | 3:9dc67659d945 | 80 | exit(0); |
lawliet | 0:cc0bec77f305 | 81 | } |
lawliet | 0:cc0bec77f305 | 82 | |
yihui | 3:9dc67659d945 | 83 | pc.printf(">>> Get IP address...\n"); |
yihui | 3:9dc67659d945 | 84 | while (1) { |
yihui | 3:9dc67659d945 | 85 | s = eth.connect(); // Connect to network |
lawliet | 0:cc0bec77f305 | 86 | |
yihui | 3:9dc67659d945 | 87 | if (s == false || s < 0) { |
yihui | 3:9dc67659d945 | 88 | pc.printf(">>> Could not connect to network. Retrying!\n"); |
yihui | 3:9dc67659d945 | 89 | wait(3); |
yihui | 3:9dc67659d945 | 90 | } else { |
yihui | 3:9dc67659d945 | 91 | break; |
yihui | 3:9dc67659d945 | 92 | } |
lawliet | 0:cc0bec77f305 | 93 | } |
yihui | 3:9dc67659d945 | 94 | pc.printf(">>> Got IP address: %s\n", eth.getIPAddress()); |
yihui | 3:9dc67659d945 | 95 | |
yihui | 3:9dc67659d945 | 96 | char clientID[] = "1095"; /*Client nanme show for MQTT server*/ |
yihui | 3:9dc67659d945 | 97 | char user[] = "yihui"; |
yihui | 3:9dc67659d945 | 98 | char password[] = "cLkFPjQa"; |
yihui | 4:14a9621ec99c | 99 | char pub_topic[] = "/users/yihui/test"; |
yihui | 4:14a9621ec99c | 100 | char sub_topic[] = "/users/yihui/test"; |
yihui | 3:9dc67659d945 | 101 | |
yihui | 3:9dc67659d945 | 102 | |
yihui | 3:9dc67659d945 | 103 | mqtt_connect: |
yihui | 3:9dc67659d945 | 104 | while (!mqtt.connect(clientID, user, password)){ |
yihui | 3:9dc67659d945 | 105 | pc.printf("\r\nConnect to server failed ..\r\n"); |
yihui | 3:9dc67659d945 | 106 | pc.printf("wait 3 seconds. Retrying\n"); |
yihui | 3:9dc67659d945 | 107 | wait(3); |
lawliet | 0:cc0bec77f305 | 108 | } |
yihui | 3:9dc67659d945 | 109 | pc.printf("\r\nConnect to server sucessed ..\r\n"); |
yihui | 3:9dc67659d945 | 110 | |
yihui | 3:9dc67659d945 | 111 | mqtt.publish(pub_topic, "Hello from Arch GPRS"); |
yihui | 3:9dc67659d945 | 112 | mqtt.subscribe(sub_topic); |
yihui | 3:9dc67659d945 | 113 | |
yihui | 3:9dc67659d945 | 114 | while(1) { |
yihui | 3:9dc67659d945 | 115 | if (!mqtt.loop()) { |
yihui | 3:9dc67659d945 | 116 | goto mqtt_connect; |
yihui | 3:9dc67659d945 | 117 | } |
lawliet | 0:cc0bec77f305 | 118 | } |
yihui | 3:9dc67659d945 | 119 | |
yihui | 3:9dc67659d945 | 120 | // Disconnect from network |
yihui | 3:9dc67659d945 | 121 | //eth.disconnect(); |
yihui | 3:9dc67659d945 | 122 | |
lawliet | 0:cc0bec77f305 | 123 | return 0; |
lawliet | 0:cc0bec77f305 | 124 | } |