Problem when combining code from two sketches

I have a short sketch which allows a ESP32 to send temperature information from MAX6675 boards to a PC based coffee roasting software but the problem is it's a one way thing and I'd like the board to also be able to receive serial commands from the coffee roasting software so that I can control the heater from the software.

Obviously when you take a bit of this and that and have no programming knowledge, issues are bound to happen during compilation.

I believe it's not a very big issue but I'm really completely illiterate as far as coding is concerned so can folks here please help me with this?

/**************
 *  
 *  RoastESP32 - NodeMCU ESP32 thermocouple reader based on Arduino TC4
 *  *******************************************************************
 *  
 *  Resources
 *  =========
 *  TC-4
 *  https://github.com/FilePhil/TC4-Emulator
 *  https://github.com/greencardigan/TC4-shield/blob/master/applications/Artisan/aArtisan/trunk/src/aArtisan/commands.txt
 *  https://www.youtube.com/watch?v=0-Co-pXF2NM
 *  DHT22
 *  https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/
 *  MAX6675
 *  https://arduino.stackexchange.com/questions/37193/multiple-3-wire-spi-sensor-interfacing-with-arduino
 *  SPI
 *  https://www.thaieasyelec.com/article-wiki/embedded-electronics-application/09-espino32-spi.html
 *  
 *  Command sequence from artisan
 *  =============================
 *  CHAN;1200
 *  UNITS;C
 *  FILT;85;85;70;70
 *  READ
 *  https://www.home-barista.com/home-roasting/configuring-artisan-pid-t32351.html
 *  
 */

#include "user.h"
#include <SerialCommands.h> // https://github.com/ppedro74/Arduino-SerialCommands
#include <BluetoothSerial.h> // Classic Bluetooth
#include <DHT.h> // DHT22
#include <SPI.h> // MAX6675 over hardware SPI

// Bluetooth
#define ROAST_ESP32_BLUETOOTH_NAME "U.A.F. Mk. 2" //Bluetooth device name
BluetoothSerial SerialBT;

// DHT22
#define DHTPIN                 22 // GPIO22 -> DHT22 Output
#define DHTTYPE             DHT22
DHT dht(DHTPIN, DHTTYPE);
double humidity;
double ambientc;
double ambientf;

// MAX6675
#define TC1_CS                 32 // GPIO32 -> CS MAX6675[TC1]
#define TC2_CS                 33 // GPIO33 -> CS MAX6675[TC2]
#define TC3_CS                 25 // GPIO25 -> CS MAX6675[TC3]
#define TC4_CS                 26 // GPIO26 -> CS MAX6675[TC4]
/* Note: All MAX6675
 *  MAX6675 to  EPS32
 *  VCC     ->  3.3V
 *  GND     ->  GND
 *  SCK     ->  GPIO18/CLK
 *  SO      ->  GPIO19/MISO
 */

bool unit_F               = false;

#define SMA                     5
int sma_idx                   = 0;
bool sma_filled           = false;
double tc1s[SMA], tc2s[SMA], tc3s[SMA], tc4s[SMA];
double tc1, tc2, tc3, tc4;

// DHT22
void readDHT(){
  float h = dht.readHumidity();
  float c = dht.readTemperature();
  float f = dht.readTemperature(true);

  if(!isnan(h)){
    humidity = h;
  }
  if(!isnan(c)){
    ambientc = c;
  }
  if(!isnan(f)){
    ambientf = f;
  }
}

// MAX6675
double readCelsius(uint8_t cs) {
  uint16_t v;

  digitalWrite(cs, LOW);
  v = SPI.transfer(0x00);
  v <<= 8;
  v |= SPI.transfer(0x00);
  digitalWrite(cs, HIGH);

  if (v & 0x4) {
    // uh oh, no thermocouple attached!
    return NAN; 
  }

  v >>= 3;

  return v*0.25;
}

double readFahrenheit(uint8_t cs) {
  return readCelsius(cs) * 1.8 + 32;
}

