@@ -34,18 +34,9 @@ File dataFile;
3434const uint8_t BASE_NAME_SIZE = sizeof (FILE_BASE_NAME) - 1 ;
3535char fileName[] = FILE_BASE_NAME " 00.csv" ;
3636
37- #define node_name " ESP8266 /networkSD"
37+ #define node_name " papawai /networkSD"
3838#define all " #"
3939
40- #define hTopic " moturoa/ahumid" // DHT11 humidity
41- #define tTopic " moturoa/atemp" // DHT11 temp
42- #define humidTopic " moturoa/humid" // soil
43- #define rainTopic " moturoa/rain" // rain
44- #define ecTopic " moturoa/ec" // conduct
45- #define spikeTopic " moturoa/spike" // spike in water
46- #define wTopic " moturoa/watertemp"
47-
48-
4940// Connect to the WiFi
5041const char * ssid = " Moturoa_Transmissions" ;
5142const char * password = " Tangaroa" ;
@@ -54,69 +45,90 @@ const char* mqtt_server = "192.168.42.1";
5445WiFiClient espClient;
5546PubSubClient client (espClient);
5647
48+ boolean debug = false ;
49+
5750void callback (char * topic, byte* payload, unsigned int length) {
58- Serial.print (" Message arrived [" );
59- Serial.print (topic);
60- Serial.print (" ] " );
51+ if (debug) {
52+ Serial.print (" Message arrived [" );
53+ Serial.print (topic);
54+ Serial.print (" ] " );
55+ }
6156 dataFile = SD.open (fileName, FILE_WRITE);
6257 if (dataFile) {
63- Serial.print (" opened " );
64- Serial.print (fileName);
65- Serial.println (" ..." );
66- Serial.print (topic);
67- Serial.print (" " );
58+ if (debug) {
59+ Serial.print (" opened " );
60+ Serial.print (fileName);
61+ Serial.println (" ..." );
62+ Serial.print (topic);
63+ Serial.print (" " );
64+ }
6865 String myMessage = " " ;
6966 for (int i = 0 ; i < length; i++) {
7067 char receivedChar = (char )payload[i];
7168 myMessage += receivedChar;
7269 }
73- Serial.println (myMessage);
70+ if (debug) {
71+ Serial.println (myMessage);
72+ }
7473 dataFile.print (topic);
7574 dataFile.print (" ," );
7675 dataFile.print (myMessage);
7776 dataFile.print (" ," );
7877 dataFile.println (millis ());
7978 // close the file:
8079 dataFile.close ();
81- // print to the serial port too:
82- Serial.print (" closed " );
83- Serial.print (fileName);
84- Serial.println (" ..." );
80+ if (debug) {
81+ // print to the serial port too:
82+ Serial.print (" closed " );
83+ Serial.print (fileName);
84+ Serial.println (" ..." );
85+ }
8586 }
8687 // if the file isn't open, pop up an error:
8788 else {
88- Serial.println (" error opening MoturoaTransmissions.csv" );
89+ if (debug) {
90+ Serial.println (" error opening file" );
91+ }
8992 }
9093
91- // if (strcmp(topic, spikeTopic) == 0)
9294}
9395
9496
9597void reconnect () {
9698 // Loop until we're reconnected
9799 while (!client.connected ()) {
98- Serial.print (" Attempting MQTT connection..." );
99- // Attempt to connect
100+ if (debug) {
101+ Serial.print (" Attempting MQTT connection..." );
102+ // Attempt to connect
103+ }
100104
101105 if (client.connect (node_name)) {
102- Serial.println (" connected" );
106+ if (debug) {
107+ Serial.println (" connected" );
108+ }
103109 // ... and subscribe to topic
104110 client.subscribe (all);
105111
106112 } else {
107- Serial.print (" failed, rc=" );
108- Serial.print (client.state ());
109- Serial.println (" try again in 5 seconds" );
113+ if (debug) {
114+ Serial.print (" failed, rc=" );
115+ Serial.print (client.state ());
116+ Serial.println (" try again in 5 seconds" );
117+ }
110118
111119 }
112120 }
113121}
114122
115123void setup ()
116124{
117- Serial.begin (9600 );
125+ if (debug) {
126+ Serial.begin (9600 );
127+ }
118128 if (!SD.begin (CS_PIN)) {
119- Serial.println (F (" begin failed" ));
129+ if (debug) {
130+ Serial.println (F (" begin failed" ));
131+ }
120132 return ;
121133 }
122134 while (SD.exists (fileName)) {
@@ -126,31 +138,42 @@ void setup()
126138 fileName[BASE_NAME_SIZE + 1 ] = ' 0' ;
127139 fileName[BASE_NAME_SIZE]++;
128140 } else {
129- Serial.println (F (" Can't create file name" ));
141+ if (debug) {
142+ Serial.println ((" Can't create file name" ));
143+ }
130144 return ;
131145 }
132146 }
133147 dataFile = SD.open (fileName, FILE_WRITE);
134148 if (!dataFile) {
135- Serial.println (F (" open failed" ));
149+ if (debug) {
150+ Serial.println (F (" open failed" ));
151+ }
136152 return ;
137- }x```
138- Serial.print (F (" opened: " ));
139- Serial.println (fileName);
153+ }
154+ if (debug) {
155+ Serial.print (F (" opened: " ));
156+ Serial.println (fileName);
157+ }
140158 dataFile.close ();
141159
142160 setup_wifi (); // added this line of code !!!!!!!
143161
144162 client.setServer (mqtt_server, 1883 );
145163 client.setCallback (callback);
146-
147- Serial.print (" Initializing SD card..." );
164+ if (debug) {
165+ Serial.print (" Initializing SD card..." );
166+ }
148167 if (!SD.begin (chipSelect)) {
149- Serial.println (" Card failed, or not present" );
168+ if (debug) {
169+ Serial.println (" Card failed, or not present" );
170+ }
150171 // don't do anything more:
151172 return ;
152173 }
153- Serial.println (" card initialized." );
174+ if (debug) {
175+ Serial.println (" card initialized." );
176+ }
154177 delay (2000 );
155178}
156179
@@ -171,22 +194,26 @@ void setup_wifi() {
171194 // added this function - without it the mqtt part will not connect
172195 delay (10 );
173196 // We start by connecting to a WiFi network
174- Serial.println ();
175- Serial.print (" Connecting to " );
176- Serial.println (ssid);
197+ if (debug) {
198+ Serial.println ();
199+ Serial.print (" Connecting to " );
200+ Serial.println (ssid);
201+ }
177202
178203 WiFi.begin (ssid, password);
179204
180205 while (WiFi.status () != WL_CONNECTED) {
181206 delay (500 );
182- Serial.print (" ." );
207+ if (debug) {
208+ Serial.print (" ." );
209+ }
183210
184211
185212 }
186-
187- Serial.println (" " );
188- Serial.println (" WiFi connected" );
189-
190- Serial.println (" IP address: " );
191- Serial. println (WiFi. localIP ());
213+ if (debug) {
214+ Serial.println (" " );
215+ Serial.println (" WiFi connected" );
216+ Serial. println ( " IP address: " );
217+ Serial.println (WiFi. localIP () );
218+ }
192219}
0 commit comments