Hello guy, i'm a newbie and i've a problem with structure in my project. I use 2 type of structure to save the network and sensors parameters, loaded from sd.
#include <EEPROM.h>
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <OneWire.h>
#include "structure.h";
#define MAX_SENSORS 5
SENSORS_T sensor[MAX_SENSORS];
NETWORK_T network;
char file_net_cfg[] = {
"config/net.cfg"};
char file_sensors_cfg[] = {
"config/sensors.cfg"};
boolean from_eeprom = false;
//************************************************************************************//
//FUNCTION TO START NETWORK & IMPORT PARAMETERS IN STRUCTURE //
//**********************************************************************************//
void network_setup(String net_cfg[],boolean &eeprom )
{
File ipcfg;
char lastread[1];
network.mac[0] = 0xA2;
network.mac[1] = 0xDA;
network.mac[2] = 0x00;
network.mac[3] = 0x22;
network.mac[4] = 0x27;
network.mac[5] = 0x90;
Serial.print(F("Loading ethernet from "));
if (!eeprom)
{
Serial.print(F("SD: "));
byte *tmp = (byte *) malloc(4);
strIp2byteVect(net_cfg[0], tmp);
for(uint8_t i=0;i<4;i++) network.ip[i] = tmp[i];
strIp2byteVect(net_cfg[1], tmp);
for(uint8_t i=0;i<4;i++) network.subnet[i] = tmp[i];
strIp2byteVect(net_cfg[2], tmp);
for(uint8_t i=0;i<4;i++) network.gateway[i] = tmp[i];
free(tmp);
Ethernet.begin(network.mac, network.ip, network.gateway, network.subnet);
Serial.println(F("OK"));
Serial.print(F("IP: "));
Serial.println(Ethernet.localIP());
} else {
Serial.print(F("EEPROM: "));
uint8_t arr_count = 0; //contatore per array variabili temporanee
for (uint8_t a = 20; a < 32; a++)
{
if (arr_count == 4 && a < 32) arr_count = 0;
if ((a-20) < 4) network.ip[arr_count] = EEPROM.read(a);
if ((a-20) == 4 && (a-20) < 8) network.subnet[arr_count] = EEPROM.read(a);
if ((a-20) == 8 && (a-20) < 12) network.gateway[arr_count] = EEPROM.read(a);
arr_count++;
}
Ethernet.begin(network.mac, network.ip, network.gateway, network.subnet);
Serial.println(F("OK"));
}
}
//**********************************************************************//
//*************************************************************************//
//FUNCTION TO IMPORT SENSORS PARAMETERS IN STRUCTURE //
//***********************************************************************//
void sensors_setup(String sensors[])
{
char buffer[10];
Serial.print(F("Loading Sensors: "));
for (uint8_t i = 0; i < 5; i++){
int commaPosition = 0;
uint8_t column = 1;
do
{
commaPosition = sensors[i].indexOf(',');
switch (column) {
case 1:
sensors[i].substring(0,commaPosition).toCharArray(buffer,sensors[i].substring(0,commaPosition).length()+1);
strcpy(sensor[i].index, buffer);
break;
/* case 2:
sensors[i].substring(0,commaPosition).toCharArray(sensor[i].pintype,sensors[i].substring(0,commaPosition).length()+1);
break;
case 3:
sensors[i].substring(0,commaPosition).toCharArray(buffer,sensors[i].substring(0,commaPosition).length()+1);
sensor[i].pin = atoi(buffer);
break;
case 4:
sensors[i].substring(0,commaPosition).toCharArray(sensor[i].model,sensors[i].substring(0,commaPosition).length()+1);
break;
case 5:
sensors[i].substring(0,commaPosition).toCharArray(sensor[i].address,sensors[i].substring(0,commaPosition).length()+1);
break;
case 6:
sensors[i].substring(0,commaPosition).toCharArray(sensor[i].description,sensors[i].substring(0,commaPosition).length()+1);
break; */
}
//Serial.println(sensor[i].index[0]);
sensors[i] = sensors[i].substring(commaPosition+1, sensors[i].length());
column++;
} while(commaPosition >=0);
}
Serial.println(F("OK"));
}
//**********************************************************************//
//*************************************************************************//
//FUNCTION TO READ DATA FROM FILE IN SD //
//***********************************************************************//
void file_read(String data[],char* path){
File file_to_read;
file_to_read = SD.open(path, FILE_READ);
if (file_to_read){
uint8_t line = 0;
char lastread[1];
while (file_to_read.available())
{
//read esegue la lettura di un byte alla volta
lastread[0] = file_to_read.read();
if (lastread[0] == 10 || lastread[0] == 13)
{
line++;
} else {
data[line] += lastread[0];
}
}
file_to_read.close();
}
}
//*************************************************************************//
//FUNCTION TO SPLIT STRING TO BYTE ARRAY //
//***********************************************************************//
void strIp2byteVect(String str, byte *ipVect){
String tmpStr;
uint8_t bytesCount, //contatore per il num di byte
i; //contatore per il ciclo sulla stringa
i = 0;
for (bytesCount = 0; bytesCount < 4; bytesCount++){ //Ciclo sui byte
for ( ; (str.charAt(i) != '.') && (str.charAt(i) != '\0'); i++) //Ciclo sulla stringa
tmpStr += str.charAt(i);
ipVect[bytesCount] = tmpStr.toInt();
tmpStr = "";
++i;
}
}
//**********************************************************************//
void setup() {
Serial.begin(19200);
Wire.begin();
boolean sd_start = SD.begin(4);
String sensorcfg[MAX_SENSORS];
String netcfg[3];
if(sd_start) {
file_read(netcfg, file_net_cfg);
file_read(sensorcfg, file_sensors_cfg);
sensors_setup(sensorcfg);
network_setup(netcfg,from_eeprom);
} else {
from_eeprom = true;
network_setup(netcfg,from_eeprom);
}
}
void loop(){
}
When i try to assign (ex. strcpy(sensor_.index, buffer)
or read (ex. Serial.println(sensor*.index[0])
value to sensors struct member, value assignment of network parameters failed and Ethernet.localIP() return 0.0.0.0. I've created a new tab, named structure.h, with this content:_
_```_
#ifndef structure_h
#define structure_h
typedef struct{
char index[2];
char pintype[3];
char pin[3];
char model[11];
char address[10];
char description[31];
}SENSORS_T;
typedef struct{
byte ip[4];
byte subnet[4];
byte gateway[4];
byte mac[6];
} NETWORK_T;
#endif*
```
Where's the problem? Maybe i'm the problem?!