mbed 5.4 with sleep mode
main.cpp@34:d6ce8f961b8b, 2017-03-24 (annotated)
- Committer:
- ranaumarnaeem
- Date:
- Fri Mar 24 05:42:34 2017 +0000
- Revision:
- 34:d6ce8f961b8b
- Parent:
- 33:e27f40fada64
- Child:
- 37:43d48521d4d7
0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:4e3cb26f6019 | 1 | #include "mbed.h" |
ranaumarnaeem | 34:d6ce8f961b8b | 2 | #include <stdlib.h> |
ranaumarnaeem | 34:d6ce8f961b8b | 3 | #include <stdio.h> |
ranaumarnaeem | 34:d6ce8f961b8b | 4 | #include <string.h> |
ranaumarnaeem | 34:d6ce8f961b8b | 5 | #include <errno.h> |
ranaumarnaeem | 34:d6ce8f961b8b | 6 | #include <stdint.h> |
mazgch | 19:f022ff746eb8 | 7 | #include "GPS.h" |
mazgch | 19:f022ff746eb8 | 8 | #include "MDM.h" |
ranaumarnaeem | 34:d6ce8f961b8b | 9 | #include "main.h" |
ranaumarnaeem | 34:d6ce8f961b8b | 10 | |
ranaumarnaeem | 34:d6ce8f961b8b | 11 | extern "C" { |
ranaumarnaeem | 34:d6ce8f961b8b | 12 | #include "coap_msg.h" |
ranaumarnaeem | 34:d6ce8f961b8b | 13 | #include "coap_client.h" |
ranaumarnaeem | 34:d6ce8f961b8b | 14 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 15 | |
ranaumarnaeem | 34:d6ce8f961b8b | 16 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 34:d6ce8f961b8b | 17 | #define SIMPIN "1922" |
mazgch | 21:a090a5043e23 | 18 | #define APN NULL |
mazgch | 19:f022ff746eb8 | 19 | #define USERNAME NULL |
mazgch | 19:f022ff746eb8 | 20 | #define PASSWORD NULL |
ranaumarnaeem | 34:d6ce8f961b8b | 21 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 34:d6ce8f961b8b | 22 | int pport = 5683;//12436;//5683; |
ranaumarnaeem | 34:d6ce8f961b8b | 23 | const char* host = "leshan.eclipse.org"; |
ranaumarnaeem | 34:d6ce8f961b8b | 24 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 34:d6ce8f961b8b | 25 | int ssocket; |
ranaumarnaeem | 34:d6ce8f961b8b | 26 | MDMParser::IP ipp; |
ranaumarnaeem | 34:d6ce8f961b8b | 27 | MDMSerial* gmdm = NULL; |
ranaumarnaeem | 34:d6ce8f961b8b | 28 | Timer t; |
ranaumarnaeem | 34:d6ce8f961b8b | 29 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 34:d6ce8f961b8b | 30 | //---------------------------------------------------------------------------------------------------------------------- |
lawliet | 0:4e3cb26f6019 | 31 | int main(void) |
lawliet | 0:4e3cb26f6019 | 32 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 33 | MDMSerial mdm; |
ranaumarnaeem | 34:d6ce8f961b8b | 34 | gmdm = &mdm; |
ranaumarnaeem | 34:d6ce8f961b8b | 35 | t.start(); |
lawliet | 0:4e3cb26f6019 | 36 | |
ranaumarnaeem | 34:d6ce8f961b8b | 37 | if(initialize_modem()) |
ranaumarnaeem | 34:d6ce8f961b8b | 38 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 39 | //ussd_send(); |
ranaumarnaeem | 34:d6ce8f961b8b | 40 | |
ranaumarnaeem | 34:d6ce8f961b8b | 41 | ssocket = mdm.socketSocket(MDMParser::IPPROTO_UDP, pport); |
ranaumarnaeem | 34:d6ce8f961b8b | 42 | if (ssocket >= 0) |
ranaumarnaeem | 34:d6ce8f961b8b | 43 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 44 | mdm.socketSetBlocking(ssocket, 100000); |
ranaumarnaeem | 34:d6ce8f961b8b | 45 | ipp = gmdm->gethostbyname(host); |
ranaumarnaeem | 34:d6ce8f961b8b | 46 | //ipp = 0x97092263;//0x052753CE; |
ranaumarnaeem | 34:d6ce8f961b8b | 47 | char payload[] = "heeeelooo Worldsgfsj"; |
ranaumarnaeem | 34:d6ce8f961b8b | 48 | if (PASS == test_exchange_func(payload,sizeof(payload)-1)) |
ranaumarnaeem | 34:d6ce8f961b8b | 49 | printf("-----------------<pass>-----------------\n"); |
ranaumarnaeem | 34:d6ce8f961b8b | 50 | else |
ranaumarnaeem | 34:d6ce8f961b8b | 51 | printf("*****************[FAIL]*****************\n"); |
ranaumarnaeem | 34:d6ce8f961b8b | 52 | |
ranaumarnaeem | 34:d6ce8f961b8b | 53 | mdm.socketFree(ssocket); |
ranaumarnaeem | 34:d6ce8f961b8b | 54 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 55 | mdm.disconnect(); |
ranaumarnaeem | 34:d6ce8f961b8b | 56 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 57 | mdm.powerOff(); |
ranaumarnaeem | 34:d6ce8f961b8b | 58 | t.stop(); |
ranaumarnaeem | 34:d6ce8f961b8b | 59 | return 0; |
ranaumarnaeem | 34:d6ce8f961b8b | 60 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 61 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 34:d6ce8f961b8b | 62 | //---------------------------------------------------------------------------------------------------------------------- |
ranaumarnaeem | 34:d6ce8f961b8b | 63 | |
ranaumarnaeem | 34:d6ce8f961b8b | 64 | int recv(int socket_d, char *buf, int len) |
ranaumarnaeem | 34:d6ce8f961b8b | 65 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 66 | int ret = gmdm->socketRecvFrom(ssocket, &ipp, &pport, buf, len); |
ranaumarnaeem | 34:d6ce8f961b8b | 67 | return ret; |
ranaumarnaeem | 34:d6ce8f961b8b | 68 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 69 | |
ranaumarnaeem | 34:d6ce8f961b8b | 70 | int send(int socket_d, char *buf, int len) |
ranaumarnaeem | 34:d6ce8f961b8b | 71 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 72 | ipp = gmdm->gethostbyname(host); |
ranaumarnaeem | 34:d6ce8f961b8b | 73 | //ipp = 0x97092263;//0x052753CE; |
ranaumarnaeem | 34:d6ce8f961b8b | 74 | int ret = gmdm->socketSendTo(ssocket, ipp, pport, (char*)buf, len); |
ranaumarnaeem | 34:d6ce8f961b8b | 75 | return ret; |
ranaumarnaeem | 34:d6ce8f961b8b | 76 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 77 | |
ranaumarnaeem | 34:d6ce8f961b8b | 78 | int getseconds(void) |
ranaumarnaeem | 34:d6ce8f961b8b | 79 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 80 | return t.read(); |
ranaumarnaeem | 34:d6ce8f961b8b | 81 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 82 | |
ranaumarnaeem | 34:d6ce8f961b8b | 83 | int initialize_modem(void) |
ranaumarnaeem | 34:d6ce8f961b8b | 84 | { |
mazgch | 19:f022ff746eb8 | 85 | MDMParser::DevStatus devStatus = {}; |
ranaumarnaeem | 34:d6ce8f961b8b | 86 | bool mdmOk = gmdm->init(SIMPIN, &devStatus); |
ranaumarnaeem | 34:d6ce8f961b8b | 87 | gmdm->dumpDevStatus(&devStatus); |
mazgch | 20:52f0e5de8c3d | 88 | if (mdmOk) |
mazgch | 20:52f0e5de8c3d | 89 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 90 | MDMParser::IP ipp = gmdm->join(APN,USERNAME,PASSWORD); // join the internet connection |
ranaumarnaeem | 34:d6ce8f961b8b | 91 | if (ipp == NOIP) |
ranaumarnaeem | 34:d6ce8f961b8b | 92 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 93 | printf("Not able to join network\n"); |
ranaumarnaeem | 34:d6ce8f961b8b | 94 | return 0; |
ranaumarnaeem | 34:d6ce8f961b8b | 95 | } |
mazgch | 32:b838fcaba45e | 96 | else |
mazgch | 2:b77151f111a9 | 97 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 98 | return 1; |
mazgch | 2:b77151f111a9 | 99 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 100 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 101 | else |
ranaumarnaeem | 34:d6ce8f961b8b | 102 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 103 | return 0; |
mazgch | 10:d2da2028a233 | 104 | } |
lawliet | 0:4e3cb26f6019 | 105 | } |
ranaumarnaeem | 34:d6ce8f961b8b | 106 | |
ranaumarnaeem | 34:d6ce8f961b8b | 107 | void ussd_send(void) |
ranaumarnaeem | 34:d6ce8f961b8b | 108 | { |
ranaumarnaeem | 34:d6ce8f961b8b | 109 | int ret; |
ranaumarnaeem | 34:d6ce8f961b8b | 110 | char buffer[512]; |
ranaumarnaeem | 34:d6ce8f961b8b | 111 | const char* ussd = "*222#";//"*100#"; |
ranaumarnaeem | 34:d6ce8f961b8b | 112 | printf("Ussd Send Command %s\r\n", ussd); |
ranaumarnaeem | 34:d6ce8f961b8b | 113 | ret = gmdm->ussdCommand(ussd, buffer); |
ranaumarnaeem | 34:d6ce8f961b8b | 114 | if (ret > 0) |
ranaumarnaeem | 34:d6ce8f961b8b | 115 | printf("Ussd Got Answer: \"%s\"\r\n", buffer); |
ranaumarnaeem | 34:d6ce8f961b8b | 116 | } |