Advertisement
salwaniza95

DFRobot_sim808.h

Apr 28th, 2017
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.91 KB | None | 0 0
  1. /*!
  2.  * @file DFRobot_sim808.h
  3.  * @n Header file for DFRobot's SIM808 GPS/DFRobot_SIM808/GSM Shield
  4.  *
  5.  * @copyright   [DFRobot](http://www.dfrobot.com), 2016
  6.  *
  7.  * @author [Jason](jason.ling@dfrobot.com)
  8.  * @version  V1.0
  9.  * @date  2016-09-23
  10.  
  11.   * The MIT License (MIT)
  12.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  13.  * of this software and associated documentation files (the "Software"), to deal
  14.  * in the Software without restriction, including without limitation the rights
  15.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16.  * copies of the Software, and to permit persons to whom the Software is
  17.  * furnished to do so, subject to the following conditions:
  18.  *
  19.  * The above copyright notice and this permission notice shall be included in
  20.  * all copies or substantial portions of the Software.
  21.  *
  22.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28.  * THE SOFTWARE.
  29.  */
  30.  
  31. #ifndef __DFROBOT_SIM808_H__
  32. #define __DFROBOT_SIM808_H__
  33.  
  34. #include "sim808.h"
  35.  
  36. /** DFRobot_SIM808 class.
  37.  *  used to realize DFRobot_SIM808 communication
  38.  */
  39.  
  40. enum Protocol {
  41.     CLOSED = 0,
  42.     TCP    = 1,
  43.     UDP    = 2,
  44. };
  45.  
  46. class DFRobot_SIM808
  47. {
  48. public:
  49.     /** Create DFRobot_SIM808 instance
  50.      *  @param number default phone number during mobile communication
  51.      */
  52.      
  53.     DFRobot_SIM808(uint8_t tx, uint8_t rx, uint32_t baudRate = 9600 );
  54.     DFRobot_SIM808(HardwareSerial *mySerial);
  55.     DFRobot_SIM808(SoftwareSerial *mySerial);
  56.    
  57.     /** get instance of DFRobot_SIM808 class
  58.      */
  59.     static DFRobot_SIM808* getInstance() {
  60.         return inst;
  61.     };
  62.    
  63.     /** initialize DFRobot_SIM808 module including SIM card check & signal strength
  64.      *  @return true if connected, false otherwise
  65.      */
  66.  
  67.     bool init(void);
  68.  
  69.    
  70.     /** check if DFRobot_SIM808 module is powered on or not
  71.      *  @returns
  72.      *      true on success
  73.      *      false on error
  74.      */
  75.     bool checkPowerUp(void);
  76.  
  77.    
  78.     /** power Up DFRobot_SIM808 module (JP has to be soldered)
  79.      *  @param  pin pin 9 connected to JP jumper so we can power up and down through software
  80.      *  @returns
  81.      *      
  82.      */
  83.     void powerUpDown(uint8_t pin);  
  84.    
  85.     /** power reset for SIM800 board
  86.      *  @param  pin (preconfigurated as OUTPUT)
  87.      *  @returns
  88.      *      
  89.      */
  90.     void powerReset(uint8_t pin);
  91.      
  92.      
  93.     /** send text SMS
  94.      *  @param  *number phone number which SMS will be send to
  95.      *  @param  *data   message that will be send to
  96.      *  @returns
  97.      *      false on success
  98.      *      true on error
  99.      */
  100.     bool sendSMS(char* number, char* data);
  101.  
  102.     /** Check if there is any UNREAD SMS: this function DOESN'T change the UNREAD status of the SMS
  103.      *  @returns
  104.      *      1..20 on success, position/index where SMS is stored, suitable for the function ReadSMS
  105.      *      -1 on error
  106.      *       0 - there is no SMS with specified status (UNREAD)
  107.      */
  108.  
  109.     char isSMSunread();
  110.    
  111.     /** read SMS, phone and date if getting a SMS message. It changes SMS status to READ
  112.      *  @param  messageIndex  SIM position to read
  113.      *  @param  message  buffer used to get SMS message
  114.      *  @param  length  length of message buffer
  115.      *  @param  phone  buffer used to get SMS's sender phone number
  116.      *  @param  datetime  buffer used to get SMS's send datetime
  117.      *  @returns
  118.      *      true on success
  119.      *      false on error
  120.      */
  121.     bool readSMS(int messageIndex, char *message, int length, char *phone, char *datetime);
  122.  
  123.     /** read SMS if getting a SMS message
  124.      *  @param  buffer  buffer that get from DFRobot_SIM808 module(when getting a SMS, DFRobot_SIM808 module will return a buffer array)
  125.      *  @param  message buffer used to get SMS message
  126.      *  @param  check   whether to check phone number(we may only want to read SMS from specified phone number)
  127.      *  @returns
  128.      *      true on success
  129.      *      false on error
  130.      */
  131.     bool readSMS(int messageIndex, char *message, int length);
  132.  
  133.     /** delete SMS message on SIM card
  134.      *  @param  index   the index number which SMS message will be delete
  135.      *  @returns
  136.      *      true on success
  137.      *      false on error
  138.      */
  139.     bool deleteSMS(int index);
  140.  
  141.     /** call someone
  142.      *  @param  number  the phone number which you want to call
  143.      *  @returns
  144.      *      true on success
  145.      *      false on error
  146.      */
  147.     bool callUp(char* number);
  148.  
  149.     /** auto answer if coming a call
  150.      *  @returns
  151.      */    
  152.     void answer(void);
  153.    
  154.     /** hang up if coming a call
  155.      *  @returns
  156.      *      true on success
  157.      *      false on error
  158.      */    
  159.     bool hangup(void);  
  160.  
  161.     /** Disable +CLIP notification when an incoming call is active, RING text is always shown. See isCallActive function
  162.      *  This is done in order no to overload serial outputCheck if there is a call active and get the phone number in that case
  163.      *  @returns
  164.      *      true on success
  165.      *      false on error
  166.      */
  167.     bool disableCLIPring(void);
  168.    
  169.     /** Get Subscriber Number (your number) using AT+CNUM command, but if nothing returns, then
  170.      *  you need to command this to your SIM900. (See AT+CPBS, AT+CPBW)
  171.      *  AT+CPBS="ON"
  172.      *  AT+CPBW=1,"+{Your Number}",145
  173.      *  AT+CPBS="SM"
  174.      *  @param
  175.      *  @return
  176.      *      true on success
  177.      *      false on error
  178.      */
  179.     bool getSubscriberNumber(char *number);
  180.    
  181.     /** Check if there is a call active and get the phone number in that case
  182.      *  @returns
  183.      *      true on success
  184.      *      false on error
  185.      */
  186.     bool isCallActive(char *number);  
  187.  
  188.     /** get DateTime from SIM900 (see AT command: AT+CLTS=1) as string
  189.      *  @param
  190.      *  @returns
  191.      *      true on success
  192.      *      false on error
  193.      *
  194.      *     If it doesn't work may be for two reasons:
  195.      *      1. Your carrier doesn't give that information
  196.      *      2. You have to configurate the SIM900 IC.
  197.          *          - First with SIM900_Serial_Debug example try this AT command: AT+CLTS?
  198.      *          - If response is 0, then it is disabled.
  199.      *          - Enable it by: AT+CLTS=1
  200.      *          - Now you have to save this config to EEPROM memory of SIM900 IC by: AT&W
  201.      *          - Now, you have to power down and power up again the SIM900
  202.      *          - Try now again: AT+CCLK?
  203.      *          - It should work now
  204.      *
  205.      */
  206.        
  207.     bool getDateTime(char *buffer);
  208.    
  209.     /** get Signal Strength from SIM900 (see AT command: AT+CSQ) as integer
  210.     *  @param
  211.     *  @returns
  212.     *      true on success
  213.     *      false on error
  214.     */
  215.     bool getSignalStrength(int *buffer);
  216.    
  217.     /** Send USSD Command Synchronously (Blocking call until unsolicited response is received)
  218.      *  @param
  219.      *      *ussdCommand string command UUSD, ex: *123#
  220.      *      *resultCode char Result Code, see AT+CUSD command
  221.      *      *response   string response
  222.      *      *cellBroadcast  int Cell Broadcast Data Coding Scheme
  223.      *  @returns
  224.      *      true on success
  225.      *      false on error
  226.      */  
  227.     bool sendUSSDSynchronous(char *ussdCommand, char *resultcode, char *response);
  228.  
  229.     /** Cancel USSD Session
  230.      *  @returns
  231.      *      true on success cancel active session
  232.      *      false on error or because no active session
  233.      */
  234.     bool cancelUSSDSession(void);
  235.  
  236. //////////////////////////////////////////////////////
  237. /// DFRobot_SIM808
  238. //////////////////////////////////////////////////////  
  239.    /**  Connect the DFRobot_SIM808 module to the network.
  240.      *  @return true if connected, false otherwise
  241.      */
  242.      
  243.     bool join(const __FlashStringHelper *apn = 0, const __FlashStringHelper *userName = 0, const __FlashStringHelper *passWord = 0);
  244.  
  245.     /** Disconnect the DFRobot_SIM808 module from the network
  246.      *  @returns
  247.      */
  248.     void disconnect(void);
  249.    
  250.     /** Open a tcp/udp connection with the specified host on the specified port
  251.      *  @param socket an endpoint of an inter-process communication flow of DFRobot_SIM808 module,for SIM900 module, it is in [0,6]
  252.      *  @param ptl protocol for socket, TCP/UDP can be choosen
  253.      *  @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
  254.      *  @param port port
  255.      *  @param timeout wait seconds till connected
  256.      *  @param chartimeout wait milliseconds between characters from DFRobot_SIM808 module
  257.      *  @returns true if successful
  258.      */
  259.     bool connect(Protocol ptl, const char * host, int port, int timeout = 2 * DEFAULT_TIMEOUT, int chartimeout = 2 * DEFAULT_INTERCHAR_TIMEOUT);
  260.     bool connect(Protocol ptl, const __FlashStringHelper *host, const __FlashStringHelper *port, int timeout = 2 * DEFAULT_TIMEOUT, int chartimeout = 2 * DEFAULT_INTERCHAR_TIMEOUT);
  261.  
  262.     /** Check if a tcp link is active
  263.      *  @returns true if successful
  264.      */
  265.     bool is_connected(void);
  266.    
  267.     /** Close a tcp connection
  268.      *  @returns true if successful
  269.      */
  270.     bool close(void);
  271.    
  272.     /** check if DFRobot_SIM808 module is readable or not
  273.      *  @returns true if readable
  274.      */
  275.     int readable(void);
  276.  
  277.     /** wait a few time to check if DFRobot_SIM808 module is readable or not
  278.      *  @param socket socket
  279.      *  @param wait_time time of waiting
  280.      */
  281.     int wait_readable(int wait_time);
  282.  
  283.     /** wait a few time to check if DFRobot_SIM808 module is writeable or not
  284.      *  @param socket socket
  285.      *  @param wait_time time of waiting
  286.      */
  287.     int wait_writeable(int req_size);
  288.  
  289.     /** send data to socket
  290.      *  @param socket socket
  291.      *  @param str string to be sent
  292.      *  @param len string length
  293.      *  @returns return bytes that actually been send
  294.      */
  295.     int send(const char * str, int len);
  296.  
  297.     /** read data from socket
  298.      *  @param socket socket
  299.      *  @param buf buffer that will store the data read from socket
  300.      *  @param len string length need to read from socket
  301.      *  @returns bytes that actually read
  302.      */
  303.     int recv(char* buf, int len);
  304.  
  305.     /** Enables the selected software serial port to listen
  306.      *  @returns none
  307.      */
  308.     void listen(void);
  309.    
  310.     /** Tests to see if requested software serial port is actively listening.
  311.      *  @returns none
  312.      */
  313.     bool isListening(void);
  314.  
  315.     /** convert the host to ip
  316.      *  @param host host ip string, ex. 10.11.12.13
  317.      *  @param ip long int ip address, ex. 0x11223344
  318.      *  @returns true if successful
  319.      */
  320.     //NOT USED bool gethostbyname(const char* host, uint32_t* ip);
  321.    
  322.     char* getIPAddress();
  323.     unsigned long getIPnumber();   
  324.     bool getLocation(const __FlashStringHelper *apn, float *longitude, float *latitude);
  325.    
  326.     //Open or Close GPS
  327.     bool  attachGPS();
  328.     bool  detachGPS();
  329.    
  330.      // Parse a (potentially negative) number with up to 2 decimal digits -xxxx.yy
  331.      
  332.     void getTime(uint32_t time);
  333.     void getDate(uint32_t data);  
  334.     int32_t parseFloat(const char *term).toFixed(6);  
  335.    
  336.    
  337.     //parser Serial data
  338.     bool  parseGPRMC(char *gpsbuffer);
  339.     bool  getGPRMC();
  340.    
  341.     //get GPS signal
  342.     bool  getGPS();
  343.    
  344.    
  345.      SoftwareSerial *gprsSerial;
  346.      HardwareSerial *hgprsSerial;
  347.     Stream *sgprsSerial;
  348.    
  349. public:
  350.     struct gspdata{
  351.         uint16_t year;
  352.         uint8_t month;
  353.         uint8_t day;
  354.         uint8_t hour;
  355.         uint8_t minute;
  356.         uint8_t second;
  357.         uint8_t centisecond;
  358.         float lat;
  359.         float lon;
  360.         float speed_kph;
  361.         float heading;
  362.         float altitude;
  363.     }GPSdata;
  364.  
  365. private:
  366.     byte serialFlag;
  367.     bool checkSIMStatus(void);
  368.     uint32_t str_to_ip(const char* str);
  369.     static DFRobot_SIM808* inst;
  370.     uint32_t _ip;
  371.     char ip_string[16]; //XXX.YYY.ZZZ.WWW + \0
  372. };
  373. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement