Skip to content
Permalink
Browse files

debug sd card

  • Loading branch information...
iRGBit committed Feb 11, 2019
1 parent 22bf374 commit 5b88f3aa5cebe2087ec2f70248f73198dc954fdc
@@ -34,18 +34,9 @@ File dataFile;
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[] = FILE_BASE_NAME "00.csv";

#define node_name "ESP8266/networkSD"
#define node_name "papawai/networkSD"
#define all "#"

#define hTopic "moturoa/ahumid" // DHT11 humidity
#define tTopic "moturoa/atemp" // DHT11 temp
#define humidTopic "moturoa/humid" // soil
#define rainTopic "moturoa/rain" // rain
#define ecTopic "moturoa/ec" // conduct
#define spikeTopic "moturoa/spike" // spike in water
#define wTopic "moturoa/watertemp"


// Connect to the WiFi
const char* ssid = "Moturoa_Transmissions";
const char* password = "Tangaroa";
@@ -54,69 +45,90 @@ const char* mqtt_server = "192.168.42.1";
WiFiClient espClient;
PubSubClient client(espClient);

boolean debug = false;

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
if (debug) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
}
dataFile = SD.open(fileName, FILE_WRITE);
if (dataFile) {
Serial.print("opened ");
Serial.print(fileName);
Serial.println("...");
Serial.print(topic);
Serial.print(" ");
if (debug) {
Serial.print("opened ");
Serial.print(fileName);
Serial.println("...");
Serial.print(topic);
Serial.print(" ");
}
String myMessage = "";
for (int i = 0; i < length; i++) {
char receivedChar = (char)payload[i];
myMessage += receivedChar;
}
Serial.println(myMessage);
if (debug) {
Serial.println(myMessage);
}
dataFile.print(topic);
dataFile.print(",");
dataFile.print(myMessage);
dataFile.print(",");
dataFile.println(millis());
// close the file:
dataFile.close();
// print to the serial port too:
Serial.print("closed ");
Serial.print(fileName);
Serial.println("...");
if (debug) {
// print to the serial port too:
Serial.print("closed ");
Serial.print(fileName);
Serial.println("...");
}
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening MoturoaTransmissions.csv");
if (debug) {
Serial.println("error opening file");
}
}

// if (strcmp(topic, spikeTopic) == 0)
}


void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (debug) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
}

if (client.connect(node_name)) {
Serial.println("connected");
if (debug) {
Serial.println("connected");
}
// ... and subscribe to topic
client.subscribe(all);

} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
if (debug) {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
}

}
}
}

void setup()
{
Serial.begin(9600);
if (debug) {
Serial.begin(9600);
}
if (!SD.begin(CS_PIN)) {
Serial.println(F("begin failed"));
if (debug) {
Serial.println(F("begin failed"));
}
return;
}
while (SD.exists(fileName)) {
@@ -126,31 +138,42 @@ void setup()
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
} else {
Serial.println(F("Can't create file name"));
if (debug) {
Serial.println(("Can't create file name"));
}
return;
}
}
dataFile = SD.open(fileName, FILE_WRITE);
if (!dataFile) {
Serial.println(F("open failed"));
if (debug) {
Serial.println(F("open failed"));
}
return;
}x```
Serial.print(F("opened: "));
Serial.println(fileName);
}
if (debug) {
Serial.print(F("opened: "));
Serial.println(fileName);
}
dataFile.close();

setup_wifi(); // added this line of code !!!!!!!

client.setServer(mqtt_server, 1883);
client.setCallback(callback);

Serial.print("Initializing SD card...");
if (debug) {
Serial.print("Initializing SD card...");
}
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
if (debug) {
Serial.println("Card failed, or not present");
}
// don't do anything more:
return;
}
Serial.println("card initialized.");
if (debug) {
Serial.println("card initialized.");
}
delay(2000);
}

@@ -171,22 +194,26 @@ void setup_wifi() {
// added this function - without it the mqtt part will not connect
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
if (debug) {
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
}

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (debug) {
Serial.print(".");
}


}

Serial.println("");
Serial.println("WiFi connected");

Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (debug) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
}

0 comments on commit 5b88f3a

Please sign in to comment.
You can’t perform that action at this time.