Mindstorms 3rd Party ROBOTC Drivers RobotC
[Home] [Download] [Submit a bug/suggestion] [ROBOTC Forums] [Blog] [Support this project]

dexterind-wifi.h

Go to the documentation of this file.
00001 
00002 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00003 //
00004 //                 Setup for Wifi Sensor
00005 //                 Sets up flow control
00006 // Script for connecting to internet
00007 //
00008 // ate0     // Turn off the echo.
00009 // at&k1    // Enable software flow control.
00010 //
00011 // at+wauth=1 // Authentication mode: open
00012 // at+wwep1=0fb3ba79eb  // Set WEP key
00013 // at+wa=NPX97    // Connect to the SSID
00014 // at+ndhcp=1   // Enable DHCP <Dynamic Host Configuration Protocol>
00015 //
00016 // at+dnslookup=www.dexterindustries.com  // Lookup the DNS configuration.
00017 //
00018 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00019 
00020 #pragma systemFile
00021 
00022 #ifndef __COMMON_H__
00023 #include "common.h"
00024 #endif
00025 
00026 #ifndef __RS485_H__
00027 #include "common-rs485.h"
00028 #endif
00029 
00030 #define DEBUG_WIFI 1
00031 
00032 
00033 long nRcvChars = 0;
00034 ubyte BytesRead[8];
00035 char linefeed[] = {0x0A};
00036 char beginmarker[] = {27, 'S', 0};
00037 char endmarker[] = {27, 'E', 0};
00038 
00039 long DWIFI_baudRates[] = {9600, 19200, 38400, 57600, 115200, 230400,460800, 921600};
00040 string DWIFIscratchString;
00041 tMassiveArray DWIFIscratchArray;
00042 
00043 
00044 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00045 //
00046 //  Setup High Speed on Port 4.
00047 //
00048 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00049 bool DWIFIcheckResult(tMassiveArray &readdata, char * caller = NULL)
00050 {
00051   int len;
00052   // Read the RX buffer, but only wait 50ms max.
00053   if (!RS485read(readdata, len, 50))
00054     return false;
00055 
00056   if (len < 1)
00057     return false;
00058 
00059   if ((StringFind((char *)readdata, "OK") > -1) || (StringFind((char *)readdata, "0") > -1))
00060   {
00061     if (caller != NULL)
00062       writeDebugStreamLine("DWIFIcheckResult %s SUCCESS", caller);
00063     else
00064       writeDebugStreamLine("DWIFIcheckResult SUCCESS");
00065     return true;
00066   }
00067   else
00068   {
00069     if (caller != NULL)
00070       writeDebugStreamLine("DWIFIcheckResult %s FAILURE", caller);
00071     else
00072       writeDebugStreamLine("DWIFIcheckResult FAILURE");
00073     return false;
00074   }
00075 }
00076 
00077 
00078 bool DWIFIcheckResult(tMassiveArray &readdata, int timeout, char * caller = NULL)
00079 {
00080   int len;
00081   // Read the RX buffer, but only wait 50ms max.
00082 
00083   // writeDebugStreamLine("DWIFIcheckResult with extra timeout");
00084 
00085   if (!RS485read(readdata, len, timeout))
00086     return false;
00087 
00088   if (len < 1)
00089     return false;
00090 
00091   if ((StringFind((char *)readdata, "OK") > -1) || (StringFind((char *)readdata, "0") > -1))
00092   {
00093     if (caller != NULL)
00094       writeDebugStreamLine("DWIFIcheckResult %s SUCCESS", caller);
00095     else
00096       writeDebugStreamLine("DWIFIcheckResult SUCCESS");
00097     return true;
00098   }
00099   else
00100   {
00101     if (caller != NULL)
00102       writeDebugStreamLine("DWIFIcheckResult %s FAILURE", caller);
00103     else
00104       writeDebugStreamLine("DWIFIcheckResult FAILURE");
00105     return false;
00106   }
00107 }
00108 
00109 
00110 int checkFailure() {
00111   ubyte currByte[] = {0};
00112   ubyte prevByte[] = {0};
00113 
00114   while (nxtGetAvailHSBytes() == 0) wait1Msec(5);
00115 
00116   while (nxtGetAvailHSBytes() > 0) {
00117     nxtReadRawHS(&currByte[0], 1);
00118     if ((prevByte[0] == 27) && (currByte[0] == 'F'))
00119       return 1;
00120     else if ((currByte[0] > '0') && (currByte[0] < '7'))
00121       return 1;
00122     else if (currByte[0] == '9')
00123       return 2;
00124   }
00125   return 0;
00126 }
00127 
00128 
00129 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00130 //
00131 //      Echo All Input Off - Turns off the echo effect on the wifi.
00132 //      Sending the serial command "ate0" which turns off the echo effect.
00133 //      Sends one single byte at a time, pauses.
00134 //      Drains receiver with a read each time.
00135 //
00136 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00137 
00138 void DWIFIsetEcho(bool on)
00139 {
00140   writeDebugStreamLine("DWIFIsetEcho");
00141 
00142   ubyte nData[] = {'A','T','E','0',0x0A};
00143   if (on)
00144   {
00145     nData[3] = '1';
00146   }
00147 
00148   for(int i = 0; i < 5; i++){
00149     nxtWriteRawHS(&nData[i], 1);            // Send the command, byte by byte.
00150     nxtReadRawHS(&BytesRead[0], 8);         // Clear out the echo.
00151     wait1Msec(100);
00152   }
00153   RS485clearRead();
00154 }
00155 
00156 
00157 bool DWIFIsetVerbose(bool on)
00158 {
00159   bool res;
00160   writeDebugStreamLine("DWIFIsetVerbose");
00161 
00162   if (on)
00163     res = RS485sendString("ATV1\n");
00164   else
00165     res = RS485sendString("ATV0\n");
00166 
00167   if (!res)
00168     return false;
00169 
00170   return DWIFIcheckResult(RS485rxbuffer, "DWIFIsetVerbose");
00171 }
00172 
00173 
00174 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00175 //
00176 //      Software Flow Control On
00177 //      Send string "at&k1" and carriage return.
00178 //      Shouldn't need the wait or read now that we've got the echo off.
00179 //
00180 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00181 
00182 bool DWIFIsetSWControl()
00183 {
00184   //DWIFIscratchString = "AT&K1\n";
00185   //memcpy(RS485txbuffer, DWIFIscratchString, strlen(DWIFIscratchString));
00186   //return RS485write(RS485txbuffer, strlen(DWIFIscratchString));
00187   if (!RS485sendString("AT&K1\n"))
00188     return false;
00189 
00190   return DWIFIcheckResult(RS485rxbuffer, "DWIFIsetSWControl");
00191 
00192 }
00193 
00194 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00195 //
00196 //      Scan for networks
00197 //      Send string "AT+WS" and carriage return.
00198 //      Don't really need to do this; gets back a tonne of data.
00199 //
00200 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00201 
00202 bool DWIFIscanNetworks()
00203 {
00204   //DWIFIscratchString = "AT+ws\n";
00205   //memcpy(RS485txbuffer, DWIFIscratchString, strlen(DWIFIscratchString));
00206   //return RS485write(RS485txbuffer, strlen(DWIFIscratchString));
00207   return RS485sendString("AT+ws\n");
00208 }
00209 
00210 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00211 //
00212 //      WIFI:  Set Authentication Mode
00213 //             Send string "at+wauth=<n>" and carriage return.
00214 //              n = 0 --> Off
00215 //              n = 1 --> On
00216 //
00217 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00218 
00219 bool DWIFIsetAuthMode(int state)
00220 {
00221   if (state == 0)
00222     return RS485sendString("AT+WAUTH=0\n");
00223   else
00224     return RS485sendString("AT+WAUTH=1\n");
00225 }
00226 
00227 
00228 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00229 //
00230 //      WIFI:  Set WEP key.
00231 //             Send string "at+WWEPn=<m>" and carriage return.
00232 //
00233 //                  m = WEP Key.
00234 //                  n = type of WEP key.
00235 //                        n= 0
00236 //                        n= 1
00237 //                        n= 2
00238 //
00239 ////////////////////////////////////////////////////////////////////////////////////////////////////////
00240 
00241 bool DWIFIsetSSID(char *_ssid)
00242 {
00243   writeDebugStreamLine("DWIFIsetSSID");
00244   char *ssid_cmd = &DWIFIscratchArray;
00245   memset(ssid_cmd, 0, sizeof(tHugeByteArray));
00246   sprintf(ssid_cmd, "AT+WA=%s\n", _ssid);
00247   RS485sendString(ssid_cmd);
00248   return DWIFIcheckResult(RS485rxbuffer, 500, "DWIFIsetSSID");
00249 }
00250 
00251 
00252 bool DWIFIsetWEPKey(int keyindex, char *_wep_key)
00253 {
00254   writeDebugStreamLine("DWIFIsetWEPKey");
00255   char *set_wep_psk_cmd = &DWIFIscratchArray;
00256   memset(set_wep_psk_cmd, 0, sizeof(tHugeByteArray));
00257   sprintf(set_wep_psk_cmd, "AT+WWEP%d=%s\n", keyindex, _wep_key);
00258   return RS485sendString(set_wep_psk_cmd);
00259 }
00260 
00261 
00262 bool DWIFIsetWPAPSK(char *ssid, char *_wpa_key)
00263 {
00264   int len;
00265   writeDebugStreamLine("DWIFIsetWPAPSK");
00266 
00267   char *set_wpa_psk_cmd = &DWIFIscratchArray;
00268   memset(set_wpa_psk_cmd, 0, sizeof(tHugeByteArray));
00269   sprintf(set_wpa_psk_cmd, "AT+WPAPSK=%s,%s\n", ssid, _wpa_key);
00270   if (!RS485sendString(set_wpa_psk_cmd))
00271     return false;
00272   //RS485read(RS485rxbuffer, len, 50);
00273   wait1Msec(100);
00274   RS485clearRead();
00275   return DWIFIcheckResult(RS485rxbuffer, 30000, "DWIFIsetWPAPSK");
00276 }
00277 
00278 bool DWIFIsetDHCP(bool useDHCP)
00279 {
00280   if (useDHCP)
00281     RS485sendString("AT+NDHCP=1\n");
00282   else
00283     RS485sendString("AT+NDHCP=0\n");
00284 
00285   return DWIFIcheckResult(RS485rxbuffer, 1000, "DWIFIsetDHCP");
00286 }
00287 
00288 bool getFW()
00289 {
00290   writeDebugStreamLine("getFW");
00291   return RS485sendString("AT+VER=?\n");
00292 }
00293 
00294 
00295 bool getInfo() {
00296   writeDebugStreamLine("getInfo");
00297   return RS485sendString("AT+NSTAT=?\n");
00298 }
00299 
00300 bool getInfoWLAN() {
00301   writeDebugStreamLine("getInfo");
00302   return RS485sendString("AT+WSTATUS\n");
00303 }
00304 
00305 bool DWIFIClose(int cid = -1) {
00306   int len;
00307   bool res;
00308 
00309   writeDebugStreamLine("DWIFIClose");
00310 
00311   if (cid < 0)
00312     res = RS485sendString("AT+NCLOSEALL\n");
00313   else
00314   {
00315     StringFormat(DWIFIscratchString, "AT+NCLOSE=%d\n", cid);
00316     res = RS485sendString(DWIFIscratchString);
00317   }
00318 
00319   if (!res)
00320     return false;
00321 
00322   return DWIFIcheckResult(RS485rxbuffer, "DWIFIClose");
00323   //writeDebugStreamLine("closeAllCons");
00324   //return RS485sendString("AT+NCLOSEALL\n");
00325 }
00326 
00327 //bool closeConn(int cid) {
00328 //  writeDebugStreamLine("closeConn");
00329 //  StringFormat(DWIFIscratchString, "AT+NCLOSE=%d\n", cid);
00330 //  return RS485sendString(DWIFIscratchString);
00331 //}
00332 
00333 bool DWIFIsaveConfig() {
00334   int len;
00335   writeDebugStreamLine("save config");
00336   RS485sendString("AT&W0\n");
00337   DWIFIcheckResult(RS485rxbuffer, "DWIFIsaveConfig W0");
00338   wait1Msec(500);
00339   RS485sendString("AT&Y0\n");
00340   return DWIFIcheckResult(RS485rxbuffer, "DWIFIsaveConfig Y0");
00341 }
00342 
00343 
00344 void DWIFIresetConfig()
00345 {
00346   RS485sendString("AT&F\n");
00347   wait1Msec(100);
00348   RS485clearRead();
00349 }
00350 
00351 void DWIFITCPOpenServer(long port) {
00352   int index = 0;
00353   int len = 0;
00354 
00355   string listen_cmd;
00356   StringFormat(listen_cmd, "AT+NSTCP=%d\n", port);
00357   RS485sendString(listen_cmd);
00358   RS485read(RS485rxbuffer, len, 100);
00359 }
00360 
00361 bool checkBaudRate(long baudrate)
00362 {
00363   //int len = 0;
00364   //char *tmpbuff;
00365   string tmpString;
00366   writeDebugStreamLine("testing baudrate: %d", baudrate);
00367   nxtDisableHSPort();
00368   wait1Msec(10);
00369   nxtEnableHSPort();
00370   nxtSetHSBaudRate(baudrate);
00371   nxtHS_Mode = hsRawMode;
00372   RS485clearRead();
00373   wait1Msec(100);
00374   RS485sendString("+++\n");
00375   wait1Msec(1000);
00376   RS485clearRead();
00377   wait1Msec(100);
00378   RS485sendString("AT\n");
00379   return DWIFIcheckResult(RS485rxbuffer, "checkBaudRate");
00380 }
00381 
00382 long DWIFIscanBaudRate() {
00383   for (int i = 0; i < 8; i++) {
00384     if (checkBaudRate(DWIFI_baudRates[i]))
00385     {
00386       RS485clearRead();
00387       return DWIFI_baudRates[i];
00388     }
00389   }
00390   RS485clearRead();
00391   return 0;
00392 }
00393 
00394 
00395 void DWIFIsetBAUDRate(long baudrate) {
00396   int index = 0;
00397   long current_baudrate = 0;
00398 
00399   string baud_cmd;
00400 
00401   if (checkBaudRate(baudrate))
00402   {
00403     writeDebugStreamLine("DWIFIsetBAUDRate: already correct");
00404     return;
00405   }
00406 
00407   current_baudrate = DWIFIscanBaudRate();
00408   if (current_baudrate == 0)
00409   {
00410     writeDebugStreamLine("DWIFIsetBAUDRate could not determine baudrate");
00411     return;
00412   }
00413 
00414   StringFormat(baud_cmd, "ATB=%d\n", baudrate);
00415   RS485sendString(baud_cmd);
00416 
00417   wait1Msec(100);
00418   nxtDisableHSPort();
00419   wait1Msec(10);
00420   nxtEnableHSPort();
00421   nxtSetHSBaudRate(baudrate);
00422   nxtHS_Mode = hsRawMode;
00423   wait1Msec(10);
00424   RS485clearRead();
00425   wait1Msec(10);
00426 
00427   // Verify the speed to ensure everything went OK
00428   if (checkBaudRate(baudrate))
00429   {
00430     writeDebugStreamLine("DWIFIsetBAUDRate: reconfigured succesfully");
00431   }
00432   else
00433   {
00434     writeDebugStreamLine("DWIFIsetBAUDRate: reconfigured unsuccesfully");
00435   }
00436 }
00437 
00438 /*
00439     IP              SubNet         Gateway
00440  192.168.178.26: 255.255.255.0: 192.168.178.1
00441  */
00442 void DWIFIwaitForIP()
00443 {
00444   string IPscratch;
00445   char *e_marker = "Gateway";
00446   char *token_begin = " ";
00447   char *token_end = ":";
00448 
00449   tMassiveArray tmp_array;
00450   int len;
00451   int bpos;
00452   int epos;
00453   bool parsed = true;
00454   int startIPinfo = 0;
00455   int nextTokenStart = 0;
00456   writeDebugStreamLine("Beging parsing...");
00457   ubyte conn[] = {0};
00458   int index = 0;
00459   memset(&tmp_array[0], 0, sizeof(tMassiveArray));
00460   while (true)
00461   {
00462     RS485read(RS485rxbuffer, len, 100);
00463     if (len > 0)
00464     {
00465       writeDebugStreamLine("Appending to buffer: %d", len);
00466       parsed = false;
00467       if (index == 0)
00468         memset(&tmp_array[0], 0, sizeof(tMassiveArray));
00469 
00470       index = RS485appendToBuff(tmp_array, index, &RS485rxbuffer[0], len);
00471     }
00472     else if ((parsed == false) && (len == 0))
00473     {
00474       writeDebugStreamLine("Parsing buffer: %s", &tmp_array[0]);
00475       epos = StringFind((char *)&tmp_array[0], &e_marker[0]);
00476       // we've found a start and end marker
00477 
00478       if (epos > -1)
00479       {
00480         writeDebugStreamLine("end marker found: %d", epos);
00481         for (int i = 0; i < 3; i++)
00482         {
00483           writeDebugStreamLine("nextTokenStart: %d", nextTokenStart);
00484           nextTokenStart = epos;
00485           memmove((char *)&tmp_array[0], (char *)&tmp_array[nextTokenStart], sizeof(tMassiveArray) - nextTokenStart);
00486           bpos = StringFind((char *)&tmp_array[0], token_begin);
00487           epos = StringFind((char *)&tmp_array[0], token_end);
00488           if (bpos > -1 && epos > -1)
00489                       {
00490                         writeDebugStreamLine("IP bpos: %d, epos: %d", bpos, epos);
00491                         memcpy(IPscratch, (char *)&tmp_array[bpos], epos - bpos);
00492                         writeDebugStreamLine("IPinfo: %s", IPscratch);
00493                       }
00494         }
00495       }
00496       else
00497       {
00498         writeDebugStreamLine("no markers found");
00499       }
00500       parsed = true;
00501       index = 0;
00502       wait1Msec(2000);
00503       PlaySound(soundBeepBeep);
00504     }
00505         }
00506 }