Arch GPRS MQTT demo
Dependencies: GPRSInterface USBDevice mbed
Fork of Seeed_HTTPClient_GPRSInterface_HelloWorld by
Diff: main.cpp
- Revision:
- 3:9dc67659d945
- Parent:
- 2:ecc68e79d692
- Child:
- 4:14a9621ec99c
--- a/main.cpp Fri Mar 06 09:22:41 2015 +0000 +++ b/main.cpp Fri Apr 03 08:29:43 2015 +0000 @@ -1,43 +1,57 @@ + +/* Copyright (c) 2010-2011 mbed.org, MIT License +* +* Permission is hereby granted, free of charge, to any person obtaining a copy of this software +* and associated documentation files (the "Software"), to deal in the Software without +* restriction, including without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all copies or +* substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING +* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + #include "mbed.h" #include "GPRSInterface.h" -#include "HTTPClient.h" - -#ifdef TARGET_ARCH_GPRS +#include "PubSubClient.h" #include "USBSerial.h" -#define LOG(args...) pc.printf(args) -USBSerial pc; -#else -#define LOG(args...) pc.printf(args) -Serial pc(USBTX, USBRX); -#endif + +#define NETWORK_APN "" -#define TEST_HTTP_GET 1 -#define TEST_HTTP_POST 1 -#define TEST_HTTP_PUT 1 -#define TEST_HTTP_DELETE 1 - -#define PIN_TX P1_27 -#define PIN_RX P1_26 +#define PIN_PWR P1_2 //power up gprs module +#define PIN_PWR_KEY P1_7 +#define PIN_TX P1_27 //Serial tx pin +#define PIN_RX P1_26 //Serial rx pin -GPRSInterface gprs(PIN_TX, PIN_RX, 115200, "uninet", NULL, NULL); -HTTPClient http; -char str[1024]; +DigitalOut power(PIN_PWR); +DigitalOut powerKey(PIN_PWR_KEY); -#ifdef TARGET_ARCH_GPRS -DigitalOut power(P1_2); -DigitalOut powerKey(P1_7); +USBSerial pc; + +//Serial uart(USBTX, USBRX); +GPRSInterface eth(PIN_TX, PIN_RX, NETWORK_APN, "", ""); -/* power pin: low enable - ___ - |___ +char* serverIpAddr = "opensensors.io"; /*Sever ip address*/ +int port = 1883; /*Sever Port*/ +void callback(char* topic, char* payload, unsigned int len); /*Callback function prototype*/ +PubSubClient mqtt(serverIpAddr, port, callback); - powerKey pin: you can also power up by long press the powerKey. +void callback(char* topic, char* payload, unsigned int len) +{ + pc.printf("Topic:%s\r\n", topic); + pc.printf("Payload:%s\r\n\r\n", payload); - ___ - ___| |___ + //Send incoming payloads back to topic "/mbed". + mqtt.publish("/users/yihui/message", payload, len); +} -*/ void gprsPowerUp(void) { power = 1; @@ -52,78 +66,59 @@ powerKey = 0; wait(3); } -#endif int main() { -#ifdef TARGET_ARCH_GPRS + pc.printf("Powering up!"); gprsPowerUp(); -#endif - gprs.init(); - - while(false == gprs.connect()) { - LOG("gprs connect error\n"); - wait(2); + wait(10); + + // Initialize the interface. + int s = eth.init(); + if (s != NULL) { + pc.printf(">>> Could not initialise. Halting!\n"); + exit(0); } - // successful DHCP - LOG("IP Address is %s\n", gprs.getIPAddress()); - - int ret; - HTTPMap map; - HTTPText inText(str, 1024); - HTTPText outText(str); - -#if TEST_HTTP_GET - //GET data - LOG("\nTrying to fetch page...\n"); - ret = http.get("http://developer.mbed.org/media/uploads/mbed_official/hello.txt", str, 1024); - if (!ret) { - LOG("Page fetched successfully - read %d characters\n", strlen(str)); - LOG("Result: %s\n", str); - } else { - LOG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); - } -#endif + pc.printf(">>> Get IP address...\n"); + while (1) { + s = eth.connect(); // Connect to network -#if TEST_HTTP_POST - //POST data - map.put("Hello", "World"); - map.put("test", "1234"); - LOG("\nTrying to post data...\n"); - ret = http.post("http://httpbin.org/post", map, &inText); - if (!ret) { - LOG("Executed POST successfully\n"); - } else { - LOG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + if (s == false || s < 0) { + pc.printf(">>> Could not connect to network. Retrying!\n"); + wait(3); + } else { + break; + } } -#endif - -#if TEST_HTTP_PUT - //PUT data - strcpy(str, "This is a PUT test!"); - LOG("\nTrying to put resource...\n"); - ret = http.put("http://httpbin.org/put", outText, &inText); - if (!ret) { - LOG("Executed PUT successfully - read %d characters\n", strlen(str)); - LOG("Result: %s\n", str); - } else { - LOG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + pc.printf(">>> Got IP address: %s\n", eth.getIPAddress()); + + char clientID[] = "1095"; /*Client nanme show for MQTT server*/ + char user[] = "yihui"; + char password[] = "cLkFPjQa"; + char pub_topic[] = "/users/yihui/test"; /*Publish to topic : "/users/kinan/test" */ + char sub_topic[] = "/users/yihui/test"; /*Subscribe to topic : "/users/kinan/test" */ + + +mqtt_connect: + while (!mqtt.connect(clientID, user, password)){ + pc.printf("\r\nConnect to server failed ..\r\n"); + pc.printf("wait 3 seconds. Retrying\n"); + wait(3); } -#endif - -#if TEST_HTTP_DELETE - //DELETE data - LOG("\nTrying to delete resource...\n"); - ret = http.del("http://httpbin.org/delete", &inText); - if (!ret) { - LOG("Executed DELETE successfully\n"); - } else { - LOG("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); + pc.printf("\r\nConnect to server sucessed ..\r\n"); + + mqtt.publish(pub_topic, "Hello from Arch GPRS"); + mqtt.subscribe(sub_topic); + + while(1) { + if (!mqtt.loop()) { + goto mqtt_connect; + } } -#endif - - gprs.disconnect(); - + + // Disconnect from network + //eth.disconnect(); + return 0; }