Virtuino Bluetooth und Wifi mit Thingspeak gleichzeitig

/*========= VirtuinoBluetooth Class methods  
*  vPinMode(int pin, int state)                                  set pin as digital OUTPUT or digital INPUT.  (Insteed default pinMode method
*
*========= Virtuino General methods  
*  void vDigitalMemoryWrite(int digitalMemoryIndex, int value)   write a value to a Virtuino digital memory   (digitalMemoryIndex=0..31, value range = 0 or 1)
*  int  vDigitalMemoryRead(int digitalMemoryIndex)               read  the value of a Virtuino digital memory (digitalMemoryIndex=0..31, returned value range = 0 or 1)
*  void vMemoryWrite(int analogMemoryIndex, float value)         write a value to Virtuino float memory       (memoryIndex=0..31, value range as float value)
*  float vMemoryRead(int analogMemoryIndex)                      read the value of  Virtuino analog memory    (analogMemoryIndex=0..31, returned value range = 0..1023)
*  run()                                                         neccesary command to communicate with Virtuino android app  (on start of void loop)
*  void vDelay(long milliseconds);                               Pauses the program (without block communication) for the amount of time (in miliseconds) specified as parameter
*  int getPinValue(int pin)                                      read the value of a Pin. Usefull for PWM pins
*/
#include <Adafruit_MAX31865.h>
#include "VirtuinoBluetooth.h"                   // virtuino library 1.47 or highest
// Code to use SoftwareSerial
//#include <SoftwareSerial.h>                              //  Disable this line if you want to use hardware serial 
//SoftwareSerial bluetoothSerial =  SoftwareSerial(2,3);   // arduino RX pin=2  arduino TX pin=3    connect the arduino RX pin to bluetooth module TX pin   -  connect the arduino TX pin to bluetooth module RX pin.  Disable this line if you want to use hardware serial 
//VirtuinoBluetooth virtuino(bluetoothSerial,9600);       // Set SoftwareSerial baud rate.  -  Disable this line if you want to use hardware serial 

 // Code to use HardwareSerial
  VirtuinoBluetooth virtuino(Serial1);            // enable this line and disable all SoftwareSerial lines
                                                    // Open VirtuinoBluetooth.h file on the virtuino library folder ->  disable the line: #define BLUETOOTH_USE_SOFTWARE_SERIAL 
                                                    // Connect HC-05 module to Arduino (MEGA or DUE) Serial1. (pins: 18,19)
 

//====== Sensor 1 Teich

// Use software SPI: CS, DI, DO, CLK
Adafruit_MAX31865 thermo1 = Adafruit_MAX31865(22, 23, 24, 25);
// use hardware SPI, just pass in the CS pin
//Adafruit_MAX31865 thermo = Adafruit_MAX31865(22);

//====== Sensor 2 Umgebungsluft

// Use software SPI: CS, DI, DO, CLK
Adafruit_MAX31865 thermo2 = Adafruit_MAX31865(26, 27, 28, 29);
// use hardware SPI, just pass in the CS pin
//Adafruit_MAX31865 thermo = Adafruit_MAX31865(26);


// The value of the Rref resistor. Use 430.0 for PT100 and 4300.0 for PT1000
#define RREF      4301.0
// The 'nominal' 0-degrees-C resistance of the sensor
// 100.0 for PT100, 1000.0 for PT1000
#define RNOMINAL  1000.0



String apiKey = "";     // replace with your channel's thingspeak WRITE API key

String ssid="";    // Wifi network SSID
String password ="";  // Wifi network password

boolean DEBUG=true;

//======================================================================== showResponce
void showResponse(int waitTime){
    long t=millis();
    char c;
    while (t+waitTime>millis()){
      if (Serial2.available()){
        c=Serial2.read();
        if (DEBUG) Serial.print(c);
      }
    }
                   
}

