software SPI so I can use SD card and wifi

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
googly_eyes
 
Posts: 38
Joined: Wed Dec 05, 2012 8:22 pm

software SPI so I can use SD card and wifi

Post by googly_eyes »

I am trying to use the SD card to hold HTML files that I want the wifi (cc3000) shield to send as an http server.

I have been reading a lot and see that both the SD card and the wifi module use pin 10 for SPI. I have read also that there is something called software SPI, but I can only find bits and pieces and cannot puzzle out how to substitute it for the hardware SPI for one or the other.

Can someone point me to a complete code example so I can use software SPI on the wifi or SD card?

Thanks.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: software SPI so I can use SD card and wifi

Post by adafruit_support_mike »

Pin 10 is the Chip Select signal (CS), which can be moved to any pin.

If you're using the CC3000 Shield, the SD card's CS signal is already moved to pin 4.

User avatar
googly_eyes
 
Posts: 38
Joined: Wed Dec 05, 2012 8:22 pm

Re: software SPI so I can use SD card and wifi

Post by googly_eyes »

Thanks for your reply. One more related question - why does the CC3000 wait until there is an open serial port before it tries to connect to the AP?

For space reasons, I was attempting to remove all serial printing, but the module doesn't connect without an open serial port....

Thanks.

User avatar
googly_eyes
 
Posts: 38
Joined: Wed Dec 05, 2012 8:22 pm

Re: software SPI so I can use SD card and wifi

Post by googly_eyes »

Also, if anyone can help me compact this code so it fits, on an UNO, I would appreciate it.

Code: Select all

#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <SD.h>
File myFile;
const int chipSelect = 4;
Adafruit_CC3000 cc3000 = Adafruit_CC3000(10, 3, 5, SPI_CLOCK_DIVIDER); // you can change this clock speed
Adafruit_CC3000_Server httpServer(80);
uint8_t buffer[95];
int bufindex = 0;
char action[11];
char path[65];

void setup(void)
{
   //Serial.begin(115200);
   cc3000.begin();
   if(!cc3000.connectToAP("SSID", "PASSWWORD", WLAN_SEC_WPA2))
   {
      delay(1000);
      while(1);
   }
   // Serial.println(F("connected"));
   while (!cc3000.checkDHCP())
   {
      delay(100);
   }
   // Serial.print(F("DHCP DONE"));
   delay(5000);
   //pinMode(SS, OUTPUT);
   httpServer.begin();
   // Serial.println(F("Listening for connections..."));
   SD.begin(chipSelect);

}


void loop(void)
{
   // Try to get a client which is connected.
   Adafruit_CC3000_ClientRef client = httpServer.available();
   if (client)
   {
      //Serial.println(F("Client connected."));
      // Process this request until it completes or times out.
      // Note that this is explicitly limited to handling one request at a time!

      // Clear the incoming data buffer and point to the beginning of it.
      bufindex = 0;
      memset(&buffer, 0, sizeof(buffer));

      // Clear action and path strings.
      memset(&action, 0, sizeof(action));
      memset(&path,   0, sizeof(path));

      // Set a timeout for reading all the incoming data.
      unsigned long endtime = millis() + 500;

      // Read all the incoming data until it can be parsed or the timeout expires.
      bool parsed = false;
      while (!parsed && (millis() < endtime) && (bufindex < 94))
      {
         if (client.available())
         {
            buffer[bufindex++] = client.read();
         }
         parsed = parseRequest(buffer, bufindex, action, path);
      }

      // Handle the request if it was parsed.
      if (parsed)
      {
         //Serial.println(F("Processing request"));
         //Serial.print(F("Action: "));
         //Serial.println(action);
         //Serial.print(F("Path: "));
         //Serial.println(path);
         // Check the action to see if it was a GET request.
         if (strcmp(action, "GET") == 0)
         {
            // Respond with the path that was accessed.
            // First send the success response code.
            client.fastrprintln(F("HTTP/1.1 200 OK"));
            // Then send a few headers to identify the type of data returned and that
            // the connection will not be held open.
            client.fastrprintln(F("Content-Type: text/plain"));
            client.fastrprintln(F("Connection: close"));
            client.fastrprintln(F("Server: Adafruit CC3000"));
            // Send an empty line to signal start of body.
            client.fastrprintln(F(""));
            
            File dataFile = SD.open("html.txt");

            // if the file is available, write to it:
            while (dataFile.available())
               {
                  client.write(dataFile.read());
               }
               dataFile.close();
            
            // Now send the response data.
            //client.fastrprintln(F("Hello world!"));
            //client.fastrprint(F("You accessed path: ")); client.fastrprintln(path);
         }
         
      }

      // Wait a short period to make sure the response had time to send before
      // the connection is closed (the CC3000 sends data asyncronously).
      delay(100);

      // Close the connection when done.
      //Serial.println(F("Client disconnected"));
      client.close();
   }
}


