Pages: [1] 2 3   Go Down
Author Topic: Issue with variable declaration  (Read 1324 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 {

Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
issue

What issue?
Logged

From Idea To Invention

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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() {
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm having an issue with cross-posting.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3487
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please excuse the question as I don't have an Ethernet shield, but shouldn't the sn, gw and mydns variable have a value ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 439
Posts: 23793
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

IPAddress sn;
IPAddress gw;
IPAddress mydns;
Logged

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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 49
Posts: 3420
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

These are classic symptoms of a buffer overrun. This snippet and others like it are problematic:
Code:
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.
Logged

Offline Offline
Edison Member
*
Karma: 28
Posts: 2045
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2 3   Go Up
Jump to: