1
votes

I have the ESP8266 connected to my Arduino Uno. With a blank sketch I can use Serial Monitor to connect it to my wifi network using these commands

AT+IPR=9600
AT+CWMODE=1
AT+CWJAP="SSID_HERE",""

It get's an ip and everything. But now I want my sketch to just do this using this code

#include <SoftwareSerial.h>

#define SSID "SSID_HERE"

void setup(){
  Serial.begin(9600);
  Serial.setTimeout(5000);
  delay(1000);
}

boolean connectWiFi()
{

 // connect
  Serial.println("AT+CWMODE=1");
  Serial.println("AT+CWJAP=\"SSID_HERE\",\"\"");
  delay(2000);
  if(Serial.find("OK"))
  {
    Serial.println("AT+CIFSR");
    Serial.flush();
    delay(1000);
    return true;
  }
  else
  {
    // Can not connect to the WiFi.
    return false;
  }
}

But it doesn't work.. The Serial.println shows up in the Serial Monitor, but the ESP8266 doesn't seem to respond. What am I missing?

2
What is the reason why you don't print AT+IPR=9600 on serial too?Patrick Trentin

2 Answers

2
votes

AT -commands ends with carriage return, so you need to add '\r' to every command you print.

In your code lines looks like:

  Serial.println("AT+CWMODE=1\r");
  Serial.println("AT+CWJAP=\"SSID_HERE\",\"\"\r");
  Serial.println("AT+CIFSR\r");

Reference: https://en.wikibooks.org/wiki/Serial_Programming/Modems_and_AT_Commands/Special_Commands_and_Character_Sequences

0
votes

The problem here is that you are trying to use pins 0 & 1 for the serial comms, well its part of the problem.. Because the arduino uses serial as well, it for me is only really good to use pins 0 & 1 for serial when i've grounded the reset pin on the arduino. This turns the arduino into a dummy device.

You can use something like software serial and two different pins instead, this way you will not interfere with the hardware serial of the arduino.

Also just to note, the below example will barely work.. For some it will for others it wont.. The problem here is that software serial does not really work / run at 115200..

You can change baud rate via AT+UART_DEF=19200,8,1,0,0 which will also disable flow control, then use software serial with a different speed mySerial.begin(19200)

Using Serial.println("TEXT") will send the line returns for you, so no need to add them unless you use Serial.print("TEXT\r\n")

DO NOT USE: AT+IPR= as this will brick it and require a reflash

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11, 10); // RX, TX

void setup() {
    // Open serial communications and wait for port to open:
    Serial.begin(115200);
    pinMode(11, INPUT);
    pinMode(10, OUTPUT);
    while (!Serial) {
      ; // wait for serial port to connect. Needed for native USB port only
    }
    Serial.println("ARDUINO: Starting");
    mySerial.begin(115200);
    Serial.println("ARDUINO: Sending AT Command");
    mySerial.println("AT");
}

void loop() { // run over and over
    if (mySerial.available()) {
        Serial.write(mySerial.read());
    }
    if (Serial.available()) {
        mySerial.write(Serial.read());
    }
}