bool parseRequest(uint8_t * buf, int bufSize, char * action, char * path)
{
   if (bufSize < 2)
      return false;
   if (buf[bufSize - 2] == '\r' && buf[bufSize - 1] == '\n')
   {
      parseFirstLine((char*)buf, action, path);
      return true;
   }
   return false;
}

void parseFirstLine(char * line, char * action, char * path)
{
   char* lineaction = strtok(line, " ");
   if (lineaction != NULL)
      strncpy(action, lineaction, 10);
   char* linepath = strtok(NULL, " ");
   if (linepath != NULL)
      strncpy(path, linepath, 64);
}


User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: software SPI so I can use SD card and wifi

Post by adafruit_support_mike »

I don't know of any direct connection between the CC3000 and the Serial library..

It might be a timing issue.. opening the Serial port gives the CC3000 a few more milliseconds to work through its power-up sequence. Try adding half a second of delay before calling cc30000.begin() and see if that helps.

User avatar
googly_eyes
 
Posts: 38
Joined: Wed Dec 05, 2012 8:22 pm

Re: software SPI so I can use SD card and wifi

Post by googly_eyes »

Your suggestion (I added a 20 second delay) and removing the close connection command at the end worked - I could see the client connected to my AP.

Now if I could just smartconfig to work I would be a happy camper.

For some reason smartconfig never finds the CC3000, so it can not ssend the AP information. Has anyone tested smartconfig app on Android 5.x?
I followed the instructions and started the CC3000 first, then while it was waiting I hit start in the smartconfig (after adding the password).
It is at the latest firmware you offer, and all the other sketches work fine.

any thoughts?

User avatar
jai
 
Posts: 13
Joined: Fri Apr 26, 2013 4:41 pm

Re: software SPI so I can use SD card and wifi

Post by jai »

Just to confirm, can I use SD card and WiFi to simultaneously log data?

Can I use this just like the regular SD card data logger if I need to?

I am planning to test out both modes but do not want to buy both shields.

Thanks.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: software SPI so I can use SD card and wifi

Post by adafruit_support_mike »

SmartConfig is known to be tempermental. It's neat when it works, but is a long way from being bulletproof.

WRT the SD card, you can use it for as many different things as you want. All our SD cards share the same basic hardware and software design. They're independent of the rest of the electronics, so no breakout or shield is handcuffed to a single card.

User avatar
jai
 
Posts: 13
Joined: Fri Apr 26, 2013 4:41 pm

Re: software SPI so I can use SD card and wifi

Post by jai »

Can the Micro SD card in CC3000 shield be used for logging data? Guess I could have been this specific in my previous question. Apologies.

Maybe you answered it in your reply, Mike, but I want to be absolutely sure.

Thanks.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: software SPI so I can use SD card and wifi

Post by adafruit_support_mike »

Yeah, you can use that card for anything you want.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”