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?