bool readTCs(){
  tc1s[sma_idx] = readCelsius(TC1_CS);
  tc2s[sma_idx] = readCelsius(TC2_CS);
  tc3s[sma_idx] = readCelsius(TC3_CS);
  tc4s[sma_idx] = readCelsius(TC4_CS);
  if(!isnan(tc1s[sma_idx]) && !isnan(tc1s[sma_idx]) && !isnan(tc1s[sma_idx]) && !isnan(tc1s[sma_idx])){
    sma_idx++;
    if(sma_idx >= SMA){
      sma_filled = true;
      sma_idx = 0;
    }
    tc1 = 0;
    tc2 = 0;
    tc3 = 0;
    tc4 = 0;
    if(sma_filled){
      for(int i = 0; i<SMA; i++){
        tc1 += tc1s[i];
        tc2 += tc2s[i];
        tc3 += tc3s[i];
        tc4 += tc4s[i];
      }
      tc1 /= SMA;
      tc2 /= SMA;
      tc3 /= SMA;
      tc4 /= SMA;
    }
    return true;
  }
  return false;
}


// USB & Bluetooth SerialCommands
char serialbt_cmds_buffer[32];
char serial_cmds_buffer[32];
SerialCommands serialbt_cmds(&SerialBT, serialbt_cmds_buffer, sizeof(serialbt_cmds_buffer), "\n", ";");
SerialCommands serial_cmds(&Serial, serial_cmds_buffer, sizeof(serial_cmds_buffer), "\n", ";");

//This is the default handler, and gets called when no other command matches. 
void cmd_unrecognized(SerialCommands* sender, const char* cmd){
  sender->GetSerial()->print("Unrecognized command [");
  sender->GetSerial()->print(cmd);
  sender->GetSerial()->println("]");
}

void cmdSetChannel(SerialCommands* sender){
  sender->GetSerial()->println("#OK");
}
SerialCommand serialCmdSetChannel("CHAN", cmdSetChannel);

void cmdSetUnits(SerialCommands* sender){
  char* units = sender->Next();
  if (units[0] == 'F'){
    unit_F = true;
    sender->GetSerial()->println("#OK Farenheit");
  }else if (units[0] == 'C'){
    unit_F = false;
    sender->GetSerial()->println("#OK Celsius");
  }
}
SerialCommand serialCmdSetUnits("UNITS", cmdSetUnits);

void cmdSetFilter(SerialCommands* sender){
  sender->GetSerial()->println("#OK");
}
SerialCommand serialCmdSetFilter("FILT", cmdSetFilter);

void cmdRead(SerialCommands* sender){
  readDHT();
  if(unit_F){
    sender->GetSerial()->print(ambientf);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc1 * 1.8 + 32);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc2 * 1.8 + 32);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc3 * 1.8 + 32);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc4 * 1.8 + 32);
    sender->GetSerial()->print(",0.00,0.00,0.00"); // Heater, Fan, PID set value
  }else{
    sender->GetSerial()->print(ambientc);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc1);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc2);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc3);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc4);
    sender->GetSerial()->print(",0.00,0.00,0.00"); // Heater, Fan, PID set value
  }
  sender->GetSerial()->println("");
}
SerialCommand serialCmdRead("READ", cmdRead);


