I am currently investigating an easy way to record AGC voltages from a radio network for my employer an discovered Arduino's. As I am new to Arduino's I am in need of help. As can be seen by my code I am using a local timeserver on our LAN to get the time for a timestamp of the voltages. The voltages is then recorded as dBm values.
My problems is as follows:
I having difficulty logging to multiple files. I have looked at a few samples like adafruit's light and temp logger which logs to multiple files, but I cannot get my SD card initialized.
My second is how to handle the ethernet and SD access, as each log needs a timestamp(ethernet) and then writes the log to file(SD), without any SPI clash.
If I can get the above working I then need to access the above created logs via a webserver.
Any help?
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <Time.h>
#include <Udp.h>
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message
#define TIME_REQUEST 7 // ASCII bell character requests a time sync message
const int chipSelect = 4;
int sensorPin0 = A0;// select the input pin for the potentiometer
int sensorPin1 = A1;
int ledPin = 13; // select the pin for the LED
int sensorValue0 = 0; // variable to store the value coming from the sensor
int sensorValue1 = 0;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { XXX,XXX,XXX,XXX };
unsigned int localPort = 8080; // local port to listen for UDP packets
byte timeServer[] = {
XXX,XXX,XXX,XXX}; // timeserver on LAN
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
void setup()
{
// start Ethernet and UDP
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.begin(9600);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
Serial.println("Waiting for sync message");
delay(5000);
}
void loop()
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(5000);
if ( Udp.available() ) {
Udp.readPacket(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = " );
Serial.println(secsSince1900);
// now convert NTP time into everyday time:
Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
Serial.println(epoch);
setTime(epoch);
// print the hour, minute and second:
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
//Serial.println(epoch % 31556926 + 1970);
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Serial.print(':');
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':');
Serial.println(epoch %60); // print the second
}
}
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(byte *address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.sendPacket( packetBuffer,NTP_PACKET_SIZE, address, 123); //NTP requests are to port 123
pinMode (10,OUTPUT);
digitalWrite(10,HIGH);
{
digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh
}
// read the value from the sensors:
sensorValue0 = analogRead(sensorPin0);
sensorValue1 = analogRead(sensorPin1);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue0);
delay(sensorValue1);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue0);
delay(sensorValue1);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.csv", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print("TX Level =, ");
dataFile.print((sensorValue0/19.6)+6.5);
dataFile.print(", RX Level =, ");
dataFile.print((sensorValue1/9.8)-78.5);
dataFile.print(" , ");
dataFile.print(day());
dataFile.print("/");
dataFile.print(month());
dataFile.print("/");
dataFile.print(year());
dataFile.print(" ");
dataFile.print(hour());
dataFile.print(" : ");
dataFile.print(minute());
dataFile.print(" : ");
dataFile.print(second());
dataFile.println();
dataFile.close();
// print to the serial port too:
Serial.print("TX Level =, " );
Serial.print((sensorValue0/19.6)+6.5);
Serial.print(", RX Level =, ");
Serial.println((sensorValue1/9.8)-78.5);
Serial.print(" , ");
Serial.print(day());
Serial.print("/");
Serial.print(month());
Serial.print("/");
Serial.print(year());
Serial.print(" ");
Serial.print(hour());
Serial.print(" : ");
Serial.print(minute());
Serial.print(" : ");
Serial.print(second());
Serial.println();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening file.");
}
delay(5000);
}