Go Down

Topic: Issue with variable declaration (Read 2 times) previous topic - next topic

brantn

I am having and issue with a variable declaration that I hoping someone call tell me what is happening. The declaration is as follows

Code: [Select]

byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED}; //Default MAC
byte ip[] = {192,168,168,168}; //Default IP
IPAddress sn;
IPAddress gw;
IPAddress mydns;

char test[] = "test";
char mywebhost[] = "mywebhost";
boolean lastconnected = true;
boolean hostrequest = true;
unsigned long lastwebrequest = 0;
unsigned long webrequestintvl = 60000;
int failedrequests = 0;

void setup {


Cybernetician

From Idea To Invention

brantn

I am having an issue with my variable declaration anything that is directly after the space below mydns doesn't return a value when referenced if I move a different var there it behaves the same and if I move it somewhere else as long as it isn't directly below mydns it works fine. Any help would be appreciated.

Code: [Select]

#include <SPI.h>
#include <SdFat.h>
#include <Ethernet.h>

const uint8_t chipSelect = 4; //SD chip select pin
SdFat sd; //file system object

byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED}; //Default MAC
byte ip[] = {192,168,168,168}; //Default IP
IPAddress sn;
IPAddress gw;
IPAddress mydns;

char test[] = "test";
char mywebhost[] = "mywebhost";
boolean lastconnected = true;
boolean hostrequest = true;
unsigned long lastwebrequest = 0;
unsigned long webrequestintvl = 60000;
int failedrequests = 0;

#define buffermax 50
int buffersize;
char buffer[buffermax];
boolean startbuffer = false;

EthernetServer server(80);
EthernetClient client;

void MyFileRead(char* myfile) {
Serial.print("My File: ");
Serial.println(myfile);

const int line_buffer_size = 30;
char sdbuffer[line_buffer_size];
ifstream sdin(myfile);

while (sdin.getline(sdbuffer, line_buffer_size, '\n') || sdin.gcount()) {
if(strncmp(sdbuffer, "mac", 3) == 0) {
sscanf(sdbuffer, "mac=%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
} else if (strncmp(sdbuffer, "ip", 2) == 0) {
sscanf(sdbuffer, "ip=%u.%u.%u.%u", &ip[0], &ip[1], &ip[2], &ip[3]);
} else if (strncmp(sdbuffer, "sn", 2) == 0) {
sscanf(sdbuffer, "sn=%u.%u.%u.%u", &sn[0], &sn[1], &sn[2], &sn[3]);
} else if (strncmp(sdbuffer, "gw", 2) == 0) {
sscanf(sdbuffer, "gw=%u.%u.%u.%u", &gw[0], &gw[1], &gw[2], &gw[3]);
} else if (strncmp(sdbuffer, "dns", 3) == 0) {
sscanf(sdbuffer, "dns=%u.%u.%u.%u", &mydns[0], &mydns[1], &mydns[2], &mydns[3]);
}
}
}

void WebRequest() {
Serial.print("Requesting: ");
Serial.println(test);
Serial.print("Requesting: ");
Serial.println(mywebhost);
Serial.println("LastConnected: ");
Serial.println(lastconnected);
Serial.println("Request: ");
Serial.println(hostrequest);
}

void setup() {
Serial.begin(9600);

delay(500);

Serial.print("Initializing SD card...");
 
pinMode(10, OUTPUT);

if (!sd.begin(chipSelect, SPI_HALF_SPEED)) {
Serial.println("Initialization Failed.");
sd.initErrorHalt();
}

Serial.println("Initialization Done.");

char myfile[] = "ipconf.txt";
MyFileRead(myfile);
WebRequest();

delay(500);

Ethernet.begin(mac,ip,mydns,gw,sn);

Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());
}

void loop() {
}

Nick Gammon

I'm having an issue with cross-posting.
http://www.gammon.com.au/electronics

brantn

Sorry about that the first post disappeared from my browser before I could get all the code.

UKHeliBob

Please excuse the question as I don't have an Ethernet shield, but shouldn't the sn, gw and mydns variable have a value ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

brantn

They are getting their values from a text file on an SD card. Eventually I am going to add a check if the values are not present to start ethernet with a default set of values. Just to check that it wasn't messing with my variables I reran the sketch with values and had the same issue.

CrossRoads

Don't these need byte, int, long, something, in front of them?

IPAddress sn;
IPAddress gw;
IPAddress mydns;
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

PaulS

Quote
Just to check that it wasn't messing with my variables I reran the sketch with values and had the same issue.

But you still aren't going to tell us what that issue was? Well, good luck with it. Whatever it is.

brantn


But you still aren't going to tell us what that issue was? Well, good luck with it. Whatever it is.


Sorry if I wasn't very clear in the posting of the issue as it is at the top of the code after the double post. To restate the issue if I move the variable after the space below mydns in the code to anywhere else such as above byte ip[] then a Serial.print of the variable will print a value. Whatever variable is directly after that space when doing a serial.print prints nothing or false if it is a boolean variable.

I have tried with sn, gw, dns being byte instead of IPAddress with the same result. http://arduino.cc/en/Reference/EthernetIPAddress

brantn

Upon some other testing this appears to only happen after using sdfat getline. If I move the function WebRequest() before MyFileRead() then the variables appear fine. So it appears that something in MyFileRead() is breaking the variable.

PaulS

A couple of suggestions. First, print the values of all 5 arrays after each call to sscanf. Perhaps something is stepping on some part of memory that it shouldn't. Determining that that is happening is the first step in solving the problem. Determining where is the second step.

Second, sscanf() returns a value. Save and print it, instead of discarding. Again, a clue by four might present itself.

wildbill

These are classic symptoms of a buffer overrun. This snippet and others like it are problematic:
Code: [Select]
sscanf(sdbuffer, "dns=%u.%u.%u.%u", &mydns[0], &mydns[1], &mydns[2], &mydns[3]);

Your intent is to treat mydns as an array of ints, but it has type IPAddress, so you're actually treating it as an array of IPAddress. Indexes 1, 2 and 3 don't exist, so that code is stepping all over whatever variables are next in memory, as you observe.

michinyon

I am wondering if the byte should be unsigned byte,   because if it is a signed byte,  the numbers 192 and 168 don't fit.

PaulS

Quote
I am wondering if the byte should be unsigned byte,   because if it is a signed byte,  the numbers 192 and 168 don't fit.

On an Arduino, byte is unsigned.

Go Up