0

I am working on a code that accepts input from serial monitor and connects to Wifi, and have been getting issues.

my SSID for my wifi is "SamF" but for some reason arduino only likes it when I initialize at the top, not when I accept it from serial monitor even though they are the same..

Example The below code works:

#include <WiFi.h>

char ssid[] = "SamF"; //OR const char* = "SamF"
char pass[] = "secretPassword";
void setup()
{
WiFi.begin(ssid, pass);
}
void loop () {}

The Code Below Does not work..

#include <WiFi.h>
char pass[] = "secretPassword";
void setup()
{
ssidSerial = my code that receives Serial input and converts to char

for(int k=0;k<=3;k++){
ssidPlaceHolder[k]=ssidSerial[k];
}
const char* ssid = ssidPlaceHolder;
WiFi.begin(ssid, pass);
}
void loop () {}

Everything compiles fine, I debugged by using Serial.print and they are both displaying "SamF" and sizeof(ssid) for both is 4... so I am not sure why one works and the other doesn't? any ideas?

Thanks in advance for any help, and if anyone needs the code after I am done I will be more than glad to share it on git!

7
  • add a terminating 0 to ssidPlaceholder end. 4 char string needs 5 char array to have the 5th position set to 0 – Juraj Jun 20 '18 at 4:58
  • why do you copy the string from ssidSerial to ssidPlaceHolder and what for is the assignment to ssid pointer? – Juraj Jun 20 '18 at 7:01
  • Thanks for response Juraj, I will try add the terminating 0 to the variable once I get home and will let you know what happens, I copied it over because ssidSerial adds garbage data at the end, because I am using Bluetooth Serial, ssidSerial is "SamF garbage symbol" and I needed "SamF" that is why I am coping it over. – AceSammy Jun 20 '18 at 13:21
  • just to clarify more, receiving after receiving Bluetooth serial data the variable ssidSerial becomes "SamF garbage symbol/data" of length 10 which I thought would mess it up. the wifi library accepts data in the form of a pointer which is why it's there, I know its the same thing as just ssidPlaceHolder but I thought it would help – AceSammy Jun 20 '18 at 13:30
  • perhaps you send the new line character(s) \n or \r\n, not garbage – Juraj Jun 20 '18 at 14:03
0

Basic string in C is an array of characters terminated by a 0. All functions taking char* or const char* use this 0 to determine the end of the string, because there is no other information about the length of that string.

In your question you copy characters from one characters buffer to other, but you do not set the terminating 0, so the function begin takes the data after the pass pointer until some random memory location with 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.