So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By PabloViGar
#65169 Hello everyone!

I am kinda new on ESP8266 programming and I am having troubles connecting the device to some networks. I have been able to connect it to my phone's WiFi hotspot, but not to my home 2.4GHz WiFi.

I am using an Arduino Nano and an ESP8266 for an IoT project. Here are the codes for both devices:

ARDUINO NANO

#include <SoftwareSerial.h>

SoftwareSerial SoftSerial(10, 11);

String ssid = "mySSID";
String password = "myPASSWORD";
byte resetPin = 2;
String receivedString;
char receivedChar;
bool ESPready = false;
bool sentSSID = false;
bool sentPassword = false;
bool debug = false;

void setup() {

//----- SERIAL CONNECTION -----//
Serial.begin(9600);

Serial.println("Arduino Serial Connected");

//----- SOFT PORT CONNECTION -----//
SoftSerial.begin(9600);
SoftSerial.setTimeout(2000);

Serial.println("Arduino SoftSerial connected");

pinMode(resetPin, OUTPUT);
resetWifi();

ESPready = cleanESP();

while(!ESPready){
resetWifi();
ESPready = cleanESP();
}
Serial.println("ESP8266 Serial connected");

while(!sentSSID || !sentPassword){
while(!SoftSerial.available()){

}
receivedString = getBracketedStringFromWifi();
if (receivedString == "{\"request\":\"SSID\"}"){
if (debug) {Serial.println("recibido " + receivedString);}
SoftSerial.print(ssid);
sentSSID = true;
}
if (receivedString == "{\"request\":\"password\"}"){
if (debug) {Serial.print("recibido " + receivedString);}
SoftSerial.println(password);
sentPassword = true;
}
}
Serial.println("CREDENTIALS SENT");

}

void loop() {

while(!SoftSerial.available()){

}
receivedString = SoftSerial.readString();
Serial.println(receivedString);

}


//----- FUNCTIONS -----//

void resetWifi() { // resets WiFi module
digitalWrite(resetPin, LOW);
Serial.println("resetting...");
delay(200);
digitalWrite(resetPin, HIGH);
}

bool cleanESP() { // cleans trash from buffer
bool ACK = false;
long initMillis=millis();
long elapsedMillis=millis();
while(!SoftSerial.available()){}
while((ACK == false) && (elapsedMillis-initMillis) < 10000){
receivedChar = SoftSerial.read();
elapsedMillis=millis();
if(receivedChar == '{'){
while(!SoftSerial.available()){}
receivedChar = SoftSerial.read();
if(receivedChar == '{'){
while(!SoftSerial.available()){}
receivedChar = SoftSerial.read();
if(receivedChar == '{'){
ACK = true;
}
}
}
}
return ACK;
}

String getBracketedStringFromWifi(){

String chain;
char character;
while(SoftSerial.available())
{
character=SoftSerial.read();
if (character == '{') {
chain = String(chain+character);
break;
}
}
while(SoftSerial.available())
{
character=SoftSerial.read();
chain=String(chain+character);
if (character == '}') {
break;
}
}
return chain;
}


ESP8266

#include <ESP8266WiFi.h>
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJso ... tag/v5.0.7

String ssidString = "#.#";
String passwordString = "#.#";
char ssidChar[20];
char passwordChar[50];
String msgType;
String msgValue;

WiFiClient wifiClient;
bool WiFiconnected = false;

void setup() {

//----- SERIAL CONNECTION -----//
Serial.begin(9600);
delay(2000);

Serial.write("{{{"); // to clean the trash

delay(1000);

while(ssidString == "#.#"){
msgType="request";
msgValue="SSID";
sendJSONtoSerial();
while(!Serial.available()){

}
ssidString = Serial.readString();
// turn String into char
ssidString.toCharArray(ssidChar, ssidString.length()+1);
}
while(passwordString == "#.#"){
msgType="request";
msgValue="password";
sendJSONtoSerial();
while(!Serial.available()){

}
passwordString = Serial.readString();
// turn String into char
passwordString.toCharArray(passwordChar, passwordString.length()+1);
}

//----- SEARCH NETWORK -----//
bool networkFound = false;
Serial.print("Scan start ... ");
int n = WiFi.scanNetworks();
Serial.print(n);
Serial.println(" network(s) found");
for (int i = 0; i < n; i++){
Serial.println(WiFi.SSID(i));
if(WiFi.SSID(i) == ssidString){
networkFound = true;
}
}
Serial.print(ssidString);
if(networkFound){
Serial.println(" found");
}
else{
Serial.println(" not found");
}

if(networkFound){
int counter = 1;
while (!WiFiconnected && counter < 4){
Serial.println("Attempt " + String(counter));
WiFiconnected = wifiConnect();
counter++;
delay(50);
}
}
if(WiFiconnected){
Serial.println("CONNECTED TO WIFI " + ssidString);
}
}

void loop() {

}


//----- FUNCTIONS -----//

void sendJSONtoSerial(){
StaticJsonBuffer<100> jsonBuffer; //size of the pool in bytes.

// Create the root of the object tree.
JsonObject& root = jsonBuffer.createObject();

// Add values in the object

root[msgType] = msgValue;
root.printTo(Serial);
}

boolean wifiConnect() {
long initMillis=millis();
long elapsedMillis;
Serial.print("Connecting to "); Serial.print(ssidString);
WiFi.begin(ssidChar, passwordChar);
while (WiFi.status() != WL_CONNECTED) {
elapsedMillis=millis();
if (elapsedMillis > (initMillis + 30000)) {return false;}
delay(500);
}
return true;
}

Does anyone have any idea of what could be happening?

Thank you.