Quelltext /~heha/ewa/Ofen/Arduino-180530.zip/unused/Überwachungseinheit/Überwachungseinheit.ino

/*
  SD card read/write

  This example shows how to read and write data to and from an SD card file
  The circuit:
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4

  created   Nov 2010
  by David A. Mellis
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

/***************************************************
  This is a library example for the MLX90614 Temp Sensor

  Designed specifically to work with the MLX90614 sensors in the
  adafruit shop
  ----> https://www.adafruit.com/products/1748
  ----> https://www.adafruit.com/products/1749

  These sensors use I2C to communicate, 2 pins are required to
  interface
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ****************************************************/


// A 10 uF CAPACITOR IS NECASSARY BETWEEN RESET AND GND TO PREVENT RESET AN INIT. OF SERIAL COMMUNICATION
// Do not use pin 4 and then. they are used for the ethernet shield
#include <SD.h>
#include <SPI.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <Temboo.h>
#include "TembooAccount.h" // Contains Temboo account information
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>

//************sensor variables***************

#define ONE_WIRE_BUS 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();

float T_Thyristor = 0;
float reading = 0;
float T_Raum = 0;
float T_Ofen = 0;
DeviceAddress tempDeviceAddress;
int ds18b20_wait = 0;
unsigned long t_lastheartbeat = 0;
volatile int counter = 0;
int counter_old = 0;
int relais_pin = 19;
const byte interruptPin_Rauchmelder = 3;
volatile int smoke_counter = 0;
int smoke_counter_old = 0;
unsigned long t_lastsmoke = 0;

//**************SD CARD VARIABLES**********************
File myFile;
int T_Thyristor_Max = 80;
float reading_Max = 1;
int T_Ofen_Max = 200;
int t_timeout = 10000;
int  resolution = 12;
unsigned long lastTempRequest = 0;
int  delayInMillis = 0;
String Emailto = "Maximilian_Geidel@yahoo.de";
//**************ETHERNET*****************

byte ethernetMACAddress[] = ETHERNET_SHIELD_MAC;
EthernetClient client;

int numRuns = 1;   // Execution count, so this doesn't run forever
int maxRuns = 10;   // Maximum number of times the Choreo should be executed
bool ethernet_connected = false;


//*******status variables*****
bool Stop_Execution = false;
int statPin = 13;
const byte interruptPin = 2;
int testPin = 5; //with GPIO 5 you can check the output
byte testPinState = LOW;
String Reason = "";
byte blinking = LOW;

