Skip to content

Commit

Permalink
Added reception-logger adn check-gps
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronRP committed Aug 15, 2023
1 parent cc4be4b commit 6f71702
Show file tree
Hide file tree
Showing 10 changed files with 425 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/dist
modemd
/modemd
30 changes: 28 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,43 @@ release:
name_template: '{{.Tag}}'

builds:
- goos:
- id: modemd
goos:
- linux
goarch:
- arm
- arm64
goarm:
- "7"
main: .
main: ./cmd/modemd
ldflags: -s -w -X main.version={{.Version}}
binary: modemd

- id: reception-logger
goos:
- linux
goarch:
- arm
- arm64
goarm:
- "7"
main: ./cmd/reception-logger
ldflags: -s -w -X main.version={{.Version}}
binary: reception-logger

- id: check-gps
goos:
- linux
goarch:
- arm
- arm64
goarm:
- "7"
main: ./cmd/check-gps
ldflags: -s -w -X main.version={{.Version}}
binary: check-gps


nfpms:
- vendor: The Cacophony Project
homepage: http://cacophony.org.nz/
Expand Down
6 changes: 3 additions & 3 deletions _release/postinstall.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
systemctl daemon-reload
systemctl enable modemd.service
systemctl restart modemd.service
#systemctl daemon-reload
#systemctl enable modemd.service
#systemctl restart modemd.service
273 changes: 273 additions & 0 deletions cmd/check-gps/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"

"github.com/tarm/serial"
)

type gpsData struct {
latitude float64
longitude float64
utcDateTime time.Time
altitude float64
speed float64
course float64
}

func main() {
log.SetFlags(0)

log.Println("Checking if GPS is enabled")
out, err := runATCommand("AT+CGPS?")
if err != nil {
log.Fatal(err)
}
if out != "+CGPS: 1,1" {
log.Println("Enabling GPS, GPS state was ", out)
_, err := runATCommand("AT+CGPS=1")
if err != nil {
log.Fatal(err)
}
} else {
log.Println("GPS already enabled")
}

//Loop checking for GPS connection.
for {
out, err := runATCommand("AT+CGPSINFO")
if err != nil {
log.Fatal(err)
}
log.Println(out)
//out = "+CGPSINFO: 4333.256890,S,17237.550876,E,100823,033054.0,10.2,0.0,"
out = strings.TrimSpace(out)
out = strings.TrimPrefix(out, "+CGPSINFO:")
out = strings.TrimSpace(out)
parts := strings.Split(out, ",")
gps, err := processGPSOut(parts)
if err != nil {
log.Println(err)
}
log.Printf("%+v", gps)

//for _, part := range parts {
// log.Println("part:", part)
// }
time.Sleep(5 * time.Second)
}
}

//+CGPSINFO: 4333.256890,S,17237.550876,E,100823,033054.0,10.2,0.0,

