2

I'm trying to print out the temperature from a DS18B20 sensor to my app via a tpc connection. Because I have two sensors connected I will need to call the one needed by an Index. This index comes from my app and is a char value which I need to convert it to an int for comparision later. The problem I have is the conversion from to input char value to int. My code:

void loop() {
  //setting default sensor to 0
  sensorindex = 0;
  //starting server
  WiFiClient client = wifiServer.available();

  if (client) {
  Serial.write("Client connected");
          client.println("connected");
    while (client.connected()) {
      while (client.available()>0) {

        //instream from client/mobile device
        char c = client.read();
        //converting char c to int
        sensorindex = String(c).toInt() ;  
        Serial.println(sensorindex);
      }

      //comparing client input to DS18B20 sensor index
      sensors.requestTemperatures();
      if(sensorindex == 1){
           client.println(sensors.getTempCByIndex(1));
           Serial.println("Index 1");
      }
      if(sensorindex == 0){
           client.println(sensors.getTempCByIndex(0));
             Serial.println("Index 0");
      }

      delay(50);
    }

    client.stop();
    Serial.println("Client disconnected");
  }
}

The serial output:

Client connectedIndex 0
Index 0
Index 0
Index 0
Index 0
Index 0
1
0
Index 0
Index 0
Index 0
Index 0
Index 0
Index 0
Index 0
Index 0
Client disconnected

When I type in 1 from my android app it shows

1
0

in the output. What am I doing wrong?

12
  • why do you present a sketch with WiFi if you ask about Serial input conversion?
    – Juraj
    Commented Feb 26, 2019 at 18:35
  • @Juraj It's the same in the end because I'm I will get a char value in both cases which I need to convert to an integer. And I've never asked directly for a serial input conversion but rather the wifi input, which is basically the same
    – Janik
    Commented Feb 26, 2019 at 19:02
  • so while client is connected you print the output based on the last sensorindex value. why?
    – Juraj
    Commented Feb 26, 2019 at 19:22
  • So it refreshed the app every 50ms to show the current temp of the sensor which is chosen by the index value, which is based on the input which came from the app
    – Janik
    Commented Feb 26, 2019 at 19:40
  • so the only thing that bothers you is the 0 after the 1?
    – Juraj
    Commented Feb 26, 2019 at 19:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.