void setup() {
  Serial.begin(9600);
  mlx.begin();


  //  *************SETUP OneWire Sensor DS18B20*************
  sensors.begin();
  sensors.getAddress(tempDeviceAddress, 0);
  sensors.setResolution(tempDeviceAddress, resolution);
  sensors.setWaitForConversion(false);
  sensors.requestTemperatures();

  //  *************SETUP Interrupt to check the looptime of the control unit*************
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), reset_timer, CHANGE);

  //  *************SETUP Interrupt to check the smoke detector from abus*************
  pinMode(interruptPin_Rauchmelder, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin_Rauchmelder), get_smoke, CHANGE);


  //  *************SETUP digital Outputs*************
  pinMode(relais_pin, OUTPUT);
  digitalWrite(relais_pin, LOW);
  pinMode(statPin, OUTPUT);
  digitalWrite(statPin, LOW);
  pinMode(testPin, OUTPUT);

  //  *************Read Config from SD Card*************

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
  }
  Serial.println("initialization done.");

  //******************************************GET CONFIG DATA FROM SD CARD*************************************

  myFile = SD.open("TThyr.txt");
  if (myFile) {
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      T_Thyristor_Max = myFile.parseInt();
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening T_Thyristor.txt, using init. value instead");
  }


  myFile = SD.open("TObj.txt");
  if (myFile) {
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      T_Ofen_Max = myFile.parseInt();
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening T_Object.txt, using init. value instead");
  }


  myFile = SD.open("VMICS.txt");
  if (myFile) {

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      reading_Max = myFile.parseFloat();
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening V_MICS.txt, using init. value instead");
  }


  myFile = SD.open("ttimeout.txt");
  if (myFile) {
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      t_timeout = myFile.parseInt();
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening t_timeout.txt, using init. value instead");
  }

  myFile = SD.open("Emailto.txt");
  if (myFile) {
    // read from the file until there's nothing else in it:
    Emailto = "";
    while (myFile.available()) {
      Emailto += char(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening Emailto.txt, using init. value instead");
  }

  //  Serial.println();
  //  Serial.println("T_Thyr_max=     " + String(T_Thyristor_Max));
  //  Serial.println("V_MICS_max=     " + String(reading_Max));
  //  Serial.println("T_Furn_max=     " + String(T_Ofen_Max));
  //  Serial.println("t_timeout=      " + String(t_timeout));
  //  Serial.println("Emailto=        " + Emailto);

  //  ****SETUP ETHERNET***************
  // For debugging, wait until the serial console is connected
  // delay(4000);


  Serial.print("DHCP:           ");
  if (Ethernet.begin(ethernetMACAddress) == 0) {
    Serial.println("FAIL");
    //    while (true);
  }
  else {
    Serial.println("OK");
  }


  Serial.println("Setup complete.");
  smoke_counter = 0;
}

void loop() {

  digitalWrite(testPin, !testPinState); //this is for testing the timeout
  testPinState = !testPinState;

  // ********************* Wait for Start of Control Unit**************************
  while (counter == 0) {
    //wait until first heartbeat of LabVIEW
    Serial.println("waiting for Control Unit to start.");
    delay(100);
    t_lastheartbeat = millis();
    smoke_counter = 0;
    digitalWrite(statPin, blinking);
    blinking = !blinking;


    digitalWrite(testPin, !testPinState); //this is for testing the timeout
    testPinState = !testPinState;
  }

  if (counter != counter_old) {
    t_lastheartbeat = millis();
    counter_old = counter;
  }

  // ********************* Check if timed out**************************
  if (millis() - t_lastheartbeat > t_timeout) {
    Stop_Execution = true;
    Reason = "Control Unit timed out.";
  }

  // ********************* Check Abus Smoke Detector*************************
  
  if (millis() - t_lastsmoke > 12000) {
    t_lastsmoke = millis();
    smoke_counter = 0;
  }

  if (smoke_counter >= 3) {
    Stop_Execution = true; //if the sensor blinks multiple times in a short range, the sensor detected smoke
    Reason = "Smoke detected from photoelectric sensor.";
  }

  // *********************Measure**************************
  T_Thyristor = sensors.getTempCByIndex(0);
  sensors.requestTemperatures();
  reading = analogRead(A0) * 5.0 / 1023.0;
  T_Raum = mlx.readAmbientTempC();
  T_Ofen = mlx.readObjectTempC();

  // *********************Check if values are in range**************************
  if (T_Thyristor > T_Thyristor_Max or reading > reading_Max or T_Ofen > T_Ofen_Max) {
    Stop_Execution = true; // if not in Range: stop execution, output of error message
    Reason = "Overtemperature or gas detected.";
  }

  // *********************create string of meassured data**************************
  String Daten = "T_Thyristor=\t" + String(T_Thyristor) + "*C" + "\n" +  "V_MICS5524=\t" + String(reading) + "*V" + "\n" +  "T_Ambient=\t" + String(T_Raum) + "*C" + "\n" +  "T_Furnance=\t" + String(T_Ofen) + "*C";
  Serial.print(Daten + "\r\n");

  // ********************* If Error: Set Relais AND SEND THE EMAIL **************************
  if (Stop_Execution == true) {
    digitalWrite(relais_pin, HIGH);
    Serial.print("relais closed.");
    digitalWrite(statPin, HIGH);
    TembooChoreo SendEmailChoreo(client);

    // Invoke the Temboo client
    SendEmailChoreo.begin();

    // Set Temboo account credentials
    SendEmailChoreo.setAccountName(TEMBOO_ACCOUNT);
    SendEmailChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    SendEmailChoreo.setAppKey(TEMBOO_APP_KEY);

    // Set Choreo inputs
    String FromAddressValue = "\"Ofen\" <sinterofen.arduino@gmail.com>";
    SendEmailChoreo.addInput("FromAddress", FromAddressValue);
    String UsernameValue = "sinterofen.arduino@gmail.com";
    SendEmailChoreo.addInput("Username", UsernameValue);
    String ToAddressValue = Emailto;
    SendEmailChoreo.addInput("ToAddress", ToAddressValue);
    String SubjectValue = "Error @ monitoring unit";
    SendEmailChoreo.addInput("Subject", SubjectValue);
    String MessageBodyValue = Reason;
    SendEmailChoreo.addInput("MessageBody", MessageBodyValue);
    String PasswordValue = "etuortxscekrhfzk";
    SendEmailChoreo.addInput("Password", PasswordValue);

    // Identify the Choreo to run
    SendEmailChoreo.setChoreo("/Library/Google/Gmail/SendEmail");

    // Run the Choreo; when results are available, print them to serial
    SendEmailChoreo.run();

    while (SendEmailChoreo.available()) {
      char c = SendEmailChoreo.read();
      Serial.print(c);
    }
    SendEmailChoreo.close();
    while (true);
  }

  delay(100);


}

void reset_timer() {
  counter++;
}

void get_smoke() {
  smoke_counter++;
}

Vorgefundene Kodierung: ASCII (7 bit)2