Drag to orbit · scroll to zoom
Single-phase DIN-rail meter (DDS238-2 ZN/S, 2000 imp/kWh) feeding an ESP32 over its pulse output.
The phone mirrors the 3D meter. Move the load slider above past the app’s limit to trigger a high-load push, let the daily budget run out, or tap Drop Wi-Fi to see the offline alert.
iOS suspends the app in the background, so real push alerts need a small server (Node-RED or a cloud function) that watches the MQTT topic and sends Apple Push Notifications. The app itself only needs the live view.
import SwiftUI
import CocoaMQTT
struct Reading: Decodable { let kw: Double; let kwh: Double }
final class MeterVM: ObservableObject {
@Published var kw = 0.0, kwh = 0.0
let mqtt = CocoaMQTT(clientID: "hoko-ios",
host: "broker.example.com", port: 1883)
func start() {
mqtt.didReceiveMessage = { _, m, _ in
guard let r = try? JSONDecoder()
.decode(Reading.self, from: Data(m.payload)) else { return }
DispatchQueue.main.async {
self.kw = r.kw; self.kwh = r.kwh }
}
_ = mqtt.connect()
mqtt.subscribe("hoko/site1/meter1/energy")
}
}
struct ContentView: View {
@StateObject var vm = MeterVM()
var body: some View {
VStack {
Text(String(format: "%.1f kW", vm.kw)).font(.largeTitle)
Text(String(format: "%.2f kWh", vm.kwh))
}.onAppear { vm.start() }
}
}