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.
loopfunction,Received_Messageshould be achar*. I honestly don't know whatGetNumberdoes, but it seems to expect achar*as a parameter. - Federico klez Cullocachar Recieved_Message = Serial.write(Recieved_Bytes);Are you sure that's whatSerial.writereturns? - Bartek Banachewicz