void setup(){
  // DHT22 for ambient and humidity
  pinMode(DHTPIN, INPUT);
  dht.begin();

  // Thermocouple (MAX6675 x4 over hardware SPI)
  pinMode(TC1_CS, OUTPUT);
  pinMode(TC2_CS, OUTPUT);
  pinMode(TC3_CS, OUTPUT);
  pinMode(TC4_CS, OUTPUT);
  digitalWrite(TC1_CS, HIGH);
  digitalWrite(TC2_CS, HIGH);
  digitalWrite(TC3_CS, HIGH);
  digitalWrite(TC4_CS, HIGH);
  SPI.begin();
  SPI.beginTransaction (SPISettings (1000000, MSBFIRST, SPI_MODE0));

//Parsing Serial Commands
void handleSerialCommand(){   

    if (SerialBT.available()>0){
        String msg = SerialBT.readStringUntil('\n');
//    if (Serial.available()>0){
 //       String msg = Serial.readStringUntil('\n');        //Serial.println(msg);   //use for debug

        if (msg.indexOf("CHAN;")== 0){  //connected to Artisan
            Started = true;
            SerialBT.print("#OK");
//            Serial.print("#OK");
        }
        else if (msg.indexOf("UNITS;")== 0){

            if (msg.substring(6,7)=="F"){   //Change to Farenheit
                unit_F = true;
//                Serial.println("#OK Farenheit");
                SerialBT.println("#OK Farenheit");
            }
            else if (msg.substring(6,7)=="C"){  //Change to Celsius
                unit_F = false;
 //               Serial.println("#OK Celsius");
                SerialBT.println("#OK Celsius");
            }

        }
        else if (msg.indexOf("OT1")==0){  //heater set command
//            Serial.print("#OK");
            SerialBT.print("#OK");
            // get OT1 value, convert to integer to save in dutyCycle
            dutyCycle = (msg.substring(4).toInt());
            //TelnetStream.print("dutyCycle is ");
            //TelnetStream.println (dutyCycle);
            if (ManualMode == false) {
              ManDutyCycle = dutyCycle;  //set manual duty cycle to last artisan duty cycle
              //set PWM for heater
              PWMDutyCycle = map(dutyCycle, 0, 100, 1, MaxDutyCycle);
              ledcWrite(HeatChannel, PWMDutyCycle);   
            }
        }
        
        else if (msg.indexOf("IO3")==0){  //fan power set command
            SerialBT.print("#OK");
            // get OT1 value, convert to integer to save in dutyCycle
            fanPwr = (msg.substring(4).toInt());
            FanDutyCycle = map(fanPwr, 0, 100, 0, 255);
            dacWrite(FanPin, FanDutyCycle);
        }  
             
        else if (msg.indexOf("READ")==0){   //Send Temps
           Command_READ();

       }
   }

}
  
  
  // USB Serial
  Serial.begin(115200);
  serial_cmds.SetDefaultHandler(cmd_unrecognized);
  serial_cmds.AddCommand(&serialCmdSetChannel);
  serial_cmds.AddCommand(&serialCmdSetUnits);
  serial_cmds.AddCommand(&serialCmdSetFilter);
  serial_cmds.AddCommand(&serialCmdRead);
  // Bluetooth Serial
  SerialBT.begin(ROAST_ESP32_BLUETOOTH_NAME);
  serialbt_cmds.SetDefaultHandler(cmd_unrecognized);
  serialbt_cmds.AddCommand(&serialCmdSetChannel);
  serialbt_cmds.AddCommand(&serialCmdSetUnits);
  serialbt_cmds.AddCommand(&serialCmdSetFilter);
  serialbt_cmds.AddCommand(&serialCmdRead);

  sma_idx = 0;
  sma_filled = true;
  tc1 = 0;
  tc2 = 0;
  tc3 = 0;
  tc4 = 0;
}

void loop(){
  serialbt_cmds.ReadSerial();
  serial_cmds.ReadSerial();
  if(readTCs()){
    delay(200);
  }
}

The code that I've tried to integrate into the sketch is this part.

//Parsing Serial Commands
void handleSerialCommand(){   

    if (SerialBT.available()>0){
        String msg = SerialBT.readStringUntil('\n');
//    if (Serial.available()>0){
 //       String msg = Serial.readStringUntil('\n');        //Serial.println(msg);   //use for debug

        if (msg.indexOf("CHAN;")== 0){  //connected to Artisan
            Started = true;
            SerialBT.print("#OK");
//            Serial.print("#OK");
        }
        else if (msg.indexOf("UNITS;")== 0){

            if (msg.substring(6,7)=="F"){   //Change to Farenheit
                unit_F = true;
//                Serial.println("#OK Farenheit");
                SerialBT.println("#OK Farenheit");
            }
            else if (msg.substring(6,7)=="C"){  //Change to Celsius
                unit_F = false;
 //               Serial.println("#OK Celsius");
                SerialBT.println("#OK Celsius");
            }

        }
        else if (msg.indexOf("OT1")==0){  //heater set command
//            Serial.print("#OK");
            SerialBT.print("#OK");
            // get OT1 value, convert to integer to save in dutyCycle
            dutyCycle = (msg.substring(4).toInt());
            //TelnetStream.print("dutyCycle is ");
            //TelnetStream.println (dutyCycle);
            if (ManualMode == false) {
              ManDutyCycle = dutyCycle;  //set manual duty cycle to last artisan duty cycle
              //set PWM for heater
              PWMDutyCycle = map(dutyCycle, 0, 100, 1, MaxDutyCycle);
              ledcWrite(HeatChannel, PWMDutyCycle);   
            }
        }
        
        else if (msg.indexOf("IO3")==0){  //fan power set command
            SerialBT.print("#OK");
            // get OT1 value, convert to integer to save in dutyCycle
            fanPwr = (msg.substring(4).toInt());
            FanDutyCycle = map(fanPwr, 0, 100, 0, 255);
            dacWrite(FanPin, FanDutyCycle);
        }  
             
        else if (msg.indexOf("READ")==0){   //Send Temps
           Command_READ();

       }
   }

}

