インターフェース2014年10月号のu-blox C027で3G通信する記事で使用したプログラム。 CQ publishing Interface 2014.10 issue, C027 3G test program.
Dependencies: C027_Support C027_SupportTest mbed picojson
Fork of C027_SupportTest by
インターフェース2014年10月号のu-blox C027で3G通信する記事で使用したプログラムです。
main.cpp
- Committer:
- ntaka206
- Date:
- 2014-07-28
- Revision:
- 33:65bcfee03271
- Parent:
- 32:2ad9c1d1afd0
File content as of revision 33:65bcfee03271:
#include "mbed.h" /* Interface 2014/10 u-blolx C027 3G test sample 2014/07 Naoya Takamura */ //------------------------------------------------------------------------------------ /* This example was tested on C027-U20 and C027-G35 with the on board modem. Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this configuration the following platforms were tested (it is likely that others will work as well) - U-BLOX: C027-G35, C027-U20, C027-C20 (for shield set define C027_FORCE_SHIELD) - NXP: LPC1549v2, LPC4088qsb - Freescale: FRDM-KL05Z, FRDM-KL25Z, FRDM-KL46Z, FRDM-K64F - STM: NUCLEO-F401RE, NUCLEO-F030R8 mount resistors SB13/14 1k, SB62/63 0R */ #include "GPS.h" #include "MDM.h" #include "picojson.h" //------------------------------------------------------------------------------------ // You need to configure these cellular modem / SIM parameters. // These parameters are ignored for LISA-C200 variants and can be left NULL. //------------------------------------------------------------------------------------ //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. #define SIMPIN NULL /*! The APN of your network operator SIM, sometimes it is "internet" check your contract with the network operator. You can also try to look-up your settings in google: https://www.google.de/search?q=APN+list */ //#define APN NULL #define APN "3g-d-2.ocn.ne.jp" //! Set the user name for your APN, or NULL if not needed //#define USERNAME NULL #define USERNAME "hoge@one.ocn.ne.jp" //! Set the password for your APN, or NULL if not needed //#define PASSWORD NULL #define PASSWORD "hogehoge" //---- Xively -------------------------------------------------------------- #define XI_FEED_ID "SET your Feed ID" // set Xively Feed ID (numerical, no quoutes) #define XI_API_KEY "SET your Feed API KEY" // set Xively API key (double-quoted string) #define XI_SERVER "api.xively.com" #define XI_PATH "/v2/feeds/" XI_FEED_ID ".json" #define XI_HEADER "X-ApiKey: " XI_API_KEY #define XI_UPLOAD_INTERVAL 2 // sec //------------------------------------------------------------------------------------ int main(void) { int ret; char buf[512] = ""; // Add by ntaka for USB Console Serial pc(USBTX,USBRX); pc.baud(115200); // Create the GPS object // use GPSI2C class GPSI2C gps; // Create the modem object MDMSerial mdm; //mdm.setDebug(4); // enable this for debugging issues // initialize the modem MDMParser::DevStatus devStatus = {}; MDMParser::NetStatus netStatus = {}; bool mdmOk = mdm.init(SIMPIN, &devStatus); mdm.dumpDevStatus(&devStatus); if (mdmOk) { #if 0 // file system API const char* filename = "File"; char buf[] = "Hello World"; printf("writeFile \"%s\"\r\n", buf); if (mdm.writeFile(filename, buf, sizeof(buf))) { memset(buf, 0, sizeof(buf)); int len = mdm.readFile(filename, buf, sizeof(buf)); if (len >= 0) printf("readFile %d \"%.*s\"\r\n", len, len, buf); mdm.delFile(filename); } #endif // wait until we are connected mdmOk = mdm.registerNet(&netStatus); mdm.dumpNetStatus(&netStatus); } if (mdmOk) { // http://www.geckobeach.com/cellular/secrets/gsmcodes.php // http://de.wikipedia.org/wiki/USSD-Codes const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION" printf("Ussd Send Command %s\r\n", ussd); ret = mdm.ussdCommand(ussd, buf); if (ret > 0) printf("Ussd Got Answer: \"%*s\"\r\n", ret, buf); // join the internet connection MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); if (ip != NOIP) { mdm.dumpIp(ip); printf("Make a Http Post Request\r\n"); int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); if (socket >= 0) { mdm.socketSetBlocking(socket, 10000); if (mdm.socketConnect(socket, "mbed.org", 80)) { const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"; mdm.socketSend(socket, http, sizeof(http)-1); ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); if (ret > 0) printf("Socket Recv \"%*s\"\r\n", ret, buf); mdm.socketClose(socket); } mdm.socketFree(socket); } #if 0 int port = 7; const char* host = "echo.u-blox.com"; MDMParser::IP ip = mdm.gethostbyname(host); char data[] = "\r\nxxx Socket Hello World\r\n" "End\r\n"; printf("Testing TCP sockets with ECHO server\r\n"); socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); if (socket >= 0) { mdm.socketSetBlocking(socket, 10000); if (mdm.socketConnect(socket, host, port)) { memcpy(data, "\r\nTCP", 5); ret = mdm.socketSend(socket, data, sizeof(data)-1); if (ret == sizeof(data)-1) { printf("Socket Send %d \"%s\"\r\n", ret, data); } ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); if (ret >= 0) { printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf); } mdm.socketClose(socket); } mdm.socketFree(socket); } printf("Testing UDP sockets with ECHO server\r\n"); socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port); if (socket >= 0) { mdm.socketSetBlocking(socket, 10000); memcpy(data, "\r\nUDP", 5); ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1); if (ret == sizeof(data)-1) { printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data); } ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1); if (ret >= 0) { printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf); } mdm.socketFree(socket); } #endif // no disconnect // disconnect // mdm.disconnect(); } } // Upload GPS data to Xively printf("SMS and GPS Loop\r\n"); char link[128] = ""; unsigned int i = 0xFFFFFFFF; const int wait = 100; bool abort = false; int xv_upload_cnt = XI_UPLOAD_INTERVAL; //DigitalOut led(LED1); while (!abort) { // led = !led; while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) { int len = LENGTH(ret); //printf("NMEA: %.*s\r\n", len-2, msg); if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) { if (!strncmp("$GPGLL", buf, 6)) { double la = 0, lo = 0; char ch; if (gps.getNmeaAngle(1,buf,len,la) && gps.getNmeaAngle(3,buf,len,lo) && gps.getNmeaItem(6,buf,len,ch) && ch == 'A') { printf("GPS Location: %.5f %.5f\r\n", la, lo); sprintf(link, "I am here!\n" "https://maps.google.com/?q=%.5f,%.5f", la, lo); // Upload to Xively if (xv_upload_cnt++ >= XI_UPLOAD_INTERVAL) { xv_upload_cnt = 0; int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); if (socket >= 0) { mdm.socketSetBlocking(socket, 10000); if (mdm.socketConnect(socket, XI_SERVER, 80)) { // char data[128]; // sprintf(data, "lat,%f\r\nlon,%f\r\n", la, lo); // Make JSON location picojson::object v; picojson::object loc; string mb="mobile"; loc["disposition"] = picojson::value(mb); loc["lat"] = picojson::value(la); loc["lon"] = picojson::value(lo); v["location"] = picojson::value(loc); // JSON serialize string str = picojson::value(v).serialize(); // printf("serialized content = %s\r\n" , str.c_str()); // PUT HTTP data to Xively char http[512]; sprintf(http, "PUT %s HTTP/1.1\r\nHost: %s\r\n%s\r\nContent-Length: %d\r\n\r\n%s\r\n", XI_PATH, XI_SERVER, XI_HEADER, strlen(str.c_str()), str.c_str()); printf(http); mdm.socketSend(socket, http, strlen(http)); // Recv responce ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); if (ret > 0) printf("Socket Recv \"%*s\"\r\n", ret, buf); mdm.socketClose(socket); } mdm.socketFree(socket); } } } } else if (!strncmp("$GPGGA", buf, 6)) { double a = 0; if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m] printf("GPS Altitude: %.1f\r\n", a); } else if (!strncmp("$GPVTG", buf, 6)) { double s = 0; if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h] printf("GPS Speed: %.1f\r\n", s); } } } if (mdmOk && (i++ == 5000/wait)) { i = 0; // check the network status if (mdm.checkNetStatus(&netStatus)) { mdm.dumpNetStatus(&netStatus, fprintf, stdout); } // checking unread sms int ix[8]; int n = mdm.smsList("REC UNREAD", ix, 8); if (8 < n) n = 8; while (0 < n--) { char num[32]; printf("Unread SMS at index %d\r\n", ix[n]); if (mdm.smsRead(ix[n], num, buf, sizeof(buf))) { printf("Got SMS from \"%s\" with text \"%s\"\r\n", num, buf); printf("Delete SMS at index %d\r\n", ix[n]); mdm.smsDelete(ix[n]); // provide a reply const char* reply = "Hello my friend"; if (strstr(buf, /*w*/"here are you")) reply = *link ? link : "I don't know"; // reply wil location link else if (strstr(buf, /*s*/"hutdown")) abort = true, reply = "bye bye"; printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num); mdm.smsSend(num, reply); } } } wait_ms(wait); } gps.powerOff(); mdm.powerOff(); return 0; }