1
votes

This is really giving me hard time; this simple logic. I will explain how things are going and what I am trying to achieve at this point.

I have an ESP8266 module programed to subscribe to a topic "switches" on a server running Mosquitto MQTT Server.

From any device having MQTT Client, I am able to publish a message to this topic (switches) and send this message through serial using the callback function below.

    void callback(char* topic, byte* payload, unsigned int length) { 

    for (int i = 0; i < length; i++) {
       Serial.print((char)payload[i]);
    }
      Serial.println();
    }

On the Arduino side I am trying to process whatever is coming through the serial port and turn on/off digital pins on the Arduino.

At this point on the Arduino, I have the following simple piece of code that receives bytes and store it in a variable "Recieved_Bytes" and then I am trying to convert those bytes to characters using another variable "Recieved_Message" of type char.

void loop(){

  if(esp8266.available()){ // check if the esp is sending a message 

      int Recieved_Bytes = esp8266.read(); //Byte Data
      char Recieved_Message = Serial.write(Recieved_Bytes); // char data;
      GetNumber(Recieved_Message);

   }

Everything is fine by far, however when I try to pass this "Recieved_Message" to a function that should return the integer value of what is being sent.

const char * Names[] = {"SwitchOne", "SwitchTwo", "SwitchThree", "SwitchFour", "SwitchThirteen"};
const int Numbers[] = {1,2,3,4,13};

int GetNumber(char *name) {
   for (int i=0; i < sizeof Names / sizeof Names[0]; i++) {
       if (strcmp(name, Names[i]) == 0)
            return Numbers[i];
   }
   return -1;  // No match found
}

I receive an error - below.

PIO_ESP_Control:6: error: initializing argument 1 of 'int GetNumber(char*)' [-fpermissive]
invalid conversion from 'char' to 'char*' [-fpermissive]

NOTE: Assuming that I will be sending a message "SwitchOne" for example

mosquitto_pub -t switches -m "SwitchOne"

The question is, what is it that I am doing wrong and how can I fix it? Thanks for your help in advance.

1
In your loop function, Received_Message should be a char*. I honestly don't know what GetNumber does, but it seems to expect a char* as a parameter. - Federico klez Culloca
char Recieved_Message = Serial.write(Recieved_Bytes); Are you sure that's what Serial.write returns? - Bartek Banachewicz
@BartekBanachewicz you're right char Recieved_Bytes = esp8266.read(); Serial.print(Recieved_Bytes); outputs "SwitchOne" as well - user2119344
The thing is that if I used 'GetNumber("SwitchOne");' this works fine and returns "1". Same goes for the rest of the "defined" keywords. SwitchTwo return "2"....etc - user2119344
esp8266.read(); return the ASCII code representation for each character. It returns something like "8311910511699104841041051141161011011101310" - user2119344

1 Answers

0
votes

The compiler error is because the method GetNumber is expecting char*, and Received_Message is a just a char. It works when you call GetNumber("SwitchOne"); because "SwitchOne" is an array of characters.

However, I do not think that is the only issue. It seems like in your code you want Recieved_Message to be one of the strings stored in Names, but according to this it is just storing the number of bytes written by Serial.write(Recieved_Bytes);