-1

I have connected an ESP8266-01 module to an Arduino as in the following schematic with the RX and TX pins changed to pins 8 and 9 instead of 0 and 1:

enter image description here

I am using the ESP8266wifi library at the following link:

https://github.com/ekstrand/ESP8266wifi

I have looked at the ESP8266HTTPClient library but that is for the ESP8266 board. I want my code to run on the Arduino Uno and not on the ESP8266! I have written the following code:

#include <SoftwareSerial.h>
#include <SerialESP8266wifi.h>

const String API_PASSWORD = "stn1234";

#define RX_PIN          8   // RX of Arduino (connected to TX of ESP8266)
#define TX_PIN          9   // TX of Arduino (connected to RX of ESP8266)
#define ESP_RESET_PIN   12 

#define SERVER_IP           "192.168.43.234"
#define SERVER_PORT         "80"
#define WIFI_SSID           "MyWIFI"
#define WIFI_PASSWORD       "myWifiPassword"

uint8_t wifi_started = false;

const char RST[] PROGMEM = "RST";
const char IDN[] PROGMEM = "*IDN?";

#define CHANGE_DELAY        5000


void initializeWifi();
void transmitData(String);
String getResponse();
void transmitStatus();

SoftwareSerial swSerial =  SoftwareSerial(RX_PIN, TX_PIN);
SerialESP8266wifi wifi(swSerial, swSerial, ESP_RESET_PIN, Serial);

void setup() 
{
    Serial.begin(9600);
    Serial.println("Initializing...");
    initializeWifi();
    Serial.println("Initialization complete");
}

void loop() 
{
    transmitStatus();
    Serial.print("Transmission complete, ");
    int s = (CHANGE_DELAY/1000);
    Serial.print(s);
    Serial.println("s until next cycle");
    delay(CHANGE_DELAY);
}

void transmitStatus()
{
    int a = (int)random(100);
    int b = (int)random(100);
    String url = "/LocalWebProject/api/testConnection.php?a=" + String(a) + "&b=" + String(b);

    transmitData(url);
    String response = getResponse();
    Serial.print("Reply received: ");
    Serial.println(response);
}

void initializeWifi()
{
    swSerial.begin(115200);
    Serial.println("Starting Wifi");
    wifi.setTransportToTCP();
    wifi.endSendWithNewline(false);     // Will end all transmissions with a newline and carrage return ie println.. default is true

    wifi_started = wifi.begin();
    if(wifi_started) 
    {
        Serial.println("Wifi started, connecting to AP...");
        wifi.connectToAP(WIFI_SSID, WIFI_PASSWORD);
        Serial.println("Connected to AP");
    } else {
        Serial.println("ESP module not working");
    }
}

void transmitData(String url)
{
    Serial.println("Connecting to server...");
    wifi.connectToServer(SERVER_IP, SERVER_PORT);

    String data = String("GET ") + url + " HTTP/1.1\r\n" +
           "Host: " + wifi.getIP() + "\r\n" + 
           "Connection: close\r\n\r\n";

    Serial.println("Connected. Transmitting data...");
    wifi.send(SERVER, data);
}

String getResponse()
{
    while(1)
    {
        Serial.println("Waiting for response...");
        WifiMessage in = wifi.listenForIncomingMessage(6000);
        if(in.hasData) 
        {
            if(in.channel == SERVER)
            {
                Serial.print("Reply received from the server: ");
                Serial.println(in.message);             
                return in.message;
            }
        }

        delay(1000);
        Serial.print("Retrying, ");
    }
}

I have an Apache server running on Ubuntu 18.10 with a PHP project running on port 80. The testConnection.php page takes 2 GET parameters (integers) and returns the sum.

Problem: It says that ESP module is not working. The AP in my case is a local hotspot on my mobile.

I have tried running the given example "SerialESP8266_library_test" and confusingly enough, it prints that it has successfully connected to my AP, but on my phone I do not see it connected. Only 1 device is visible as connected, which is my laptop which is running the Apache server and the web project. Also connectToAP returns 1, and connectToServer returns 0.

What am I doing wrong? I know that the ESP module requires power and the Arduino 3V3 is insufficient but I am just testing the code right now and I will do the required changes later on. Due to this, I am placing my Android phone just beside the ESP module so that it has no problems connecting to it.

Any help would be greatly appreciated! Thanks in advance.

  • 1
    swSerial.begin(115200). I don't think softwareSerial can even run that fast. The library you are using for wifi uses 9600 baud. I'd give that a try. – Gerben Nov 23 '19 at 13:47
  • @Gerben Some modules are 9600, some are 115200 - it depends on the version of the firmware. SoftwareSerial can run at 115200, but it can't react fast enough to incoming bytes to reliably receive a stream of data. – Majenko Nov 23 '19 at 14:07
  • @Juraj The WiFiEspAt requires flashing with AT 1.7, I did not want to do that – APN Nov 23 '19 at 14:08
  • @Gerben I tried with 9600 and made some other changes. Now, it correctly connects to the AP (shows up on my phone), also connects to the server. But receives a HTTP 400 Bad request error. Is there any problem in the GET request format? My page is at: 192.168.43.234/LocalWebProject/api/testConnection.php – APN Nov 23 '19 at 14:10
0

Finally it is working by setting baud rate to 9600 and shortening the GET URL. Maybe there is a length restriction in the library.

| improve this answer | |

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.