ESP32 TCP/IP Server

Hi,

I'm new to the ESP32. I'm setting up a personal project to monitor voltage and current for a solar system via TCP/IP. I'm using LabVIEW to monitor the readings of 4 parameters . I've found some examples from the internet related to ESP32 wifiserver and made some modifications to the program. Previously, I used the ESP8266, but since ESP8266 only has 1 analog input, I tried using ESP32 which is more convenient. The ESP8266 program works fine together with the LabVIEW, however when using ESP32, the program doesn't read sensor readings. Can anyone help me identify where I might have gone wrong?

Thank you.

#include <WiFi.h>    
#define SERVER_PORT 8000    
const int V_Panel = 34;
const int V_batt = 35;
const int curr_panel = 36;
const int curr_load = 37;
const int led = 4;

const char* ssid = "zippo z78";       
const char* password = "flash76"; 

WiFiServer server(SERVER_PORT);   


float temp=0.0;
float current_IN = 0.00;
float current_LOAD =0.00;
float V_panel = 0.00;
float V_battery = 0.00;                                               
float Vout1 = 0.00;                                             
float Vout2 = 0.00;
float Vout5 = 0.00;
float Vout6 = 0.00;
float Vout7 = 0.00;
float Vout8 = 0.00;
float Vout9 = 0.00;
float R1 = 30900.0;
float R2 = 4700.0;
float R5 = 3300.0;
float R6 = 6800.0;
int i;
int Vpanel;
int Vbatt;
int ipanel;
int iload;


void setup() 
{              
     Serial.begin(115200);   
     Serial.println("");
     Serial.println("");
     WiFi.begin(ssid, password); 
     while (WiFi.status() != WL_CONNECTED)  
     { Serial.print("->");
       delay(200);
     }
     Serial.println("");
     Serial.println("WiFi Successfully Connected");   
     Serial.print("NodeMCU IP address: "); 
     Serial.println(WiFi.localIP());     
     server.begin();             
     Serial.println("NodeMCU as a Server Role Started");
     for(i=0;i<25;i++)    
     { digitalWrite(led,HIGH);
       delay(10);
       digitalWrite(led,LOW);
       delay(10);
     }
}

void loop()  
{       WiFiClient client = server.available();  
        if (client)                              
        {   Serial.println("Hi...New Client");   
            for(i=0;i<2;i++)                     
              delay(20);
              digitalWrite(led,LOW);
              //delay(20);
            }
            while(1)     
              
            { while(client.available())   
              {  digitalWrite(led,HIGH);   
                 delay(10);
                 digitalWrite(led,LOW);
                 delay(10);
                 uint8_t data =client.read();  
                 Serial.write(data);           
                 switch (data)                 
                 { 

                    case 'b':            // take sample of solar panel voltage
                                         
                     Vpanel = analogRead(V_Panel);
                     Vout1 = Vpanel*3.3/1023;
                     Vout2 = Vout1*(R1+R2)/R2;
                     V_panel= Vout2;
                     Serial.println(V_panel);
                     client.println(V_panel); 
                     delay(100);                                     
                   break;    
                   
                   case 'c':                  //Take sample of battery voltage (V)
                                         
                     Vbatt = analogRead(V_batt);
                     Vout5 = Vbatt*3.3/1023;
                     V_battery = Vout5*(R1+R2)/R2;
                     Serial.println(V_battery);
                     client.println(V_battery);                      
                     delay(100);                     
                   break;

                   case 'd':                // take sample of input current (A)
                                         
                     ipanel = analogRead(curr_panel);
                     Vout6 = ipanel*3.3/1023;
                     Vout7 = Vout6*((R5+R6)/R6);
                     current_IN = (Vout7-2.5)/0.185;
                     Serial.println(current_IN);
                     client.println(current_IN);                      
                     delay(100);
                     break;
                  
                   case 'e':                // take sample of load current (A)
                                          
                     iload = analogRead(curr_load);
                     Vout8 = iload*3.3/1023;
                     Vout9 = Vout8*((R5+R6)/R6);
                     current_LOAD = (Vout9-2.5)/0.185;
                     Serial.println(current_LOAD);
                     client.println(current_LOAD);                      
                     delay(100);
                     break;
                 } 
              }
             if(server.hasClient())               
             { return;               
             }
           }

}   