And the error message received during compilation is :

Compilation error: a function-definition is not allowed here before '{' token

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) on your code then look in setup() where you inserted the handleSerialCommand(). You are missing a } to close out the setup() function before the handleSerialCommand() function definition.

You inserted the new function into the middle of the setup() function so that is why you get that error. You can't define a function inside another function.

In the future, please include the entire text of the error message. Paraphrasing leaves out important information, ie. the line number of the error.

1 Like

It would be useful to see the code you have started with, two sketches each of which compile and do what you need, as well as a full description of what you intend to accomplish by combining them.

If indeed you started with full sketches not literally a bunch of code grabbed from its context and presented here.

Also it would be useful to have more information on the nature of the communication between whatever it is you want to get talking.

And a description of how Bluetooth enters into this, or does it? A high level sales type description of your invention if we may call it that.

Do you have a link to the operating specs for the roaster?

a7

Your first code has several errors.
The first: The setup() function missing the closing brace "}".
Next, many variables are missing from the definition, such as those that I was able to identify in the list below.
There must be more errors, but I stopped here for now.

int dutyCycle;
int ManDutyCycle;
int PWMDutyCycle;
int MaxDutyCycle;
int HeatChannel;
int fanPwr;
int FanDutyCycle;
int FanPin;
bool Started;
bool ManualMode;

Those parameters are defined inside the user.h file.

It was the author of the code from which I'd taken the heater control bit who'd told me that that's the relevant section to copy but that's as far as he was willing to go.

Post this file, here.

Here you go. This is not the original user.h file but the one at my end because I've commented out a lot of the stuff which I don't need.

// user.h
// This file contains user definable compiler directives

// *************************************************************************************
// NOTE TO USERS: the following parameters should be
// be reviewed to suit your preferences and hardware setup.  
// First, load and edit this sketch in the Arduino IDE.
// Next compile the sketch and upload it to the Arduino.


////////////////////
// Roasting software
// Comment out all if using TC4 stand alone
//#define ROASTLOGGER
//#define ARTISAN
//#define ANDROID


//define output pins
#define HeatPin 27    //gpio pin for pwm to heater SSR
//#define FanPin 25      //gpio pin to output analog value to set fan power

// set Heater PWM properties
const int freq = 1;
const int HeatChannel = 1;    //use PWM channel 0 for heater
const int resolution = 10;  // changing resolution changes max duty cycle
const int MaxDutyCycle = 1023; //max duty cycle for resolution = 10, 2exp10-1 = 1024-1 = 1023

////////////////////
// Temperature Unit
#define CELSIUS // controls only the initial conditions.  Comment out for F.


////////////////////
// BAUD Rate for serial communications
#define BAUD 115200

#define BT_FILTER 10 // filtering level (percent) for displayed BT
#define ET_FILTER 10 // filtering level (percent) for displayed ET

// Thermocouple inputs
#define NC 4 // maximum number of physical channels on the TC4

// ambient sensor should be stable, so quick variations are probably noise -- filter heavily
//#define AMB_FILTER 70 // 70% filtering on ambient sensor readings

// *************************************************************************************
////////////////////
// Heater and Fan Limits/Options
//#define MIN_OT1 0 // Set output % for lower limit for OT1.  0% power will always be available
//#define MAX_OT1 100 // Set output % for upper limit for OT1

//#define MIN_OT2 0 // Set output % for lower limit for OT2.  0% power will always be available
//#define MAX_OT2 100 // Set output % for upper limit for OT2

//#define MIN_IO3 0 // Set output % for lower limit for IO3.  0% power will always be available
//#define MAX_IO3 100  // Set output % for upper limit for IO3

