An Interface for SIMCOM modules.
Dependencies: ATCommand Azure_HTTPS_SIMCOM
Dependents: Azure_SIM800_HelloWorld
Fork of SIMInterface by
SIMInterface.cpp@1:31e884326b43, 2017-08-10 (annotated)
- Committer:
- BorjaTarazona
- Date:
- Thu Aug 10 11:06:54 2017 +0000
- Revision:
- 1:31e884326b43
- Parent:
- 0:f0ed40fee75d
Creation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BorjaTarazona | 0:f0ed40fee75d | 1 | #define _DEBUG 1 |
BorjaTarazona | 0:f0ed40fee75d | 2 | |
BorjaTarazona | 0:f0ed40fee75d | 3 | #if _DEBUG |
BorjaTarazona | 0:f0ed40fee75d | 4 | //Enable debug |
BorjaTarazona | 0:f0ed40fee75d | 5 | #define __DEBUG__ |
BorjaTarazona | 0:f0ed40fee75d | 6 | #include <cstdio> |
BorjaTarazona | 0:f0ed40fee75d | 7 | #define DBG(x, ...) printf("[SIMInterface : DBG] "x"\r\n", ##__VA_ARGS__); |
BorjaTarazona | 0:f0ed40fee75d | 8 | #define WARN(x, ...) printf("[SIMInterface : WARN] "x"\r\n", ##__VA_ARGS__); |
BorjaTarazona | 0:f0ed40fee75d | 9 | #define ERR(x, ...) printf("[SIMInterface : ERR] "x"\r\n", ##__VA_ARGS__); |
BorjaTarazona | 0:f0ed40fee75d | 10 | |
BorjaTarazona | 0:f0ed40fee75d | 11 | #else |
BorjaTarazona | 0:f0ed40fee75d | 12 | //Disable debug |
BorjaTarazona | 0:f0ed40fee75d | 13 | #define DBG(x, ...) |
BorjaTarazona | 0:f0ed40fee75d | 14 | #define WARN(x, ...) |
BorjaTarazona | 0:f0ed40fee75d | 15 | #define ERR(x, ...) |
BorjaTarazona | 0:f0ed40fee75d | 16 | #endif |
BorjaTarazona | 0:f0ed40fee75d | 17 | |
BorjaTarazona | 0:f0ed40fee75d | 18 | #include "SIMInterface.h" |
BorjaTarazona | 0:f0ed40fee75d | 19 | |
BorjaTarazona | 0:f0ed40fee75d | 20 | char* responseInterface; |
BorjaTarazona | 0:f0ed40fee75d | 21 | |
BorjaTarazona | 0:f0ed40fee75d | 22 | /** An Interface for SIMCOM modules |
BorjaTarazona | 0:f0ed40fee75d | 23 | * |
BorjaTarazona | 0:f0ed40fee75d | 24 | * @brief Currently tested on SIM900, SIM800, SIM800L and SIM800F |
BorjaTarazona | 0:f0ed40fee75d | 25 | * |
BorjaTarazona | 0:f0ed40fee75d | 26 | */ |
BorjaTarazona | 0:f0ed40fee75d | 27 | |
BorjaTarazona | 0:f0ed40fee75d | 28 | SIMInterface::SIMInterface(PinName tx, PinName rx, int baudrate, PinName pwr) // ATSerial |
BorjaTarazona | 0:f0ed40fee75d | 29 | : sim800Interface(tx, rx, baudrate), PWRKEY(pwr) |
BorjaTarazona | 0:f0ed40fee75d | 30 | { |
BorjaTarazona | 0:f0ed40fee75d | 31 | |
BorjaTarazona | 0:f0ed40fee75d | 32 | } |
BorjaTarazona | 0:f0ed40fee75d | 33 | |
BorjaTarazona | 0:f0ed40fee75d | 34 | /** Function to check if the module is on |
BorjaTarazona | 0:f0ed40fee75d | 35 | * |
BorjaTarazona | 0:f0ed40fee75d | 36 | * @param timeout is the time in ms that we will wait for the module to answer |
BorjaTarazona | 0:f0ed40fee75d | 37 | * |
BorjaTarazona | 0:f0ed40fee75d | 38 | **/ |
BorjaTarazona | 0:f0ed40fee75d | 39 | |
BorjaTarazona | 0:f0ed40fee75d | 40 | int SIMInterface::SIMOnOff(int timeout) |
BorjaTarazona | 0:f0ed40fee75d | 41 | { |
BorjaTarazona | 0:f0ed40fee75d | 42 | int count = 0; |
BorjaTarazona | 0:f0ed40fee75d | 43 | sim800Interface.sendCmd("AT\r\n"); |
BorjaTarazona | 0:f0ed40fee75d | 44 | count = sim800Interface.getResponseLength(timeout); |
BorjaTarazona | 0:f0ed40fee75d | 45 | DBG("%d", count); |
BorjaTarazona | 0:f0ed40fee75d | 46 | if(count>0) |
BorjaTarazona | 0:f0ed40fee75d | 47 | return 1; |
BorjaTarazona | 0:f0ed40fee75d | 48 | else |
BorjaTarazona | 0:f0ed40fee75d | 49 | return 0; |
BorjaTarazona | 0:f0ed40fee75d | 50 | } |
BorjaTarazona | 0:f0ed40fee75d | 51 | |
BorjaTarazona | 0:f0ed40fee75d | 52 | /** Function to wake up the SIM module |
BorjaTarazona | 0:f0ed40fee75d | 53 | **/ |
BorjaTarazona | 0:f0ed40fee75d | 54 | |
BorjaTarazona | 0:f0ed40fee75d | 55 | void SIMInterface::wakeUp() |
BorjaTarazona | 0:f0ed40fee75d | 56 | { |
BorjaTarazona | 0:f0ed40fee75d | 57 | while(SIMOnOff(2000) == 0) |
BorjaTarazona | 0:f0ed40fee75d | 58 | { |
BorjaTarazona | 0:f0ed40fee75d | 59 | PWRKEY = 0; |
BorjaTarazona | 0:f0ed40fee75d | 60 | wait_ms(1000); |
BorjaTarazona | 0:f0ed40fee75d | 61 | PWRKEY = 1; |
BorjaTarazona | 0:f0ed40fee75d | 62 | wait_ms(1000); |
BorjaTarazona | 0:f0ed40fee75d | 63 | PWRKEY = 0; |
BorjaTarazona | 0:f0ed40fee75d | 64 | wait_ms(2000); |
BorjaTarazona | 0:f0ed40fee75d | 65 | } |
BorjaTarazona | 0:f0ed40fee75d | 66 | sim800Interface.clearBuffer(); |
BorjaTarazona | 0:f0ed40fee75d | 67 | } |
BorjaTarazona | 0:f0ed40fee75d | 68 | |
BorjaTarazona | 0:f0ed40fee75d | 69 | /** Function to reset the module |
BorjaTarazona | 0:f0ed40fee75d | 70 | **/ |
BorjaTarazona | 0:f0ed40fee75d | 71 | |
BorjaTarazona | 0:f0ed40fee75d | 72 | void SIMInterface::reset() |
BorjaTarazona | 0:f0ed40fee75d | 73 | { |
BorjaTarazona | 0:f0ed40fee75d | 74 | PWRKEY = 0; |
BorjaTarazona | 0:f0ed40fee75d | 75 | wait_ms(1000); |
BorjaTarazona | 0:f0ed40fee75d | 76 | PWRKEY = 1; |
BorjaTarazona | 0:f0ed40fee75d | 77 | wait_ms(1000); |
BorjaTarazona | 0:f0ed40fee75d | 78 | PWRKEY = 0; |
BorjaTarazona | 0:f0ed40fee75d | 79 | wait_ms(1000); |
BorjaTarazona | 0:f0ed40fee75d | 80 | |
BorjaTarazona | 0:f0ed40fee75d | 81 | wakeUp(); |
BorjaTarazona | 0:f0ed40fee75d | 82 | } |
BorjaTarazona | 0:f0ed40fee75d | 83 | |
BorjaTarazona | 0:f0ed40fee75d | 84 | /** Function to set the date on the module |
BorjaTarazona | 0:f0ed40fee75d | 85 | * |
BorjaTarazona | 0:f0ed40fee75d | 86 | * @param Date is a char in the format "17/08/10,10:27:00+00" |
BorjaTarazona | 0:f0ed40fee75d | 87 | * |
BorjaTarazona | 0:f0ed40fee75d | 88 | **/ |
BorjaTarazona | 0:f0ed40fee75d | 89 | |
BorjaTarazona | 0:f0ed40fee75d | 90 | void SIMInterface::setDateRTC(char* Date) // char* Date has to be in this format 17/07/28,19:59:00+00 |
BorjaTarazona | 0:f0ed40fee75d | 91 | { |
BorjaTarazona | 0:f0ed40fee75d | 92 | char dateCmd [60]; |
BorjaTarazona | 0:f0ed40fee75d | 93 | DBG("%s", Date); |
BorjaTarazona | 0:f0ed40fee75d | 94 | sprintf(dateCmd, "AT+CCLK=\"%s\"\r\n", Date); |
BorjaTarazona | 0:f0ed40fee75d | 95 | DBG("AT+CLTS=1"); |
BorjaTarazona | 0:f0ed40fee75d | 96 | responseInterface = sim800Interface.sendCmdAndWaitForResp("AT+CLTS=1\r\n", 200); |
BorjaTarazona | 0:f0ed40fee75d | 97 | DBG("%s", responseInterface); |
BorjaTarazona | 0:f0ed40fee75d | 98 | DBG("AT+CCLK"); |
BorjaTarazona | 0:f0ed40fee75d | 99 | responseInterface = sim800Interface.sendCmdAndWaitForResp(dateCmd, 1000); |
BorjaTarazona | 0:f0ed40fee75d | 100 | DBG("%s", responseInterface); |
BorjaTarazona | 0:f0ed40fee75d | 101 | } |
BorjaTarazona | 0:f0ed40fee75d | 102 | |
BorjaTarazona | 0:f0ed40fee75d | 103 | /** Function to get the date from the module |
BorjaTarazona | 0:f0ed40fee75d | 104 | **/ |
BorjaTarazona | 0:f0ed40fee75d | 105 | |
BorjaTarazona | 0:f0ed40fee75d | 106 | char* SIMInterface::getDateRTC() |
BorjaTarazona | 0:f0ed40fee75d | 107 | { |
BorjaTarazona | 0:f0ed40fee75d | 108 | responseInterface = sim800Interface.sendCmdAndWaitForResp("AT+CCLK?\r\n", 80); |
BorjaTarazona | 0:f0ed40fee75d | 109 | DBG("%s", responseInterface); |
BorjaTarazona | 0:f0ed40fee75d | 110 | return responseInterface; |
BorjaTarazona | 0:f0ed40fee75d | 111 | } |
BorjaTarazona | 0:f0ed40fee75d | 112 | |
BorjaTarazona | 0:f0ed40fee75d | 113 | /** Function to turn the module off |
BorjaTarazona | 0:f0ed40fee75d | 114 | **/ |
BorjaTarazona | 0:f0ed40fee75d | 115 | |
BorjaTarazona | 0:f0ed40fee75d | 116 | void SIMInterface::turnOff() |
BorjaTarazona | 0:f0ed40fee75d | 117 | { |
BorjaTarazona | 0:f0ed40fee75d | 118 | if(SIMOnOff(2000) == 0) |
BorjaTarazona | 0:f0ed40fee75d | 119 | { |
BorjaTarazona | 0:f0ed40fee75d | 120 | |
BorjaTarazona | 0:f0ed40fee75d | 121 | }else |
BorjaTarazona | 0:f0ed40fee75d | 122 | { |
BorjaTarazona | 0:f0ed40fee75d | 123 | PWRKEY = 0; |
BorjaTarazona | 0:f0ed40fee75d | 124 | wait_ms(1000); |
BorjaTarazona | 0:f0ed40fee75d | 125 | PWRKEY = 1; |
BorjaTarazona | 0:f0ed40fee75d | 126 | wait_ms(1000); |
BorjaTarazona | 0:f0ed40fee75d | 127 | PWRKEY = 0; |
BorjaTarazona | 0:f0ed40fee75d | 128 | wait_ms(1000); |
BorjaTarazona | 0:f0ed40fee75d | 129 | } |
BorjaTarazona | 0:f0ed40fee75d | 130 | sim800Interface.clearBuffer(); |
BorjaTarazona | 0:f0ed40fee75d | 131 | } |
BorjaTarazona | 0:f0ed40fee75d | 132 | |
BorjaTarazona | 0:f0ed40fee75d | 133 |