Components and supplies
General Purpose Transistor NPN
GPS Module (Generic)
Resistor 1k ohm
Arduino MKR Fox 1200
Project description
Code
MKRFOX GPS Tracker
arduino
1#include <SigFox.h> 2#include <ArduinoLowPower.h> 3#include <TinyGPS.h>//incluimos TinyGPS 4 5#define WAITING_TIME 15 6#define GPS_PIN 2 7#define GPS_INFO_BUFFER_SIZE 128 8 9bool debug = false; 10 11TinyGPS gps;//GPS Object 12 13//GPS data variables 14int year; 15byte month, day, hour, minute, second, hundredths; 16unsigned long chars; 17unsigned short sentences, failed_checksum; 18char GPS_info_char; 19char GPS_info_buffer[GPS_INFO_BUFFER_SIZE]; 20unsigned int received_char; 21bool message_started = false; 22int i = 0; 23 24// GPS coordinate structure, 12 bytes size on 32 bits platforms 25struct gpscoord { 26 float a_latitude; // 4 bytes 27 float a_longitude; // 4 bytes 28 float a_altitude; // 4 bytes 29}; 30 31float latitude = 0.0f; 32float longitude = 0.0f; 33float altitud = 0; 34 35 36//////////////// Waiting function ////////////////// 37void Wait(int m, bool s) { 38 //m minutes to wait 39 //s slow led pulses 40 if (debug) { 41 Serial.print("Waiting: "); Serial.print(m); Serial.println(" min."); 42 } 43 44 digitalWrite(LED_BUILTIN, LOW); 45 46 if (s) { 47 48 int seg = m * 30; 49 for (int i = 0; i < seg; i++) { 50 digitalWrite(LED_BUILTIN, HIGH); //LED on 51 delay(1000); 52 digitalWrite(LED_BUILTIN, LOW); //LED off 53 delay(1000); 54 } 55 56 } else { 57 int seg = m * 15; 58 for (int i = 0; i < seg; i++) { 59 digitalWrite(LED_BUILTIN, HIGH); //LED on 60 delay(1000); 61 digitalWrite(LED_BUILTIN, LOW); //LED off 62 delay(3000); 63 64 } 65 } 66} 67 68/////////////////// Sigfox Send Data function //////////////// 69void SendSigfox(String data) { 70 if (debug) { 71 Serial.print("Sending: "); Serial.println(data); 72 if (data.length() > 12) { 73 Serial.println("Message too long, only first 12 bytes will be sent"); 74 } 75 } 76 77 // Remove EOL 78 //data.trim(); 79 80 // Start the module 81 SigFox.begin(); 82 // Wait at least 30mS after first configuration (100mS before) 83 delay(100); 84 // Clears all pending interrupts 85 SigFox.status(); 86 delay(1); 87 if (debug) SigFox.debug(); 88 delay(100); 89 90 SigFox.beginPacket(); 91 SigFox.print(data); 92 93 94 if (debug) { 95 int ret = SigFox.endPacket(true); // send buffer to SIGFOX network and wait for a response 96 if (ret > 0) { 97 Serial.println("No transmission"); 98 } else { 99 Serial.println("Transmission ok"); 100 } 101 102 Serial.println(SigFox.status(SIGFOX)); 103 Serial.println(SigFox.status(ATMEL)); 104 105 if (SigFox.parsePacket()) { 106 Serial.println("Response from server:"); 107 while (SigFox.available()) { 108 Serial.print("0x"); 109 Serial.println(SigFox.read(), HEX); 110 } 111 } else { 112 Serial.println("Could not get any response from the server"); 113 Serial.println("Check the SigFox coverage in your area"); 114 Serial.println("If you are indoor, check the 20dB coverage or move near a window"); 115 } 116 Serial.println(); 117 } else { 118 SigFox.endPacket(); 119 } 120 SigFox.end(); 121} 122 123 124////////////////// Convert GPS function ////////////////// 125/* Converts GPS float data to Char data */ 126 127String ConvertGPSdata(const void* data, uint8_t len) { 128 uint8_t* bytes = (uint8_t*)data; 129 String cadena ; 130 if (debug) { 131 Serial.print("Length: "); Serial.println(len); 132 } 133 134 for (uint8_t i = len - 1; i < len; --i) { 135 if (bytes[i] < 12) { 136 cadena.concat(byte(0)); // Not tested 137 } 138 cadena.concat(char(bytes[i])); 139 if (debug) Serial.print(bytes[i], HEX); 140 } 141 142 if (debug) { 143 Serial.println(""); 144 Serial.print("String to send: "); Serial.println(cadena); 145 } 146 147 return cadena; 148} 149 150 151////////////////////////// Get GPS position function///////////////////// 152String GetGPSpositon() { 153 154 int messages_count = 0; 155 String pos; 156 157 if (debug) Serial.println("GPS ON"); 158 digitalWrite(GPS_PIN, HIGH); //Turn GPS on 159 Wait(1, false); 160 while (messages_count < 5000) { 161 while (Serial1.available()) { 162 163 int GPS_info_char = Serial1.read(); 164 165 if (GPS_info_char == '$') messages_count ++; // start of message. Counting messages. 166 167 168 if (debug) { 169 if (GPS_info_char == '$') { // start of message 170 message_started = true; 171 received_char = 0; 172 } else if (GPS_info_char == '*') { // end of message 173 for (i = 0; i < received_char; i++) { 174 Serial.write(GPS_info_buffer[i]); // writes the message to the PC once it has been completely received 175 } 176 Serial.println(); 177 message_started = false; // ready for the new message 178 } else if (message_started == true) { // the message is already started and I got a new character 179 if (received_char <= GPS_INFO_BUFFER_SIZE) { // to avoid buffer overflow 180 GPS_info_buffer[received_char] = GPS_info_char; 181 received_char++; 182 } else { // resets everything (overflow happened) 183 message_started = false; 184 received_char = 0; 185 } 186 } 187 } 188 189 if (gps.encode(GPS_info_char)) { 190 gps.f_get_position(&latitude, &longitude); 191 altitud = gps.altitude() / 100; 192 193 // Store coordinates into dedicated structure 194 gpscoord coords = {altitud, longitude, latitude}; 195 196 gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths); 197 198 if (debug) { 199 Serial.println(); 200 Serial.println(); 201 Serial.print("Latitud/Longitud: "); 202 Serial.print(latitude, 5); 203 Serial.print(", "); 204 Serial.println(longitude, 5); 205 Serial.println(); 206 Serial.print("Fecha: "); Serial.print(day, DEC); Serial.print("/"); 207 Serial.print(month, DEC); Serial.print("/"); Serial.print(year); 208 Serial.print(" Hora: "); Serial.print(hour, DEC); Serial.print(":"); 209 Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC); 210 Serial.print("."); Serial.println(hundredths, DEC); 211 Serial.print("Altitud (metros): "); Serial.println(gps.f_altitude()); 212 Serial.print("Rumbo (grados): "); Serial.println(gps.f_course()); 213 Serial.print("Velocidad(kmph): "); Serial.println(gps.f_speed_kmph()); 214 Serial.print("Satelites: "); Serial.println(gps.satellites()); 215 Serial.println(); 216 217 } 218 219 gps.stats(&chars, &sentences, &failed_checksum); 220 if (debug) Serial.println("GPS turned off"); 221 digitalWrite(GPS_PIN, LOW); //GPS turned off 222 pos = ConvertGPSdata(&coords, sizeof(gpscoord)); //Send data 223 return pos; 224 225 } 226 227 } 228 } 229 pos = "No Signal"; 230} 231 232 233 234 235//////////////////SETUP/////////////////// 236 237void setup() { 238 if (debug) { 239 Serial.begin(9600); 240 while (!Serial) {}// wait for serial port to connect. Needed for native USB port only 241 Serial.println("Serial Connected"); 242 } 243 244 //Serial1 pins 13-14 for 3.3V connection to GPS. 245 Serial1.begin(9600); 246 while (!Serial1) {} 247 if (debug) { 248 Serial.println("GPS Connected"); 249 } 250 251 pinMode(GPS_PIN, OUTPUT); //pin de interruptor del GPS 252 253 if (!SigFox.begin()) { 254 Serial.println("Shield error or not present!"); 255 return; 256 } 257 258 // Enable debug led and disable automatic deep sleep 259 if (debug) { 260 SigFox.debug(); 261 } else { 262 SigFox.end(); // Send the module to the deepest sleep 263 } 264 265 266} 267 268 269 270//////////////////////LOOP//////////////////////// 271 272void loop() { 273 274 String position_data; 275 276 position_data = GetGPSpositon(); 277 SendSigfox(position_data); 278 279 Wait(WAITING_TIME, false); 280 281 282} 283 284
MKRFOX GPS Tracker
arduino
1#include <SigFox.h> 2#include <ArduinoLowPower.h> 3#include <TinyGPS.h>//incluimos TinyGPS 4 5#define WAITING_TIME 15 6#define GPS_PIN 2 7#define GPS_INFO_BUFFER_SIZE 128 8 9bool debug = false; 10 11TinyGPS gps;//GPS Object 12 13//GPS data variables 14int year; 15byte month, day, hour, minute, second, hundredths; 16unsigned long chars; 17unsigned short sentences, failed_checksum; 18char GPS_info_char; 19char GPS_info_buffer[GPS_INFO_BUFFER_SIZE]; 20unsigned int received_char; 21bool message_started = false; 22int i = 0; 23 24// GPS coordinate structure, 12 bytes size on 32 bits platforms 25struct gpscoord { 26 float a_latitude; // 4 bytes 27 float a_longitude; // 4 bytes 28 float a_altitude; // 4 bytes 29}; 30 31float latitude = 0.0f; 32float longitude = 0.0f; 33float altitud = 0; 34 35 36//////////////// Waiting function ////////////////// 37void Wait(int m, bool s) { 38 //m minutes to wait 39 //s slow led pulses 40 if (debug) { 41 Serial.print("Waiting: "); Serial.print(m); Serial.println(" min."); 42 } 43 44 digitalWrite(LED_BUILTIN, LOW); 45 46 if (s) { 47 48 int seg = m * 30; 49 for (int i = 0; i < seg; i++) { 50 digitalWrite(LED_BUILTIN, HIGH); //LED on 51 delay(1000); 52 digitalWrite(LED_BUILTIN, LOW); //LED off 53 delay(1000); 54 } 55 56 } else { 57 int seg = m * 15; 58 for (int i = 0; i < seg; i++) { 59 digitalWrite(LED_BUILTIN, HIGH); //LED on 60 delay(1000); 61 digitalWrite(LED_BUILTIN, LOW); //LED off 62 delay(3000); 63 64 } 65 } 66} 67 68/////////////////// Sigfox Send Data function //////////////// 69void SendSigfox(String data) { 70 if (debug) { 71 Serial.print("Sending: "); Serial.println(data); 72 if (data.length() > 12) { 73 Serial.println("Message too long, only first 12 bytes will be sent"); 74 } 75 } 76 77 // Remove EOL 78 //data.trim(); 79 80 // Start the module 81 SigFox.begin(); 82 // Wait at least 30mS after first configuration (100mS before) 83 delay(100); 84 // Clears all pending interrupts 85 SigFox.status(); 86 delay(1); 87 if (debug) SigFox.debug(); 88 delay(100); 89 90 SigFox.beginPacket(); 91 SigFox.print(data); 92 93 94 if (debug) { 95 int ret = SigFox.endPacket(true); // send buffer to SIGFOX network and wait for a response 96 if (ret > 0) { 97 Serial.println("No transmission"); 98 } else { 99 Serial.println("Transmission ok"); 100 } 101 102 Serial.println(SigFox.status(SIGFOX)); 103 Serial.println(SigFox.status(ATMEL)); 104 105 if (SigFox.parsePacket()) { 106 Serial.println("Response from server:"); 107 while (SigFox.available()) { 108 Serial.print("0x"); 109 Serial.println(SigFox.read(), HEX); 110 } 111 } else { 112 Serial.println("Could not get any response from the server"); 113 Serial.println("Check the SigFox coverage in your area"); 114 Serial.println("If you are indoor, check the 20dB coverage or move near a window"); 115 } 116 Serial.println(); 117 } else { 118 SigFox.endPacket(); 119 } 120 SigFox.end(); 121} 122 123 124////////////////// Convert GPS function ////////////////// 125/* Converts GPS float data to Char data */ 126 127String ConvertGPSdata(const void* data, uint8_t len) { 128 uint8_t* bytes = (uint8_t*)data; 129 String cadena ; 130 if (debug) { 131 Serial.print("Length: "); Serial.println(len); 132 } 133 134 for (uint8_t i = len - 1; i < len; --i) { 135 if (bytes[i] < 12) { 136 cadena.concat(byte(0)); // Not tested 137 } 138 cadena.concat(char(bytes[i])); 139 if (debug) Serial.print(bytes[i], HEX); 140 } 141 142 if (debug) { 143 Serial.println(""); 144 Serial.print("String to send: "); Serial.println(cadena); 145 } 146 147 return cadena; 148} 149 150 151////////////////////////// Get GPS position function///////////////////// 152String GetGPSpositon() { 153 154 int messages_count = 0; 155 String pos; 156 157 if (debug) Serial.println("GPS ON"); 158 digitalWrite(GPS_PIN, HIGH); //Turn GPS on 159 Wait(1, false); 160 while (messages_count < 5000) { 161 while (Serial1.available()) { 162 163 int GPS_info_char = Serial1.read(); 164 165 if (GPS_info_char == '$') messages_count ++; // start of message. Counting messages. 166 167 168 if (debug) { 169 if (GPS_info_char == '$') { // start of message 170 message_started = true; 171 received_char = 0; 172 } else if (GPS_info_char == '*') { // end of message 173 for (i = 0; i < received_char; i++) { 174 Serial.write(GPS_info_buffer[i]); // writes the message to the PC once it has been completely received 175 } 176 Serial.println(); 177 message_started = false; // ready for the new message 178 } else if (message_started == true) { // the message is already started and I got a new character 179 if (received_char <= GPS_INFO_BUFFER_SIZE) { // to avoid buffer overflow 180 GPS_info_buffer[received_char] = GPS_info_char; 181 received_char++; 182 } else { // resets everything (overflow happened) 183 message_started = false; 184 received_char = 0; 185 } 186 } 187 } 188 189 if (gps.encode(GPS_info_char)) { 190 gps.f_get_position(&latitude, &longitude); 191 altitud = gps.altitude() / 100; 192 193 // Store coordinates into dedicated structure 194 gpscoord coords = {altitud, longitude, latitude}; 195 196 gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths); 197 198 if (debug) { 199 Serial.println(); 200 Serial.println(); 201 Serial.print("Latitud/Longitud: "); 202 Serial.print(latitude, 5); 203 Serial.print(", "); 204 Serial.println(longitude, 5); 205 Serial.println(); 206 Serial.print("Fecha: "); Serial.print(day, DEC); Serial.print("/"); 207 Serial.print(month, DEC); Serial.print("/"); Serial.print(year); 208 Serial.print(" Hora: "); Serial.print(hour, DEC); Serial.print(":"); 209 Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC); 210 Serial.print("."); Serial.println(hundredths, DEC); 211 Serial.print("Altitud (metros): "); Serial.println(gps.f_altitude()); 212 Serial.print("Rumbo (grados): "); Serial.println(gps.f_course()); 213 Serial.print("Velocidad(kmph): "); Serial.println(gps.f_speed_kmph()); 214 Serial.print("Satelites: "); Serial.println(gps.satellites()); 215 Serial.println(); 216 217 } 218 219 gps.stats(&chars, &sentences, &failed_checksum); 220 if (debug) Serial.println("GPS turned off"); 221 digitalWrite(GPS_PIN, LOW); //GPS turned off 222 pos = ConvertGPSdata(&coords, sizeof(gpscoord)); //Send data 223 return pos; 224 225 } 226 227 } 228 } 229 pos = "No Signal"; 230} 231 232 233 234 235//////////////////SETUP/////////////////// 236 237void setup() { 238 if (debug) { 239 Serial.begin(9600); 240 while (!Serial) {}// wait for serial port to connect. Needed for native USB port only 241 Serial.println("Serial Connected"); 242 } 243 244 //Serial1 pins 13-14 for 3.3V connection to GPS. 245 Serial1.begin(9600); 246 while (!Serial1) {} 247 if (debug) { 248 Serial.println("GPS Connected"); 249 } 250 251 pinMode(GPS_PIN, OUTPUT); //pin de interruptor del GPS 252 253 if (!SigFox.begin()) { 254 Serial.println("Shield error or not present!"); 255 return; 256 } 257 258 // Enable debug led and disable automatic deep sleep 259 if (debug) { 260 SigFox.debug(); 261 } else { 262 SigFox.end(); // Send the module to the deepest sleep 263 } 264 265 266} 267 268 269 270//////////////////////LOOP//////////////////////// 271 272void loop() { 273 274 String position_data; 275 276 position_data = GetGPSpositon(); 277 SendSigfox(position_data); 278 279 Wait(WAITING_TIME, false); 280 281 282} 283 284
Downloadable files
Connections
Connections
Comments
Only logged in users can leave comments