Home » Java » c++ – Comunication betwen ESP8266 and Java getting slower-Exceptionshub

c++ – Comunication betwen ESP8266 and Java getting slower-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Im planning to make iot project.
My plan is to have connected phone and esp8266 to java server and get and send data to server.
Comunication between phone and server and phone works well.
When I connect esp8266 to server comunication is working but after some time connection is getting slower and slower.
My arduino code:

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#ifndef STASSID
#define STASSID "WIFI"
#define STAPSK  "*****"
#endif


ESP8266WiFiMulti WiFiMulti;
WiFiClient client;

bool rgbOn = false;
byte rgbRed = 0;
byte rgbGreen = 0;
byte rgbBlue = 0;

bool a01On = false;
byte a01Value = 0;

const char* ssid     = STASSID;
const char* password = STAPSK;

const char* host = "server";
const uint16_t port = 38182;
void setup() {
  Serial.begin(115200);
  pinMode(D7,OUTPUT);
  pinMode(D2,OUTPUT);
  pinMode(D3,OUTPUT);
  pinMode(D4,OUTPUT);
  // We start by connecting to a WiFi network
  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP(ssid, password);

  Serial.println();
  Serial.println();
  Serial.print("Wait for WiFi... ");

  while (WiFiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.print("connecting to ");
  Serial.print(host);
  Serial.print(':');
  Serial.println(port);

  while(!client.connect(host, port)){
    Serial.println("connection failed");
    Serial.println("wait 5 s eec...");
    delay(5000);
  }
  Serial.println("Connected");
  Serial.println(client.connected());
}
//========================main loop=============================================
void loop() {
  client.println("?hb");
  if(client.available()>0){
    String line = client.readStringUntil('\n');
    if(!line.startsWith("?hb")){
      Serial.println(line);
      int index = line.indexOf(';');
      int lightId = line.substring(0,index).toInt();
      bool on = false;
      line = line.substring(index+1);
      index = line.indexOf(';');
      if(line.startsWith("true")){
        on = true;
      }
      line = line.substring(index+1);

      index = line.indexOf(',');
      byte value=0;
      byte value2=0;
      byte value3=0;
      if(index==-1){
        value = line.toInt();
      }else{
        value = line.substring(0,index).toInt();
        line = line.substring(index+1);

        index = line.indexOf(',');
        value2 = line.substring(0,index).toInt();
        line = line.substring(index+1);
        value3 = line.toInt();

      }
      switch(lightId){
        case 1:
          rgbOn=on;
          rgbRed=value;
          rgbGreen=value2;
          rgbBlue=value3;
          break;
        case 2:
          a01On=on;
          a01Value=value;
          break;
      }
    }
  }
  updateLeds();
  delay(1);

}
void updateLeds(){
  if(!rgbOn){
    digitalWrite(D2,0);
    digitalWrite(D3,0);
    digitalWrite(D4,0);
  }else{
    analogWrite(D2,map(rgbRed,0,255,0,120));
    analogWrite(D3,rgbGreen);
    analogWrite(D4,map(rgbBlue,0,255,0,140));
  }
  if(!a01On){
    digitalWrite(D7,0);
  }else{
    analogWrite(D7,a01Value);
  }
}

Part of my java code

public void run(){
        try{
            ip = socket.getInetAddress().getHostAddress();
            outStream = new PrintStream(socket.getOutputStream());
            inStream = new Scanner(socket.getInputStream());
            Data.clients.add(this);
            id=Data.clients.indexOf(this);
            while(!exit){
                outStream.println("?hb");
                for(Entry<Integer, Light> entry : Data.lights.entrySet()) {
                    if(entry.getValue().getEditCode()!=editCodes.get(entry.getKey())) {//if changed
                        editCodes.put(entry.getKey(),entry.getValue().getEditCode());
                        String out = entry.getValue().getId()+";"+entry.getValue().isState()+";"+(entry.getValue() instanceof RGBLight?((RGBLight)entry.getValue()).getRed()+","+((RGBLight)entry.getValue()).getGreen()+","+((RGBLight)entry.getValue()).getBlue():((NormalLight)entry.getValue()).getValue());
                        outStream.println(out);
                    }
                }
                if(inStream.hasNextLine()) {
                    String in = inStream.nextLine();
                    if(!in.startsWith("?hb")) {

                        String[] strings=in.split(";");
                        if(strings.length==3) {
                            Light light = Data.lights.get(Integer.parseInt(strings[0]));
                            if(light!=null) {
                                //System.out.println("OK");
                                light.increaseEditCode();
                                editCodes.put(light.getId(),light.getEditCode());
                                light.setState(Boolean.parseBoolean(strings[1]));
                                String[] values = strings[2].split(",");
                                if(values.length>1) {
                                    ((RGBLight)light).setRed((short) Integer.parseInt(values[0]));
                                    ((RGBLight)light).setGreen((short) Integer.parseInt(values[1]));
                                    ((RGBLight)light).setBlue((short) Integer.parseInt(values[2]));
                                }else {
                                    ((NormalLight)light).setValue((short) Integer.parseInt(values[0]));
                                }
                            }
                        }


                    }

                }

            }
            exit();
        }
        catch(Exception e){
            e.printStackTrace();
            exit();
        }

    }
How to&Answers: