Project:
Making a letterbox alerting system that sends an email when post (paper) is
inserted into a letterbox.
Pictures of layout and mockup of letterbox here:
http://abdallah.hiof.no/biff-box/
The alerting email is sent when post is inserted, tripping the light
reaching the photocell. A LED is located in the ceiling of the
letterbox to provide a reference light if it is dark outside the
letterbox when a letter is inserted.
Problem:
Ethernet.begin() appears to interfere with the control of the LED:
In the case when a letter covers the photocell, there is
post. Therefore there is no reason to leave the LED lit. If the
ambient light is not strong enough to sense that the letter has
been removed uncovering the photocell, it would be night
anyway. As I'm not usually in the office at night, it would not
matter if the email alert is not send before the morning when there
is enough light to trip the cell.
When the dummy sketch is run (without the "Ethernet.begin()"
intitialization), everything works fine. The first "if" test of the loop
which sends the email, also turns off the LED as intended:
/* trip light from LED to photocell */
#include <SPI.h>
#include <Ethernet.h>
const int yelLEDPin = 11;
const int yelSensorPin = A2;
int yelValue = 0; // "Mapped"
int yelSensorValue = 0; // "Raw"
int inverseValue = 0; // inverse (value sent to LED)
int sentmail = 0; // email sent =1, not = 0
// Stuff to send the message
// this must be unique
byte mac[] = { 0xX0, 0xX2, 0xXX, 0xZZ, 0xYY, 0xYY };
// change network settings to yours
IPAddress ip( ww,xx,yy,zzz );
IPAddress gateway( ww,xx,yy,zzz );
IPAddress subnet( 255, 255, 255, 0 );
// change server to your email server ip or domain
// IPAddress server( 1, 2, 3, 4 );
char server[] = "myserver.mydomain.no";
EthernetClient client;
void setup() {
Serial.begin(9600);
// pinMode(yelLEDPin, OUTPUT);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
delay(500); // wait while the board initializes
}
void loop() {
yelSensorValue = analogRead(yelSensorPin);
yelValue = yelSensorValue/4;
inverseValue = 250 - yelValue;
delay(5000);
Serial.print("Raw Sensor Values\t Raw: ");
Serial.print(yelSensorValue);
Serial.print("\r\n");
Serial.print("Mapped Sensor Values\t Yel: ");
Serial.print(yelValue);
Serial.print("\r\n");
Serial.print("Inverse sensor Values \t Inv: ");
Serial.print(inverseValue);
Serial.print("\n\r");
Serial.print("Sentmail\t");
Serial.print(sentmail);
Serial.print("\r\n");
if (yelValue < 50 && sentmail == 0)
{
sendEmail(); // send mail
sentmail = 1; // mail has been sent
analogWrite(yelLEDPin, LOW);
delay(1000);
}
if (yelValue > 0 && sentmail == 0)
{
analogWrite(yelLEDPin, inverseValue);
}
else{
if (yelValue > 25 && sentmail == 1)
{
sentmail = 0;
Serial.print("Sentmail reset \r\n");
analogWrite(yelLEDPin, inverseValue);
}
}
}
byte sendEmail()
{
Serial.print("I'm sending a message from the letterbox. \n\r");
}
If the "Ethernet.begin()" is uncommented, the LED is not turned
down, and when the real sketch (which otherwise works fine) is run,
the test also does not turn the LED down:
/*
A sketch to detect post in a post box adapted from
various sources. Børre Ludvigsen, 2013
Ver 0.2 20130723 Tue 14:28:06
Ver 0.1 20130723 Tue 09:52:59
Email client sketch for IDE v1.0.1 and w5100/w5200
Posted December 2012 by SurferTim
*/
#include <SPI.h>
#include <Ethernet.h>
/* trip light from LED to photocell */
const int yelLEDPin = 11;
const int yelSensorPin = A2;
int yelValue = 0;
int yelSensorValue = 0;
int inverseValue = 0; // inverse (value sent to LED)
int sentmail = 0; // email sent =1, not = 0
// Stuff to send the message
// this must be unique
byte mac[] = { 0xX0, 0xX2, 0xXX, 0xZZ, 0xYY, 0xYY };
// change network settings to yours
IPAddress ip( ww,xx,yy,zzz );
IPAddress gateway( ww,xx,yy,zzz );
IPAddress subnet( 255, 255, 255, 0 );
// change server to your email server ip or domain
// IPAddress server( 1, 2, 3, 4 );
char server[] = "myserver.mydomain.no";
EthernetClient client;
void setup()
{
pinMode(yelLEDPin, OUTPUT);
Serial.begin(9600);
pinMode(4,OUTPUT);
pinMode(yelLEDPin, OUTPUT);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
delay(1000); // wait while the board initializes
}
// the main loop
// LED control from the Arduino Projects Book
void loop() {
yelSensorValue = analogRead(yelSensorPin);
delay(5000);
Serial.print("Raw Sensor Values\t Raw: ");
Serial.print(yelSensorValue);
Serial.print("\r\n");
yelValue = yelSensorValue/4;
inverseValue = 750 - yelValue;
Serial.print("Mapped Sensor Values\t Yel: ");
Serial.print(yelValue);
Serial.print("\r\n");
Serial.print("Inverse sensor Values \t Inv: ");
Serial.print(inverseValue);
Serial.print("\n\r");
Serial.print("Sentmail\t");
Serial.print(sentmail);
Serial.print("\r\n");
if (yelValue < 50 && sentmail < 1)
{
// analogWrite(yelLEDPin, 0);
sendEmail(); // send mail
sentmail = 1; // mail has been sent
delay(6000);
}
if (yelValue > 125 && sentmail < 1)
{
analogWrite(yelLEDPin, inverseValue);
}
if (yelValue > 125 && sentmail > 0)
{
sentmail = 0;
Serial.print("Sentmail reset \r\n");
analogWrite(yelLEDPin, inverseValue);
}
if (yelValue < 50 && sentmail > 0)
{
analogWrite(yelLEDPin, LOW);
}
}
// script to send email
// Posted December 2012 by SurferTim
byte sendEmail()
{
byte thisByte = 0;
byte respCode;
if(client.connect(server,25)) {
Serial.println(F("connected"));
} else {
Serial.println(F("connection failed"));
return 0;
}
if(!eRcv()) return 0;
Serial.println(F("Sending helo"));
// change to your public ip
client.println(F("helo ww,xx,yy,zzz"));
if(!eRcv()) return 0;
Serial.println(F("Sending From <me@mydomain.no>"));
// change to your email address (sender)
client.println(F("Mail From: <me@mydomain.no>"));
if(!eRcv()) return 0;
// change to recipient address
Serial.println(F("Sending To me"));
client.println(F("RCPT To: me"));
if(!eRcv()) return 0;
Serial.println(F("Sending DATA"));
client.println(F("DATA"));
if(!eRcv()) return 0;
Serial.println(F("Sending email"));
// change to recipient address
client.println(F("To: Me <me@mydomain.no>"));
// change to your address
client.println(F("From: Me <me@mydomain.no>"));
client.println(F("Subject: You have snail mail\r\n"));
client.println(F("This is from your mailbox. I have something for you."));
client.println(F("."));
if(!eRcv()) return 0;
Serial.println(F("Sending QUIT"));
client.println(F("QUIT"));
if(!eRcv()) return 0;
client.stop();
Serial.println(F("disconnected"));
return 1;
}
byte eRcv()
{
byte respCode;
byte thisByte;
int loopCount = 0;
while(!client.available()) {
delay(1);
loopCount++;
// if nothing received for 10 seconds, timeout
if(loopCount > 10000) {
client.stop();
Serial.println(F("\r\nTimeout"));
return 0;
}
}
respCode = client.peek();
while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}
if(respCode >= '4')
{
efail();
return 0;
}
return 1;
}
void efail()
{
byte thisByte = 0;
int loopCount = 0;
client.println(F("QUIT"));
while(!client.available()) {
delay(1);
loopCount++;
// if nothing received for 10 seconds, timeout
if(loopCount > 10000) {
client.stop();
Serial.println(F("\r\nTimeout"));
return;
}
}
while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}
client.stop();
Serial.println(F("disconnected"));
}
Is there a bug in the "Ethernet.h" library, or am I doing something
else wrong?
- Børre