Trouble shoot my code?

Hey guys,

Im new here, and new to coding in general. Im working on a project where Im sending a packet from a PC to an Arduino with a wifi shield and then appending the split up packets to the SD card on the shield. A little bit about my project scope…PC generates little 800byte packet -----> UDP does packet checks and CRCs behind the scenes → Then that packet is stored in the internal program flash of the arduino atmega → append that packet to SD—>keep appending then at the end you have a 16M file on the SD card, then do a MD5 hash checksum

I know there’s alot of information there, and I know for a fact that my code needs alot of work. But if anyone could take a look and offer some changes I’d greatly appreciate it.

Thanks
Mike

#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
//#include <MD5.h>

char ssid[] = "WAP-Burn-204-A"; //  your network SSID (name)
char pass[] = "yellowhouse14";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

WiFiServer server(80);

int status = WL_IDLE_STATUS;

unsigned int localPort = 137;      // local port to listen on

char packetBuffer[1000]; //buffer to hold incoming packet
char  ReplyBuffer[] = "acknowledged";       // a string to send back
int i;

WiFiUDP Udp;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  //Serial.begin(4000000);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
 
  
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(1000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  int test = Udp.begin(localPort);
  Serial.println(test);
  Udp.begin(localPort);
}

void loop() 
int packetSize = Udp.parsePacket();
  // if there's data available, read a packet
  if (packetSize) {
//    Serial.print("Received packet of size ");
//    Serial.println(packetSize);
//    Serial.print("From ");
//    IPAddress remoteIp = Udp.remoteIP();
//    Serial.print(remoteIp);
//    Serial.print(", port ");
//    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
      int len = Udp.read(packetBuffer, 1000);
      if (len > 0) {
        packetBuffer[len] = 0;
      }
      //Serial.println("Contents:");
      Serial.println(packetBuffer);
      Serial.println("\n");
      //unsigned char* hash=MD5::make_hash(packetBuffer); //Hash
      //char *md5str = MD5::make_digest(hash, 16);
      //free(hash);
      Serial.print("HASH #");
      Serial.print(i);
      Serial.print(": ");
     //Serial.println(md5str);
      Serial.print("\n");
     //free(md5str);
     packetBuffer[len] = ' ';

Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

      
//      // send a reply, to the IP address and port that sent us the packet we received
//      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
//      Udp.write(ReplyBuffer);
//      Udp.endPacket();
      i++;
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Wifi_DualPacketRecieve_toSDcard.ino (3.63 KB)

char packetBuffer[1000]; //buffer to hold incoming packet

On which Arduino?

Hi Paul, thanks for the quick response. Its an Uno

So, half of SRAM is used by that array, and another 512+ bytes to open the file on the SD card, plus the incoming and outgoing serial buffers...

I'm guessing that you are having memory issues.

oh really? Honestly if you try running the code you'll see that it has its issues. Im still trying to teach myself the ropes, but the code isnt far enough along to try testing it. Too many issues so far

Reading the Original Post I can't help wondering why all that is described cannot be done more easily on the PC without any Arduino.

And if there is some reason why it can't be done on a PC a Raspberry Pi or an Arduino Yun would seem to be more suitable than an Uno.

...R

Thanks for the response Robin, yes it easily could have been done on a PC. Its more of a concept being tested and in this case I'm looking to use the uno as a module to receive a few packets in an enclosure. There actually no room for a PC. But as far as alternative boards go I do agree. Currently I want to work with the uno for testing purposes, but in the future I'll look into other options.

Mike

mikesnap: I'm looking to use the uno as a module to receive a few packets in an enclosure.

The Uno, Mega and their near equivalents are best suited to real-time control and data acquisiton rather than handling large quantities of data. They are very slow compared to a PC or an RPi and they have very little SRAM.

...R