Go Down

Topic: Send email and send data to Emoncms in the same sketch (Read 991 times) previous topic - next topic

Hi,

I have 2 working sketch:
1° send data to Emoncms
2° sends mail if one sensor (like light) reads value lower than 25

I can not put together the sketcht. If i put together the sketch, arduino no works. Why?

Before my 2 sketch:
Code: [Select]
#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 = "mymail1";
String Recipient = "mymail2";
String Subject = "Messaggio inviato da Arduino!";
String Body = "message.";

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();
}


the second sketch

Code: [Select]
char foo; //without a simple variable declaration, use of #ifdef at the top of your code raises an error!

#include <SPI.h>
#include <Ethernet.h>

// Include Emon Library
#include "EmonLib.h"

//network configuration, WIRED or WIFI

  //if using WIRED
  byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x69, 0xD5};
 
  // fill in an available IP address on your network here,
  // for auto configuration:
  IPAddress ip(192, 168, 0, 120);
  IPAddress subnet(255, 255, 255, 0);
  IPAddress DNS(8, 8, 8, 8);
  IPAddress gw(192, 168, 0, 254);
 
  EthernetClient client;

//Calibrations
const int volt = 230;
const float ct_calibration = 28;
const float temp_offset = 1;

// Sensor pins
const int lightSensorPin = A0;
int tempSensorPin = A1;
const int currentSensor1Pin = A2;
const int currentSensor2Pin = A3;

float tempValue = 0;
float Irms1 = 0;
float Irms2 = 0;
int lightValue = 0;

// Create an Emon instance
EnergyMonitor emon1;
EnergyMonitor emon2;

//Emoncms configurations
char server[] = "emoncms.org"; // name address for emoncms.org
//IPAddress server(213, 138, 101, 177); // numeric IP for emoncms.org (no DNS)

String apikey = "myapikey"; //api key
int node = 0; //if 0, not used

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 3*1000; // delay between updates, in milliseconds

void setup() {
  // start serial port:
  Serial.begin(9600);

  // Display a welcome message
  Serial.println("Emoncms client starting...");

  emon1.current(currentSensor1Pin, ct_calibration);
  emon2.current(currentSensor2Pin, ct_calibration);

#ifdef WIFI
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while(true);
  }
 
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network.
    status = WiFi.begin(ssid, pass);
 
    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
#else
  if (!Ethernet.begin(mac)) {
    // if DHCP fails, start with a hard-coded address:
    Serial.println("Failed to get an IP address using DHCP, forcing manually");
    Ethernet.begin(mac, ip, dns, gw, subnet);
  }
#endif

  printStatus();
}

void loop() {
 
  // if there's incoming data from the net connection.
  // send it out the serial port. This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("Disconnecting...");
    client.stop();
  }
 
  // if you're not connected, and at least <postingInterval> milliseconds have
  // passed sinceyour last connection, then connect again and
  // send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
   
    //read sensors
    lightValue = analogRead(lightSensorPin);
    tempValue = analogRead(tempSensorPin);
    tempValue = (5 * tempValue * 100)/1024;
    Irms1 = emon1.calcIrms(1480);
    Irms2 = emon2.calcIrms(1480);
   
    //Print values (debug)
    Serial.println();
    Serial.print("Temp : ");
    Serial.print(tempValue);
    Serial.print(" ; Light : ");
    Serial.print(lightValue);
    Serial.print(" ; Power1 : ");
    Serial.print(Irms1 * volt - 10);
    Serial.print(" ; Power2 : ");
    Serial.println(Irms2 * volt - 10);
     
    //send values
    sendData();
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void sendData() {
  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("Connecting...");
    // send the HTTP GET request:
    client.print("GET /api/post?apikey=");
    client.print(apikey);
    if (node > 0) {
      client.print("&node=");
      client.print(node);
    }
    client.print("&json={temp");
    client.print(":");
    client.print(tempValue - temp_offset);
    client.print(",light:");
    client.print(lightValue);
    client.print(",power1:");
    client.print(Irms1 * volt - 10);
    client.print(",power2:");
    client.print(Irms2 * volt - 10);
    client.println("} HTTP/1.1");
    client.println("Host:emoncms.org");
    client.println("User-Agent: Arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection:
    Serial.println("Connection failed");
    Serial.println("Disconnecting...");
    client.stop();
  }
}


void printStatus() {
  #ifdef WIFI
    // print the SSID of the network you're attached to:
    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());

    // print your WiFi shield's IP address:
    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);

    // print the received signal strength:
    long rssi = WiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.print(" dBm");
  #else
    // print your local IP address:
    Serial.print("IP address: ");
    for (byte thisByte = 0; thisByte < 4; thisByte++) {
      // print the value of each byte of the IP address:
      Serial.print(Ethernet.localIP()[thisByte], DEC);
      Serial.print(".");
    }
  #endif
  Serial.println();
}

float getCelsius(int sensorValue) {
/*
created by Federico Vanzati for TinkerKit Thermistor Library
*/
  const static float ADCres = 1023.0;
  const static int Beta = 3950; // Beta parameter
  const static float Kelvin = 273.15; // 0°C = 273.15 K
  const static int Rb = 10000; // 10 kOhm
  const static float Ginf = 120.6685; // Ginf = 1/Rinf
 
  float Rthermistor = Rb * (ADCres / sensorValue - 1);
  float _temperatureC = Beta / (log( Rthermistor * Ginf )) ;
  return _temperatureC - Kelvin;
}

SurferTim

If you are using an Uno, you are probably running out of SRAM.

I recommend using one email send function and both code sections use that same email send function. I have an example email sketch in the playground. It uses an 'e' sent by the serial moitor as a trigger, but you could change that to suit your needs.
http://playground.arduino.cc/Code/Email

Call Ethernet.begin() in setup(), not in loop().

wildbill

For a first attempt at a fix, use the F macro to put all that text you're serial.printing in progmem.

yes. i use UNO.

if I match the code of "Email Sends" and another code to use arduino how web server, i have the same problem...

I try your suggestion.

if i use "Ethernet.begin() in setup()" don't work... =(

Go Up