Hello everyone. I've made a setup to log some data (power of a signal) and display it on a website. The code is as follows:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <Wire.h>
#include <Ard2499.h>
#include <stdio.h>
#include <DS3231.h>
#define FILE_NAME_LEN 30
#define HTTP_invalid 0
#define HTTP_GET 1
#define HTTP_POST 2
#define FT_HTML 0
#define FT_ICON 1
#define FT_CSS 2
#define FT_JAVASCRIPT 3
#define FT_JPG 4
#define FT_PNG 5
#define FT_GIF 6
#define FT_TEXT 7
#define FT_INVALID 8
#define PIN_ETH_SPI 10
File logDates;
char filename[30], path[15], currentDate[12], previousDate[12], temp[12], dateStr[12], timeStr[10];
int m, d, y, lastLine;
long log_time_ms = 500;
long prev_log_time = 0;
Ard2499 ard2499Board;
DS3231 rtc(SDA, SCL);
const byte mac[] PROGMEM = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
const byte ip[] = { 195, 130, 115, 124 };
const byte gateway[] PROGMEM = { 195, 130, 115, 200 };
const byte subnet[] PROGMEM = { 255, 255, 255, 0 };
EthernetServer server(80);
void setup() {
pinMode(PIN_ETH_SPI, OUTPUT);
digitalWrite(PIN_ETH_SPI, HIGH);
Wire.begin();
rtc.begin();
ard2499Board.begin(ARD2499_ADC_ADDR_ZZZ, ARD2499_EEP_ADDR_ZZ);
ard2499Board.ltc2499ChangeConfiguration(LTC2499_CONFIG2_SPEED_2X);
ard2499Board.ltc2499ChangeChannel(LTC2499_CHAN_SINGLE_0P);
Serial.begin(115200);
if (!SD.begin(4)) {
Serial.println("SD card intitialization failed");
}
logDates = SD.open("logDates.txt", FILE_WRITE);
logDates.close();
Ethernet.begin((uint8_t*)mac, ip);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found.");
while (true) {
delay(1);
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("No cable");
}
server.begin();
Serial.print("IP:");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (ServiceClient(&client)) {
break;
}
}
delay(1);
client.stop();
}
LogData();
}
void LogData(void)
{
unsigned long current_time;
File logFile;
current_time = millis();
if ((current_time - prev_log_time) > log_time_ms) {
**************************************************
IF I COMMENT THE NEXT 3 LINES THEN THE SERVER WORKS
**************************************************
m = rtc.getTime().mon;
d = rtc.getTime().date;
y = rtc.getTime().year;
sprintf(path,"/%s/%d","LOGFILES",y);
if (!SD.exists(path)) {
SD.mkdir(path);
}
sprintf(filename, "/%s/%d/%d-%d.txt","LOGFILES", y, m, d);
prev_log_time = current_time;
logFile = SD.open(filename, FILE_WRITE);
if (logFile) {
double adc = ard2499Board.ltc2499Read();
double Vadc = (2.048/16777215.0)*adc;
double P = -(Vadc-0.6447)/0.025;
//write time
logFile.print(rtc.getTimeStr());
logFile.print("\t");
//write power (dBm)
logFile.println(P);
logFile.close();
}
logDates = SD.open("LOGDATES.txt", FILE_READ);
sprintf(currentDate,"%02d/%02d/%d", m, d, y);
lastLine = logDates.size()-12;
if (lastLine < 0) {
lastLine = 0;
}
logDates.seek(lastLine);
char temp[12];
logDates.readStringUntil('\r').toCharArray(temp,12);
strcpy(previousDate, temp);
logDates.close();
if (strcmp(currentDate, previousDate)!=0) {
logDates = SD.open("LOGDATES.txt", FILE_WRITE);
if (lastLine == 0) {
logDates.println("MM/DD/YYYY");
logDates.println("==========");
}
logDates.println(currentDate);
logDates.close();
}
}
}
bool ServiceClient(EthernetClient *client)
{
static boolean currentLineIsBlank = true;
char cl_char;
File webFile;
// file name from request including path + 1 of null terminator
char file_name[FILE_NAME_LEN + 1] = {0};
char http_req_type = 0;
char req_file_type = FT_INVALID;
const char *file_types[] = {"text/html", "image/x-icon", "text/css", "application/javascript", "image/jpeg", "image/png", "image/gif", "text/plain"};
static char req_line_1[40] = {0};
static unsigned char req_line_index = 0;
static bool got_line_1 = false;
if (client->available()) {
cl_char = client->read();
if ((req_line_index < 39) && (got_line_1 == false)) {
if ((cl_char != '\r') && (cl_char != '\n')) {
req_line_1[req_line_index] = cl_char;
req_line_index++;
}
else {
got_line_1 = true;
req_line_1[39] = 0;
}
}
if ((cl_char == '\n') && currentLineIsBlank) {
http_req_type = GetRequestedHttpResource(req_line_1, file_name, &req_file_type);
if (http_req_type == HTTP_GET) {
if (req_file_type < FT_INVALID) {
webFile = SD.open(file_name);
if (webFile) {
// send a standard http response header
client->println(F("HTTP/1.1 200 OK"));
client->print(F("Content-Type: "));
client->println(file_types[req_file_type]);
client->println(F("Connection: close"));
client->println();
// send web page
while(webFile.available()) {
int num_bytes_read;
char byte_buffer[64];
// get bytes from requested file
num_bytes_read = webFile.read(byte_buffer, 64);
// send the file bytes to the client
client->write(byte_buffer, num_bytes_read);
}
webFile.close();
}
}
}
req_line_1[0] = 0;
req_line_index = 0;
got_line_1 = false;
return 1;
}
if (cl_char == '\n') {
currentLineIsBlank = true;
}
else if (cl_char != '\r') {
currentLineIsBlank = false;
}
} // if (client.available())
return 0;
}
char GetRequestedHttpResource(char *req_line, char *file_name, char *file_type)
{
char request_type = HTTP_invalid;
char *str_token;
*file_type = FT_INVALID;
str_token = strtok(req_line, " ");
if (strcmp(str_token, "GET") == 0) {
request_type = HTTP_GET;
str_token = strtok(NULL, " "); // get the file name
if (strcmp(str_token, "/") == 0) {
strcpy(file_name, "index.htm");
*file_type = FT_HTML;
}
else if (strlen(str_token) <= FILE_NAME_LEN) {
strcpy(file_name, str_token);
// get the file extension
str_token = strtok(str_token, ".");
str_token = strtok(NULL, ".");
if (strcmp(str_token, "htm") == 0) {*file_type = 0;}
else if (strcmp(str_token, "ico") == 0) {*file_type = 1;}
else if (strcmp(str_token, "css") == 0) {*file_type = 2;}
else if (strcmp(str_token, "js") == 0) {*file_type = 3;}
else if (strcmp(str_token, "jpg") == 0) {*file_type = 4;}
else if (strcmp(str_token, "png") == 0) {*file_type = 5;}
else if (strcmp(str_token, "gif") == 0) {*file_type = 6;}
else if (strcmp(str_token, "txt") == 0) {*file_type = 7;}
else {*file_type = 8;}
}
}
else if (strcmp(str_token, "POST") == 0) {
request_type = HTTP_POST;
}
return request_type;
}
There is a function called "LogData()" that does all the logging. Everything used to work, but then i made a few small changes on the code. As a result i am no longer able to connect to the server. Considering that those changes were just some small tweaks, i didn't bother keeping a backup of the previous working state of the code. Sadly those changes were made about 2 months ago and i am having a hard time remembering exactly what they were. But i'm sure almost all of them were inside the LogData() function. I noticed that if i comment out 3 lines in that function (i mention them on the code), then the server works.Those 3 lines were exactly the same when the code worked. Can anyone help me out?
Libraries not included in the IDE:
- DS3231: DS3231 - Rinky-Dink Electronics
- ARD-LTC2499: ARD-LTC2499 Library Reference
PS. Sorry but i had to remove most comments to remain within the message character-limit.