Skip to content

Commit 2a87c55

Browse files
committed
Handle configuration file at startup
1 parent b422140 commit 2a87c55

File tree

4 files changed

+76
-39
lines changed

4 files changed

+76
-39
lines changed

WifiModule/SenseData.cpp

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ void SenseData::init(void)
5656
void SenseData::run(void)
5757
{
5858
sched.execute();
59+
/* //can do some logic here
60+
if(SensorArray[0].getValue() > 128)
61+
SensorArray[7].setValue(1);
62+
else
63+
SensorArray[7].setValue(0);
64+
65+
*/
5966
}
6067

6168
//void addSensor(Sensor sensor);
@@ -77,6 +84,35 @@ String SenseData::getData(char* name)
7784
return ret;
7885
}
7986

87+
88+
int16_t SenseData::getValue(char* name)
89+
{
90+
int ret = 0;
91+
for(int i = 0;i<MAX_SENSOR_NB;i++)
92+
{
93+
if(SensorArray[i].m_name[0] != '\0')
94+
{
95+
if(strcmp(name,SensorArray[i].m_name) == 0 || strcmp("",name) == 0)
96+
{
97+
ret = SensorArray[i].getValue();
98+
}
99+
}
100+
}
101+
return ret;
102+
}
103+
104+
void SenseData::setValue(char* name, int16_t value)
105+
{
106+
Serial.print("setValue: ");Serial.print(name);Serial.print(" ");Serial.println(value);
107+
for(int i = 0;i<MAX_SENSOR_NB;i++)
108+
{
109+
if(strcmp(name,SensorArray[i].m_name) == 0)
110+
{
111+
SensorArray[i].setValue(value);
112+
}
113+
}
114+
}
115+
80116
String SenseData::getHistory(char* name,int qty,int long_history)
81117
{
82118
//manage qty and long_history argument
@@ -117,15 +153,5 @@ String SenseData::getHistory(char* name,int qty,int long_history)
117153
return ret;
118154
}
119155

120-
void SenseData::setData(char* name, int value)
121-
{
122-
Serial.print("setData: ");Serial.print(name);Serial.print(" ");Serial.println(value);
123-
for(int i = 0;i<MAX_SENSOR_NB;i++)
124-
{
125-
if(strcmp(name,SensorArray[i].m_name) == 0)
126-
{
127-
SensorArray[i].setValue(value);
128-
}
129-
}
130-
}
156+
131157

WifiModule/SenseData.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ class SenseData
1717
void run(void);
1818
//void addSensor(Sensor sensor);
1919
String getData(char* name);
20+
int16_t getValue(char* name);
21+
void setValue(char* name, int16_t val);
2022
//void getData(Sensor sensor);
2123
String getHistory(char* name,int qty,int long_history);
2224
//void getHistory(Sensor sensor,int qty);
23-
void setData(char* name, int value);
2425
private:
2526
};
2627

WifiModule/WifiModule.ino

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
WiFiServer server(80);
1010
File webFile;
1111
File confFile;
12-
const char* ssid = "HUAWEI";
13-
const char* password = "menerval";
12+
const char* configfile = "/my_data.json";
13+
14+
15+
String payload;
16+
// Allocate the JSON document and Deserialize the JSON document
17+
//DynamicJsonDocument jsondoc(2048);
18+
StaticJsonDocument<1024> jsondoc;
1419

1520
SenseData sense_data;
1621
//////////////////////////////
@@ -22,26 +27,36 @@ void setup()
2227
digitalWrite(D4, LOW);
2328
Serial.begin(115200);
2429

25-
26-
sense_data.init();
27-
2830
//SPIFFS initialization
2931
bool result = SPIFFS.begin();
30-
Serial.print("SPIFFS opened: ");
31-
Serial.println(result);
32-
/*String str = "";
33-
Dir dir = SPIFFS.openDir("/");
34-
while (dir.next()) {
35-
str += dir.fileName();
36-
str += " / ";
37-
str += dir.fileSize();
38-
str += "\r\n";
32+
Serial.println("SPIFFS opened: "+result);
33+
34+
confFile = SPIFFS.open(configfile, "r");
35+
if (confFile) {
36+
DeserializationError error = deserializeJson(jsondoc, confFile);
37+
if (error)
38+
Serial.println(F("Failed to read file, using default configuration"));
39+
else
40+
{
41+
String WifiName = jsondoc["config"]["WifiName"];
42+
/*Serial.print(F("Connect to : "));
43+
Serial.println(WifiName);*/
44+
}
45+
confFile.close();
46+
}
47+
else
48+
{
49+
Serial.println("##### error while reading configuration");
3950
}
40-
Serial.println(str);*/
51+
52+
sense_data.init();
4153

4254
//Wifi initialization
43-
Serial.print("Connect to "); Serial.println(ssid);
44-
WiFi.begin(ssid, password);
55+
String SSID = jsondoc["config"]["WifiName"];
56+
String password = jsondoc["config"]["WifiPWD"];
57+
/* -------------- to be removed !!!! ------------------*/ password = "menerval";
58+
Serial.println("Connect to " + SSID);
59+
WiFi.begin(SSID, password);
4560
while (WiFi.status() != WL_CONNECTED) {
4661
delay(500);
4762
Serial.print(".");
@@ -56,8 +71,6 @@ void setup()
5671
//////////////////////////////
5772
////////////LOOP//////////////
5873
//////////////////////////////
59-
60-
String payload;
6174
bool got_config = false;
6275
void loop()
6376
{
@@ -68,10 +81,7 @@ void loop()
6881
if(got_config)
6982
{
7083
got_config = false;
71-
// Allocate the JSON document and Deserialize the JSON document
72-
//DynamicJsonDocument doc(2048);
73-
StaticJsonDocument<1024> doc;
74-
DeserializationError error = deserializeJson(doc, payload);
84+
DeserializationError error = deserializeJson(jsondoc, payload);
7585
if (error) {
7686
Serial.print(F("deserializeJson() failed: "));
7787
Serial.println(error.c_str());
@@ -80,7 +90,7 @@ void loop()
8090
else
8191
{
8292
//save file
83-
confFile = SPIFFS.open("/my_data.json", "w+");
93+
confFile = SPIFFS.open(configfile, "w+");
8494
if (confFile) {
8595
confFile.print(payload);
8696
confFile.close();
@@ -89,7 +99,7 @@ void loop()
8999
{
90100
Serial.println("##### error while saving configuration");
91101
}
92-
//const char* test = doc["config"]["WifiName"] ;
102+
//const char* test = jsondoc["config"]["WifiName"] ;
93103
}
94104
}
95105

@@ -142,7 +152,7 @@ void loop()
142152
}
143153
else if(path.indexOf("/setData") != -1){
144154
client.print(F("HTTP/1.1 200 OK\r\n"));
145-
sense_data.setData(param_name,value);
155+
sense_data.setValue(param_name,value);
146156
}
147157
}
148158
else

WifiModule/data/my_data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
{
1818
"sampling_T":15,
1919
"history_T":600,
20-
"WifiName":"blabla",
20+
"WifiName":"HUAWEI",
2121
"WifiPWD":"pwd"
2222
}
2323
}

0 commit comments

Comments
 (0)