ESP-07 wont connect to AP

Hi

After managing to get my 1284p - mighty core to initialise the ESP-07 module connected to serial1 i am now unable to connect to the local wifi AP using the test sketch below :

#include "WiFiEsp.h"

 #define esp8266Enable  1   
 #define esp8266Reset   4


const char ssid[] = "Skynet3";            // your network SSID (name)
const char pass[] = "XXXXXX";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

void setup()
{
  
  pinMode(esp8266Enable,OUTPUT);
  pinMode(esp8266Reset,OUTPUT);

  digitalWrite(esp8266Enable,HIGH);
  digitalWrite(esp8266Reset,HIGH);

  // initialize serial for debugging
  Serial.begin(9600);
  // initialize serial for ESP module
  Serial1.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
    Serial.println(status);
    delay(500);
  }

  Serial.println("You're connected to the network");
}

void loop()
{
  // print the network connection information every 10 seconds
  Serial.println();
  printCurrentNet();
  printWifiData();
  Serial1.print("AT+GMR");
  delay(10000);
}

void printWifiData()
{
  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print your MAC address
  byte mac[6];
  WiFi.macAddress(mac);
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
  Serial.print("MAC address: ");
  Serial.println(buf);
}

void printCurrentNet()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the MAC address of the router you're attached to
  byte bssid[6];
  WiFi.BSSID(bssid);
  char buf[20];
  sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
  Serial.print("BSSID: ");
  Serial.println(buf);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI): ");
  Serial.println(rssi);
}

Output

[WiFiEsp] Initializing ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Initilization successful - 2.0.0
Attempting to connect to WPA SSID: Skynet3
[WiFiEsp] Failed connecting to Skynet3
2
Attempting to connect to WPA SSID: Skynet3
[WiFiEsp] Failed connecting to Skynet3

Note that I added a line to print out the number of the 'status' state which gives out '2'

I have also tried the above with a second AP with the same result.

AT+GMR

AT version:1.3.0.0(Jul 14 2016 18:54:01)
SDK version:2.0.0(5a875ba)
v1.0.0.3
Mar 13 2018 09:37:06
OK

AT ESP Boot:

ets Jan  8 2013,rst cause:1, boot mode:(3,7)

load 0x40100000, len 2408, room 16 
tail 8
chksum 0xe5
load 0x3ffe8000, len 776, room 0 88f  
8
 .e0M F

Is there anything to check (other than correct SSID and password)

shouldn't this only be done once?

The idea is too keep trying till connected.
Basic code was taken from the library's own example.

I have a similar setup hardware wise on my robot. But I put the WiFi code on the ESP-07 instead of trying to deal with AT commands. I've seen too many people burn up too much time trying to get that right. The code on the ESP is pretty easy.

This goes on the ESP-07 and handles the WiFi connection. There'a also an LoRa radio that can be attached, you can ignore that part. The WiFi code is real simple.

This goes on the 1284P. But there's not really anything to see here except robot code. It just spits commands out Serial1 and the ESP-07 parses them and/or sends them out the WiFi to the computer.

Very interesting.
However my application also has an ethernet connection, rs485, 4 analog inputs etc. I needed a separate mcu and decided to use the esp as a wifi modem...

but you don't need to reset the credentials each time

some of my code


#ifdef ESP32
# include <WiFi.h>
#elif defined(ESP8266)
# include <ESP8266WiFi.h>
#endif

#include "AsyncUDP.h"

#include "eeprom.h"
#include "node.h"
#include "signals.h"
#include "wifi.h"

int dbgWifi = 1;

static void _wifiScan (void);
static void _wifiMsg  (const char *msg);

// -----------------------------------------------------------------------------
// WiFiClient          wifi;
AsyncUDP udp;

char host [STR_SIZE] = "";
char ssid [STR_SIZE] = "";
char pass [STR_SIZE] = "";

static int  port  = 4445;

// ---------------------------------------------------------
enum { ST_NUL, ST_INIT, ST_CHK, ST_CFG_UDP, ST_UP, ST_ERROR };

const char *wifiStStr [] = {
    "ST_NUL",
    "ST_INIT",
    "ST_CHK",
    "ST_CFG_UDP",
    "ST_UP",
    "ST_ERROR"
};

int state    = ST_NUL;
int stateLst = ST_NUL;

// -------------------------------------
static bool
_wifiCheck (void)
{
    static int           fails = 0;
    static unsigned long msecLst = 0;

    if ( (msec - msecLst) < 1000)
        return false;
    msecLst = msec;

    printf (" %s:", __func__);

#if 0
    if (6 <= fails)  {
        _wifiScan ();
        return false;
    }
#endif

    if (WL_CONNECTED != WiFi.status ())  {
        printf (" not connected\n");
        fails++;
        return false;
   }

   IPAddress ip = WiFi.localIP ();

   printf (" connected %d:%d:%d:%d\n", ip [0], ip[1], ip [2], ip[3]);

   return true;
}

// -------------------------------------
// https://arduino.clanweb.eu/udp-control-esp32.php?lang=en

static void
_wifiCfgUdp (void)
{
    printf (" %s:\n", __func__);

    if (udp.listen (port)) {
        Serial.print ("  UDP Listening on IP: ");
        Serial.println (WiFi.localIP ());

        udp.onPacket ([] (AsyncUDPPacket packet) {
#if 0
            Serial.print   ("UDP Packet Type: ");
            Serial.println (packet.isBroadcast ()
                        ? "Broadcast"
                        : packet.isMulticast () ? "Multicast" : "Unicast" );
            Serial.print   (" From: ");
            Serial.print   (packet.remoteIP ());
            Serial.print   (":");
            Serial.println (packet.remotePort ());
            Serial.print   (" To: ");
            Serial.print   (packet.localIP ());
            Serial.print   (":");
            Serial.println (packet.localPort ());
            Serial.print   (" Length: ");
            Serial.println (packet.length ());
            Serial.print   (" Data: ");
            Serial.write   (packet.data (), packet.length ());
            Serial.println ();
#endif

            _wifiMsg ((const char *) packet.data ());
        });
  }
}