// cut power to Heater if fan duty is less than HTR_CUTOFF_FAN_VAL (to protect heater in air roaster). Set to 0 for no cutoff
//#define HTR_CUTOFF_FAN_VAL 0

//#define FAN_AUTO_COOL 100 // Set fan output duty for auto cool when using PID;STOP command

So I took the heater control code out which reverted the temperature monitoring code to its original form, compiled to check whether I'd messed with anything else and it compiled fine. Then I inserted the heater control code at the end and this was the error messages I received.

/**************
 *  
 *  RoastESP32 - NodeMCU ESP32 thermocouple reader based on Arduino TC4
 *  *******************************************************************
 *  
 *  Resources
 *  =========
 *  TC-4
 *  https://github.com/FilePhil/TC4-Emulator
 *  https://github.com/greencardigan/TC4-shield/blob/master/applications/Artisan/aArtisan/trunk/src/aArtisan/commands.txt
 *  https://www.youtube.com/watch?v=0-Co-pXF2NM
 *  DHT22
 *  https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/
 *  MAX6675
 *  https://arduino.stackexchange.com/questions/37193/multiple-3-wire-spi-sensor-interfacing-with-arduino
 *  SPI
 *  https://www.thaieasyelec.com/article-wiki/embedded-electronics-application/09-espino32-spi.html
 *  
 *  Command sequence from artisan
 *  =============================
 *  CHAN;1200
 *  UNITS;C
 *  FILT;85;85;70;70
 *  READ
 *  https://www.home-barista.com/home-roasting/configuring-artisan-pid-t32351.html
 *  
 */

#include "user.h"
#include <SerialCommands.h>   // https://github.com/ppedro74/Arduino-SerialCommands
#include <BluetoothSerial.h>  // Classic Bluetooth
#include <DHT.h>              // DHT22
#include <SPI.h>              // MAX6675 over hardware SPI

// Bluetooth
#define ROAST_ESP32_BLUETOOTH_NAME "U.A.F. Mk. 2"  //Bluetooth device name
BluetoothSerial SerialBT;

// DHT22
#define DHTPIN 22  // GPIO22 -> DHT22 Output
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
double humidity;
double ambientc;
double ambientf;

// MAX6675
#define TC1_CS 32  // GPIO32 -> CS MAX6675[TC1]
#define TC2_CS 33  // GPIO33 -> CS MAX6675[TC2]
#define TC3_CS 25  // GPIO25 -> CS MAX6675[TC3]
#define TC4_CS 26  // GPIO26 -> CS MAX6675[TC4]
/* Note: All MAX6675
 *  MAX6675 to  EPS32
 *  VCC     ->  3.3V
 *  GND     ->  GND
 *  SCK     ->  GPIO18/CLK
 *  SO      ->  GPIO19/MISO
 */

bool unit_F = false;

#define SMA 5
int sma_idx = 0;
bool sma_filled = false;
double tc1s[SMA], tc2s[SMA], tc3s[SMA], tc4s[SMA];
double tc1, tc2, tc3, tc4;

// DHT22
void readDHT() {
  float h = dht.readHumidity();
  float c = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (!isnan(h)) {
    humidity = h;
  }
  if (!isnan(c)) {
    ambientc = c;
  }
  if (!isnan(f)) {
    ambientf = f;
  }
}

// MAX6675
double readCelsius(uint8_t cs) {
  uint16_t v;

  digitalWrite(cs, LOW);
  v = SPI.transfer(0x00);
  v <<= 8;
  v |= SPI.transfer(0x00);
  digitalWrite(cs, HIGH);

  if (v & 0x4) {
    // uh oh, no thermocouple attached!
    return NAN;
  }

  v >>= 3;

  return v * 0.25;
}

double readFahrenheit(uint8_t cs) {
  return readCelsius(cs) * 1.8 + 32;
}

