Compile error: invalid conversion from 'const char*' to 'char' [-fpermissive] [SOLVED]

As a neophtye I admit to only having a cursory understanding of the C language. I get by, barely. Lol.
I have some sort of a mental block understanding this compiler error. If i add "char" to the Alm_Relay[4] variable in the void loop the compiler error changes to "unused variable."
I have a C Programming Absolute Beginners Guide and have looked up online the "cons char*
to char" snafu but still no eureka moment for me.
Any explanation would be appreciated despite my feeling rather obtuse right now.

   char Alm_Relay[4] = "OFF";     // defined in my constants.h file


void Alm_Relay_ON()                      // Building alarm relay ON command
    {                                        // TWO triggers: temp OR pressure
     if (cycle_OFF_timer == false)
     {
     digitalWrite(relay1_outPin, HIGH);
     }
      if (digitalRead(relay1_outPin) == HIGH)
      {
       Alm_Relay[4] = "ON";             //  Building Alm relay 'ON' status
       Serial.println("      Building Alarm Relay ON");      // turn buzzer alarm OFF after X mins; cycle OFF/ON
       currentMillis = millis();  //get current "time" (actually the number of milliseconds since program started)
       if (currentMillis - startMillis >= period_1)  //check whether the Buzzer ON period has elapsed
       {
        cycle_OFF_timer = true;
        void Alm_Relay_OFF();
        startMillis = currentMillis;  
       }
      }
     }
    void Alm_Relay_OFF()                 //  Building alarm relay OFF command
    {
     digitalWrite(relay1_outPin, LOW);   
     Serial.println("      Building Alarm Relay OFF");
                                                   //  char Temp_Alm[4] = "OFF";
                                                  //   char Pressure_Alm[4] = "OFF";
                                                  //   char Alm_Relay[4] = "OFF";
  
    if (digitalRead(relay1_outPin) == LOW)
    {
     Alm_Relay[4] = "OFF";               // Building Alm relay 'OFF' status
     currentMillis = millis();  
     if (currentMillis - startMillis >= period_2)  //test whether the Buzzer OFF period has elapsed
     {
      cycle_OFF_timer = false;
      startMillis = currentMillis;  
     } 

WARNING: library StackString claims to run on avr architecture(s) and may be incompatible with your current board which runs on esp32 architecture(s).
/tmp/arduino_modified_sketch_946312/Pump_House__Monitor_ver7C.ino.ino: In function 'void Alm_Relay_ON()':
Pump_House__Monitor_ver7C.ino:215:23: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
        Alm_Relay[4] = "ON";             //  Building Alm relay 'ON' status
                       ^~~~
/tmp/arduino_modified_sketch_946312/Pump_House__Monitor_ver7C.ino.ino: In function 'void Alm_Relay_OFF()':
Pump_House__Monitor_ver7C.ino:236:21: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
      Alm_Relay[4] = "OFF";               // Building Alm relay 'OFF' status
                     ^~~~~
exit status 1
invalid conversion from 'const char*' to 'char' [-fpermissive]

You can assign string to array in a time of initialization only.
Use strcpy() instead.

strcpy(Alm_relay, "ON");

Thank you!!

Is this a common newbie mistake?

The C beginners guide I bought doesn't really cover this aspect of character assignments very well.

why not

const char *Alm_Relay = "OFF";

What is a benefit in this case?

no call to strcpy()

and avoids a compiler error

consider

const char *wifiStStr [] = {
    "ST_GET_CREDENTIALS",
    "ST_AP",
    "ST_MASTER",
    "ST_NODES",
    "ST_ERROR"
};

I you're going to use that, make the pointers constant too:

const char * const wifiStStr [] = {
    "ST_GET_CREDENTIALS",
    "ST_AP",
    "ST_MASTER",
    "ST_NODES",
    "ST_ERROR"
};

Do I have to redefine the declaration "char Alm_Relay[4]"

Pump_House__Monitor_ver7C.ino:105:38: error: statement has no effect [-Werror=unused-value]
/tmp/arduino_modified_sketch_255981/Pump_House__Monitor_ver7C.ino.ino: In function 'void Alm_Relay_ON()':
Pump_House__Monitor_ver7C.ino:215:15: error: 'Alm_relay' was not declared in this scope
        strcpy(Alm_relay, "ON");             //  Building Alm relay 'ON' status
               ^~~~~~~~~
/tmp/arduino_modified_sketch_255981/Pump_House__Monitor_ver7C.ino.ino:215:15: note: suggested alternative: 'Alm_Relay'
        strcpy(Alm_relay, "ON");             //  Building Alm relay 'ON' status
               ^~~~~~~~~
               Alm_Relay
/tmp/arduino_modified_sketch_255981/Pump_House__Monitor_ver7C.ino.ino: In function 'void Alm_Relay_OFF()':
Pump_House__Monitor_ver7C.ino:236:13: error: 'Alm_relay' was not declared in this scope
      strcpy(Alm_relay, "OFF");               // Building Alm relay 'OFF' status
             ^~~~~~~~~
/tmp/arduino_modified_sketch_255981/Pump_House__Monitor_ver7C.ino.ino:236:13: note: suggested alternative: 'Alm_Relay'
      strcpy(Alm_relay, "OFF");               // Building Alm relay 'OFF' status
             ^~~~~~~~~
             Alm_Relay


change to

no need for strcpy()

please show ALL your code

How do you intend to change contents of Alm_Relay ?

Here it is, some is commented out as I work through errors.
And no, I have no intentions of becoming a code writer. LOL

    
    // 2/4 added code from WiFiWebServer ex. sketch to create
    // browser client access; 2/24 added local alarm relay w/ 2 triggers
    //   2/6  added low water pressure alarm
    //   3/6  Note: this sketch works properly when Board assignment is for "ESP32 Dev Module"
    //   Code source: https://esp32io.com/tutorials/esp32-temperature-sensor
    //  3/24 changed descriptors to char array w/ StackString function to reduce memory usage
    //  & added cycle timer to building alarm buzzer 

    #include "hashtag_includes.h"       // see tabs at top
    #include "constants.h" 
     
                      //  ***************** VOID SETUP ***************               
   void setup()
   {
    startMillis = millis();  //initial start time for counter/timer
    DS18B20.begin();    // initialize the DS18B20 space sensor
    
    pinMode(ONBOARD_LED, OUTPUT);       //  Blink LED function
    pinMode(relay1_outPin, OUTPUT);      //  Alarm relay    
    pinMode(switchPin, INPUT);         // Pressure Alarm Input

    Serial.begin(115200);
 
    while (WiFi.status() != WL_CONNECTED)
    {
     Serial.println("Attempting to Connect To Local LAN:   ");
     Serial.println(wifissid); 
     WiFi.begin(wifissid, wifipass);
     delay(4000);
     Serial.print(".");
    }
     Serial.println("");
     Serial.print("Connected to WiFi network with IP Address: ");
     Serial.println(WiFi.localIP());
     Serial.println("Attempting to connect to WPA network...");

     server.begin();                 // added 2/4 testing
              // you're connected now, so print out the status:
     Serial.print("WiFi Status:  ");
     Serial.println(WiFi.status());
     Serial.println();
     Serial.print("ESP Board MAC Address:  ");
     Serial.println(WiFi.macAddress());
    }                              
                                    // *********** VOID LOOP ************
     void loop()
     {
      DS18B20.requestTemperatures();              // send the command to get temperatures
      space_temp_1 = DS18B20.getTempCByIndex(0);  // read temperature in °C
      sp_temp_1F = (space_temp_1 * 9 / 5) + 32;   // convert C to F
      sp_temp_1 = sp_temp_1F + tmp_adj_1;         // sensor calibration
           
      digitalWrite(ONBOARD_LED,HIGH);             //  Blink for run indicator
      delay(100);
      digitalWrite(ONBOARD_LED, LOW);
      delay(1000);                                                                                                                                               
                         //  ***************** Low Temp Alarm Function ***************
           
/*      if (sp_temp_1 <= Low_Tmp_Alm)
      {
       temp_alarm = true;
      }
       else {temp_alarm = false;}
          
                        //  ********************* If temp alarm initiate API msg **********
      
      if (temp_alarm == true && temp_alarm_state == false)
      {
      if(DEBUG){Serial.println("               Pump House Temp Alarm is True / Send message");}
      temp_alarm_state = true;
      Alm_Relay_ON();                    // turn local alrm relay/buzzer on
      char Temp_Alm[4] = "ON";
      sendToPushingBox(DEVID1);          // Send request to PushingBox when temp alm is true 
      }                                  // AND alm state "was" false; this ensures alm is not repeatedly sent.
      if (temp_alarm == false && temp_alarm_state == true)   // Temp alarm is OFF.
      {
       if(DEBUG){Serial.println("              Pump House Temp Alarm is false / No message sent.");}
       temp_alarm_state = false;
       Alm_Relay_OFF();               // turn local alrm relay/buzzer off 
       char Temp_Alm[4] = "OFF";      
      }                        
                                      //  ***************** Low Pressure Alarm Function ***************
      switchState = digitalRead(switchPin);       //Pressure Switch Input Pin Read
                                     
      if (switchState == HIGH)        // check if pressure switch has changed state
      {
       pressure_alarm = true;
      }
       else {pressure_alarm = false;}
      
      if (pressure_alarm == true && press_alarm_state == false)  // send out alarm on web
      {
      if(DEBUG){Serial.println("         Pressure alarm is TRUE / Send message");}
      press_alarm_state = true;     // Send request to PushingBox when pressure alm is true
      sendToPushingBox(DEVID2);     // AND alm state "was" false; this ensures alm is not repeatedly sent.
      Alm_Relay_ON();               // turn building alrm relay/buzzer on
      char Pressure_Alm[4] = "ON";
      }                            
      if (pressure_alarm == false && press_alarm_state == true)   // Pressure alarm is OFF.
      {
       if(DEBUG){Serial.println("           Pressure alarm is FALSE / No message sent      ");}
       press_alarm_state = false;
       Alm_Relay_OFF();              // turn local alrm relay/buzzer off
       char Pressure_Alm[4] = "OFF";
      }

*/                 
         //   ******************** WiFiWebServer Code***********
          
     WiFiClient client = server.available();    // listen for incoming clients
     if (client)
     {
     Serial.println("new client");
                                    // an http request ends with a blank line
  //   boolean currentLineIsBlank = true;
     while (client.connected())
     {
      if (client.available())
      {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n')         //     && currentLineIsBlank)
        {                                                                                                                   
          client.println();
          client.println();
          StackString<50> AString = StackString<50>("               Pump House");
          client.println(AString.c_str());
          StackString<50> BString = StackString<50>("               **********");
          client.println(BString.c_str());
          client.println();
          client.println();
          StackString<50> CString = StackString<50>("            Pump House Temp =  ");
          CString.append(sp_temp_1);
          client.println(CString.c_str());
          StackString<50> DString = StackString<50>("        Temp Alarm Setpoint =  ");
          DString.append(Low_Tmp_Alm);
          client.println(DString.c_str());
          StackString<50> EString = StackString<50>("             Low Temp Alarm =  ");
          EString.append(Temp_Alm);
          client.println(EString.c_str());
          StackString<50> FString = StackString<50>("         Low Pressure Alarm =  ");
          FString.append(Pressure_Alm);
          client.println(FString.c_str());
          StackString<50> GString = StackString<50>("       Building Alarm Relay =  ");
          GString.append(Alm_Relay);
          client.println(GString.c_str());                       
          break;
          } 
        }        
       }
      
        delay(2000);                   // give the web browser time to receive the data
        client.stop();                   // close the connection:
        Serial.println("client disonnected");      
      }
   
                  // this write the response from PushingBox Server.
                  // You should see a "200 OK"
  
 /*     if (client.available())
      {
       char c = client.read();
       if(DEBUG){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)
     {
      if(DEBUG){Serial.println();}
      if(DEBUG){Serial.println("             disconnecting");}
      client.stop();
     }
     lastConnected = client.connected();
   }

   // Function for sending the request to PushingBox
   
   void sendToPushingBox(char devid[])
   {
     client.stop(); 
     if(DEBUG){Serial.println("      connecting...");}
    if(client.connect(serverName, 80))
    { 
     if(DEBUG){Serial.println("     connected");}
     if(DEBUG){Serial.println("          sending request to PushingBox API");}
     client.print("GET /pushingbox?devid=");
     client.print(devid);
     client.println(" HTTP/1.1");
     client.print("Host: ");
     client.println(serverName);
     client.println("User-Agent: ESP32");
     client.println();
    } 
    else
    { 
    if(DEBUG){Serial.println("            connection failed");} 
    } 
   }
                                             
   void Alm_Relay_ON()                      // Building alarm relay ON command
    {                                        // TWO triggers: temp OR pressure
     if (cycle_OFF_timer == false)
     {
     digitalWrite(relay1_outPin, HIGH);
     }
      if (digitalRead(relay1_outPin) == HIGH)
      {
       char Alm_Relay[4] = "ON";             //  Building Alm relay 'ON' status
       Serial.println("      Building Alarm Relay ON");      // turn buzzer alarm OFF after X mins; cycle OFF/ON
       currentMillis = millis();  //get current "time" (actually the number of milliseconds since program started)
       if (currentMillis - startMillis >= period_1)  //check whether the Buzzer ON period has elapsed
       {
        cycle_OFF_timer = true;
        void Alm_Relay_OFF();
        startMillis = currentMillis;  
       }
      }
     }
    void Alm_Relay_OFF()                 //  Building alarm relay OFF command
    {
     digitalWrite(relay1_outPin, LOW);   
     Serial.println("      Building Alarm Relay OFF");
  
    if (digitalRead(relay1_outPin) == LOW)
    {
     char Alm_Relay[4] = "OFF";               // Building Alm relay 'OFF' status
     currentMillis = millis();  
     if (currentMillis - startMillis >= period_2)  //test whether the Buzzer OFF period has elapsed
     {
      cycle_OFF_timer = false;
      startMillis = currentMillis;  
     } 
    }
   }
   


     int status = WL_IDLE_STATUS;         // the Wifi radio's status

     WiFiClient client;

     WiFiServer server(80);               // added for testing 2/4

     OneWire oneWire(SENSOR_PIN);
     DallasTemperature DS18B20(&oneWire);

    char wifissid[] = "hug2g842673";        //     
    char wifipass[] = "xxxxxxxsouth";        //     

    char DEVID1[] = "";        //   token/key for low temp alarm    [ xxxxxxx203ED29211 ]
    char DEVID2[] = "";        //   token/key for low wtr pressure alarm    [ xxxxxxx10CAE86043 ]
    
  
    boolean DEBUG = true;            // Debug mode     !!!!

    char serverName[] = "";              //  [  api.pushingbox.com   ]

    //  int keyIndex = 0;       WiFi Communication Parameter      NEEDED????

    const int relay1_outPin = 18;              // Temp Alarm Relay output   pinMode(GPIO, OUTPUT);
    
    boolean temp_alarm = false;                   
    boolean temp_alarm_state = false;                
    boolean lastConnected = false;

              //  ************* Low Water Pressure Alm Parameters ***********
    int switchPin = 4;    // Board Digital Input GPIO4;  pinMode(GPIO, INPUT);
    int switchState = 0;
  
    boolean pressure_alarm = false;
    boolean press_alarm_state = false;
    
                                        //  ***************** Low Temp Alarm Parameters ***************
    char Temp_Alm[4] = "OFF";
    char Pressure_Alm[4] = "OFF";
    char Alm_Relay[4] = "OFF";
                                        
//    const int B = 4275;                 // B value of the thermistor;  +- 275 = .6 C (1.0 F)
//   const uint32_t R0 = 100000;            // R0 = 100k; int type uint32_t required (unsigned 32 bit)
    int tmp_adj_1 = 1;                      // sensor 1 calibration adjustment
     
    float space_temp_1;                      //    Dallas DS18B20 temp sensor
    int sp_temp_1;
    float sp_temp_1F;
    int Low_Tmp_Alm = 38.00;         // Low Alarm Setpoint
    
                                // Millis() timer function
    unsigned long startMillis;  //some global variables available anywhere in the program
    unsigned long currentMillis;
    const unsigned long period_1 = 3000000;  // X millisecs   [period_1] is ON time
    const unsigned long period_2 = 60000;   //               [period_2] is OFF time
    
    boolean cycle_OFF_timer = false;   // is only true after buzzer exceeds ON time duration


    #include <SPI.h>
    #include <WiFi.h>
    #include <math.h>

    #include <DallasTemperature.h>

//    #include <DS18B20.h>

    #include <OneWireNg.h>
    
    #define ONBOARD_LED  2

    #define SENSOR_PIN  21 // ESP32 pin GIOP21 connected to DS18B20

    #include <StackString.hpp>   // save memory using this function: character arrays
     using namespace Stack; 
     
 

sorry. so he just wants a pointer to either "OFF" or "ON".

consider


const char *pAlm;
int   flag;

void
loop ()
{
    Serial.println (pAlm);

    if (++flag % 2)
        pAlm = "OFF";
    else
        pAlm = "ON";
    
    delay (1000);
}

void
setup ()
{
    Serial.begin (9600);
    pAlm = "new";
}

Yes, I see...
I don't understand why do you need at all Alm_Relay[4] string? You only assign it, but newer use.
Your code has a lot of errors. Every time you start a variable with a type, for example :

-this is a new variable! When you create multiple variables with the same name in different part of the program, they don't exchange values...

If you intent to use variable between the procedures, you should define it only once beyond all the block - it means create global variable.

Well I am getting lost here. haha

Whatever is simple, lasts a long time, and doesn't tax my neophytic brain.
I have never used ++C code before so a learning curve is definitely involved.

Sorry, when will be memory allocated for "ON" and "OFF" strings, if you declared just a pointer? A pointer to what?
And what will happen to the memory occupied by "ON" when we assign "OFF" to the same pointer?

This explains some compile errors I have seen. I need to go back and rethink my approach and maybe even "bone up" on character strings alot more. This is "OJT" for me. LOL

whenever you describe a string in code, the compiler allocates space for it somewhere (e.g string table).

when you define a pointer, in this case to a "const char *", the compiler allocates space for the pointer value. the pointer value can be changed, just like any other variable (e.g. a = 3.14;)

so in the code i posted, a pointer and 3 strings are allocated and the pointer is set to "new" in setup() -- the pointer value is set to the address that "new" is allocated in memory. in loop(), the pointer value is changed to either the address of "Off" or "On"

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