I need help. I am trying to convert a String of numbers into individual bytes, but I am getting an "Invalid cast from type 'arduino::String' to type 'byte {aka unsigned char}" error message. The Arduino doccumentation for type conversion says I can use byte() to convert any value type to byte, so I don't understand why it is "invalid".
I understand that using Strings can lead to a whole host of issues. The main reason I am using them is because the code I copied and adapted used them. So, if there is a way I can avoid using them, feel free to suggest. But, I warn you, I am a complete novice (approx 3 weeks of experience), and every mention of char* arrays has left me more confused than before. So, dumb it down for me.
To (attempt to) explain the purpose of the code: this code is for what I am calling motorUnit. I will have another Arduino (controlUnit) that is acting as WiFi Access Point and Server Host. This controlUnit will send both commandWord and variableRequest messages. The syntax of the messages is either commandWord:xxxxxxxxxxx (where each x is a 0 or 1), or <motorUnit_x:"variable"?> The return message syntax will be <motorUnit_x:"variable":"value"> (where x is the board number, in this case 0, "variable" will be the specific variable that is being requested, such as shutterStatus, batteryLevel, signalStrength, etc, and value is the value that is returned from the sensors - not yet added).
The problem I am having is I am reading the incomingData from the controlUnit as a String so that I can use the indexOf() function to separate the incomingData into commandType (either commandWord or variableRequest) and commandValue. I am then trying to conver the command Value into a byte array so that each of the motorUnits (there will be 11) can read the byte that is specific to it.
Code and error is posted below.
G:\My Drive\arduinoProjects\testMotorUnit_2.0wButton\testMotorUnit_2.0wButton.ino: In function 'void loop()':
G:\My Drive\arduinoProjects\testMotorUnit_2.0wButton\testMotorUnit_2.0wButton.ino:73:62: error: invalid cast from type 'arduino::String' to type 'byte {aka unsigned char}'
commandWord[i] = byte(commandValue.substring(i));
^
exit status 1
Compilation error: invalid cast from type 'arduino::String' to type 'byte {aka unsigned char}'
/*
Motor Units
need to change Unit number for each one
*/
#include <Arduino.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
#include "utility/wifi_drv.h" //only needed for onboard led.
//also will need battery libraries
//variables for network information
const char ssid[] = APssid;
const char pass[] = APpass;
IPAddress server(192,168,4,1);
unsigned long previousMillis = 0;
int interval = 5000;
//variable for motorUnits
const byte motorUnits = 11;
byte commandWord[motorUnits];
const String variableRequest[] = {"shutterStatus", "batteryLevel", "signalStrength", "tempSensor"};
float motorUnitStatus[sizeof(variableRequest)];
//variables for client information
String clientName = "motorUnit_0"; //This is the name of this board. It needs to be changed for each board (0, 1, 2, 3, etc).
WiFiClient client;
//variables for LED testing
const int ledPin = 2;
const int onboardLEDRed = 25;
const int onboardLEDGreen = 26;
const int onboardLEDBlue = 27;
//variables for Serial Monitor testing
String ServerMessage; // not sure what this is for.... it is never called in the code I am copying
void setup() {
Serial.begin(9600);
Serial.print("Begin setup() function | Line: "); Serial.println(__LINE__);
Serial.print("Connecting to AP network | Line: "); Serial.println(__LINE__);
WiFi.begin(ssid, pass); //I might need to use WiFi.config() to change the ip address of each board before calling WiFi.begin(). But, I am going to leave it as is for now and see what happens. It might all work out.
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to AP network with IP Address: "); Serial.print(WiFi.localIP()); Serial.print(" | Line: "); Serial.println(__LINE__);
}
// The clientRequest() from the control board uses the following syntax: either <commandWord:xxxxxxxxxxx>, or <motorUnit_x:"variable"?> | It is expecting no response for commandWord and a response for motorUnit_x with the following syntax: <motorUnit_x:"variable":"value" | x is an int that changes for each function, "variable" is the variable type that is requested (shutterStatus, batteryLevel, etc), and value is a float returned from the sensors.
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
if(WiFi.status() == WL_CONNECTED){
Serial.print("Connecting to Server | Line: "); Serial.println(__LINE__);
client.connect(server, 80);
while(client.connected()){
Serial.print("Connected to Server | Line: "); Serial.println(__LINE__);
String incomingData = client.readStringUntil('\r'); //read String until carriage return
if(incomingData != "\0"){
Serial.print("Received data from Server: "); Serial.print(incomingData); Serial.print(" | Line: "); Serial.println(__LINE__);
int index = incomingData.indexOf(':');
String commandType = incomingData.substring(0, index);
String commandValue = incomingData.substring(index +1);
if(commandType == "commandWord"){
for(int i=0; i<commandValue.length(); i++){
commandWord[i] = byte(commandValue.substring(i, i++));
}
for(int i=0; i<sizeof(commandWord); i++){
Serial.print(commandWord[i]);
}
} else if(commandType == clientName){
}
}
}
}
}
}