commit b6d83c271a59acd1772078a977ada53962bbe179 Author: Thomas Schwery Date: Fri Apr 20 09:25:51 2018 +0200 Small program to inject temperatures from a isd csv file diff --git a/weather-generator-mqtt/csvdata-pub.go b/weather-generator-mqtt/csvdata-pub.go new file mode 100644 index 0000000..21aeb16 --- /dev/null +++ b/weather-generator-mqtt/csvdata-pub.go @@ -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) + } +} diff --git a/weather-generator-mqtt/isd-sources b/weather-generator-mqtt/isd-sources new file mode 100644 index 0000000..1d4a19e --- /dev/null +++ b/weather-generator-mqtt/isd-sources @@ -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" +