//========================================================================
boolean thingSpeakWrite(float value1, float value2){
  String cmd = "AT+CIPSTART=\"TCP\",\"";                  // TCP connection
  cmd += "184.106.153.149";                               // api.thingspeak.com
  cmd += "\",80";
  Serial2.println(cmd);
  if (DEBUG) Serial.println(cmd);
  if(Serial2.find("Error")){
    if (DEBUG) Serial.println("AT+CIPSTART error");
    return false;
  }
  
  
  String getStr = "GET /update?api_key=";   // prepare GET string
  getStr += apiKey;
  
  getStr +="&field1=";
  getStr += String(value1);
  getStr +="&field2=";
  getStr += String(value2);
  // getStr +="&field3=";
  // getStr += String(value3);
  // ...
  getStr += "\r\n\r\n";

  // send data length
  cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  Serial2.println(cmd);
  if (DEBUG)  Serial.println(cmd);
  
  //delay(100);
  if(Serial2.find(">")){
    Serial2.print(getStr);
    if (DEBUG)  Serial.print(getStr);
  }
  else{
    Serial2.println("AT+CIPCLOSE");
    // alert user
    if (DEBUG)   Serial.println("AT+CIPCLOSE");
    return false;
  }
  return true;
}

 
 //======================================================================== setup
 //========================================================================
void setup(){
  DEBUG=true;           // enable debug serial
  Serial.begin(9600); 
  Serial.begin(115200);  // start Sensor Converter PT1000
  Serial.println("PT1000 Start");
  Serial1.begin(9600);    // start serial1 HC-05 port. Enable this line only if you use hardware serial (Mega, Due, etc)
  Serial2.begin(115200);
  virtuino.DEBUG=true;               // set this value TRUE to enable the serial monitor status
 

  thermo1.begin(MAX31865_2WIRE);  // set to 2WIRE or 4WIRE as necessary
  thermo2.begin(MAX31865_2WIRE);  // set to 2WIRE or 4WIRE as necessary
  

  Serial2.println("AT+CWMODE=1");   // set esp8266 as client
  showResponse(1000);

  Serial2.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");  // set your home router SSID and password
  showResponse(5000);

   if (DEBUG)  Serial.println("Setup completed");

  pinMode(2, OUTPUT);            // Relais 1 Motor + linker Deckel On Virtuino panel add a button to control this pin
  pinMode(3, OUTPUT);            // Relais 2 Motor - linker Deckel On Virtuino panel add a button to control this pin
  pinMode(4, OUTPUT);           // Relais 3 Motor + rechter Deckel On Virtuino panel add a button to control this pin
  pinMode(5, OUTPUT);           // Relais 4 Motor - rechter Deckel On Virtuino panel add a button to control this pin
  pinMode(6, OUTPUT);           // Relais 5 Hauptpumpe Luftheber On Virtuino panel add a button to control this pin
  pinMode(7, OUTPUT);           // Relais 6 Luftpumpe On Virtuino panel add a button to control this pin
  pinMode(8, OUTPUT);           // Relais 7 UVC Lampe On Virtuino panel add a button to control this pin
  pinMode(9, OUTPUT);           // Relais 8 Pumpe On Virtuino panel add a button to control this pin    
}

//======================================================================== setup
void readTemperatures(){

     uint16_t rtd = thermo1.readRTD();
   float ratio1 = rtd;
   ratio1 /= 32768;
   Serial.print("Temperature1 = "); Serial.println(thermo1.temperature(RNOMINAL, RREF));

   uint16_t rtd2 = thermo2.readRTD();
   float ratio2 = rtd;
   ratio2 /= 32768;
   Serial.print("Temperature2 = "); Serial.println(thermo2.temperature(RNOMINAL, RREF));  

      float temperature1 = (thermo1.temperature(RNOMINAL, RREF));
      float temperature2 = (thermo2.temperature(RNOMINAL, RREF)); 
          if (isnan(temperature1) || isnan(temperature2)) {
          if (DEBUG) Serial.println("Failed to read from PT1000");
      }
      else {
            thingSpeakWrite(temperature1,temperature2);                                      // Write values to thingspeak
      }
          virtuino.vMemoryWrite(0,temperature1);    // write temperature 1 to virtual pin V0. On Virtuino panel add a value display or an analog instrument to pin V0
          virtuino.vMemoryWrite(1,temperature2);    // write temperature 1 to virtual pin V1. On Virtuino panel add a value display or an analog instrument to pin V1
           
        
}  

      

//======================================================================== setup
//========================================================================
void loop(void){
  virtuino.run();               //  neccesary command to communicate with Virtuino android app

    //------ enter your loop code below here
    //------ avoid to use delay() function in your code. Use the command virtuino.vDelay() instead of delay()
    //------ your code .....

  readTemperatures();           // Dont' read the sensors values every cicle
  virtuino.vDelay(3000);        // Add a delay at least 1 second long. 
 
}