Hello,
I have following working well:
// Prepare your HTTP POST request data
String httpRequestData = "api_key=" + apiKeyValue + "&cb_serial=" + serial + "&temp=" + String(sensors.getTempCByIndex(0)) + "&sump=" + String(digitalRead(FLOAT_SENSOR)) + "";
String(digitalRead(FLOAT_SENSOR) now gives me output 0 or 1 (writes it to mysql database).
I would like to give me when 0 some text and when 1 some other text.
How to accomplish ?
Thanks.
You could use the ternary operator.
Please remember to use code tags when posting code
Something like this ? No way of doing this shorter ?
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
if (digitalRead(FLOAT_SENSOR) == LOW) {
// Prepare your HTTP POST request data
String httpRequestData = "api_key=" + apiKeyValue + "&cb_serial=" + serial + "&temp=" + String(sensors.getTempCByIndex(0)) + "&sump=" + "Low" + "";
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
// Prepare your HTTP POST request data
String httpRequestData = "api_key=" + apiKeyValue + "&cb_serial=" + serial + "&temp=" + String(sensors.getTempCByIndex(0)) + "&sump=" + "Ok" + "";
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
blh64
4
You certainly do not have to repeat everything twice, only the part that changes which is "Low" vs. "Ok"
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
String sensor_msg;
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
if (digitalRead(FLOAT_SENSOR) == LOW) {
sensor_msg = "Low";
}
else {
sensor_msg = "Ok";
}
// Prepare your HTTP POST request data
String httpRequestData = "api_key=" + apiKeyValue + "&cb_serial=" + serial + "&temp=" + String(sensors.getTempCByIndex(0)) + "&sump=" + sensor_msg;
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
system
Closed
5
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.