Ho anche patchato la libreria ethernet secondo queste informazioni:
http://code.google.com/p/arduino/issues/detail?id=605&start=200
Ho aggiunto due led pilotati in pwm per capire lo stato di arduino: se sia partito correttamente, quando riceve dati su seriale e se invia/scrive correttamente i dati.
questa è la nuova versione
Codice: Seleziona tutto
/*
Save Rotex RSP3 data to SD card and mysql database
Use NTP time provider to sync internal time
Fabio Roverso 4/11/11
V 2.0
Led codes GREEN RED
Boot low low
Ready high off
Serial Data high low
HTTP low high
SD high high
*/
#include <SPI.h>
#include <Ethernet.h>
#include <NewSoftSerial.h>
#include <Udp.h>
#include <SD.h>
#include <Time.h>
//Network stuff
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x66, 0x38 }; //Arduino MAC address
byte ip[] = {
192, 168, 1, 177 }; //Arduino IP address
byte gateway[] = {
192, 168, 1, 250 }; //Network gateway
byte subnet[] = {
255, 255, 255, 0 }; //Network subnet mask
//NTP stuff
byte timeServer[] = {
192, 168, 1, 8 }; //NTP server address
unsigned int localPort = 8888; //local port to listen for UDP packets
const int NTP_PACKET_SIZE = 48; //NTP time stamp
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
const unsigned long timeZoneOffset = 3600; //set this to the offset in seconds to your local time;
//Web client stuff
byte server[] = {
192 ,168 ,12 ,8 }; //Web server address
unsigned int port = 82; //Web server http port (usually 80)
Client client(server, port); //Creates an http client handle
//Serial stuff
const byte rxPin = 2;
const byte txPin = 3;
NewSoftSerial mySerial(rxPin, txPin); //Initialize 2nd serial port
//led stuff
const byte GreenLed = 5;
const byte RedLed = 6;
const byte HalfLed = 5
const byte FullLed = 255;
const byte OffLed = 0;
//incoming serial buffer
const char EndChar = 13; //End of transmission
const byte MaxBufferSize = 40; //Max transmission lenght
const byte MinBufferSize = 26; //Min transmission lenght
byte i = 0; //Buffer index
boolean getSerialString = false;
// On the Ethernet Shield, CS is pin 4. Note that even if it"s not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;
boolean SDpresent = false;
boolean blndebug=true;
void setup(){
// sets the digital pin as output
pinMode(RedLed, OUTPUT);
pinMode(GreenLed, OUTPUT);
analogWrite(RedLed, HalfLed);
analogWrite(GreenLed, HalfLed);
//start Ethernet and UDP
Ethernet.begin(mac, ip, gateway, subnet);
Udp.begin(localPort);
//start serial
Serial.begin(9600);
mySerial.begin(9600);
if (blndebug) Serial.print("Initializing SD card...");
//make sure that the default chip select pin is set to
//output, even if you don"t use it:
pinMode(10, OUTPUT);
//see if the card is present and can be initialized:
if (!SD.begin(chipSelect)){
if (blndebug){
Serial.println();
Serial.println("Card failed, or not present");
}
SDpresent = false;
}
else {
if (blndebug) Serial.println("OK");
SDpresent = true;
}
//Sync time and date
setSyncProvider(getNtpTime);
setSyncInterval(86400); //24h
if (blndebug) Serial.println("Synching Clock...");
while(timeStatus() == timeNotSet){
if (i > 100){
if (blndebug) Serial.println("Time not set");
break;
}
i++;
delay(10);
}
if (blndebug){
Serial.println();
digitalClockDisplay();
Serial.println("Arduino started");
Serial.println("================================");
}
analogWrite(GreenLed, FullLed);
analogWrite(RedLed, OffLed);
delay(500);
}
void loop(){
//empty serial buffer for incomplete data
mySerial.flush();
if (blndebug) Serial.println("Waiting for serial data...");
while (mySerial.available() == 0){
delay(50);
}
if (blndebug) Serial.println("Serial data incoming...");
analogWrite(RedLed, HalfLed);
i = 0;
char serInString[MaxBufferSize+1]; //define string buffer and set to empty
getSerialString = false;
while ((mySerial.available() > 0) && (i <= MaxBufferSize)){
char incomingbyte = mySerial.read();
if ((incomingbyte == EndChar) && (i >= MinBufferSize)){
serInString[i] = 0; //finalize string buffer
getSerialString = true;
break;
}
else {
serInString[i] = incomingbyte;
i++;
}
}
analogWrite(RedLed, OffLed);
if (blndebug) Serial.println(serInString);
if (getSerialString){
WebClient(serInString); //Save to db
if(SDpresent){
SDWrite(serInString); //Save to SD
}
}
}
void WebClient(char *strArray){
if (client.connect()){
analogWrite(GreenLed, HalfLed);
analogWrite(RedLed, FullLed);
client.flush();
client.print("GET ");
client.print("/rotex.php?user=root&password=rotex&data=");
client.print(strArray);
client.println(" HTTP/1.0");
client.println();
while(!client.available()){
delay(1);
}
while (client.available()){
char c = client.read();
if (blndebug) Serial.print(c);
}
if (blndebug) Serial.println();
}
else {
if (blndebug) Serial.println("EXCEPTION: during HTTP GET. Could not connect");
return;
}
while(client.connected()){
if (blndebug) Serial.println("Waiting for server to disconnect");
}
client.stop();
analogWrite(GreenLed, FullLed);
analogWrite(RedLed, OffLed);
}
void SDWrite(char *strArray){
int digits;
File dataFile = SD.open("rotex.csv", FILE_WRITE); //define file handle
//if the file is available, write to it:
if (dataFile){
analogWrite(GreenLed, FullLed);
analogWrite(RedLed, FullLed);
if (blndebug) Serial.print("Writing to SD card...");
dataFile.print(hour());
dataFile.print(":");
digits = minute();
if (digits < 10) dataFile.print("0");
dataFile.print(digits);
dataFile.print(":");
digits = second();
if (digits < 10) dataFile.print("0");
dataFile.print(digits);
dataFile.print(" ");
dataFile.print(day());
dataFile.print("-");
dataFile.print(month());
dataFile.print("-");
dataFile.print(year());
dataFile.print(";");
dataFile.println(strArray);
dataFile.close();
if (blndebug) Serial.println("done");
}
else {
if (blndebug) Serial.println("Error opening file.");
}
analogWrite(GreenLed, FullLed);
analogWrite(RedLed, OffLed);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print("0");
Serial.print(digits);
}
/*******************************************************************************
* Get NTP time function
*******************************************************************************/
unsigned long getNtpTime(){
sendNTPpacket(timeServer); //send an NTP packet to a time server
delay(1000); //wait to see if a reply is available
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;
const unsigned long seventyYears = 2208988800UL - timeZoneOffset;
//subtract seventy years:
return secsSince1900 - seventyYears;
}
return 0; //return 0 if unable to get the time
}
/*******************************************************************************
* 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
}