Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: GPRSInterface USBDevice mbed
Fork of Seeed_HTTPClient_GPRSInterface_HelloWorld by
main.cpp
00001 00002 /* Copyright (c) 2010-2011 mbed.org, MIT License 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without 00006 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00007 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00008 * Software is furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 00020 #include "mbed.h" 00021 #include "GPRSInterface.h" 00022 #include "PubSubClient.h" 00023 #include "USBSerial.h" 00024 00025 #define NETWORK_APN "" 00026 00027 #define PIN_PWR P1_2 //power up gprs module 00028 #define PIN_PWR_KEY P1_7 00029 #define PIN_TX P1_27 //Serial tx pin 00030 #define PIN_RX P1_26 //Serial rx pin 00031 00032 00033 DigitalOut power(PIN_PWR); 00034 DigitalOut powerKey(PIN_PWR_KEY); 00035 00036 USBSerial pc; 00037 00038 //Serial uart(USBTX, USBRX); 00039 GPRSInterface eth(PIN_TX, PIN_RX, NETWORK_APN, "", ""); 00040 00041 char* serverIpAddr = "opensensors.io"; /*Sever ip address*/ 00042 int port = 1883; /*Sever Port*/ 00043 void callback(char* topic, char* payload, unsigned int len); /*Callback function prototype*/ 00044 PubSubClient mqtt(serverIpAddr, port, callback); 00045 00046 void callback(char* topic, char* payload, unsigned int len) 00047 { 00048 pc.printf("Topic:%s\r\n", topic); 00049 pc.printf("Payload:%s\r\n\r\n", payload); 00050 00051 //Send incoming payloads back to topic "/mbed". 00052 mqtt.publish("/users/yihui/message", payload, len); 00053 } 00054 00055 void gprsPowerUp(void) 00056 { 00057 power = 1; 00058 wait(2); 00059 power = 0; 00060 wait(2); 00061 00062 powerKey = 0; 00063 wait(1); 00064 powerKey = 1; 00065 wait(2); 00066 powerKey = 0; 00067 wait(3); 00068 } 00069 00070 int main() 00071 { 00072 pc.printf("Powering up!"); 00073 gprsPowerUp(); 00074 wait(10); 00075 00076 // Initialize the interface. 00077 int s = eth.init(); 00078 if (s != NULL) { 00079 pc.printf(">>> Could not initialise. Halting!\n"); 00080 exit(0); 00081 } 00082 00083 pc.printf(">>> Get IP address...\n"); 00084 while (1) { 00085 s = eth.connect(); // Connect to network 00086 00087 if (s == false || s < 0) { 00088 pc.printf(">>> Could not connect to network. Retrying!\n"); 00089 wait(3); 00090 } else { 00091 break; 00092 } 00093 } 00094 pc.printf(">>> Got IP address: %s\n", eth.getIPAddress()); 00095 00096 char clientID[] = "1095"; /*Client nanme show for MQTT server*/ 00097 char user[] = "yihui"; 00098 char password[] = "cLkFPjQa"; 00099 char pub_topic[] = "/users/yihui/test"; 00100 char sub_topic[] = "/users/yihui/test"; 00101 00102 00103 mqtt_connect: 00104 while (!mqtt.connect(clientID, user, password)){ 00105 pc.printf("\r\nConnect to server failed ..\r\n"); 00106 pc.printf("wait 3 seconds. Retrying\n"); 00107 wait(3); 00108 } 00109 pc.printf("\r\nConnect to server sucessed ..\r\n"); 00110 00111 mqtt.publish(pub_topic, "Hello from Arch GPRS"); 00112 mqtt.subscribe(sub_topic); 00113 00114 while(1) { 00115 if (!mqtt.loop()) { 00116 goto mqtt_connect; 00117 } 00118 } 00119 00120 // Disconnect from network 00121 //eth.disconnect(); 00122 00123 return 0; 00124 }
Generated on Tue Jul 19 2022 22:27:48 by
1.7.2
