Testing the module's internet connection (spoiler: it doesn't work) WARNING: this code has been written in a hurry during an hackathon. It's total crap.

Dependencies:   GPS_CanSat mbed

Testing the module's internet connection (spoiler: it doesn't work) WARNING: this code has been written in a hurry during an hackathon. It's total crap.

Committer:
gipmad
Date:
Sat Sep 19 14:47:39 2015 +0000
Revision:
1:180ed9f09775
Parent:
0:8f9b472ff818
-

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gipmad 0:8f9b472ff818 1 /*
gipmad 0:8f9b472ff818 2 modem.h
gipmad 0:8f9b472ff818 3 2014 Copyright (c) Seeed Technology Inc. All right reserved.
gipmad 0:8f9b472ff818 4
gipmad 0:8f9b472ff818 5 Author:lawliet zou(lawliet.zou@gmail.com)
gipmad 0:8f9b472ff818 6 2014-2-24
gipmad 0:8f9b472ff818 7
gipmad 0:8f9b472ff818 8 This library is free software; you can redistribute it and/or
gipmad 0:8f9b472ff818 9 modify it under the terms of the GNU Lesser General Public
gipmad 0:8f9b472ff818 10 License as published by the Free Software Foundation; either
gipmad 0:8f9b472ff818 11 version 2.1 of the License, or (at your option) any later version.
gipmad 0:8f9b472ff818 12
gipmad 0:8f9b472ff818 13 This library is distributed in the hope that it will be useful,
gipmad 0:8f9b472ff818 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
gipmad 0:8f9b472ff818 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
gipmad 0:8f9b472ff818 16 Lesser General Public License for more details.
gipmad 0:8f9b472ff818 17
gipmad 0:8f9b472ff818 18 You should have received a copy of the GNU Lesser General Public
gipmad 0:8f9b472ff818 19 License along with this library; if not, write to the Free Software
gipmad 0:8f9b472ff818 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
gipmad 0:8f9b472ff818 21 */
gipmad 0:8f9b472ff818 22
gipmad 0:8f9b472ff818 23 #ifndef __MODEM_H__
gipmad 0:8f9b472ff818 24 #define __MODEM_H__
gipmad 0:8f9b472ff818 25
gipmad 0:8f9b472ff818 26 #include "mbed.h"
gipmad 0:8f9b472ff818 27
gipmad 0:8f9b472ff818 28 #define DEFAULT_TIMEOUT 5
gipmad 0:8f9b472ff818 29
gipmad 0:8f9b472ff818 30
gipmad 0:8f9b472ff818 31 extern Serial pc;
gipmad 0:8f9b472ff818 32
gipmad 0:8f9b472ff818 33
gipmad 0:8f9b472ff818 34 enum DataType {
gipmad 0:8f9b472ff818 35 CMD = 0,
gipmad 0:8f9b472ff818 36 DATA = 1,
gipmad 0:8f9b472ff818 37 };
gipmad 0:8f9b472ff818 38
gipmad 0:8f9b472ff818 39 /** Modem class.
gipmad 0:8f9b472ff818 40 * Used for Modem communication. attention that Modem module communicate with MCU in serial protocol
gipmad 0:8f9b472ff818 41 */
gipmad 0:8f9b472ff818 42 class Modem
gipmad 0:8f9b472ff818 43 {
gipmad 0:8f9b472ff818 44
gipmad 0:8f9b472ff818 45 public:
gipmad 0:8f9b472ff818 46 /** Create Modem Instance
gipmad 0:8f9b472ff818 47 * @param tx uart transmit pin to communicate with Modem
gipmad 0:8f9b472ff818 48 * @param rx uart receive pin to communicate with Modem
gipmad 0:8f9b472ff818 49 * @param baudRate baud rate of uart communication
gipmad 0:8f9b472ff818 50 */
gipmad 0:8f9b472ff818 51 Modem(PinName tx, PinName rx, int baudRate) : serialModem(tx, rx) {
gipmad 0:8f9b472ff818 52 serialModem.baud(baudRate);
gipmad 0:8f9b472ff818 53 };
gipmad 0:8f9b472ff818 54
gipmad 0:8f9b472ff818 55 Serial serialModem;
gipmad 0:8f9b472ff818 56 //protected:
gipmad 0:8f9b472ff818 57 /** Power on Modem
gipmad 0:8f9b472ff818 58 */
gipmad 0:8f9b472ff818 59 void preInit(void);
gipmad 0:8f9b472ff818 60
gipmad 0:8f9b472ff818 61 /** check serialModem is readable or not
gipmad 0:8f9b472ff818 62 * @returns
gipmad 0:8f9b472ff818 63 * true on readable
gipmad 0:8f9b472ff818 64 * false on not readable
gipmad 0:8f9b472ff818 65 */
gipmad 0:8f9b472ff818 66 bool readable();
gipmad 0:8f9b472ff818 67
gipmad 0:8f9b472ff818 68 /** read one byte from serialModem
gipmad 0:8f9b472ff818 69 * @returns
gipmad 0:8f9b472ff818 70 * one byte read from serialModem
gipmad 0:8f9b472ff818 71 */
gipmad 0:8f9b472ff818 72 char readByte(void);
gipmad 0:8f9b472ff818 73
gipmad 0:8f9b472ff818 74 /** read from Modem module and save to buffer array
gipmad 0:8f9b472ff818 75 * @param buffer buffer array to save what read from Modem module
gipmad 0:8f9b472ff818 76 * @param count the maximal bytes number read from Modem module
gipmad 0:8f9b472ff818 77 * @param timeOut time to wait for reading from Modem module
gipmad 0:8f9b472ff818 78 * @returns
gipmad 0:8f9b472ff818 79 * 0 on success
gipmad 0:8f9b472ff818 80 * -1 on error
gipmad 0:8f9b472ff818 81 */
gipmad 0:8f9b472ff818 82 int readBuffer(char* buffer,int count, unsigned int timeOut);
gipmad 0:8f9b472ff818 83
gipmad 0:8f9b472ff818 84
gipmad 0:8f9b472ff818 85 /** clean Buffer
gipmad 0:8f9b472ff818 86 * @param buffer buffer to clean
gipmad 0:8f9b472ff818 87 * @param count number of bytes to clean
gipmad 0:8f9b472ff818 88 */
gipmad 0:8f9b472ff818 89 void cleanBuffer(char* buffer, int count);
gipmad 0:8f9b472ff818 90
gipmad 0:8f9b472ff818 91 /** send AT command to Modem module
gipmad 0:8f9b472ff818 92 * @param cmd command array which will be send to GPRS module
gipmad 0:8f9b472ff818 93 */
gipmad 0:8f9b472ff818 94 void sendCmd(const char* cmd);
gipmad 0:8f9b472ff818 95
gipmad 0:8f9b472ff818 96 /**send "AT" to Modem module
gipmad 0:8f9b472ff818 97 */
gipmad 0:8f9b472ff818 98 void sendATTest(void);
gipmad 0:8f9b472ff818 99
gipmad 0:8f9b472ff818 100 /** compare the response from GPRS module with a string
gipmad 0:8f9b472ff818 101 * @param resp buffer to be compared
gipmad 0:8f9b472ff818 102 * @param len length that will be compared
gipmad 0:8f9b472ff818 103 * @param timeout waiting seconds till timeout
gipmad 0:8f9b472ff818 104 */
gipmad 0:8f9b472ff818 105 bool respCmp(const char *resp, unsigned int len, unsigned int timeout);
gipmad 0:8f9b472ff818 106
gipmad 0:8f9b472ff818 107 /** check Modem module response before time out
gipmad 0:8f9b472ff818 108 * @param *resp correct response which Modem module will return
gipmad 0:8f9b472ff818 109 * @param *timeout waiting seconds till timeout
gipmad 0:8f9b472ff818 110 * @returns
gipmad 0:8f9b472ff818 111 * 0 on success
gipmad 0:8f9b472ff818 112 * -1 on error
gipmad 0:8f9b472ff818 113 */
gipmad 0:8f9b472ff818 114 int waitForResp(const char *resp, unsigned int timeout,DataType type);
gipmad 0:8f9b472ff818 115
gipmad 0:8f9b472ff818 116 /** send AT command to GPRS module and wait for correct response
gipmad 0:8f9b472ff818 117 * @param *cmd AT command which will be send to GPRS module
gipmad 0:8f9b472ff818 118 * @param *resp correct response which GPRS module will return
gipmad 0:8f9b472ff818 119 * @param *timeout waiting seconds till timeout
gipmad 0:8f9b472ff818 120 * @returns
gipmad 0:8f9b472ff818 121 * 0 on success
gipmad 0:8f9b472ff818 122 * -1 on error
gipmad 0:8f9b472ff818 123 */
gipmad 0:8f9b472ff818 124 int sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type);
gipmad 0:8f9b472ff818 125
gipmad 0:8f9b472ff818 126 Timer timeCnt;
gipmad 0:8f9b472ff818 127
gipmad 0:8f9b472ff818 128 private:
gipmad 0:8f9b472ff818 129
gipmad 0:8f9b472ff818 130 };
gipmad 0:8f9b472ff818 131
gipmad 0:8f9b472ff818 132 #endif