Small program to inject temperatures from a isd csv file
This commit is contained in:
commit
b6d83c271a
2 changed files with 76 additions and 0 deletions
59
weather-generator-mqtt/csvdata-pub.go
Normal file
59
weather-generator-mqtt/csvdata-pub.go
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
"encoding/csv"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type sensordata struct {
|
||||||
|
Temperature float32
|
||||||
|
Humidity float32
|
||||||
|
Battery float32
|
||||||
|
Light float32
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
file, err := os.Open("06720099999.csv")
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
reader := csv.NewReader(file)
|
||||||
|
rows, err := reader.ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := MQTT.NewClientOptions().AddBroker("tcp://localhost:1883")
|
||||||
|
opts.SetClientID("go-sipmle-publish")
|
||||||
|
|
||||||
|
c := MQTT.NewClient(opts)
|
||||||
|
if token := c.Connect(); token.Wait() && token.Error() != nil {
|
||||||
|
panic(token.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
for n, col := range rows {
|
||||||
|
if n == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ts := col[13] // temp*10,qualitycheck
|
||||||
|
tempStr := ts[0:5]
|
||||||
|
tempInt, _ := strconv.Atoi(tempStr)
|
||||||
|
temp := float32(tempInt) / 10
|
||||||
|
fmt.Printf("Got temperature %f\n", temp)
|
||||||
|
d := sensordata{temp, 0, 0, 0}
|
||||||
|
text, _ := json.Marshal(d)
|
||||||
|
token := c.Publish("abqGF3nbCeYIYzOckDrU1vOq6uuU16rb/test/counter2/", 2, false, text)
|
||||||
|
if token.Wait() && token.Error() != nil {
|
||||||
|
panic(token.Error())
|
||||||
|
}
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
17
weather-generator-mqtt/isd-sources
Normal file
17
weather-generator-mqtt/isd-sources
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
"066090-99999",
|
||||||
|
"066270-99999",
|
||||||
|
"067090-99999",
|
||||||
|
"067120-99999",
|
||||||
|
"067140-99999",
|
||||||
|
"067160-99999",
|
||||||
|
"067170-99999",
|
||||||
|
"067180-99999",
|
||||||
|
"067200-99999",
|
||||||
|
"067205-99999",
|
||||||
|
SION "067220-99999", https://www.ncei.noaa.gov/data/global-hourly/access/2018/06720099999.csv
|
||||||
|
"067230-99999",
|
||||||
|
"067240-99999",
|
||||||
|
"074990-99999",
|
||||||
|
"160530-99999",
|
||||||
|
"160540-99999"
|
||||||
|
|
Reference in a new issue