And what's the Sserial output when you connet LabVIEW to it via wifi?

Under the File menu, you may have Examples | WiFi | SimpleWiFiServer. The structure of the loop is

void loop(){
  WiFiClient client = server.available();   // listen for incoming clients
  if (client) {                             // if you get a client,
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        // do something with char
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

whereas you have

void loop() {
  WiFiClient client = server.available();
  if (client) {
  }
  while (1) {
    while (client.available()) {
      uint8_t data = client.read();
      // do something with char
    }
    if (server.hasClient()) {
      return;
    }
  }
}

Try the following the example.

Hi kenb4,

Thank you for your response. I have made some changes according to your advice on the program code shown earlier, and it has been successful. Here is the corrected code. Thank you.

#include <WiFi.h>    
#define SERVER_PORT 8000    
const int V_Panel = 34;
const int V_batt = 35;
const int curr_panel = 36;
const int curr_load = 37;
const int led = 4;
const char* ssid = "xxxxxxxxx";       
const char* password = "xxxxxxxxx"; 

WiFiServer server(SERVER_PORT);   

float current_IN = 0.00;
float current_val = 0.00;
float current_val2 = 0.00;
float current_LOAD =0.00;
float V_panel = 0.00;
float V_battery = 0.00;                                               
float Vout1 = 0.00;                                             
float Vout2 = 0.00;
float Vout5 = 0.00;
float Vout6 = 0.00;
float Vout7 = 0.00;
float Vout8 = 0.00;
float Vout9 = 0.00;
float R1 = 30900.0;
float R2 = 4700.0;
float R5 = 3300.0;
float R6 = 6800.0;
int i, Vpanel, Vbatt, ipanel, iload;

void setup() 
{              
     Serial.begin(115200);   
     Serial.println("");
     Serial.println("");
     WiFi.begin(ssid, password); 
     while (WiFi.status() != WL_CONNECTED)  
     { Serial.print("->");
       delay(200);
     }
     Serial.println("");
     Serial.println("WiFi Successfully Connected");   
     Serial.print("NodeMCU IP address: "); 
     Serial.println(WiFi.localIP());     
     server.begin();             
     Serial.println("NodeMCU as a Server Role Started");
     for(i=0;i<25;i++)    
     { digitalWrite(led,HIGH);
       delay(10);
       digitalWrite(led,LOW);
       delay(10);
     }
}

                    
        void loop(){
        WiFiClient client = server.available();   // listen for incoming clients
        if (client) {                                                // if you get a client,
        while (client.connected()) {                  // loop while the client's connected
        if (client.available()) {                           // if there's bytes to read from the client,
        uint8_t data =client.read();  
               
        switch(data) {

        case 'a':   {           
                        
        Vpanel = analogRead(V_Panel);
        Vout1 = Vpanel*3.3/1023;
        Vout2 = Vout1*(R1+R2)/R2;
        V_panel= Vout2;
        Serial.println(V_panel);
        client.println(V_panel); 
        delay(100);   
        break;

        case 'b': 
       
        Vpanel = analogRead(V_batt);
        Vout1 = Vpanel*3.3/1023;
        Vout2 = Vout1*(R1+R2)/R2;
        V_panel= Vout2;
        Serial.println(V_battery);
        client.println(V_battery);  
        delay(100);   
        break;

        case 'c':                  
                   
        ipanel = analogRead(curr_panel);
        Vout6 = ipanel*3.3/4096;
        Vout7 = Vout6*((R5+R6)/R6);
        current_val = (Vout7-2.5)/0.185;
        current_IN = (0.9676*current_val)+0.041;
        Serial.println(current_IN);
        client.println(current_IN);                      
        delay(100);
        break;

        case 'd':                
                
        iload = analogRead(curr_load);
        Vout8 = iload*3.3/4096;
        Vout9 = Vout8*((R5+R6)/R6);
        current_val2 = (Vout9-2.5)/0.185;
        current_LOAD = (0.9676*current_val2)+0.041;
        Serial.println(current_LOAD);
        client.println(current_LOAD);                      
        delay(100);
        break;
                  
        }
      }
                                                     
      }
    }
      client.stop();
      Serial.println("Client Disconnected.");
                                      
    } 
}
              
          
        
                          
  


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