// -------------------------------------
// connect to wifi
void _wifiInit (void)
{
    printf (" %s:\n", __func__);

    _wifiScan ();
    WiFi.mode (WIFI_STA);

    WiFi.hostname (host);

    printf ("%s: ssid %s, pass %s\n", __func__, ssid, pass);
    WiFi.begin (ssid, pass);
}

// -------------------------------------
static void
_wifiMsg (
    const char *msg)
{
    printf (" %s:\n", __func__);

    sigMsg (msg);
}

// -------------------------------------
void
wifiReset (void)
{
    printf ("%s: ssid %s, pass %s, host %s\n", __func__, ssid, pass, host);
    state = ST_NUL;
}

// -------------------------------------
// https://openlabpro.com/guide/scanning-of-wifi-on-esp32-controller/
static void
_wifiScan (void)
{
    printf ("%s:\n", __func__);

    // WiFi.scanNetworks will return the number of networks found
    WiFi.mode (WIFI_OFF);
    int nNet = WiFi.scanNetworks ();
    if (nNet == 0) {
        printf ("  %s: no networks found\n", __func__);
    }
    else {
        printf ("  %s: %2d networks found\n", __func__, nNet);
        if (0 > nNet)
            nNet = -nNet;
        for (int i = 0; i < nNet; ++i) {
            printf (" %s: %2d - %s (%d) %s\n", __func__, i,
                WiFi.SSID (i).c_str (),
                WiFi.RSSI (i),
                WiFi.encryptionType (i) == WIFI_AUTH_OPEN ? "open" : "locked");
        }
    }
}

// ---------------------------------------------------------
// common routine for sending strings to wifi and flushing
void
wifiSend (
    const char*  msg )
{
    if (ST_UP != state)
        return;

    if (dbgWifi)  {
        printf ("wifiSend: %s\n", msg);
    }

    udp.broadcast (msg);
}

// -------------------------------------
void
wifiMonitor (void)
{
    if (stateLst != state)
        printf ("%s: %d %s\n", __func__, state, wifiStStr [state]);
    stateLst = state;

    switch (state)  {
    case ST_NUL:
        if (ssid [0])
            state = ST_INIT;
        else {
            printf ("%s: no SSID\n", __func__);
            state = ST_ERROR;
        }
        break;

    case ST_INIT:
        _wifiInit ();
        state = ST_CHK;
        break;

    case ST_CHK:
        if (_wifiCheck ())
            state = ST_CFG_UDP;
        break;

    case ST_CFG_UDP:
        _wifiCfgUdp ();
        state = ST_UP;
        break;

    case ST_UP:
        break;

    case ST_ERROR:
    default:
        break;
    }
}

That's exactly what I did. I just didn't use AT commands to communicate with the modem. The only real difference is which of the two MCUs the code to connect to the network lives on. This way I don't have to package everything up into AT commands. I can just send it as serial to the ESP and let the ESP handle comms.

I figured if I'm going to have two boards, one for IO and one for comms then I'll let them each have the full duty of their separate jobs.

hmm lets say i follow your approach. I then need to unsolder the esp-07 from the pcb, program it somewhere else, without being able to test the code together with the other mcu (1284p) and then solder it back
on the pcb once programmed. correct?

I don't know. I haven't seen your project.

I program the ESP-07 OTA while it is in the circuit running. I use the ESP to program the 1284 in the circuit while it is running.

My setup is shown here : https://europe1.discourse-cdn.com/arduino/original/4X/7/6/5/76506fde360ffa4ca251c72eb8c67e1e21ccb5ef.png

The 1284p talks to the esp07 via Serial1.
1284p is programmed from Serial0 via an external FTDI module.
I cant see how I could program the ESP in-circuit ...
This is partly why I chose to just use the ESP AT firmware and the associated library. So that I would only have to program the 1284p.
Now stack with ESP not connecting to wifi ....

Not sure I understand your code...
If am not mistaken you use an enum state to monitor wifi connection status.
But how do you try to reconnect to wifi once disconnected or connection failed?

the code shows how the credentials are set in Wifi.begin() once, _wifiInit() and then the code waits for the connection to be established in _wifiCheck(), while in the ST_CHK state, before configuring for UDP in the ST_CFG_UDP state.

unlike your code, the credentials aren't reset every failed check that the connection is established using WiFi.status()

why doesn't you code do

  WiFi.begin (ssid, pass);

  while (WL_CONNECTED != WiFi.status ())  {
    Serial.print ("Attempting to connect to WPA SSID: ");
    Serial.println (ssid);
    delay (500);
  }

  Serial.println ("connected");

Thats because, i didnt realise that

while (WL_CONNECTED != WiFi.status ()) { }

actually tries to reconnect to the AP and not just report the connection status...

Just tried that too. Didn't make any difference. Still refuses to connect....

Update :slight_smile:
I was finally able to connect to wifi by manually sending the AT command to Serial1 port:

AT+CWJAP="ssid","password"

but still the library command

WiFi.begin(ssid, password);

wont connect !

Something wrong with the library?

are your credentials correct?

yes! thats the first thing I double check!

For anyone reading this, it seems that the problem was with the library.

I have now switched to this library and everything works fine!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.