Ho 2 sketch funzionanti, uno mi invia dati ad emoncms, l'altro invia una mail se si verifica una data condizione (tipo se spengo la luce).
Il problema è fondere i 2 sketch nello stesso codice e caricarli nello stesso arduino (uso Arduino Uno con ethernet shield).
Il codice della Email è il seguente:
#include <SPI.h>
#include <Ethernet.h>
int analogInPin = A0;
int sensorValue = 0;
// Local network configuration:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x23, 0x5D };
byte ip[] = { 192, 168, 0, 120 };
// Login data:
String UserName64 = "id";
String Password64 = "pw";
// SMTP server data:
// smtp.libero.it : 212.52.84.54
// smtp.iol.it : 212.52.84.203
// out.virgilio.it : 62.211.72.20
byte server[] = { 212, 52, 84, 54 }; // SMTP server
String ServerName = "libero.it";
// Mail data:
String Sender = "xx@libero.it";
String Recipient = "xx@gmail.com";
String Subject = "Messaggio inviato da Arduino!";
String Body = "luce spenta";
int time = 1000;
int wait = 1000;
String ServerResponse="";
EthernetClient client;
int precedente=0;
void setup() {
Serial.begin(9600);
pinMode( analogInPin, INPUT);
}
void loop() {
sensorValue = analogRead(analogInPin);
Serial.print("sensor = " );
Serial.println(sensorValue);
if (sensorValue>50)
{precedente=0;
}
delay(1000);
if ((sensorValue<50) and (precedente==0)){
precedente=1;
Serial.begin(9600);
Serial.println("Program started, waiting for router...");
delay(time); /* allow the router to identify the Arduino before the Arduino connects to the internet */
Serial.println("Starting network module...");
Ethernet.begin(mac, ip);
delay(2000);
Serial.println("connecting...");
if (client.connect(server, 25)) {
Serial.println("connected");
SendMsg("EHLO " + ServerName); /* say hello*/
SendMsg("AUTH LOGIN ");
SendMsg(UserName64); /* Username*/
SendMsg(Password64); /* Password */
SendMsg("MAIL From:<" + Sender +">"); /* identify sender */
SendMsg("RCPT To:<" + Recipient + ">"); /* identify recipient */
SendMsg("DATA");
SendMsg("To: " + Recipient); /* recipient in message header */
SendMsg("From: " + Sender); /* seder name in message header */
SendMsg("Subject: "+ Subject); /* insert subject */
SendMsg(""); /* empty line */
SendMsg(Body); /* insert body */
SendMsg(""); /* empty line */
SendMsg("."); /* end mail */
SendMsg("QUIT"); /* terminate connection */
client.println();
} else {
Serial.println("connection failed");
}
}
}
void GetResponse() {
if (client.available()) {
char c = client.read();
while (client.available()) { // Store command char by char.
ServerResponse +=c;
c = client.read();
}
Serial.println("<<<" + ServerResponse);
ServerResponse="";
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}
void SendMsg(String m) {
client.println(m);
Serial.println(">>>" + m);
delay(wait); /* wait for a response */
GetResponse();
}