func processGPSOut(parts []string) (*gpsData, error) {
if len(parts) < 8 {
return nil, fmt.Errorf("invalid GPS format")
}
latRaw := parts[0]
latNSRaw := parts[1]
longRaw := parts[2]
longEWRaw := parts[3]
utcDateRaw := parts[4]
utcTimeRaw := parts[5]
altitudeRaw := parts[6]
speedRaw := parts[7]
courseRaw := parts[8]
//log.Println("latRaw:", latRaw)
//log.Println("latNSRaw:", latNSRaw)
//log.Println("longRaw:", longRaw)
//log.Println("longEW:", longEWRaw)
//log.Println("utcDateRaw:", utcDateRaw)
//log.Println("utcTimeRaw:", utcTimeRaw)
//log.Println("altitude:", altitudeRaw)
//log.Println("speed:", speedRaw)
//log.Println("course:", courseRaw)

if latRaw == "" {
return nil, nil
}
if string(latRaw[4]) != "." {
return nil, fmt.Errorf("invalid latitude")
}
latDeg, err := strconv.ParseFloat(latRaw[:2], 64)
if err != nil {
return nil, err
}
latMinute, err := strconv.ParseFloat(latRaw[2:], 64)
if err != nil {
return nil, err
}
latDeg += latMinute / 60
if latNSRaw == "S" {
latDeg *= -1
} else if latNSRaw != "N" {
return nil, fmt.Errorf("invalid latitude direction")
}
log.Println("latDeg:", latDeg)

if string(longRaw[5]) != "." {
return nil, fmt.Errorf("invalid longitude")
}
longDeg, err := strconv.ParseFloat(longRaw[:3], 64)
if err != nil {
return nil, err
}
longMinute, err := strconv.ParseFloat(longRaw[3:], 64)
if err != nil {
return nil, err
}
longDeg += longMinute / 60
if longEWRaw == "W" {
latDeg *= -1
} else if longEWRaw != "E" {
return nil, fmt.Errorf("invalid longitude direction")
}
log.Println("longDeg:", longDeg)

const layout = "020106-150405.0" // format DDMMYY-hhmmss.s
dateTime, err := time.Parse(layout, utcDateRaw+"-"+utcTimeRaw)
log.Println(dateTime.Local().Format("2006-01-02 15:04:05"))
if err != nil {
return nil, err
}

altitude, err := strconv.ParseFloat(altitudeRaw, 64)
if err != nil {
return nil, err
}
log.Println(altitude)

speed, err := strconv.ParseFloat(speedRaw, 64)
if err != nil {
return nil, err
}
log.Println(speed)

var course float64
if courseRaw != "" {
course, err = strconv.ParseFloat(courseRaw, 64)
if err != nil {
return nil, err
}
}
log.Println(course)

return &gpsData{
latitude: latDeg,
longitude: longDeg,
utcDateTime: dateTime,
altitude: altitude,
speed: speed,
course: course,
}, nil
}

func runATCommand(atCommand string) (string, error) {
log.Printf("Running '%s'", atCommand)

c := &serial.Config{Name: "/dev/ttyUSB3", Baud: 115200}
s, err := serial.OpenPort(c)
if err != nil {
log.Fatal(err)
}
defer s.Close()

_, err = s.Write([]byte(atCommand + "\r"))
if err != nil {
log.Fatal(err)
}

// Read and log the response with timestamp
reader := bufio.NewReader(s)
failed := false
for {
//TODO Add timeout check
line, err := reader.ReadString('\n')
line = strings.TrimSpace(line)
if err != nil {
log.Fatal(err)
}
if line == "ERROR" {
failed = true
break
}
if strings.HasPrefix(line, "+") {
return line, nil
}
}
if failed {
return "", fmt.Errorf("AT command failed")
}
return "", nil
}

// ConvertLong converts a longitude in format dddmm.mmmmmm to degrees.
func ConvertLong(long string) (float64, error) {
if len(long) < 7 {
return 0, fmt.Errorf("Invalid longitude format")
}

// Split the longitude string into degrees and minutes.
degreesPart := long[:3]
minutesPart := long[3:]

degrees, err := strconv.ParseFloat(degreesPart, 64)
if err != nil {
return 0, err
}

minutes, err := strconv.ParseFloat(minutesPart, 64)
if err != nil {
return 0, err
}

// Convert minutes to degrees.
minutesInDegrees := minutes / 60.0

return degrees + minutesInDegrees, nil
}

// ConvertLat converts a latitude in format ddmm.mmmmmm to degrees.
func ConvertLat(lat string) (float64, error) {
if len(lat) < 6 {
return 0, fmt.Errorf("Invalid latitude format")
}

// Split the latitude string into degrees and minutes.
degreesPart := lat[:2]
minutesPart := lat[2:]

degrees, err := strconv.ParseFloat(degreesPart, 64)
if err != nil {
return 0, err
}

minutes, err := strconv.ParseFloat(minutesPart, 64)
if err != nil {
return 0, err
}

// Convert minutes to degrees.
minutesInDegrees := minutes / 60.0

return degrees + minutesInDegrees, nil
}

func appendToFile(filename string, data string) error {
// Open the file in append mode or create if it doesn't exist
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()

// Write the data to the file with a new line
_, err = file.WriteString(data + "\n")
return err
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion modemController.go → cmd/modemd/modemController.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (mc *ModemController) SetModemPower(on bool) error {
return err
}

pin := gpioreg.ByName("GPIO16")
pin := gpioreg.ByName("GPIO22")
if pin == nil {
return fmt.Errorf("failed to init GPIO16 pin")
}
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 6f71702

Please sign in to comment.