Small Testprogram to have WebAccess via Webserver to a 433Mhz tranmitter to control remotly some devices from remote, with TFTP, NTP and RMF. This could be a base to develop applications.

Dependencies:   ChaNFSSD TFTPServer RMFWeb

Dependents:   RMFWeb

Committer:
ED7418
Date:
Mon Jun 16 07:40:08 2014 +0000
Revision:
1:809b59c7a800
Parent:
0:51f1ef89ec7b
mbed-lib and other libs are a based on a project, published in a Elektor-book "ARM-microkontroller Part II".

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ED7418 0:51f1ef89ec7b 1
ED7418 0:51f1ef89ec7b 2 /*
ED7418 0:51f1ef89ec7b 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
ED7418 0:51f1ef89ec7b 4
ED7418 0:51f1ef89ec7b 5 Permission is hereby granted, free of charge, to any person obtaining a copy
ED7418 0:51f1ef89ec7b 6 of this software and associated documentation files (the "Software"), to deal
ED7418 0:51f1ef89ec7b 7 in the Software without restriction, including without limitation the rights
ED7418 0:51f1ef89ec7b 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ED7418 0:51f1ef89ec7b 9 copies of the Software, and to permit persons to whom the Software is
ED7418 0:51f1ef89ec7b 10 furnished to do so, subject to the following conditions:
ED7418 0:51f1ef89ec7b 11
ED7418 0:51f1ef89ec7b 12 The above copyright notice and this permission notice shall be included in
ED7418 0:51f1ef89ec7b 13 all copies or substantial portions of the Software.
ED7418 0:51f1ef89ec7b 14
ED7418 0:51f1ef89ec7b 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ED7418 0:51f1ef89ec7b 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ED7418 0:51f1ef89ec7b 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ED7418 0:51f1ef89ec7b 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ED7418 0:51f1ef89ec7b 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ED7418 0:51f1ef89ec7b 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ED7418 0:51f1ef89ec7b 21 THE SOFTWARE.
ED7418 0:51f1ef89ec7b 22 */
ED7418 0:51f1ef89ec7b 23
ED7418 0:51f1ef89ec7b 24 #include "GPRSModem.h"
ED7418 0:51f1ef89ec7b 25 #include "mbed.h"
ED7418 0:51f1ef89ec7b 26
ED7418 0:51f1ef89ec7b 27 //#define __DEBUG
ED7418 0:51f1ef89ec7b 28 #include "dbg/dbg.h"
ED7418 0:51f1ef89ec7b 29
ED7418 0:51f1ef89ec7b 30 #define WAIT_BTW_NETW_POLLS 3.
ED7418 0:51f1ef89ec7b 31
ED7418 0:51f1ef89ec7b 32 #include "netCfg.h"
ED7418 0:51f1ef89ec7b 33 #if NET_GPRS
ED7418 0:51f1ef89ec7b 34
ED7418 0:51f1ef89ec7b 35 GPRSModem::GPRSModem() : ATIf()
ED7418 0:51f1ef89ec7b 36 {
ED7418 0:51f1ef89ec7b 37 DBG("New GPRSModem@%p\n", this);
ED7418 0:51f1ef89ec7b 38 }
ED7418 0:51f1ef89ec7b 39
ED7418 0:51f1ef89ec7b 40 GPRSModem::~GPRSModem()
ED7418 0:51f1ef89ec7b 41 {
ED7418 0:51f1ef89ec7b 42
ED7418 0:51f1ef89ec7b 43 }
ED7418 0:51f1ef89ec7b 44
ED7418 0:51f1ef89ec7b 45 GPRSErr GPRSModem::getNetworkState()
ED7418 0:51f1ef89ec7b 46 {
ED7418 0:51f1ef89ec7b 47 ATIf::flushBuffer();
ED7418 0:51f1ef89ec7b 48 /*
ED7418 0:51f1ef89ec7b 49 netState can be : (Telit_AT_Reference_Guide.pdf p.98)
ED7418 0:51f1ef89ec7b 50 0 - not registered, ME is not currently searching a new operator to register to
ED7418 0:51f1ef89ec7b 51 1 - registered, home network
ED7418 0:51f1ef89ec7b 52 2 - not registered, but ME is currently searching a new operator to register to
ED7418 0:51f1ef89ec7b 53 3 - registration denied
ED7418 0:51f1ef89ec7b 54 4 - unknown
ED7418 0:51f1ef89ec7b 55 5 - registered, roaming
ED7418 0:51f1ef89ec7b 56 */
ED7418 0:51f1ef89ec7b 57 // DBG("Network?...\r\n");
ED7418 0:51f1ef89ec7b 58 ATIf::setReadMode(false); //Discard chars
ED7418 0:51f1ef89ec7b 59 ATIf::setTimeout(10000);
ED7418 0:51f1ef89ec7b 60 ATIf::setLineMode(true); //Line mode
ED7418 0:51f1ef89ec7b 61 int netState = 0;
ED7418 0:51f1ef89ec7b 62 int len;
ED7418 0:51f1ef89ec7b 63 len = ATIf::printf("AT+CREG?"); //Registered ?
ED7418 0:51f1ef89ec7b 64 if(!len) DBG("\r\nprintf - len=%d\r\n",len);
ED7418 0:51f1ef89ec7b 65 if(!len)
ED7418 0:51f1ef89ec7b 66 return GPRS_MODEM; //Nothing was actually sent
ED7418 0:51f1ef89ec7b 67
ED7418 0:51f1ef89ec7b 68 len = ATIf::scanf("+CREG: 0,%d", &netState); //Get status
ED7418 0:51f1ef89ec7b 69 if(len != 1) DBG("\r\nscanf - len=%d\r\n",len);
ED7418 0:51f1ef89ec7b 70 if(len != 1) //Likely +CMS ERROR was returned
ED7418 0:51f1ef89ec7b 71 return GPRS_MODEM;
ED7418 0:51f1ef89ec7b 72
ED7418 0:51f1ef89ec7b 73 if( !!ATIf::checkOK() ) //Should not be a problem
ED7418 0:51f1ef89ec7b 74 {DBG("\r\nNOK\r\n"); return GPRS_MODEM; }
ED7418 0:51f1ef89ec7b 75
ED7418 0:51f1ef89ec7b 76 switch(netState)
ED7418 0:51f1ef89ec7b 77 {
ED7418 0:51f1ef89ec7b 78 case 1:
ED7418 0:51f1ef89ec7b 79 case 5: //TODO: Option allow roaming
ED7418 0:51f1ef89ec7b 80 DBG("\r\nNetwork is up!\r\n");
ED7418 0:51f1ef89ec7b 81 return GPRS_OK;
ED7418 0:51f1ef89ec7b 82 case 3:
ED7418 0:51f1ef89ec7b 83 DBG("\r\nAccess to network denied.\r\n");
ED7418 0:51f1ef89ec7b 84 return GPRS_DENIED;
ED7418 0:51f1ef89ec7b 85 case 0:
ED7418 0:51f1ef89ec7b 86 DBG("\r\nNo network.\r\n");
ED7418 0:51f1ef89ec7b 87 return GPRS_NONETWORK;
ED7418 0:51f1ef89ec7b 88 case 4:
ED7418 0:51f1ef89ec7b 89 case 2:
ED7418 0:51f1ef89ec7b 90 //DBG("\r\nRegistering...\r\n");
ED7418 0:51f1ef89ec7b 91 return GPRS_REGISTERING;
ED7418 0:51f1ef89ec7b 92 }
ED7418 0:51f1ef89ec7b 93
ED7418 0:51f1ef89ec7b 94 return GPRS_MODEM; // Should not reach this
ED7418 0:51f1ef89ec7b 95
ED7418 0:51f1ef89ec7b 96 }
ED7418 0:51f1ef89ec7b 97
ED7418 0:51f1ef89ec7b 98 GPRSErr GPRSModem::setNetworkUp()
ED7418 0:51f1ef89ec7b 99 {
ED7418 0:51f1ef89ec7b 100 ATIf::flushBuffer();
ED7418 0:51f1ef89ec7b 101 GPRSErr err = GPRS_REGISTERING;
ED7418 0:51f1ef89ec7b 102 while(true)
ED7418 0:51f1ef89ec7b 103 {
ED7418 0:51f1ef89ec7b 104 err = getNetworkState();
ED7418 0:51f1ef89ec7b 105 if(err != GPRS_REGISTERING)
ED7418 0:51f1ef89ec7b 106 break;
ED7418 0:51f1ef89ec7b 107 wait(WAIT_BTW_NETW_POLLS);
ED7418 0:51f1ef89ec7b 108 }
ED7418 0:51f1ef89ec7b 109 return err;
ED7418 0:51f1ef89ec7b 110 }
ED7418 0:51f1ef89ec7b 111
ED7418 0:51f1ef89ec7b 112 //Same, but for GPRS
ED7418 0:51f1ef89ec7b 113 GPRSErr GPRSModem::getGPRSState()
ED7418 0:51f1ef89ec7b 114 {
ED7418 0:51f1ef89ec7b 115 ATIf::flushBuffer();
ED7418 0:51f1ef89ec7b 116 /*
ED7418 0:51f1ef89ec7b 117 netState can be : (Telit_AT_Reference_Guide.pdf p.192)
ED7418 0:51f1ef89ec7b 118 0 - not registered, terminal is not currently searching a new operator to register to
ED7418 0:51f1ef89ec7b 119 1 - registered, home network
ED7418 0:51f1ef89ec7b 120 2 - not registered, but terminal is currently searching a new operator to register to
ED7418 0:51f1ef89ec7b 121 3 - registration denied
ED7418 0:51f1ef89ec7b 122 4 - unknown
ED7418 0:51f1ef89ec7b 123 5 - registered, roaming
ED7418 0:51f1ef89ec7b 124 */
ED7418 0:51f1ef89ec7b 125
ED7418 0:51f1ef89ec7b 126 DBG("GPRS?...\r\n");
ED7418 0:51f1ef89ec7b 127 ATIf::setReadMode(false); //Discard chars
ED7418 0:51f1ef89ec7b 128 ATIf::setTimeout(10000);
ED7418 0:51f1ef89ec7b 129 ATIf::setLineMode(true); //Line mode
ED7418 0:51f1ef89ec7b 130 int netState = 0;
ED7418 0:51f1ef89ec7b 131 int len;
ED7418 0:51f1ef89ec7b 132 len = ATIf::printf("AT+CGREG?"); //Registered ?
ED7418 0:51f1ef89ec7b 133 if(!len)
ED7418 0:51f1ef89ec7b 134 return GPRS_MODEM; //Nothing was actually sent
ED7418 0:51f1ef89ec7b 135
ED7418 0:51f1ef89ec7b 136 len = ATIf::scanf("+CGREG: %*d,%d", &netState); //Get GPRS status, see GSM 07.07 spec as Telit AT ref is wrong
ED7418 0:51f1ef89ec7b 137 if(len != 1) DBG("\r\nscanf - len=%d\r\n",len);
ED7418 0:51f1ef89ec7b 138 if(len != 1) //Likely +CMS ERROR was returned
ED7418 0:51f1ef89ec7b 139 return GPRS_MODEM;
ED7418 0:51f1ef89ec7b 140
ED7418 0:51f1ef89ec7b 141 if( !!ATIf::checkOK() ) //Should not be a problem
ED7418 0:51f1ef89ec7b 142 return GPRS_MODEM;
ED7418 0:51f1ef89ec7b 143
ED7418 0:51f1ef89ec7b 144 switch(netState)
ED7418 0:51f1ef89ec7b 145 {
ED7418 0:51f1ef89ec7b 146 case 1:
ED7418 0:51f1ef89ec7b 147 case 5: //TODO: Option allow roaming
ED7418 0:51f1ef89ec7b 148 DBG("\r\nNetwork is up!\r\n");
ED7418 0:51f1ef89ec7b 149 return GPRS_OK;
ED7418 0:51f1ef89ec7b 150 case 3:
ED7418 0:51f1ef89ec7b 151 DBG("\r\nAccess to network denied.\r\n");
ED7418 0:51f1ef89ec7b 152 return GPRS_DENIED;
ED7418 0:51f1ef89ec7b 153 case 0:
ED7418 0:51f1ef89ec7b 154 DBG("\r\nNo network.\r\n");
ED7418 0:51f1ef89ec7b 155 return GPRS_NONETWORK;
ED7418 0:51f1ef89ec7b 156 case 4:
ED7418 0:51f1ef89ec7b 157 case 2:
ED7418 0:51f1ef89ec7b 158 DBG("\r\nRegistering...\r\n");
ED7418 0:51f1ef89ec7b 159 return GPRS_REGISTERING;
ED7418 0:51f1ef89ec7b 160 }
ED7418 0:51f1ef89ec7b 161
ED7418 0:51f1ef89ec7b 162 return GPRS_MODEM; // Should not reach this
ED7418 0:51f1ef89ec7b 163
ED7418 0:51f1ef89ec7b 164 }
ED7418 0:51f1ef89ec7b 165
ED7418 0:51f1ef89ec7b 166 GPRSErr GPRSModem::setGPRSUp()
ED7418 0:51f1ef89ec7b 167 {
ED7418 0:51f1ef89ec7b 168 ATIf::flushBuffer();
ED7418 0:51f1ef89ec7b 169 GPRSErr err;
ED7418 0:51f1ef89ec7b 170
ED7418 0:51f1ef89ec7b 171 err = setNetworkUp();
ED7418 0:51f1ef89ec7b 172 if(err)
ED7418 0:51f1ef89ec7b 173 return err;
ED7418 0:51f1ef89ec7b 174
ED7418 0:51f1ef89ec7b 175 DBG("\r\nAttaching GPRS...\r\n");
ED7418 0:51f1ef89ec7b 176 ATIf::setReadMode(false); //Discard chars
ED7418 0:51f1ef89ec7b 177 ATIf::setTimeout(10000);
ED7418 0:51f1ef89ec7b 178 ATIf::setLineMode(true); //Line mode
ED7418 0:51f1ef89ec7b 179 int len;
ED7418 0:51f1ef89ec7b 180
ED7418 0:51f1ef89ec7b 181 err = getGPRSState();
ED7418 0:51f1ef89ec7b 182 if(err == GPRS_NONETWORK)
ED7418 0:51f1ef89ec7b 183 {
ED7418 0:51f1ef89ec7b 184 len = ATIf::printf("AT+CGATT=1"); //Attach
ED7418 0:51f1ef89ec7b 185 if(!len)
ED7418 0:51f1ef89ec7b 186 return GPRS_MODEM; //Nothing was actually sent
ED7418 0:51f1ef89ec7b 187
ED7418 0:51f1ef89ec7b 188 if( !!ATIf::checkOK() ) //Should not be a problem
ED7418 0:51f1ef89ec7b 189 return GPRS_MODEM;
ED7418 0:51f1ef89ec7b 190 }
ED7418 0:51f1ef89ec7b 191
ED7418 0:51f1ef89ec7b 192 while(true)
ED7418 0:51f1ef89ec7b 193 {
ED7418 0:51f1ef89ec7b 194 err = getGPRSState();
ED7418 0:51f1ef89ec7b 195 if(err != GPRS_REGISTERING)
ED7418 0:51f1ef89ec7b 196 break;
ED7418 0:51f1ef89ec7b 197 wait(WAIT_BTW_NETW_POLLS);
ED7418 0:51f1ef89ec7b 198 }
ED7418 0:51f1ef89ec7b 199 return err;
ED7418 0:51f1ef89ec7b 200 }
ED7418 0:51f1ef89ec7b 201
ED7418 0:51f1ef89ec7b 202 GPRSErr GPRSModem::setGPRSDown()
ED7418 0:51f1ef89ec7b 203 {
ED7418 0:51f1ef89ec7b 204 ATIf::flushBuffer();
ED7418 0:51f1ef89ec7b 205 DBG("\r\nDetaching GPRS...\r\n");
ED7418 0:51f1ef89ec7b 206 ATIf::setReadMode(false); //Discard chars
ED7418 0:51f1ef89ec7b 207 ATIf::setTimeout(10000);
ED7418 0:51f1ef89ec7b 208 ATIf::setLineMode(true); //Line mode
ED7418 0:51f1ef89ec7b 209 int len;
ED7418 0:51f1ef89ec7b 210
ED7418 0:51f1ef89ec7b 211 len = ATIf::printf("AT+CGATT=0"); //Detach
ED7418 0:51f1ef89ec7b 212 if(!len)
ED7418 0:51f1ef89ec7b 213 return GPRS_MODEM; //Nothing was actually sent
ED7418 0:51f1ef89ec7b 214
ED7418 0:51f1ef89ec7b 215 if( !!ATIf::checkOK() ) //Should not be a problem
ED7418 0:51f1ef89ec7b 216 return GPRS_MODEM;
ED7418 0:51f1ef89ec7b 217
ED7418 0:51f1ef89ec7b 218 return GPRS_OK;
ED7418 0:51f1ef89ec7b 219 }
ED7418 0:51f1ef89ec7b 220
ED7418 0:51f1ef89ec7b 221
ED7418 0:51f1ef89ec7b 222 GPRSErr GPRSModem::connect(const char* apn /*=NULL*/)
ED7418 0:51f1ef89ec7b 223 {
ED7418 0:51f1ef89ec7b 224 ATIf::flushBuffer();
ED7418 0:51f1ef89ec7b 225 GPRSErr err;
ED7418 0:51f1ef89ec7b 226
ED7418 0:51f1ef89ec7b 227 ATIf::setReadMode(false); //Discard chars
ED7418 0:51f1ef89ec7b 228 ATIf::setTimeout(5000);
ED7418 0:51f1ef89ec7b 229 ATIf::setLineMode(true); //Line mode
ED7418 0:51f1ef89ec7b 230
ED7418 0:51f1ef89ec7b 231 DBG("\r\nConnecting...\r\n");
ED7418 0:51f1ef89ec7b 232
ED7418 0:51f1ef89ec7b 233 int len;
ED7418 0:51f1ef89ec7b 234
ED7418 0:51f1ef89ec7b 235 if( apn != NULL ) //Config APN
ED7418 0:51f1ef89ec7b 236 {
ED7418 0:51f1ef89ec7b 237 len = ATIf::printf("AT+CGDCONT=1,\"IP\",\"%s\"",apn); //Define APN
ED7418 0:51f1ef89ec7b 238 if(!len)
ED7418 0:51f1ef89ec7b 239 return GPRS_MODEM; //Nothing was actually sent
ED7418 0:51f1ef89ec7b 240
ED7418 0:51f1ef89ec7b 241 if( !!ATIf::checkOK() ) //Should not be a problem
ED7418 0:51f1ef89ec7b 242 return GPRS_MODEM;
ED7418 0:51f1ef89ec7b 243 }
ED7418 0:51f1ef89ec7b 244
ED7418 0:51f1ef89ec7b 245 err = setGPRSUp();
ED7418 0:51f1ef89ec7b 246 if(err)
ED7418 0:51f1ef89ec7b 247 return err;
ED7418 0:51f1ef89ec7b 248
ED7418 0:51f1ef89ec7b 249 ATIf::setReadMode(false); //Discard chars
ED7418 0:51f1ef89ec7b 250 ATIf::setTimeout(60000);
ED7418 0:51f1ef89ec7b 251 ATIf::setLineMode(true); //Line mode
ED7418 0:51f1ef89ec7b 252 //
ED7418 0:51f1ef89ec7b 253 //len = ATIf::printf("AT+CGDATA=\"PPP\",1"); //Connect using PDP context #1
ED7418 0:51f1ef89ec7b 254 // len = ATIf::printf("ATDT *99***1#");
ED7418 0:51f1ef89ec7b 255 len = ATIf::printf("ATDT *99#");
ED7418 0:51f1ef89ec7b 256 if(!len)
ED7418 0:51f1ef89ec7b 257 return GPRS_MODEM; //Nothing was actually sent
ED7418 0:51f1ef89ec7b 258
ED7418 0:51f1ef89ec7b 259 len = ATIf::scanf("CONNECT"); //Beginning of session
ED7418 0:51f1ef89ec7b 260 if(len != 0) //Likely +CME ERROR was returned or NO CARRIER
ED7418 0:51f1ef89ec7b 261 return GPRS_MODEM;
ED7418 0:51f1ef89ec7b 262
ED7418 0:51f1ef89ec7b 263 //ATIf::setSignals(false);
ED7418 0:51f1ef89ec7b 264
ED7418 0:51f1ef89ec7b 265 DBG("\r\nConnected.\r\n");
ED7418 0:51f1ef89ec7b 266
ED7418 0:51f1ef89ec7b 267 return GPRS_OK; //Time to enter a PPP Session !
ED7418 0:51f1ef89ec7b 268
ED7418 0:51f1ef89ec7b 269 }
ED7418 0:51f1ef89ec7b 270
ED7418 0:51f1ef89ec7b 271 GPRSErr GPRSModem::disconnect()
ED7418 0:51f1ef89ec7b 272 {
ED7418 0:51f1ef89ec7b 273 ATIf::flushBuffer();
ED7418 0:51f1ef89ec7b 274 ATIf::setReadMode(false); //Discard chars
ED7418 0:51f1ef89ec7b 275 ATIf::setTimeout(5000);
ED7418 0:51f1ef89ec7b 276 ATIf::setLineMode(true); //Line mode
ED7418 0:51f1ef89ec7b 277
ED7418 0:51f1ef89ec7b 278 if( !!ATIf::checkOK() ) //Should be present at the end of connection
ED7418 0:51f1ef89ec7b 279 return GPRS_MODEM;
ED7418 0:51f1ef89ec7b 280
ED7418 0:51f1ef89ec7b 281 GPRSErr err;
ED7418 0:51f1ef89ec7b 282 err = setGPRSDown();
ED7418 0:51f1ef89ec7b 283 if(err)
ED7418 0:51f1ef89ec7b 284 return err;
ED7418 0:51f1ef89ec7b 285
ED7418 0:51f1ef89ec7b 286 DBG("\r\nDisconnected.\r\n");
ED7418 0:51f1ef89ec7b 287
ED7418 0:51f1ef89ec7b 288 return GPRS_OK;
ED7418 0:51f1ef89ec7b 289 }
ED7418 0:51f1ef89ec7b 290
ED7418 0:51f1ef89ec7b 291 #endif
ED7418 0:51f1ef89ec7b 292