Forum Rule: Always post complete source code & details to reproduce any issue!
Page 1 of 2 1 2 LastLast
Results 1 to 25 of 43

Thread: My Ethernet woes...

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    Seattle, WA
    Posts
    25

    My Ethernet woes...

    I've built up a sensor collection system out of 4 teensy 3.1's and about 20 data sources. It's been working great collecting, parsing and storing data on the SD card, I'm using the wiz820/sd card adaptor, so I figured it was time I started using the Ethernet port to serve up the files I create, and that's where my trouble started. When I try and compile the "web server with SD" example using ethernetV2_0 I get the following errors:

    C:\Program Files (x86)\Arduino\libraries\EthernetV2_0\utility\w5200 .cpp: In member function 'void W5100Class::read_data(SOCKET, volatile uint8_t*, volatile uint8_t*, uint16_t)':
    C:\Program Files (x86)\Arduino\libraries\EthernetV2_0\utility\w5200 .cpp:129:24: error: cast from 'volatile uint8_t* {aka volatile unsigned char*}' to 'uint16_t {aka short unsigned int}' loses precision [-fpermissive]


    If I use the standard Ethernet library, compiling the webserver example I get this error:

    In file included from C:\Program Files (x86)\Arduino\libraries\Ethernet\utility\socket.cp p:1:0:
    C:\Program Files (x86)\Arduino\libraries\Ethernet\utility\w5100.h:1 51:8: error: initializing argument 2 of 'void W5100Class::read_data(SOCKET, volatile uint8_t*, volatile uint8_t*, uint16_t)' [-fpermissive]


    I'm sure there's something big and stupid I'm missing here, and I don't think that changing the .cpp and header files is the right answer since most people don't seem to have these types of issues.

    Any guidance would be appreciated.

  2. #2
    Senior Member
    Join Date
    Jan 2013
    Posts
    966
    What Teensyduino Version are you using and what Arduino IDE ?
    Ethernet_V2 does not sound like one of the libraries that comes standard with Teensyduino.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    Seattle, WA
    Posts
    25
    Arduino 1.05r2, teensy 1.18-rc1
    I started with the "Ethernet" library which gave a similar error (it's the second one I show) and then tried v2.
    from your response my situation doesn't sound normal, Perhaps I should start by reinstalling the IDE. I used to have 1.55, but when I got the first teensy, I reverted back. I didn't see a different Ethernet library listed with the 1.18 install. I keep a spreadsheet of the additional libraries I use for the various projects so maybe I'll start with none of them, get the Ethernet working and then start adding them. Or am I going too far?

  4. #4
    Member
    Join Date
    Jan 2014
    Location
    Seattle, WA
    Posts
    25
    I reinstalled the IDE and loader and presto...things compile.

    so on to the next problem, I run the web server example and it starts replies with an address of 0.0.0.0. I run the same code on another teensy3.1 with sd and wiz and it works. (i.e. identical teensy hardware So from that I would think that the wiz gets power and has a good data signal (I'm assuming it's who's replying with the 0.0.0.0) and that the CS setup is OK or else it wouldn't work on the other device.

  5. #5
    Member
    Join Date
    Jan 2014
    Location
    Seattle, WA
    Posts
    25
    So I got everything working...on my test teensy. When I drop the same code the other teensy, No Ethernet. The SD works fine, yellow and green lights come on but no response from the wiz. it won't respond to the "r" command from the serial monitor either. I put a scope on the pins at the wiz and the signal looks the same as the one that works. I'm stumped... It's connected to a TLink 702N

    Any ideas on a next step?

  6. #6
    Senior Member xxxajk's Avatar
    Join Date
    Nov 2013
    Location
    Buffalo, NY USA
    Posts
    528
    Quote Originally Posted by mikeleslie View Post
    C:\Program Files (x86)\Arduino\libraries\EthernetV2_0\utility\w5200 .cpp: In member function 'void W5100Class::read_data(SOCKET, volatile uint8_t*, volatile uint8_t*, uint16_t)':
    C:\Program Files (x86)\Arduino\libraries\EthernetV2_0\utility\w5200 .cpp:129:24: error: cast from 'volatile uint8_t* {aka volatile unsigned char*}' to 'uint16_t {aka short unsigned int}' loses precision [-fpermissive]


    If I use the standard Ethernet library, compiling the webserver example I get this error:

    In file included from C:\Program Files (x86)\Arduino\libraries\Ethernet\utility\socket.cp p:1:0:
    C:\Program Files (x86)\Arduino\libraries\Ethernet\utility\w5100.h:1 51:8: error: initializing argument 2 of 'void W5100Class::read_data(SOCKET, volatile uint8_t*, volatile uint8_t*, uint16_t)' [-fpermissive]
    Pointers on the teensy 3.x, which uses a 32bit ARM, are 32bits. On the AVR, they are normally 16, with a few exceptions.

    The right way to write this is to use size_t, not uint16_t..
    You will have to fix the library by changing the code and the prototype to use size_t.

    HTH

  7. #7
    Senior Member
    Join Date
    Jan 2013
    Posts
    966
    Quote Originally Posted by xxxajk View Post
    Pointers on the teensy 3.x, which uses a 32bit ARM, are 32bits. On the AVR, they are normally 16, with a few exceptions.

    The right way to write this is to use size_t, not uint16_t..
    You will have to fix the library by changing the code and the prototype to use size_t.

    HTH
    While that is true for some Arduino libraries that are not yet adapted to Arm MCU, the Ethernet library that installs with Teensyduino should not throw such compile errors. It does not here and I also run a Teesny3 and WIZ820io on my own adapter board.
    Last edited by Headroom; 02-25-2014 at 03:35 PM.

  8. #8
    Senior Member
    Join Date
    Jan 2013
    Posts
    966
    Quote Originally Posted by mikeleslie View Post
    When I drop the same code the other teensy.......

    Any ideas on a next step?
    Post your code ;-)

    Code brackets (#) can be found in the "go advanced" edit settings

  9. #9
    Senior Member xxxajk's Avatar
    Join Date
    Nov 2013
    Location
    Buffalo, NY USA
    Posts
    528
    size_t works on any pointer size (that's the entire idea). If you fix it now, it will work for everything, even into the future when mcu is 64bit and our desktops are 256bit ;-)
    The original author was attempting to out-smart the compiler, and that is not a good thing to do.

    So fix it, post your code.

  10. #10
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    Quote Originally Posted by mikeleslie View Post
    So I got everything working...on my test teensy. When I drop the same code the other teensy, No Ethernet. The SD works fine, yellow and green lights come on but no response from the wiz. it won't respond to the "r" command from the serial monitor either. I put a scope on the pins at the wiz and the signal looks the same as the one that works. I'm stumped... It's connected to a TLink 702N

    Any ideas on a next step?
    Try configuring the Wis820 to use an unoccupied LAN IP address and matching mask and gateway IP in your network, rather than DHCP. And static IP for DNS such as 4.2.2.2.

  11. #11
    Member
    Join Date
    Jan 2014
    Location
    Seattle, WA
    Posts
    25

    Post

    I use the same hard coded address and MAC on both devices (but only have 1 plugged in at a time as the TP-Link only has 1 Ethernet plug available)

    I sorted out the compile issues by using the standard libraries, and the code works.

    I suspect I did something bad to the wiz820 itself, as I don't think it's responding to commands (and I can't ping it, and it won't show a connection status etc) but I don't know how I might test it.

    Since all the signals look the same between the two devices I have, my plan is to attempt to remove the wiz from the sd card PCB, and put a new one on. 50/50 I can unsolder it successfully (it might be the SD card PCB, but it isn't coming off the teensy) , I've already ordered another set of teensy's, SD cards and 820's for when I irrecoverably mess this up. That's what I get for soldering it all together before testing each component separately.

    I'll post the code when I get back to that PC.
    Thanks for the suggestions'. Mike

    Here's a simple version.It works on one but not the other
    Code:
    /* 
       Web server sketch for IDE v1.0.3 and w5100/w5200
       Originally posted October 2012 by SurferTim
       Modified April 2013 by SurferTim
    */
    
    #include <SPI.h>
    #include <Ethernet.h>
    // uncomment next line if using SD
     #include <SD.h>
    
    // this must be unique
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC };
    
    // change to your network settings
    IPAddress ip( 192,168,0,10 );
    IPAddress gateway( 192,168,0,254 );
    IPAddress subnet( 255,255,255,0 );
    
    EthernetServer server(80);
    
    void setup()
    {
      Serial.begin(19200);
    
      // disable w5100 while setting up SD
      // uncomment next 5 lines if using a microSD card
    
      pinMode(10,OUTPUT);
      digitalWrite(10,HIGH);
      Serial.print(F("Starting SD.."));
      if(!SD.begin(4)) Serial.println(F("failed"));
      else Serial.println(F("ok"));
    
      Ethernet.begin(mac, ip, gateway, gateway, subnet);
      digitalWrite(10,HIGH);
    
      delay(2000);
      server.begin();
      Serial.println(F("Ready"));
    }
    
    void loop()
    {
      EthernetClient client = server.available();
      if(client) {
        boolean currentLineIsBlank = true;
        boolean currentLineIsGet = true;
        int tCount = 0;
        char tBuf[64];
        int r,t;
        char *pch;
    
        Serial.print(F("Client request: "));
    
        // this controls the timeout
        int loopCount = 0;
    
        while (client.connected()) {
          while(client.available()) {
            // if packet, reset loopCount
            loopCount = 0;
            char c = client.read();
    
            if(currentLineIsGet && tCount < 63)
            {
              tBuf[tCount] = c;
              tCount++;
              tBuf[tCount] = 0;          
            }
    
            if (c == '\n' && currentLineIsBlank) {
              // send a standard http response
              Serial.println(tBuf);
              Serial.print(F("POST data: "));
              while(client.available()) Serial.write(client.read());
              Serial.println();
    
              pch = strtok(tBuf,"?");
    
              while(pch != NULL)
              {
                if(strncmp(pch,"t=",2) == 0)
                {
                  t = atoi(pch+2);
                  Serial.print("t=");
                  Serial.println(t,DEC);             
                }
    
                if(strncmp(pch,"r=",2) == 0)
                {
                  r = atoi(pch+2);
                  Serial.print("r=");              
                  Serial.println(r,DEC);
                }
    
    
                pch = strtok(NULL,"& ");
              }
              Serial.println(F("Sending response"));
              client.print(F("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<html>"));
    
              client.println(F("<head><script type=\"text/javascript\">"));
              client.println(F("function show_alert() {alert(\"This is an alert\");}"));
              client.println(F("</script></head>"));
    
    
              client.println(F("<body><H1>TEST</H1>"));
    
              client.println(F("<form method=GET onSubmit=\"show_alert()\">T: <input type=text name=t><br>"));
              client.println(F("R: <input type=text name=r><br><input type=submit></form>"));
    
    
              client.println(F("</body></html>"));
              client.stop();
            }
            else if (c == '\n') {
              currentLineIsBlank = true;
              currentLineIsGet = false;
            } 
            else if (c != '\r') {
              currentLineIsBlank = false;
            }
          }
    
          loopCount++;
    
          // if 10000ms has passed since last packet
          if(loopCount > 10000) {
            // close connection
            client.stop();
            Serial.println("\r\nTimeout");
          }
    
          // delay 1ms for timeout timing
          delay(1);
        }
        Serial.println(F("done"));
      }
    }
    Last edited by mikeleslie; 02-26-2014 at 12:53 AM. Reason: added code

  12. #12
    Member
    Join Date
    Jan 2014
    Location
    Seattle, WA
    Posts
    25
    Let me start this off by saying that I have a test setup of the same hardware (3.1 black, not green though) that works well. Iv'e been pushing data through it nonstop for about 5 days without a problem

    I got the new hardware (3.1, wiz820io R1, pauls sd holder,) soldered it all together except the wiz which I put in a socket, and here's what happens:

    with the wiz out, the SD card and everything else works. with the wiz in, neither the SD nor the wiz comes up (the SD fails to initialize)

    If I scope the data and clock lines I see the initial bursts go through in both cases and they rest at 3.3v.

    I then figured I messed something up so I soldered a second set together, same results.

    and on the software side, I've done the following:

    Removed and replaced the IDE with a fresh set
    Removed the loader and the libraries it installs and replaced them with a fresh set
    Only loaded libraries additional libraries which I needed (in this case tinyGPS++)
    recompiled and put on my test teensy 3.1 with a wiz820 and sd holder, which works well.


    basically the program reads data from a couple of sources, all on serial, builds an output record and writes it to the SD card. On occasion, the file is downloaded over Ethernet.

    I'm now truly stuck here,

    Here's the code:

    Code:
    //////////////////////////////////////////////////////////////////////////////////
    //     1.1.2 removed rtc, 
    //     1.1.3 added 2 commas in non bms record.  added bms updates to web server mlaas and mlarr records
    //     1.1.4 added dynamic file names based on coledate from exp1 record
    //////////////////////////////////////////////////////////////////////////////////
    
     #include <SPI.h>
     #include <SD.h>
     #include <DS1307RTC.h>
     #include <Time.h>
     #include <Wire.h>
     #include <Ethernet.h>
     #include <utility/w5100.h>
     //#define ethernetON
     #define ServerDEBUG
     
     //Ethernet Setup Info
     byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC };
     IPAddress ip( 192,168,0,10 );    // AJ1 Network
     IPAddress gateway( 192,168,0,254 );  //blue TP-Link Router, AJ1
     IPAddress subnet( 255,255,255,0 );
     EthernetServer server(80);
    
     const char legalChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890/.-_~";
     unsigned int requestNumber = 0;
    
     const int chipSelect = 4; //sd card cs pin  
     String logFileExt = "000.csv";
     String logFileString;
     char logFile[] = "logfile0.csv"; //file name to store into
    
          
     //ColeTime Variables
     float ColeTimeSecond;
     int ColeTimeYear;
     long fakeSecond;
     int LeapDays = 0;
    
     //SD file write intermediate records
     String expRecord1 = ("");
     String expRecord2 = (",,,,,,,,,,,,,,,,,,,,,,"); // 22 commas
     String expRecord3 = ("");
     String expRecord  = ("");
     unsigned long ExpDelay = 0;
     int ExpReady = 0;
    
     //Serial input read strings 
     String inData;
     char inChar=-1;
     String inData2;char inChar2=-1;
     String inData3;
     char inChar3 =-1;
    
    
    void setup()
     {
      // Serial Ports Setup
      Serial.begin(19200);  // USB Monitoring Port
      Serial1.begin(4800);  //  input from Sensor module, includes all Insturment input
      //Serial2.begin(19200); //  EXP records from BMS module
      Serial3.begin(38400); // Output to other systems
      
      //Begin Sensor Initialization
      #ifdef ServerDEBUG
      Serial.println("DEBUG ON");
      delay(3000);
      #endif  
      delay(2000); //wait
      
      //announce yourself to the modules  ///
      Serial.println ("  Distribution Module Version 1.1.4 SD, SensorPac S1-4800, NO RTC,  ETHERNET BMS S2-19200,  S3-38400");
      Serial1.println("  Distribution Module Version 1.1.4 SD, SensorPac S1-4800, NO RTC,  ETHERNET BMS S2-19200,  S3-38400");
      Serial3.println("  Distribution Module Version 1.1.4 SD, SensorPac S1-4800, NO RTC,  ETHERNET BMS S2-19200, S3-38400");
      
      //SD Card setup and Initialization
      pinMode(10,OUTPUT);     //setup to disable 5200 during SD statrup
      digitalWrite(10,HIGH);  //Enet disable
      Serial.print(".....Initializing SD card....."); 
      pinMode(4, OUTPUT);    //CS line for SD card, disables ethernet
      if (!SD.begin(chipSelect)) 
         {
           Serial.println("NO SD Card");
           return;
         }
      Serial.print("SD Setup Sucessful. Ethernet Setup Begins......");
      Ethernet.begin(mac, ip, gateway, gateway, subnet);
      digitalWrite(10,HIGH);
      delay(2000);
      #ifdef ethernetON
      server.begin();
      Serial.println(F("Server Ready"));
      #endif
      int loopCount = 0;
     }   //end of setup
    
    void loop() 
    //This loop checks the server for activity and reads from the sensors on Serial1, BMS module on Serial2, and SD write Records on Serial3 outputs a combined stream to port 3, writes logs to the SD card.
       {
         //check for network client activity
         #ifdef ethernetON
         checkServer();
         #endif
         //Combined II Sensor port IN
         while (Serial1.available() > 0)     //Port 1 Has NKE Instruments, remove HDG, ZDA, GLL MTA, VWR and VTG from output stream as the data conflicts with the GPS data. 
        {
            char recieved = Serial1.read();
            inData += recieved; 
            if (recieved == '\n') // Process message when new line character is recieved
            {                       
               #ifdef ServerDEBUG 
                 Serial.print("port  1: ");     
               #endif  
               Serial.print(inData);   //send all messages to USB
               Serial3.print(inData);  //send all messages to Output
               //Serial2.print(inData);  
               //Serial1.print(inData);      
               inData = "";             // Clear recieved buffer
           }    //end  /N EOL line processing
        }    //end while port available
    
        //BMS 3v Transfer Port
        while (Serial2.available() > 0)
            {
            char recieved2 = Serial2.read();
            inData2 += recieved2;
            if (recieved2 == '\n') // Process message when new line character is recieved
            {    
              //#ifdef ServerDEBUG  
                Serial.print("port  2: "); 
                Serial.print(inData2);    
             // #endif
                if (inData2.substring(1,6) =="MEXP2")                              //Read the Expedition Sentence
                      {
                      int poundloc =  inData2.indexOf(',#');
                      int poundloc2 = inData2.indexOf('#', poundloc+1);
                      String exp02 =  inData2.substring(poundloc+1,poundloc2);
                      expRecord2 = exp02;
                    }           
             else  // forward all other sentences
                    {
                     Serial.print(inData2);                                    //send all remaining messages to USB
                     Serial3.print(inData2);                                   //send all remaining messages to output port                 
                     } //end sentence print  
             inData2 = "";                                             // Clear recieved buffer
           }
         }
            
        //expedition record Port in, Vesper port out  
         while (Serial3.available() > 0)     //Port 3 Has Expedition data records only 
        {
            char recieved3 = Serial3.read();
            inData3 += recieved3; 
            if (recieved3 == '\n') // Process message when new line character is recieved
            { 
               #ifdef ServerDEBUG
                 Serial.print("port  3: ");  
                 Serial.print(inData3);            
                #endif
    
               if (inData3.substring(1,6) =="MEXP1") //Read the first Expedition Record
               {       
                 int poundloc = inData3.indexOf(',#');
                 int poundloc2 = inData3.indexOf('#', poundloc+1);
                 String exp01 = inData3.substring(poundloc+1,poundloc2);
                 String cDate = exp01.substring(0,5);
                 logFileString = cDate+ logFileExt;
                 #ifdef ServerDEBUG
                 Serial.print("**********************************************date***********************");
                 Serial.println(logFileString);
                 #endif
                 expRecord1 = exp01;
               }
               else if (inData3.substring(1,6) =="MEXP3") //Trigger for  Expedition Record Write
               { 
                 int poundloc = inData3.indexOf(',#');
                 int poundloc2 = inData3.indexOf('#', poundloc+1);
                 String exp03 = inData3.substring(poundloc+1,poundloc2);
                 expRecord3 = exp03;
                 inData3 = "";                                           // Clear recieved buffer 
                 #ifdef ServerDEBUG
                 Serial.print("ExpReady Count ");
                 Serial.println(ExpReady);
                 #endif
                 if (ExpReady >= 3)
                   {
                    
                     LogExpeditionInfo();
                   }
                }
                 //Serial.print(inData3);                                   //send all remaining messages to USB
                 //Serial3.print(inData3);                                  //send all remaining messages to output port
                 //Serial1.print(inData3);                                  //send all remaining messages to II port
              ExpReady = ExpReady+1;
              inData3 = "";                                           // Clear recieved buffer 
             }        
       } //end port read  
     }//end main loop 
    
    void LogExpeditionInfo()
        { 
         expRecord = expRecord1+expRecord2+expRecord3; //combine the 3 records
         logFileString.toCharArray(logFile,13);
         File dataFile = SD.open(logFile, FILE_WRITE);      
         dataFile.println(expRecord);  
         dataFile.close(); 
       #ifdef ServerDEBUG
         Serial.print(logFile);
         Serial.println("  Written to SD*************************************************************************");
         Serial.println(expRecord);
       #endif
        expRecord = "";
        expRecord1 = "";
        //expRecord2 = "";
        expRecord3 = "";
       
       }
    
    void checkServer() {
    
      EthernetClient client = server.available();
      if(client) {
        boolean currentLineIsBlank = true;
        boolean currentLineIsGet = true;
        int tCount = 0;
        char tBuf[64];
        int r,t;
        char *pch;
        char methodBuffer[8];
        char requestBuffer[48];
        char pageBuffer[48];
        char paramBuffer[48];
        char protocolBuffer[9];
        char fileName[32];
        char fileType[4];
        int clientCount = 0;
    
        requestNumber++;
    #ifdef ServerDEBUG
        Serial.print(F("\r\nClient request #"));
        Serial.print(requestNumber);
        Serial.print(F(": "));
    #endif
    
        // this controls the timeout
        int loopCount = 0;
    
        while (client.connected()) {
          while(client.available()) {
            // if packet, reset loopCount
    //        loopCount = 0;
            char c = client.read();
    
            if(currentLineIsGet && tCount < 63)
            {
              tBuf[tCount] = c;
              tCount++;
              tBuf[tCount] = 0;          
            }
    
            if (c == '\n' && currentLineIsBlank) {
    #ifdef ServerDEBUG
              Serial.print(tBuf);
    #endif
    //          Serial.print(F("POST data: "));
              while(client.available()) client.read();
    
              int scanCount = sscanf(tBuf,"%7s %47s %8s",methodBuffer,requestBuffer,protocolBuffer);
    
              if(scanCount != 3) {
    
    #ifdef ServerDEBUG
                Serial.println(F("bad request"));
    #endif
                sendBadRequest(client);
                return;
              }
    
              char* pch = strtok(requestBuffer,"?");
              if(pch != NULL) {
                strncpy(fileName,pch,31);
                strncpy(tBuf,pch,31);
    
                pch = strtok(NULL,"?");
                if(pch != NULL) {
                  strcpy(paramBuffer,pch);
                }            
                else paramBuffer[0] = 0;            
              }
    
              strtoupper(requestBuffer);
              strtoupper(tBuf);
    
              for(int x = 0; x < strlen(requestBuffer); x++) {
                if(strchr(legalChars,requestBuffer[x]) == NULL) {
                  Serial.println(F("bad character"));  
                  sendBadRequest(client);
                  return;
                }            
              }
    
    #ifdef ServerDEBUG
              Serial.print(F("file = "));
              Serial.println(requestBuffer);
    #endif
              pch = strtok(tBuf,".");
    
              if(pch != NULL) {
                pch = strtok(NULL,".");
    
                if(pch != NULL) strncpy(fileType,pch,4);
                else fileType[0] = 0;
    
    //#ifdef ServerDEBUG
                Serial.print(F("file type = "));
                Serial.println(fileType);
                if(fileType == "LOG") {ExpReady = 0; Serial.println("trapped LOG");} // 
    //#endif
               }
    
    #ifdef ServerDEBUG
              Serial.print(F("method = "));
              Serial.println(methodBuffer);
    #endif
              if(strcmp(methodBuffer,"GET") != 0 && strcmp(methodBuffer,"HEAD") != 0) {
                sendBadRequest(client);
                return;
              }
    
    #ifdef ServerDEBUG
              Serial.print(F("params = "));
              Serial.println(paramBuffer);
    
              Serial.print(F("protocol = "));
              Serial.println(protocolBuffer);
    #endif          
    
              // if dynamic page name 
              if(strcmp(requestBuffer,"/MYTEST.PHP") == 0) {
    #ifdef ServerDEBUG
                Serial.println(F("dynamic page"));            
    #endif
              }
              else {
                if(strcmp(fileName,"/") == 0) {
                  strcpy(fileName,"/INDEX.HTM");
                  strcpy(fileType,"HTM");
    
    #ifdef ServerDEBUG
                  Serial.print(F("Home page "));            
    #endif
                }
    
    #ifdef ServerDEBUG
                Serial.println(F("SD file"));            
    #endif
                if(strlen(fileName) > 30) {
    #ifdef ServerDEBUG
                  Serial.println(F("filename too long"));
    #endif
                  sendBadRequest(client);
    
                  return;
                }
                else if(strlen(fileType) > 3 || strlen(fileType) < 1) {
    
    #ifdef ServerDEBUG
                  Serial.println(F("file type invalid size"));
    #endif
                  sendBadRequest(client);
                  return;
                }
                else {
    #ifdef ServerDEBUG
                  Serial.println(F("filename format ok"));
    #endif
                  if(SD.exists(fileName)) { 
    #ifdef ServerDEBUG
     
                    Serial.print(F("file found.."));                
    #endif
    
                    File myFile = SD.open(fileName); 
                   ExpReady = 0; 
    
                    if(!myFile) {
    #ifdef ServerDEBUG
                      Serial.println(F("open error"));
    #endif
                      sendFileNotFound(client);
                      return;
                    }
    #ifdef ServerDEBUG
                    else Serial.print(F("opened.."));
    #endif
    
                    strcpy_P(tBuf,PSTR("HTTP/1.0 200 OK\r\nContent-Type: "));
    //                client.write(tBuf);
    //                client.print(F("HTTP/1.0 200 OK\r\nContent-Type: "));
    
                    // send Content-Type
                    if(strcmp(fileType,"HTM") == 0) strcat_P(tBuf,PSTR("text/html"));
                    else if(strcmp(fileType,"PHP") == 0) strcat_P(tBuf,PSTR("text/html"));
                    else if(strcmp(fileType,"TXT") == 0) strcat_P(tBuf,PSTR("text/plain"));
                    else if(strcmp(fileType,"CSS") == 0) strcat_P(tBuf,PSTR("text/css"));
                    else if(strcmp(fileType,"GIF") == 0) strcat_P(tBuf,PSTR("image/gif"));
                    else if(strcmp(fileType,"JPG") == 0) strcat_P(tBuf,PSTR("image/jpeg"));
                    else if(strcmp(fileType,"JS") == 0) strcat_P(tBuf,PSTR("application/javascript"));
                    else if(strcmp(fileType,"ICO") == 0) strcat_P(tBuf,PSTR("image/x-icon"));
                    else if(strcmp(fileType,"PNG") == 0) strcat_P(tBuf,PSTR("image/png"));
                    else if(strcmp(fileType,"PDF") == 0) strcat_P(tBuf,PSTR("application/pdf"));
                    else if(strcmp(fileType,"ZIP") == 0) strcat_P(tBuf,PSTR("application/zip"));
                    else strcat_P(tBuf,PSTR("text/plain"));
    
                    strcat_P(tBuf,PSTR("\r\nConnection: close\r\n\r\n"));
                    client.write(tBuf);
    
                    if(strcmp(methodBuffer,"GET") == 0)  {
    #ifdef ServerDEBUG
                      Serial.print(F("send.."));
    #endif
    
                      while(myFile.available()) {
                        tBuf[clientCount] = myFile.read();
                        clientCount++;
                        tBuf[clientCount] = 0;
    
                        if(clientCount > 63) {
                          client.write((byte*)tBuf,64);
                          clientCount = 0;
                        }
    
                      }
                      if(clientCount > 0) {
                        client.write((byte*)tBuf,clientCount);
                      }
                    }
    
                    myFile.close();              
    #ifdef ServerDEBUG
                    Serial.println(F("closed"));
    #endif
                    client.stop();                
    #ifdef ServerDEBUG
                    Serial.println(F("disconnected"));
    #endif
                    return;
                  }
                  else {
    #ifdef ServerDEBUG
                    Serial.println(F("File not found"));
    #endif
                    sendFileNotFound(client);
                    return;
                  }
    
                }
              }
    
              pch = strtok(paramBuffer,"&");
    
              while(pch != NULL)
              {
                if(strncmp(pch,"t=",2) == 0)
                {
                  t = atoi(pch+2);
                  Serial.print("$MLAAC,"); Serial.print(t);  Serial.println(",*23" );   //modifies bat capacity remaining       
    #ifdef ServerDEBUG
                  Serial.print("t=");
                  Serial.println(t,DEC);             
    #endif
                }
    
                if(strncmp(pch,"r=",2) == 0)
                {
                  r = atoi(pch+2);
                  Serial.print("$MLARR,"); Serial.print(r);  Serial.println(",*23" );   //modifies bat capacity remaining       
    
    #ifdef ServerDEBUG
                  Serial.print("r=");              
                  Serial.println(r,DEC);
    #endif
                }
    
    
                pch = strtok(NULL,"& ");
              }
    #ifdef ServerDEBUG
              Serial.println(F("Sending response"));
    #endif
              client.print(F("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"));
    
              if(strcmp(methodBuffer,"GET") == 0) {
    
                strcpy_P(tBuf,PSTR("<html><head><script type=\"text/javascript\">"));
                client.write(tBuf);
                strcpy_P(tBuf,PSTR("function show_alert() {alert(\"are you sure\");}"));
                client.write(tBuf);
                strcpy_P(tBuf,PSTR("</script></head>"));
                client.write(tBuf);
    
                strcpy_P(tBuf,PSTR("<body><H1>BMS MODIFICATION PAGE</H1><form method=GET onSubmit=\"show_alert()\">"));
                client.write(tBuf);
                strcpy_P(tBuf,PSTR("AVAILABLE: <input type=text name=t><br>"));
                client.write(tBuf);
                strcpy_P(tBuf,PSTR("RATE: <input type=text name=r><br><input type=submit></form></body></html>"));
                client.write(tBuf);
              }
    
              client.stop();
            }
            else if (c == '\n') {
              currentLineIsBlank = true;
              currentLineIsGet = false;
            } 
            else if (c != '\r') {
              currentLineIsBlank = false;
            }
          }
    
          loopCount++;
    
          // if 1000ms has passed since last packet
          if(loopCount > 1000) {
            // close connection
            client.stop();
    #ifdef ServerDEBUG
            Serial.println("\r\nTimeout");
    #endif
          }
    
          // delay 1ms for timeout timing
          delay(1);
        }
    #ifdef ServerDEBUG
        Serial.println(F("disconnected"));
    #endif
      }
    }
    
    void sendFileNotFound(EthernetClient thisClient) {
      char tBuf[64];
      strcpy_P(tBuf,PSTR("HTTP/1.0 404 File Not Found\r\n"));
      thisClient.write(tBuf);
      strcpy_P(tBuf,PSTR("Content-Type: text/html\r\nConnection: close\r\n\r\n"));
      thisClient.write(tBuf);
      strcpy_P(tBuf,PSTR("<html><body><H1>FILE NOT FOUND</H1></body></html>"));
      thisClient.write(tBuf);
      thisClient.stop();  
    #ifdef ServerDEBUG
      Serial.println(F("disconnected"));
    #endif
    }
    
    void sendBadRequest(EthernetClient thisClient) {
      char tBuf[64];
      strcpy_P(tBuf,PSTR("HTTP/1.0 400 Bad Request\r\n"));
      thisClient.write(tBuf);
      strcpy_P(tBuf,PSTR("Content-Type: text/html\r\nConnection: close\r\n\r\n"));
      thisClient.write(tBuf);
      strcpy_P(tBuf,PSTR("<html><body><H1>BAD REQUEST</H1></body></html>"));
      thisClient.write(tBuf);
      thisClient.stop();  
    #ifdef ServerDEBUG
      Serial.println(F("disconnected"));
    #endif
    }
    
    void  strtoupper(char* aBuf) {
    
      for(int x = 0; x<strlen(aBuf);x++) {
        aBuf[x] = toupper(aBuf[x]);
      }
    }
    
    byte socketStat[MAX_SOCK_NUM];
    
    void ShowSockStatus()
    {
      for (int i = 0; i < MAX_SOCK_NUM; i++) {
        Serial.print(F("Socket#"));
        Serial.print(i);
        uint8_t s = W5100.readSnSR(i);
        socketStat[i] = s;
        Serial.print(F(":0x"));
        Serial.print(s,16);
        Serial.print(F(" "));
        Serial.print(W5100.readSnPORT(i));
        Serial.print(F(" D:"));
        uint8_t dip[4];
        W5100.readSnDIPR(i, dip);
        for (int j=0; j<4; j++) {
          Serial.print(dip[j],10);
          if (j<3) Serial.print(".");
        }
        Serial.print(F("("));
        Serial.print(W5100.readSnDPORT(i));
        Serial.println(F(")"));
      }
    }
    Last edited by mikeleslie; 03-04-2014 at 08:34 PM.

  13. #13
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    chip selects vs. SD card vs. WizNet board vs. the SPI library for Teensyduino (which has code to emulate the AVR's SPI)... is this sorted out OK?

  14. #14
    Member
    Join Date
    Jan 2014
    Location
    Seattle, WA
    Posts
    25
    pinMode(10,OUTPUT); //setup to disable 5200 during SD statrup
    digitalWrite(10,HIGH); //Enet disable
    Serial.print(".....Initializing SD card.....");
    pinMode(4, OUTPUT); //CS line for SD card, disables Ethernet

    this is all I do explicitly. I don't enable/ disable any other time.

  15. #15
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    If you use the Teensyduino SPI library, I believe that it assers/deasserts chip select. As I recall, you'd have two instances of the SPI library code, each initialized (.begin) with a unique CS pin number).
    That is, this library that Paul did, to emulate the AVR's SPI library, manages the CS pin assertion, the application using the library does not.

    Perhaps you're not using the library.

  16. #16
    Member
    Join Date
    Jan 2014
    Location
    Seattle, WA
    Posts
    25
    I just reinstalled 1.18 just in case.
    I don't see an different SPI library in the install (or at least for 1.18). there is the"spi4teensy3" library (not in 1.18) from ajax. Is that the one you're referring to?
    I tried it but Ethernet.h had issues with it (mostly because it called SPI.h) although when I renamed spi4teensy3 to SPI, it didn't compile either.

  17. #17
    Member
    Join Date
    Jan 2014
    Location
    Seattle, WA
    Posts
    25
    i commented out the SD code and Ethernet works, but the CS parts still look right to me.

    here is the commented section


    Thanks for your help so far.

    Mike


    Code:
      
      //SD Card setup and Initialization
     // pinMode(10,OUTPUT);     //setup to disable 5200 during SD statrup
     // digitalWrite(10,HIGH);  //Enet disable
     // Serial.print(".....Initializing SD card....."); 
      //pinMode(4, OUTPUT);    //CS line for SD card, disables ethernet
      //if (!SD.begin(chipSelect)) 
        // {
      //     Serial.println("NO SD Card");
     //      return;
      //   }
    Last edited by mikeleslie; 03-05-2014 at 12:33 AM.

  18. #18
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    Do you have two instances of the SPI library (class)? Each with a different CS pin?

  19. #19
    Senior Member
    Join Date
    Jan 2013
    Posts
    966
    That talk of two instances is rather disturbing ;-)
    The Ethernet library handles the SPI communication in W5100.cpp and W5100.h. There is no reason to call the SPI library from the sketch to communicate through Ethernet! The Ethernet library assumes pin 10 as the CS (Slave Select rather than Chip Select but we shall not be picky)

    Code:
    #elif defined(__MK20DX128__) || defined(__MK20DX256__)
      inline static void initSS()    { pinMode(10, OUTPUT); };
      inline static void setSS()     { digitalWriteFast(10, LOW); };
      inline static void resetSS()   { digitalWriteFast(10, HIGH); };
    The SD library that comes with Teensyduino does not use the SPI library at all.

  20. #20
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    For the 5100 yes, but the other SPI device is present, and uses a different pin for slave select.
    If I have it right... the Arduino SPI library hard coded the SS pin and there's no simple way to have code to use a different SS pin based on which device is being addressed.

    The Teensy 3 SPI code allows a pin to be specified for each instance.. as shown in this snippet for the non-default SPI library

    Code:
    class SPIFIFOclass
    {
    public:
    	inline void begin(uint8_t pin, uint32_t speed, uint32_t mode=SPI_MODE0) __attribute__((always_inline)) {

  21. #21
    Senior Member
    Join Date
    Jan 2013
    Posts
    966
    The W5100 or in this case W5200 is communicated to through the Ethernet library, which in turn uses the Teensyduino SPI library, which has the pin 10 assigned as CS(SS). in W5100.h in the /utility directory of the Ethernet library. That could be changed if one really wanted to but in this case is really not necessary, as Ethernet seems to work when not operating in conjunction with the SD library.

    The other device - the SD card - uses the SD library, which speaks directly to the SPI bus without using any calls to or instance of any SPI library.
    It also allows to assign a pin for the CC and the OP assigned pin # 4 so the pure pin assignment would not cause a conflict.

    It would be interesting to see whether the CS signals, each on their given pin, are switched correctly in order to allow both devices to function properly on the bus. The OP has a scope, so that should be a relatively easy next step.

  22. #22
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    Ah, SD card library doesn't use the SPI library. I mistakenly assumed the SD card library would not reinvent that code.

  23. #23
    Member
    Join Date
    Jan 2014
    Location
    Seattle, WA
    Posts
    25
    I've tried every variation of this CS theme I can think of, including explicitly changing them both for each begin. like this:
    Code:
      
      pinMode(10,OUTPUT);     //CS l;ine for Ethernet 
      pinMode(4, OUTPUT);     //CS line for SD card
      digitalWrite(4,LOW);
      digitalWrite(10,HIGH);  
       if (!SD.begin(4)) 
         {
           Serial.println("NO SD Card");
           return;
         }
      digitalWrite(10,LOW);  //Enet enaable
      digitalWrite(4,HIGH);  //SD disable
      Ethernet.begin(mac, ip, gateway, gateway, subnet);
      delay(2000);
      server.begin();
      Serial.println(F("Server Ready"));
      int loopCount = 0;
     }   //end of setup
    here's the weird part, under some circumstances they both work, although both will not start in the same boot. I can start the wiz, change the code, start it again they both work at the same time....sometimes.

  24. #24
    Senior Member
    Join Date
    Jan 2013
    Posts
    966
    You don't need or perhaps even shouldn't set the CS pin for the Ethernet library separately in your sketch. The Ethernet library asserts and releases CS transparently with out any need to do this in your sketch. The pin definitions setSS and resetSS I posted above are from the Ethenet library and for every read/write operation these are asserted and released.

    Not only is there no need to do this separately in your sketch, it may interfere!

    Actually I would not be overly surprised if the SD card library does this as well with its assigned CS pin
    Last edited by Headroom; 03-05-2014 at 02:28 AM.

  25. #25
    Senior Member
    Join Date
    Jun 2013
    Location
    So. Calif
    Posts
    2,828
    I read that the Ethernet library's begin() function can leave the slave select pin low (true). Perhaps that's fixed in the version used for Teensyduino (5100/5200 chips), but this code won't hurt

    Code:
       // set up w5100
       Ethernet.begin(mac,ip);
       // disable w5100 SPI
       digitalWrite(10,HIGH);

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •