From bf95fe6aad3b130a16e505a658c38f96937c0f0a Mon Sep 17 00:00:00 2001 From: Andy Liu Date: Tue, 29 Oct 2024 06:39:34 -0700 Subject: [PATCH] Update Float/Double in string (#31) Signed-off-by: Andy Liu --- .../11WiFi/TemperatureDataLogger/Sources/main.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Examples/SwiftIOPlayground/11WiFi/TemperatureDataLogger/Sources/main.swift b/Examples/SwiftIOPlayground/11WiFi/TemperatureDataLogger/Sources/main.swift index dfbb87e..c16c107 100644 --- a/Examples/SwiftIOPlayground/11WiFi/TemperatureDataLogger/Sources/main.swift +++ b/Examples/SwiftIOPlayground/11WiFi/TemperatureDataLogger/Sources/main.swift @@ -43,8 +43,8 @@ while true { if esp.wifiStatus == .ready { do { // Read temperature and humidity values from the sensor. - let temp = humiture.readCelsius() - let humidity = humiture.readHumidity() + let temp = getFloatString(humiture.readCelsius()) + let humidity = getFloatString(humiture.readHumidity()) // Send the values to ThingSpeak using HTTP POST requests to visualize them. _ = try esp.httpPost(url: "https://api.thingspeak.com/update?api_key=WCGQWXCBJA2WS03F&field1=\(temp)&field2=\(humidity)", headers: ["Content-Type: application/x-www-form-urlencoded"], timeout: 20000) } catch { @@ -54,4 +54,10 @@ while true { _ = try? esp.readLine(timeout: 1000) print("WiFi status: \(esp.wifiStatus)") } +} + +func getFloatString(_ num: Float) -> String { + let int = Int(num) + let frac = Int((num - Float(int)) * 100) + return "\(int).\(frac)" } \ No newline at end of file