bool readTCs() {
  tc1s[sma_idx] = readCelsius(TC1_CS);
  tc2s[sma_idx] = readCelsius(TC2_CS);
  tc3s[sma_idx] = readCelsius(TC3_CS);
  tc4s[sma_idx] = readCelsius(TC4_CS);
  if (!isnan(tc1s[sma_idx]) && !isnan(tc1s[sma_idx]) && !isnan(tc1s[sma_idx]) && !isnan(tc1s[sma_idx])) {
    sma_idx++;
    if (sma_idx >= SMA) {
      sma_filled = true;
      sma_idx = 0;
    }
    tc1 = 0;
    tc2 = 0;
    tc3 = 0;
    tc4 = 0;
    if (sma_filled) {
      for (int i = 0; i < SMA; i++) {
        tc1 += tc1s[i];
        tc2 += tc2s[i];
        tc3 += tc3s[i];
        tc4 += tc4s[i];
      }
      tc1 /= SMA;
      tc2 /= SMA;
      tc3 /= SMA;
      tc4 /= SMA;
    }
    return true;
  }
  return false;
}


// USB & Bluetooth SerialCommands
char serialbt_cmds_buffer[32];
char serial_cmds_buffer[32];
SerialCommands serialbt_cmds(&SerialBT, serialbt_cmds_buffer, sizeof(serialbt_cmds_buffer), "\n", ";");
SerialCommands serial_cmds(&Serial, serial_cmds_buffer, sizeof(serial_cmds_buffer), "\n", ";");

//This is the default handler, and gets called when no other command matches.
void cmd_unrecognized(SerialCommands* sender, const char* cmd) {
  sender->GetSerial()->print("Unrecognized command [");
  sender->GetSerial()->print(cmd);
  sender->GetSerial()->println("]");
}

void cmdSetChannel(SerialCommands* sender) {
  sender->GetSerial()->println("#OK");
}
SerialCommand serialCmdSetChannel("CHAN", cmdSetChannel);

void cmdSetUnits(SerialCommands* sender) {
  char* units = sender->Next();
  if (units[0] == 'F') {
    unit_F = true;
    sender->GetSerial()->println("#OK Farenheit");
  } else if (units[0] == 'C') {
    unit_F = false;
    sender->GetSerial()->println("#OK Celsius");
  }
}
SerialCommand serialCmdSetUnits("UNITS", cmdSetUnits);

void cmdSetFilter(SerialCommands* sender) {
  sender->GetSerial()->println("#OK");
}
SerialCommand serialCmdSetFilter("FILT", cmdSetFilter);

void cmdRead(SerialCommands* sender) {
  readDHT();
  if (unit_F) {
    sender->GetSerial()->print(ambientf);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc1 * 1.8 + 32);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc2 * 1.8 + 32);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc3 * 1.8 + 32);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc4 * 1.8 + 32);
    sender->GetSerial()->print(",0.00,0.00,0.00");  // Heater, Fan, PID set value
  } else {
    sender->GetSerial()->print(ambientc);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc1);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc2);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc3);
    sender->GetSerial()->print(",");
    sender->GetSerial()->print(tc4);
    sender->GetSerial()->print(",0.00,0.00,0.00");  // Heater, Fan, PID set value
  }
  sender->GetSerial()->println("");
}
SerialCommand serialCmdRead("READ", cmdRead);


void setup() {
  // DHT22 for ambient and humidity
  pinMode(DHTPIN, INPUT);
  dht.begin();

  // Thermocouple (MAX6675 x4 over hardware SPI)
  pinMode(TC1_CS, OUTPUT);
  pinMode(TC2_CS, OUTPUT);
  pinMode(TC3_CS, OUTPUT);
  pinMode(TC4_CS, OUTPUT);
  digitalWrite(TC1_CS, HIGH);
  digitalWrite(TC2_CS, HIGH);
  digitalWrite(TC3_CS, HIGH);
  digitalWrite(TC4_CS, HIGH);
  SPI.begin();
  SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));


  // USB Serial
  Serial.begin(115200);
  serial_cmds.SetDefaultHandler(cmd_unrecognized);
  serial_cmds.AddCommand(&serialCmdSetChannel);
  serial_cmds.AddCommand(&serialCmdSetUnits);
  serial_cmds.AddCommand(&serialCmdSetFilter);
  serial_cmds.AddCommand(&serialCmdRead);
  // Bluetooth Serial
  SerialBT.begin(ROAST_ESP32_BLUETOOTH_NAME);
  serialbt_cmds.SetDefaultHandler(cmd_unrecognized);
  serialbt_cmds.AddCommand(&serialCmdSetChannel);
  serialbt_cmds.AddCommand(&serialCmdSetUnits);
  serialbt_cmds.AddCommand(&serialCmdSetFilter);
  serialbt_cmds.AddCommand(&serialCmdRead);

  sma_idx = 0;
  sma_filled = true;
  tc1 = 0;
  tc2 = 0;
  tc3 = 0;
  tc4 = 0;
}

