Hello world
I'm trying to write and read data structs in EEPROM. This is an auxiliary sketch to test the data structs and their behaviour in EEPROM so it can be applied in a web client project.
I'm using Arduino Duemilanove ATmega328 in Arduino IDE in Windows 10.
What I do is basically define a struct Arduino with some fixed parameters, write them using EEPROM.put and then I try to read them using EEPROM.get inside a function I defined. The whole code is here:
#include <EEPROM.h>
byte MAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte IP[] = { 192, 168, 1, 228};
byte Gateway[] = { 192, 168, 1, 254};
byte DNS[] = { 192, 168, 1, 254};
byte Subnet[] = { 255,255,255,0 };
char arduinoName = "test_arduino";
byte serverIP[] = { 192, 168, 1, 218 };
int ServerPort = 8080;
int Bus = 2;
int period = 10000;
int headerSize = 105;
int MaxSize = 1000;
struct Arduino {
char Name[20];
int sendPeriod;
int respHeaderSize;
int serverPort;
byte serverName[4];
byte mac[6];
byte ip[4];
byte subnet[4];
byte gateway[4];
byte dns[4];
int respMaxSize;
int OneWireBus;
};
Arduino arduino = {
arduinoName,
period,
headerSize,
ServerPort,
serverIP,
MAC,
IP,
Subnet,
Gateway,
DNS,
MaxSize,
Bus
};
void setup(){
int eeAddress = 1;
Serial.begin(9600);
EEPROM.put(eeAddress, arduino);
Serial.println(arduino.sendPeriod);
Serial.print("SUCCESS\n");
readArduinoinEEPROM();
}
void loop(){ /* Empty loop */ }
Arduino readArduinoinEEPROM() {
Arduino arduino;
EEPROM.get(1, arduino);
Serial.println(":::ARDUINO FIXED PARAMETERS:::");
Serial.println();
Serial.println("Name: " + String(arduino.Name));
Serial.print("IP: ");
printIPAddress(arduino.ip);
Serial.print("MAC: ");
printMACAddress(arduino.mac);
Serial.println("One wire bus (DS18B20): " + String(arduino.OneWireBus));
Serial.print("DNS: ");
printIPAddress(arduino.dns);
Serial.print("Subnet: ");
printIPAddress(arduino.subnet);
Serial.print("Gateway: ");
printIPAddress(arduino.gateway);
Serial.print("Server IP: ");
printIPAddress(arduino.serverName);
Serial.println("Server port: " + String(arduino.serverPort));
Serial.println("Send cycle period: " + String(arduino.sendPeriod));
Serial.println("Server response header size (chars): " + String(arduino.respHeaderSize));
Serial.println("Server response max size (chars): " + String(arduino.respMaxSize));
Serial.println();
return arduino;
}
void printIPAddress(byte IP[]) {
for (byte thisByte = 0; thisByte < 4; thisByte++) {
if (thisByte == 3) {
Serial.println(IP[thisByte], DEC);
}
Serial.print(IP[thisByte], DEC);
Serial.print(".");
}
}
void printMACAddress(byte MAC[]) {
for (byte thisByte = 0; thisByte < 4; thisByte++) {
if (thisByte == 3) {
Serial.println(MAC[thisByte], HEX);
}
Serial.print(MAC[thisByte], HEX);
Serial.print(".");
}
}
What I get in the Serial Monitor is:
0
SUCCESS
:::ARDUINO FIXED PARAMETERS:::
Name: 7i⸮
IP: 0.0.0.0
0.MAC: 0.0.0.0
0.One wire bus (DS18B20): 0
DNS: 0.0.0.0
0.Subnet: 0.0.0.0
0.Gateway: 0.0.0.0
0.Server IP: 0.0.0.0
0.Server port: 0
Send cycle period: 0
Server response header size (chars): 0
Server response max size (chars): 0
This doesn't add up. Why?
Thanks for your attention
Best regards,
arocha17