aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.ino
blob: fe4ef95212b5c980a9d47d53dc465e223895b010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <Arduino.h>
#include <RadioLib.h>
#include <Wire.h>
#include <SparkFun_u-blox_GNSS_Arduino_Library.h>

// SX1278 has the following connections:
// NSS pin:   10
// DIO0 pin:  2
// RESET pin: 9
// DIO1 pin:  3
RFM96 radio = new Module(10, 2, 9, 3);

HardwareSerial SerialGNSS(PD6, PD5);
SFE_UBLOX_GNSS gnss;
long lastGnssPoll = 0;

static char letterize(int x) {
    return (char) x + 65;
}
static char* get_mh(double lat, double lon, int size) {
    static char locator[11];
    double LON_F[]={20,2.0,0.083333,0.008333,0.0003472083333333333};
    double LAT_F[]={10,1.0,0.0416665,0.004166,0.0001735833333333333};
    int i;
    lon += 180;
    lat += 90;

    if (size <= 0 || size > 10) size = 6;
    size/=2; size*=2;

    for (i = 0; i < size/2; i++){
        if (i % 2 == 1) {
            locator[i*2] = (char) (lon/LON_F[i] + '0');
            locator[i*2+1] = (char) (lat/LAT_F[i] + '0');
        } else {
            locator[i*2] = letterize((int) (lon/LON_F[i]));
            locator[i*2+1] = letterize((int) (lat/LAT_F[i]));
        }
        lon = fmod(lon, LON_F[i]);
        lat = fmod(lat, LAT_F[i]);
    }
    locator[i*2]=0;
    return locator;
}

void setup() {
    Serial.begin(9600);
    pinMode(LED_BUILTIN, OUTPUT);

    Wire.begin();

    SerialGNSS.begin(9600);
    if (!gnss.begin(SerialGNSS)) {
        while (true) {
            Serial.println(F("Check GNSS"));
            delay(4000);
        }
    }

    //gnss.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial

    Serial.print(F("Version: "));
    byte versionHigh = gnss.getProtocolVersionHigh();
    Serial.print(versionHigh);
    Serial.print(".");
    byte versionLow = gnss.getProtocolVersionLow();
    Serial.println(versionLow);

#if 0
    Serial.print(F("[RFM] Init "));
    int state = radio.begin();
    if (state == RADIOLIB_ERR_NONE) {
        Serial.println(F("success!"));
    } else {
        Serial.print(F("failed, code "));
        Serial.println(state);
        while (true);
    }

    // radio.setRfSwitchPins(4, 5);
#endif
}

void loop()
{
    const auto now = millis();
    if (now - lastGnssPoll > 1000) {
        lastGnssPoll = now;

        long latitude = gnss.getLatitude();
        Serial.print(F("Lat: "));
        Serial.print(latitude);

        long longitude = gnss.getLongitude();
        Serial.print(F(" Long: "));
        Serial.print(longitude);

        char* locator = get_mh((double)latitude/1e7, (double)longitude/1e7, 6);
        Serial.print(F(" Loc: "));
        Serial.print(locator);

        long altitude = gnss.getAltitude();
        Serial.print(F(" Alt: "));
        Serial.print(altitude);
        Serial.print(F(" (mm)"));

        long speed = gnss.getGroundSpeed();
        Serial.print(F(" Speed: "));
        Serial.print(speed);
        Serial.print(F(" (mm/s)"));

        long heading = gnss.getHeading();
        Serial.print(F(" Heading: "));
        Serial.print(heading);
        Serial.print(F(" (degrees * 10^-5)"));

        byte SIV = gnss.getSIV();
        Serial.print(F(" SIV: "));
        Serial.print(SIV);

        Serial.println();
    }
}

void loop2()
{
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);

    Serial.print(F("[RFM] Transmitting packet ... "));

    int state = radio.transmit("Hello World!");

    if (state == RADIOLIB_ERR_NONE) {
        Serial.println(F(" success!"));
        Serial.print(F("[RFM] Datarate:\t"));
        Serial.print(radio.getDataRate());
        Serial.println(F(" bps"));

    } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
        Serial.println(F("too long!"));

    } else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
        Serial.println(F("timeout!"));

    } else {
        Serial.print(F("failed, code "));
        Serial.println(state);
    }

    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
}