Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.74 KB | None | 0 0
  1. //--- THIS CODE controls the BLDC using CC3000 wifi shield, ESC, arduino, and a telnet program
  2. //--- use puTTy or the developed C# program.
  3. //--- Hold 'W' for incrementing the speed of the motor, 'S' for decrease or reverse.
  4.  
  5. //--- TO DO: Motor is fluctuating, probably because of the wifi codes.
  6.  
  7. /***************************************************
  8. * Adafruit CC3000 Breakout/Shield TCP Chat Server
  9. * This is a simple chat server which allows clients to connect
  10. * with telnet and exchange messages. Anything sent by one
  11. * client will be written out to all connected clients.
  12. * See the CC3000 tutorial on Adafruit's learning system
  13. * for more information on setting up and using the
  14. * CC3000:
  15. * http://learn.adafruit.com/adafruit-cc3000-wifi
  16. * Requirements:
  17. * This sketch requires the Adafruit CC3000 library. You can
  18. * download the library from:
  19. * https://github.com/adafruit/Adafruit_CC3000_Library
  20. * For information on installing libraries in the Arduino IDE
  21. * see this page:
  22. * http://arduino.cc/en/Guide/Libraries
  23. * Usage:
  24. * Update the SSID and, if necessary, the CC3000 hardware pin
  25. * information below, then run the sketch and check the
  26. * output of the serial port. After connecting to the
  27. * wireless network successfully the sketch will output
  28. * the IP address of the server and start listening for
  29. * connections. Once listening for connections, connect
  30. * to the server from your computer using a telnet client
  31. * on port 23.
  32. * For example on Linux or Mac OSX, if your CC3000 has an
  33. * IP address 192.168.1.100 you would execute in a command
  34. * window:
  35. * telnet 192.168.1.100 23
  36. * Connect multiple clients and notice that whatever one client
  37. * sends will be echoed to all other clients. Press ctrl-] and
  38. * type quit at the prompt to close the telnet session.
  39. * On Windows you'll need to download a telnet client. PuTTY
  40. * is a good, free GUI client:
  41. * http://www.chiark.greenend.org.uk/~sgtatham/putty/
  42. * License:
  43. * This example is copyright (c) 2013 Tony DiCola (tony@tonydicola.com)
  44. * and is released under an open source MIT license. See details at:
  45. * http://opensource.org/licenses/MIT
  46. * This code was adapted from Adafruit CC3000 library example
  47. * code which has the following license:
  48. * Designed specifically to work with the Adafruit WiFi products:
  49. * ----> https://www.adafruit.com/products/1469
  50. * Adafruit invests time and resources providing this open source code,
  51. * please support Adafruit and open-source hardware by purchasing
  52. * products from Adafruit!
  53. * Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  54. * BSD license, all text above must be included in any redistribution.
  55. ****************************************************/
  56. #include <NewPing.h>
  57. //====== ADAFRUIT WIFI CODES
  58. #include <Adafruit_CC3000.h>
  59. #include <String.h>
  60. #include <SPI.h>
  61. #include "utility/debug.h"
  62. #include "utility/socket.h"
  63. // These are the interrupt and control pins
  64. #define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
  65. // These can be any two pins
  66. #define ADAFRUIT_CC3000_VBAT 5
  67. #define ADAFRUIT_CC3000_CS 10
  68. // Use hardware SPI for the remaining pins
  69. // On an UNO, SCK = 13, MISO = 12, and MOSI = 11
  70. Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
  71. SPI_CLOCK_DIVIDER); // you can change this clock speed
  72. #define WLAN_SSID "ClintGlenn" // cannot be longer than 32 characters!
  73. #define WLAN_PASS "john3:16"
  74. // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
  75. #define WLAN_SECURITY WLAN_SEC_WPA2
  76. #define LISTEN_PORT 23 // What TCP port to listen on for connections.
  77. Adafruit_CC3000_Server chatServer(LISTEN_PORT);
  78. String cmd;
  79. #define MAX_CMD_LENGTH 25
  80. //============== END OF ADAFRUIT WIFI CODES ==============//
  81.  
  82.  
  83. //============sensor codes
  84. #define SONAR_NUM 4// Number or sensors.
  85. #define MAX_DISTANCE 350 // Max distance in cm.
  86. #define PING_INTERVAL 33 // Milliseconds between pings.
  87.  
  88. unsigned long pingTimer[SONAR_NUM]; // When each pings.
  89. unsigned int cm[SONAR_NUM]; // Store ping distances.
  90. uint8_t currentSensor = 0; // Which sensor is active.
  91.  
  92. NewPing sonar[SONAR_NUM] = { // Sensor object array.
  93. //trigger,pin,maxdist
  94. NewPing(22, 24, MAX_DISTANCE),
  95. NewPing(26, 28, MAX_DISTANCE),
  96. NewPing(30, 32, MAX_DISTANCE),
  97. NewPing(34, 36, MAX_DISTANCE)
  98. };
  99.  
  100.  
  101. void setup(void)
  102. {
  103.  
  104. Serial.begin(115200);
  105.  
  106. //====sensors codes
  107. pingTimer[0] = millis() + 75; // First ping start in ms.
  108. for (uint8_t i = 1; i < SONAR_NUM; i++)
  109. pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
  110.  
  111. //===end of sensors
  112.  
  113.  
  114.  
  115. // Serial.println(F("Hello, CC3000!\n"));
  116. // Serial.print("Free RAM: ");
  117. //Serial.println(getFreeRam(), DEC);
  118.  
  119. /* Initialise the module */
  120. // Serial.println(F("\nInitializing..."));
  121. if (!cc3000.begin())
  122. {
  123. // Serial.println(F("Couldn't begin()! Check your wiring?"));
  124. while(1);
  125. }
  126. if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
  127. // Serial.println(F("Failed!"));
  128. while(1);
  129. }
  130. // Serial.println(F("Connected!"));
  131. // Serial.println(F("Request DHCP"));
  132. while (!cc3000.checkDHCP())
  133. {
  134. delay(100); // ToDo: Insert a DHCP timeout!
  135. }
  136. /* Display the IP address DNS, Gateway, etc. */
  137. while (! displayConnectionDetails()) {
  138. digitalWrite(13,LOW);
  139. delay(1000);
  140. }
  141. /*********************************************************/
  142. /* You can safely remove this to save some flash memory! */
  143. /*********************************************************/
  144. // Serial.println(F("\r\nNOTE: This sketch may cause problems with other sketches"));
  145. // Serial.println(F("since the .disconnect() function is never called, so the"));
  146. // Serial.println(F("AP may refuse connection requests from the CC3000 until a"));
  147. // Serial.println(F("timeout period passes. This is normal behaviour since"));
  148. // Serial.println(F("there isn't an obvious moment to disconnect with a server.\r\n"));
  149. // Start listening for connections
  150. chatServer.begin();
  151. digitalWrite(13,HIGH);
  152. // Serial.println(F("Listening for connections..."));
  153.  
  154.  
  155. //----- passing to arduino UNO serially
  156. //Serial.begin(115200);
  157.  
  158.  
  159. }
  160. void loop(void)
  161. {
  162.  
  163. //================= START SENSOR (NewPing) CODES ==========//
  164. // --------- ERROR STARTS FROM HERE I GUESS... Arduino doesn't receive
  165. // --------- the data from the telnet client because of these pieces of codes.
  166. // --------- If these are deleted, program works 100% smoothly.
  167.  
  168. for (uint8_t i = 0; i < SONAR_NUM; i++) {
  169. if (millis() >= pingTimer[i]) {
  170. pingTimer[i] += PING_INTERVAL * SONAR_NUM;
  171. if (i == 0 && currentSensor == SONAR_NUM - 1)
  172. oneSensorCycle(); // Do something with results.
  173. sonar[currentSensor].timer_stop();
  174. currentSensor = i;
  175. cm[currentSensor] = 0;
  176. sonar[currentSensor].ping_timer(echoCheck);
  177. }
  178. }
  179. //====================== END SENSOR (NewPing) CODES ===========//
  180. // Try to get a client which is connected.
  181. Adafruit_CC3000_ClientRef client = chatServer.available();
  182. if (client) {
  183.  
  184. client.flush();
  185. // Check if there is data available to read.
  186. if (client.available() > 0) {
  187. // Read a byte and write it to all clients.
  188. char thisChar = client.read();
  189. if (thisChar == '\n')
  190. {
  191. // Serial.println(cmd);
  192.  
  193. char cmdArray[1024];
  194. cmd.toCharArray(cmdArray,cmd.length()+1);
  195. if(strcmp(cmdArray,"W")==0)
  196. {
  197. // Serial.println("XXX - arduino to arduino serial monitor"); // do whatever you want
  198. // chatServer.println("Equal - XXX arduino to server");
  199. // chatServer.println(cmdArray);
  200. // Serial.write("W");
  201. Serial.print("W"); // send W signal to UNO
  202. digitalWrite(44,HIGH);
  203. }
  204. if(strcmp(cmdArray,"S")==0)
  205. {
  206. // Serial.println("S");
  207.  
  208. // chatServer.println("OK GOT IT not XXX arduino to server");
  209. // chatServer.println(cmdArray);
  210. // Serial.write("S");
  211. Serial.print("S");
  212. digitalWrite(44,LOW);
  213. }
  214.  
  215. cmd = "";
  216. }
  217.  
  218. else
  219. {
  220. cmd += thisChar;
  221. }
  222.  
  223.  
  224. }
  225. }
  226. }
  227.  
  228. //=========== DO NOT MIND THE CODES BELOW.... DEFAULT ADAFRUIT WIFI CODES ================//
  229.  
  230.  
  231. /**************************************************************************/
  232. /*!
  233. @brief Tries to read the IP address and other connection details
  234. */
  235. /**************************************************************************/
  236. bool displayConnectionDetails(void)
  237. {
  238. uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
  239. if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  240. {
  241. // Serial.println(F("Unable to retrieve the IP Address!\r\n"));
  242. return false;
  243. }
  244. else
  245. {
  246. // Serial.print(F("\nIP Addr: "));
  247.  
  248. cc3000.printIPdotsRev(ipAddress);
  249. // Serial.print(F("\nNetmask: "));
  250. cc3000.printIPdotsRev(netmask);
  251. // Serial.print(F("\nGateway: "));
  252. cc3000.printIPdotsRev(gateway);
  253. // Serial.print(F("\nDHCPsrv: "));
  254. cc3000.printIPdotsRev(dhcpserv);
  255. // Serial.print(F("\nDNSserv: "));
  256. cc3000.printIPdotsRev(dnsserv);
  257. // Serial.println();
  258. return true;
  259. }
  260. }
  261.  
  262. //=============sensors codes
  263. void echoCheck() { // If ping echo, set distance to array.
  264. if (sonar[currentSensor].check_timer())
  265. cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
  266. }
  267.  
  268. void oneSensorCycle() { // Do something with the results.
  269. for (uint8_t i = 0; i < SONAR_NUM; i++) {
  270. chatServer.print("sensor");
  271. chatServer.print(i);
  272. chatServer.print("=");
  273. chatServer.print(cm[i]);
  274. chatServer.print("cm ");
  275. }
  276. chatServer.println("");
  277. }
  278. //==========end of sensors codes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement