Skip to content

Commit 203dd0e

Browse files
committed
SD card .csv file numbered
1 parent 6c8126d commit 203dd0e

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
// _ _ _ _ _ _ _
2+
// (_) | | | | | | (_) (_)
3+
// __ ___| | __| | |_| |__ _ _ __ __ _ ___ _ ___
4+
// \ \ /\ / / | |/ _` | __| '_ \| | '_ \ / _` / __| | |/ _ \
5+
// \ V V /| | | (_| | |_| | | | | | | | (_| \__ \_| | (_) |
6+
// \_/\_/ |_|_|\__,_|\__|_| |_|_|_| |_|\__, |___(_)_|\___/
7+
// __ / |
8+
// | ___ /
9+
10+
// Papawai Transmissions
11+
// MQTT Client for Wemos D1
12+
// wildthings.io - Birgit Bachler, Aotearoa/New Zealand, 2017
13+
//
14+
// Wemos D1 with SD card recording to CSV file
15+
//
16+
// credits to
17+
// ESP8266learning http://www.esp8266learning.com/logging-dht11-data-sd-card.php
18+
// https://www.baldengineer.com/mqtt-tutorial.html
19+
// with adaptions as in the comments by Dag Rende & William Brinkman
20+
21+
22+
23+
#include <ESP8266WiFi.h>
24+
#include <PubSubClient.h>
25+
26+
#include <SPI.h>
27+
#include <SD.h>
28+
29+
const int chipSelect = D8;
30+
31+
#define FILE_BASE_NAME "Log"
32+
const uint8_t CS_PIN = 10;
33+
File dataFile;
34+
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
35+
char fileName[] = FILE_BASE_NAME "00.csv";
36+
37+
#define node_name "ESP8266/networkSD"
38+
#define all "#"
39+
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+
49+
// Connect to the WiFi
50+
const char* ssid = "Moturoa_Transmissions";
51+
const char* password = "Tangaroa";
52+
const char* mqtt_server = "192.168.42.1";
53+
54+
WiFiClient espClient;
55+
PubSubClient client(espClient);
56+
57+
void callback(char* topic, byte* payload, unsigned int length) {
58+
Serial.print("Message arrived [");
59+
Serial.print(topic);
60+
Serial.print("] ");
61+
dataFile = SD.open(fileName, FILE_WRITE);
62+
if (dataFile) {
63+
Serial.print("opened ");
64+
Serial.print(fileName);
65+
Serial.println("...");
66+
Serial.print(topic);
67+
Serial.print(" ");
68+
String myMessage = "";
69+
for (int i = 0; i < length; i++) {
70+
char receivedChar = (char)payload[i];
71+
myMessage += receivedChar;
72+
}
73+
Serial.println(myMessage);
74+
dataFile.print(topic);
75+
dataFile.print(",");
76+
dataFile.print(myMessage);
77+
dataFile.print(",");
78+
dataFile.println(millis());
79+
// close the file:
80+
dataFile.close();
81+
// print to the serial port too:
82+
Serial.print("closed ");
83+
Serial.print(fileName);
84+
Serial.println("...");
85+
}
86+
// if the file isn't open, pop up an error:
87+
else {
88+
Serial.println("error opening MoturoaTransmissions.csv");
89+
}
90+
91+
// if (strcmp(topic, spikeTopic) == 0)
92+
}
93+
94+
95+
void reconnect() {
96+
// Loop until we're reconnected
97+
while (!client.connected()) {
98+
Serial.print("Attempting MQTT connection...");
99+
// Attempt to connect
100+
101+
if (client.connect(node_name)) {
102+
Serial.println("connected");
103+
// ... and subscribe to topic
104+
client.subscribe(all);
105+
106+
} else {
107+
Serial.print("failed, rc=");
108+
Serial.print(client.state());
109+
Serial.println(" try again in 5 seconds");
110+
111+
}
112+
}
113+
}
114+
115+
void setup()
116+
{
117+
Serial.begin(9600);
118+
if (!SD.begin(CS_PIN)) {
119+
Serial.println(F("begin failed"));
120+
return;
121+
}
122+
while (SD.exists(fileName)) {
123+
if (fileName[BASE_NAME_SIZE + 1] != '9') {
124+
fileName[BASE_NAME_SIZE + 1]++;
125+
} else if (fileName[BASE_NAME_SIZE] != '9') {
126+
fileName[BASE_NAME_SIZE + 1] = '0';
127+
fileName[BASE_NAME_SIZE]++;
128+
} else {
129+
Serial.println(F("Can't create file name"));
130+
return;
131+
}
132+
}
133+
dataFile = SD.open(fileName, FILE_WRITE);
134+
if (!dataFile) {
135+
Serial.println(F("open failed"));
136+
return;
137+
}
138+
Serial.print(F("opened: "));
139+
Serial.println(fileName);
140+
dataFile.close();
141+
142+
setup_wifi(); // added this line of code !!!!!!!
143+
144+
client.setServer(mqtt_server, 1883);
145+
client.setCallback(callback);
146+
147+
Serial.print("Initializing SD card...");
148+
if (!SD.begin(chipSelect)) {
149+
Serial.println("Card failed, or not present");
150+
// don't do anything more:
151+
return;
152+
}
153+
Serial.println("card initialized.");
154+
delay(2000);
155+
}
156+
157+
void loop()
158+
{
159+
if (WiFi.status() != WL_CONNECTED) {
160+
setup_wifi();
161+
162+
}
163+
if (!client.connected()) {
164+
reconnect();
165+
}
166+
167+
client.loop();
168+
}
169+
170+
void setup_wifi() {
171+
// added this function - without it the mqtt part will not connect
172+
delay(10);
173+
// We start by connecting to a WiFi network
174+
Serial.println();
175+
Serial.print("Connecting to ");
176+
Serial.println(ssid);
177+
178+
WiFi.begin(ssid, password);
179+
180+
while (WiFi.status() != WL_CONNECTED) {
181+
delay(500);
182+
Serial.print(".");
183+
184+
185+
}
186+
187+
Serial.println("");
188+
Serial.println("WiFi connected");
189+
190+
Serial.println("IP address: ");
191+
Serial.println(WiFi.localIP());
192+
}

0 commit comments

Comments
 (0)