void loop() {
  serialbt_cmds.ReadSerial();
  serial_cmds.ReadSerial();
  if (readTCs()) {
    delay(200);
  }
}

//Parsing Serial Commands
void handleSerialCommand() {

  if (SerialBT.available() > 0) {
    String msg = SerialBT.readStringUntil('\n');
    //    if (Serial.available()>0){
    //       String msg = Serial.readStringUntil('\n');        //Serial.println(msg);   //use for debug

    if (msg.indexOf("CHAN;") == 0) {  //connected to Artisan
      Started = true;
      SerialBT.print("#OK");
      //            Serial.print("#OK");
    } else if (msg.indexOf("UNITS;") == 0) {

      if (msg.substring(6, 7) == "F") {  //Change to Farenheit
        unit_F = true;
        //                Serial.println("#OK Farenheit");
        SerialBT.println("#OK Farenheit");
      } else if (msg.substring(6, 7) == "C") {  //Change to Celsius
        unit_F = false;
        //               Serial.println("#OK Celsius");
        SerialBT.println("#OK Celsius");
      }

    } else if (msg.indexOf("OT1") == 0) {  //heater set command
                                           //            Serial.print("#OK");
      SerialBT.print("#OK");
      // get OT1 value, convert to integer to save in dutyCycle
      dutyCycle = (msg.substring(4).toInt());
      //TelnetStream.print("dutyCycle is ");
      //TelnetStream.println (dutyCycle);
      if (ManualMode == false) {
        ManDutyCycle = dutyCycle;  //set manual duty cycle to last artisan duty cycle
        //set PWM for heater
        PWMDutyCycle = map(dutyCycle, 0, 100, 1, MaxDutyCycle);
        ledcWrite(HeatChannel, PWMDutyCycle);
      }
    }

    else if (msg.indexOf("IO3") == 0) {  //fan power set command
      SerialBT.print("#OK");
      // get OT1 value, convert to integer to save in dutyCycle
      fanPwr = (msg.substring(4).toInt());
      FanDutyCycle = map(fanPwr, 0, 100, 0, 255);
      dacWrite(FanPin, FanDutyCycle);
    }

    else if (msg.indexOf("READ") == 0) {  //Send Temps
      Command_READ();
    }
  }
}

