Can't use millis() and receive something from the server with nodemcu

I’m using a nodemcu development board. If I’m running the codes separately i.e. momentary push button string, voltage reading and receiving string from server, they’re all working fine but when i’m putting it together the client is not sending anything.
Also it’s not receiving anything the server is sending. The server might send a string at anytime and not only when the client sends something.
The client can send strings when I remove the millis() function from the code but then the analog reading is done continuously which incurs battery drainage. The analog pin is reading the battery voltage only. The same problem is happening if I’m using a delay functions.
I’m using a momentary push button to send a string to the server.

Here’s the following code:

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

int wifiStatus;

unsigned long previousMillis = 0;
const long interval = 6000;

IPAddress server(192,168,0,80);
WiFiClient client;

int red1 = 4;

void setup() {
Serial.begin(9600);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(15, OUTPUT);

pinMode(red1, INPUT_PULLUP);

WiFiManager wifiManager;

wifiManager.autoConnect(“abcd”, “12345678”);

}

void loop() {

wifiStatus = WiFi.status();

if(wifiStatus == WL_CONNECTED){
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);
}

client.connect(server, 80);

red1 = digitalRead(4);

digitalWrite(red1, HIGH);

if (red1 == LOW) {
client.println(“RU2\r”);
}

if (client.available()) {
String c = client.readStringUntil(’\r’);
if (c == “vibrate”){
digitalWrite(12, HIGH);
}
}

int sensorValue = analogRead(A0);
float voltage = sensorValue * (3.3 / 1024.0);
Serial.println(voltage * 11.2);
if (voltage * 11.2 <= 3.5){
digitalWrite(15, HIGH);
}
else{
digitalWrite(15, LOW);
}
}

Please post within </> code-tags.
Here

red1 = digitalRead(4);


   digitalWrite(red1, HIGH);

you do a digitalWrite() to either pin 1 or 0. Is that what you meant to do ? is suspect not also since you do Serial.println(voltage * 11.2); and pin 1 is the TX pin.
So where is that call to millis() ?
anyway, please update your post, also please use ctrl-T for auto-format the indent and remove all the useless empty lines, so it becomes a lot easier to read for others.

see the linkWithoutDelay example

I’m using a nodemcu development board. If I’m running the codes separately i.e. momentary push button string, voltage reading and receiving string from server, they’re all working fine but when i’m putting it together the client is not sending anything.
Also it’s not receiving anything the server is sending. The server might send a string at anytime and not only when the client sends something.
The client can send strings when I remove the millis() function from the code but then the analog reading is done continuously which incurs battery drainage. The analog pin is reading the battery voltage only. The same problem is happening if I’m using a delay functions.
I’m using a momentary push button to send a string to the server and take voltage readings at regular interval of times. Also without delay or millis() how can I make esp sends 10-20 strings with a single push of the momentary button and if I use delay or millis() it sends nothing.

Here’s the code:

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

int wifiStatus;

unsigned long previousMillis = 0;
const long interval = 6000;

IPAddress server(192,168,0,80);
WiFiClient client;

int red1 = 4;

void setup() {
Serial.begin(9600);
pinMode(12, OUTPUT);
pinMode(2, OUTPUT);
pinMode(15, OUTPUT);

pinMode(red1, INPUT_PULLUP);

digitalWrite(2, HIGH);

WiFiManager wifiManager;

wifiManager.autoConnect(“DO Sports Joystick”, “dosports”);

}

void loop() {

wifiStatus = WiFi.status();

if(wifiStatus == WL_CONNECTED){
digitalWrite(2, LOW);
}
else{
digitalWrite(2, HIGH);
}

client.connect(server, 80);

red1 = digitalRead(4);

digitalWrite(red1, HIGH);

if (red1 == LOW) {
client.println(“RU2\r”);
}

if (client.available()) {
String c = client.readStringUntil(’\r’);
if (c == “vibrate”){
digitalWrite(12, HIGH);
}
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
int sensorValue = analogRead(A0);
float voltage = sensorValue * (3.3 / 1024.0);
Serial.println(voltage * 11.2);
}
}

sketch_feb12a.ino (1.28 KB)

The server might send a string at anytime

Servers do NOT push anything. They respond ONLY to client requests.

int red1 = 4;

What the heck does red1 mean?

     pinMode(red1, INPUT_PULLUP);

Not at all what I guessed red1's mode would be. Why do you have an INPUT uselessly called red1?

   red1 = digitalRead(4);

That makes not a lick of sense.

   digitalWrite(red1, HIGH);

And that makes even less sense.

You REALLY need to think about what you are doing. Use Pin in the names of all variables that are supposed to hold pin numbers, AND make the variables const, so you don't do stupid stuff like this.

You are writing to the HIGH or LOW pin, which is NOT what you want to be doing. That WILL interfere with your Serial input/output.

if (red1 == LOW) {
  client.println("RU2\r");
  }

Writing to the client, before testing that there IS a client to write to is just plain wrong.

Sharing your serial output would be a good idea.