From 0b76884112a211ac43998b52befc7e18771a1b41 Mon Sep 17 00:00:00 2001 From: yotia Date: Wed, 7 Aug 2024 17:34:56 +0300 Subject: [PATCH] Added RMS. --- api/modem/gps/nmea.go | 2 +- api/modem/gps/status.go | 38 ++++++++++++++++++++++++++++++-------- api/modem/modem.go | 15 +++++++++++++-- api/modem/sms/sms.go | 3 ++- main.go | 27 +++++++++++++++++++-------- 5 files changed, 65 insertions(+), 20 deletions(-) diff --git a/api/modem/gps/nmea.go b/api/modem/gps/nmea.go index 082e6c2..06cfe54 100644 --- a/api/modem/gps/nmea.go +++ b/api/modem/gps/nmea.go @@ -30,7 +30,7 @@ const ( nmeaFlagsMask = (1 << 18) - 1 - collectTimeout = 2 * time.Second + collectTimeout = 1 * time.Second ) func secondCountDownTimer(title string, logger *log.Logger, t time.Duration) { diff --git a/api/modem/gps/status.go b/api/modem/gps/status.go index 8333f54..368f2b5 100644 --- a/api/modem/gps/status.go +++ b/api/modem/gps/status.go @@ -7,10 +7,11 @@ import ( ) type Status struct { - GotResponses bool `json:"gotResponses"` - IsValidData bool `json:"isValidData"` - FoundSatelitesCount int `json:"foundSatelitesCount"` - ActiveSatelitesCount int `json:"activeSatelitesCount"` + GotResponses bool `json:"gotResponses"` + IsValidData bool `json:"isValidData"` + FoundSatelitesCount int `json:"foundSatelitesCount"` + ActiveSatelitesCount int `json:"activeSatelitesCount"` + Rms float32 `json:"rms"` // Root mean square } var StatusNil = Status{ @@ -18,6 +19,7 @@ var StatusNil = Status{ IsValidData: false, FoundSatelitesCount: 0, ActiveSatelitesCount: 0, + Rms: 0, } func (g *gps) GetStatus() (Status, error) { @@ -46,7 +48,7 @@ checkLoop: return StatusNil, fmt.Errorf("nmea invalid sentence: %s", s) } switch values[0][3:] { // Switch by content - case "GSV": + case "GSV": // Any satelites g.logger.Println("check GSV") // Check len if len(values) < 17 { @@ -80,7 +82,7 @@ checkLoop: continue checkLoop } st.FoundSatelitesCount = satCount - case "GSA": + case "GSA": // Active satelites g.logger.Println("check GSA") // Check len @@ -104,7 +106,7 @@ checkLoop: } } st.ActiveSatelitesCount = max(st.ActiveSatelitesCount, count) - case "RMC": + case "RMC": // Minimum GPS data g.logger.Println("check RMC") // Check len if len(values) < 12 { @@ -118,10 +120,30 @@ checkLoop: // 2 - is data valid or not // 3: - other data - // Msg index + // Is valid value if values[2] == "A" { st.IsValidData = true } + case "GST": + g.logger.Println("check GST") + // Check len + if len(values) < 8 { + g.logger.Println("GST too small values") + continue checkLoop + } + + // Decode + // 0 - msg type + // 1 - time + // 2 - Root Mean Square + // 3: - other data + + rms, err := strconv.ParseFloat(values[2], 32) + if err != nil { + g.logger.Println("RMS decode:", err.Error()) + continue checkLoop + } + st.Rms = float32(rms) } } diff --git a/api/modem/modem.go b/api/modem/modem.go index 7234773..84728ca 100644 --- a/api/modem/modem.go +++ b/api/modem/modem.go @@ -54,6 +54,9 @@ type Modem interface { Update() error GetData() ModemData + PowerOn() error + PowerOff() error + // Access to SMS, GPS, AT interfaces mostly for debug At() at.Port // Send Gps() gps.Gps // Update, GetData, GetStatus @@ -81,8 +84,6 @@ func (m *modem) Init() error { if err := m.onOffPin.Init(); err != nil { return fmt.Errorf("gpio pin init: %w", err) } - m.logger.Println("TURNING ON IS COMMENTED NOW!!!") - // m.onOffPin.PowerOn() // DEBUG do not want to wait 30 seconds // Search m.logger.Println("=============================== Search") @@ -169,6 +170,16 @@ func (m *modem) GetData() ModemData { } } +func (m *modem) PowerOn() error { + m.onOffPin.PowerOn() // DEBUG do not want to wait 30 seconds + return nil +} + +func (m *modem) PowerOff() error { + _, err := m.At().Send("AT+CPOF") + return err +} + func (m *modem) Sms() sms.Sms { return m.sms } diff --git a/api/modem/sms/sms.go b/api/modem/sms/sms.go index 7ca39a7..eb17c6b 100644 --- a/api/modem/sms/sms.go +++ b/api/modem/sms/sms.go @@ -44,6 +44,7 @@ func (d *dialer) Init() error { } // Ensure text format + d.logger.Println(d.port.Send("AT+CMGF")) if resp, err := d.port.Send("AT+CMGF=1"); err != nil || !resp.Check() { if err != nil { return fmt.Errorf("set to text format request: %w", err) @@ -74,7 +75,7 @@ func (d *dialer) Send(number, msg string) error { // Reads all new messages func (d *dialer) ReadNew() ([]string, error) { - resp, err := d.port.Send("AT+CMGL=\"ALL\"") + resp, err := d.port.Send("AT+CMGL=\"UNREAD\"") if err != nil { return nil, fmt.Errorf("AT+CMGL request: %w", err) } diff --git a/main.go b/main.go index 6d09e7a..35021d4 100644 --- a/main.go +++ b/main.go @@ -20,25 +20,34 @@ func mainE() error { m := modem.New(log.New(logger.Writer(), "modem : ", log.LstdFlags)) logger.Println("||||||||||||||||| INIT |||||||||||||||") if err := m.Init(); err != nil { - return err + logger.Println("Init ended with error:", err.Error()) + logger.Println("Try to turn on") + if err := m.PowerOn(); err != nil { + return err + } + logger.Println("Reinit") + if err := m.Init(); err != nil { + return err + } } if !m.IsConnected() { logger.Println("AAAAAAAAAAAAAAA Modem is not connected") return nil } logger.Println("||||||||||||||||| GET INFO |||||||||||||||||") + logger.Println(m.Update()) logger.Printf("DATA: %+v\n", m.GetData()) - // logger.Println("||||||||||||||||| SEND SMS |||||||||||||||||") - // logger.Println(m.At().Send("AT+CNUM")) + logger.Println("||||||||||||||||| SEND SMS |||||||||||||||||") + logger.Println(m.At().Send("AT+CNUM")) // if err := m.Sms().Send("+79218937173", "CGSG forever"); err != nil { // return err // } - // if ms, err := m.Sms().ReadNew(); err != nil { - // return err - // } else { - // logger.Println("NEW:", ms) - // } + if ms, err := m.Sms().ReadNew(); err != nil { + return err + } else { + logger.Println("NEW:", ms) + } logger.Println("||||||||||||||||| Checking gps status |||||||||||||||||") st, err := m.Gps().GetStatus() if err != nil { @@ -46,5 +55,7 @@ func mainE() error { } logger.Printf("GPS Status:%+v\n", st) + // logger.Println("Turn off", m.PowerOff()) + return nil }