C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino: In function 'void handleSerialCommand()':
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:266:13: error: 'Started' was not declared in this scope
             Started = true;
             ^~~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:266:13: note: suggested alternative: 'btStarted'
             Started = true;
             ^~~~~~~
             btStarted
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:288:13: error: 'dutyCycle' was not declared in this scope
             dutyCycle = (msg.substring(4).toInt());
             ^~~~~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:288:13: note: suggested alternative: 'MaxDutyCycle'
             dutyCycle = (msg.substring(4).toInt());
             ^~~~~~~~~
             MaxDutyCycle
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:291:17: error: 'ManualMode' was not declared in this scope
             if (ManualMode == false) {
                 ^~~~~~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:291:17: note: suggested alternative: 'pinMode'
             if (ManualMode == false) {
                 ^~~~~~~~~~
                 pinMode
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:292:15: error: 'ManDutyCycle' was not declared in this scope
               ManDutyCycle = dutyCycle;  //set manual duty cycle to last artisan duty cycle
               ^~~~~~~~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:292:15: note: suggested alternative: 'MaxDutyCycle'
               ManDutyCycle = dutyCycle;  //set manual duty cycle to last artisan duty cycle
               ^~~~~~~~~~~~
               MaxDutyCycle
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:294:15: error: 'PWMDutyCycle' was not declared in this scope
               PWMDutyCycle = map(dutyCycle, 0, 100, 1, MaxDutyCycle);
               ^~~~~~~~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:294:15: note: suggested alternative: 'MaxDutyCycle'
               PWMDutyCycle = map(dutyCycle, 0, 100, 1, MaxDutyCycle);
               ^~~~~~~~~~~~
               MaxDutyCycle
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:302:13: error: 'fanPwr' was not declared in this scope
             fanPwr = (msg.substring(4).toInt());
             ^~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:302:13: note: suggested alternative: 'rand_r'
             fanPwr = (msg.substring(4).toInt());
             ^~~~~~
             rand_r
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:303:13: error: 'FanDutyCycle' was not declared in this scope
             FanDutyCycle = map(fanPwr, 0, 100, 0, 255);
             ^~~~~~~~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:303:13: note: suggested alternative: 'MaxDutyCycle'
             FanDutyCycle = map(fanPwr, 0, 100, 0, 255);
             ^~~~~~~~~~~~
             MaxDutyCycle
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:304:22: error: 'FanPin' was not declared in this scope
             dacWrite(FanPin, FanDutyCycle);
                      ^~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:304:22: note: suggested alternative: 'asin'
             dacWrite(FanPin, FanDutyCycle);
                      ^~~~~~
                      asin
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:308:12: error: 'Command_READ' was not declared in this scope
            Command_READ();
            ^~~~~~~~~~~~

exit status 1

Compilation error: 'Started' was not declared in this scope

Kill the first error & compile again. Start isn't defined or used in the main code or user.h except that one line where it's set true. I'd comment that line out and see if anything else complains about it when you re-compile.

1 Like

It opens up another can of worms though. :sleepy:

C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino: In function 'void handleSerialCommand()':
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:284:7: error: 'dutyCycle' was not declared in this scope
       dutyCycle = (msg.substring(4).toInt());
       ^~~~~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:284:7: note: suggested alternative: 'MaxDutyCycle'
       dutyCycle = (msg.substring(4).toInt());
       ^~~~~~~~~
       MaxDutyCycle
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:287:11: error: 'ManualMode' was not declared in this scope
       if (ManualMode == false) {
           ^~~~~~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:287:11: note: suggested alternative: 'pinMode'
       if (ManualMode == false) {
           ^~~~~~~~~~
           pinMode
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:288:9: error: 'ManDutyCycle' was not declared in this scope
         ManDutyCycle = dutyCycle;  //set manual duty cycle to last artisan duty cycle
         ^~~~~~~~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:288:9: note: suggested alternative: 'MaxDutyCycle'
         ManDutyCycle = dutyCycle;  //set manual duty cycle to last artisan duty cycle
         ^~~~~~~~~~~~
         MaxDutyCycle
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:290:9: error: 'PWMDutyCycle' was not declared in this scope
         PWMDutyCycle = map(dutyCycle, 0, 100, 1, MaxDutyCycle);
         ^~~~~~~~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:290:9: note: suggested alternative: 'MaxDutyCycle'
         PWMDutyCycle = map(dutyCycle, 0, 100, 1, MaxDutyCycle);
         ^~~~~~~~~~~~
         MaxDutyCycle
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:298:7: error: 'fanPwr' was not declared in this scope
       fanPwr = (msg.substring(4).toInt());
       ^~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:298:7: note: suggested alternative: 'rand_r'
       fanPwr = (msg.substring(4).toInt());
       ^~~~~~
       rand_r
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:299:7: error: 'FanDutyCycle' was not declared in this scope
       FanDutyCycle = map(fanPwr, 0, 100, 0, 255);
       ^~~~~~~~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:299:7: note: suggested alternative: 'MaxDutyCycle'
       FanDutyCycle = map(fanPwr, 0, 100, 0, 255);
       ^~~~~~~~~~~~
       MaxDutyCycle
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:300:16: error: 'FanPin' was not declared in this scope
       dacWrite(FanPin, FanDutyCycle);
                ^~~~~~
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:300:16: note: suggested alternative: 'asin'
       dacWrite(FanPin, FanDutyCycle);
                ^~~~~~
                asin
C:\Users\XiaoShu\Documents\Arduino\RoastESP32PID\RoastESP32PID.ino:304:7: error: 'Command_READ' was not declared in this scope
       Command_READ();
       ^~~~~~~~~~~~

exit status 1

Compilation error: 'dutyCycle' was not declared in this scope

There are a bunch of similar errors there - variables like ManualMode and dutyCycle are not declared. Try and find them in the code that contained handleSerialCommand and add them in.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.