help! ESP8266 WifiWebServer + Controlling Arduino digital pins

Hi everyone,

excuse me if the question is obvious, I’m new at Arduino and I have a project that is needed to be submitted asap. I looked up everywhere but I couldn’t figure this out.

I have ESP8266-01, it works fine with the WifiWebServer example. However, it seems that I can only control the gpio pins in the module.

The project is to control multiple applications using relays that are connected to arduino digital pins.

#include <ESP8266WiFi.h>

const char* ssid = "***";
const char* password = "****";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);
      
  // prepare GPIO2
  pinMode(2, OUTPUT);
  digitalWrite(2, 0);

  pinMode(13, OUTPUT); // [color=red]*this is supposed to be the digital pin in the arduino*[/color]
  digitalWrite(13, 0);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
  
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();


  // Match the request
  int val;
  int val2;
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
    
  else if (req.indexOf("/p13/0") != -1) [color=red]*[/color]
    val2 = 0;
  else if (req.indexOf("/p13/1") != -1)
    val2 = 1;

    
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO2 according to the request
  digitalWrite(2, val);
  digitalWrite(13, val2);[color=red] *[/color]

  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected 
  // when the function returns and 'client' object is detroyed
}

I noticed that the connection instructions of the module is to connect RX-RX,TX-TX (unlike the Bluetooth module I used before) I don’t know if that has to do with the issue.

I also found a code for that purpose but it is for the module as access point, which doesn’t serve my needs. I tried to combine the two codes to one but I couldn’t make it work.

I think you need to be a little more specific than “it works fine”. How do you know?

And I am probably going to be YELLED at by the more experienced users of this forum, but I am not afraid to look stupid, it would not be the first time, but I think Rx to Rx and Tx to Tx for the default pins on Uno, Mega, etc. will allow serial monitor send/receive AT commands, but for SoftwareSerial.h library Rx to Tx and Tx to Rx.

ZeyJ:
I noticed that the connection instructions of the module is to connect RX-RX,TX-TX (unlike the Bluetooth module I used before) I don’t know if that has to do with the issue.

That’s just for programming the ESP8266. The Arduino is not used (only the USB interface).
If you just want to drive relays, you don’t need an Arduino, that would only complicate matters even more. Use a port expander or shift registers.

ZeyJ:
I also found a code for that purpose but it is for the module as access point, which doesn’t serve my needs. I tried to combine the two codes to one but I couldn’t make it work.

Then you have to study the code some more. My motto is: only use code that understand completely (i.e. every single character). If you don’t understand something, you have to do some research before just blindly copying and pasting it into your sketch.
This could be a place to start:
A Beginner’s Guide to the ESP8266

This is the code I used for my web interface to control the ESP’s outputs. It uses a fance asynchronous interface that’s synced across all connected clients.
https://github.com/tttapa/Projects/tree/master/ESP8266/Control%20Panel/Control-Panel-WebSocket

Pieter