Added RMS.

This commit is contained in:
yotia 2024-08-07 17:34:56 +03:00
parent 59bd2c5db3
commit 0b76884112
5 changed files with 65 additions and 20 deletions

View File

@ -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) {

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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)
}

27
main.go
View File

@ -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
}