A simple WIP that logs data from a Grove sensor, and can send and receive information over USB and SMS.

Dependencies:   DHT DS_1337 SDFileSystem USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GprsHandler.h Source File

GprsHandler.h

00001 #ifndef GPRSHANDLER_H_
00002 #define GPRSHANDLER_H_
00003 
00004 #include "config.h"
00005 #ifdef ENABLE_GPRS_TESTING
00006 /*
00007  *  This section of the program is currently under development. The rest of the functions
00008  * can run wtihout this component by using the ENABLE_GPRS_TESTING flag.
00009  */
00010 
00011 #include "USBDevice.h"  // need to include this, so that USBSerial has correct typedefs!
00012 #include "USBSerial.h"
00013 #include "AbstractHandler.h"
00014 
00015 #define GPRS_BUF_LEN 20
00016 
00017 #define GPRS_MESSAGE_MAXLEN 160
00018 #define GPRS_RECIPIENTS_MAXLEN 20
00019 
00020 struct GprsRequest
00021 {
00022     char message[GPRS_MESSAGE_MAXLEN];
00023     char recipients[GPRS_RECIPIENTS_MAXLEN];
00024 };
00025 class UsbComms;
00026 class CircBuff;
00027 
00028 /*!
00029  * \brief The GprsHandler class saves recipients and looks after incoming and outgoing messages
00030  *
00031  * Options: save recipients internally to this class.
00032  * Or - request sends a struct that includes recipients list and message string
00033  */
00034 class GprsHandler : public AbstractHandler
00035 {
00036 public:
00037     GprsHandler(MyTimers * _timer, UsbComms *_usb);
00038     virtual ~GprsHandler();
00039 
00040     void run();
00041 
00042     void setRequest(int request, void *data = 0);
00043 
00044     enum request_t {
00045         gprsreq_GprsNone,       ///< No request (for tracking what the last request was, this is initial value for that)
00046         gprsreq_SmsSend,        ///< got a string to send to recipient(s)
00047         gprsreq_SetRecipients   ///< got a string holding the number(s) we want to send
00048     };
00049 
00050 private:
00051     enum mode_t{
00052         gprs_Start,             ///< Set up the state machine and the hardware
00053 
00054         // Restart sequence
00055         gprs_PowerOff,          ///< Ensure module is powered down first, so we know it is in its initial state
00056         gprs_PowerOffWait,      ///< Give module time to power down
00057         gprs_PowerSupplyOn,     ///< Once module has fully powered down, power back up to begin using it
00058         gprs_PowerSupplyOnWait, ///< Give time to power on properly
00059         gprs_PowerSwitchOn,
00060         gprs_PowerSwitchOnWait,
00061 
00062         gprs_CheckATReqs,       ///< Check was AT request is next
00063 
00064         // read AT+CMGR=?
00065         // write AT+CMGS=?
00066         gprs_PostTx,            ///< Send the current buffer to SIM900 and setup timeouts
00067         gprs_WaitRx,            ///< Wait for a response over serial
00068         gprs_CheckRx,           ///< Check the response. Go back into state machine depending on response
00069         
00070         gprs_WaitUntilNextRequest, 
00071 
00072         gprs_RxTimeout,         ///< no response
00073         gprs_RxError            ///< wrong response
00074 
00075 
00076     };
00077     mode_t mode;            ///< the current state in the state machine
00078     mode_t returnMode;      ///< the state to go back to when the expected reply returns from the SIM900
00079 
00080     ///
00081     /// \brief The at_req enum stores what AT command should be sent/was just sent to the SIM900
00082     ///
00083     enum at_req {
00084         atreq_Test,         ///< Ping the SIM900
00085         atreq_CheckSMS,     ///< Check if an SMS is available
00086         atreq_SendSMS       ///< Send an SMS, according to m_lastRequest
00087     };
00088     at_req m_atReq;
00089 
00090     request_t  m_lastRequest;
00091     GprsRequest m_lastMessage;
00092 
00093     Serial * m_serial; //!< Serial port for comms with SIM900
00094 
00095     DigitalOut * m_sim900_pwr;  //!< pin used to enable the SIM900 power switch
00096     DigitalOut * m_sim900_on;   //!< pin used to drive the power key
00097 
00098     uint8_t m_reqReg;   ///< request register
00099 
00100     unsigned char txBuf[GPRS_BUF_LEN];
00101     uint16_t txBufLen;
00102     unsigned char rxBuf[GPRS_BUF_LEN];
00103     
00104     UsbComms *m_usb;
00105     CircBuff *m_rxBuff;
00106     
00107 };
00108 
00109 #endif
00110 
00111 #endif /* GPRSHANDLER_H_ */
00112