วันเสาร์ที่ 29 เมษายน พ.ศ. 2560

GPS Data logger for google map

GPS Data logger for google map


Video






Hard ware

SD card to Arduino connect





CS        >>    4
SCK     >>   13
MOSI   >>   11
MISO   >>   12
VCC    >>     5V
GND    >>    GND

GPS




TX     >>  2
RX     >>  3
VCC  >>  3.3 V
GND >>  GND

Google fusion table



Code


#include <SoftwareSerial.h> #include <SPI.h> #include <SD.h> File dataFile; SoftwareSerial GPSModule(2, 3); // RX, TX // process and flow related variables int updates; int failedUpdates; int pos; int stringplace = 0; // // variable to hold out nmea data and labels. String nmea[12]; String labels[12] {"Time: ", "Lat: ", "N/S: ", "Lng: ", "E/W: ", "Fix: ", "Sats: ", "HDP: ", "Alt: ", "Metres: ", "MSL: ", "Metres: "}; // void setup() { Serial.begin(9600); GPSModule.begin(9600); if (!SD.begin(4)) { Serial.println("failed"); // don't do anything more } else{ Serial.println("ok"); } } void loop() { dataFile = SD.open("gps.txt", FILE_WRITE); //flushing out data at the start of each loop, not sure it is really neccessay Serial.flush(); GPSModule.flush(); while (GPSModule.available() > 0) { GPSModule.read(); } // This is the meat of it really, we look for our nmea string and then split it into comma seperated values if (GPSModule.find("$GPGGA,")) { String tempMsg = GPSModule.readStringUntil('\n'); //Serial.println(tempMsg); for (int i = 0; i < tempMsg.length(); i++) { if (tempMsg.substring(i, i + 1) == ",") { nmea[pos] = tempMsg.substring(stringplace, i); stringplace = i + 1; pos++; } if (i == tempMsg.length() - 1) { nmea[pos] = tempMsg.substring(stringplace, i); } } updates++; // Here we convert our minutes and seconds latitute and longitute to decimal degrees nmea[1] = ConvertLat(); nmea[3] = ConvertLng(); // Test to see if GPS is fixed if(nmea[5].toInt() > 0){ // save data to SD if data is fixed if (dataFile) { Serial.println("writing"); // update number dataFile.print(updates); dataFile.print(","); // time dataFile.print(nmea[0]); dataFile.print(","); // latitude dataFile.print(nmea[1]); dataFile.print(","); // longitude dataFile.print(nmea[3]); dataFile.println(""); Serial.println("done"); } else{ Serial.println("error"); } } } else { failedUpdates++; Serial.println(failedUpdates); } // Print out data to the Serial Monitor for (int i = 0; i < 11; i++) { Serial.print(labels[i]); Serial.print(nmea[i]); Serial.println(""); } // Zero our flow control varibles stringplace = 0; pos = 0; // close file on SD card dataFile.close(); } // Here is the funky stuff - More details here https://www.youtube.com/watch?v=bgOZLgaLa0g String ConvertLat() { String posneg = ""; if (nmea[2] == "S") { posneg = "-"; } String latfirst; float latsecond; for (int i = 0; i < nmea[1].length(); i++) { if (nmea[1].substring(i, i + 1) == ".") { latfirst = nmea[1].substring(0, i - 2); latsecond = nmea[1].substring(i - 2).toFloat(); } } latsecond = latsecond / 60; String CalcLat = ""; char charVal[9]; dtostrf(latsecond, 4, 6, charVal); for (int i = 0; i < sizeof(charVal); i++) { CalcLat += charVal[i]; } latfirst += CalcLat.substring(1); latfirst = posneg += latfirst; return latfirst; } String ConvertLng() { String posneg = ""; if (nmea[4] == "W") { posneg = "-"; } String lngfirst; float lngsecond; for (int i = 0; i < nmea[3].length(); i++) { if (nmea[3].substring(i, i + 1) == ".") { lngfirst = nmea[3].substring(0, i - 2); //Serial.println(lngfirst); lngsecond = nmea[3].substring(i - 2).toFloat(); //Serial.println(lngsecond); } } lngsecond = lngsecond / 60; String CalcLng = ""; char charVal[9]; dtostrf(lngsecond, 4, 6, charVal); for (int i = 0; i < sizeof(charVal); i++) { CalcLng += charVal[i]; } lngfirst += CalcLng.substring(1); lngfirst = posneg += lngfirst; return lngfirst; }

วันเสาร์ที่ 1 เมษายน พ.ศ. 2560

HC06+Inventor 2 +Arduino











Code



#include<SoftwareSerial.h>

char receivedChar;

int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=4;//connected to Arduino's port 4
int IN4=9;//connected to Arduino's port 9


SoftwareSerial mySerial(10, 11); // RX, TX



//RX on Bluetooth to TCX on Arduino through a voltage divider 50ohm from arduino and 100ohm to ground. This is so we can drop the the voltage down to 3.3v (roughly)

// Use blueterm on android to get messages



void setup()



 pinMode(ENA,OUTPUT);//output
 pinMode(ENB,OUTPUT);
 pinMode(IN1,OUTPUT);
 pinMode(IN2,OUTPUT);
 pinMode(IN3,OUTPUT);
 pinMode(IN4,OUTPUT);
  
  mySerial.begin(9600); 
}



void loop() // run over and over



{

  


  while (!mySerial.available());   // stay here so long as COM port is empty

  receivedChar = mySerial.read();

  if (receivedChar == 'A') {

            //digitalWrite(ENA,LOW);
            // digitalWrite(ENB,LOW);// High is start driving car
              
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,LOW); 
             digitalWrite(IN2,HIGH);//setting motorA's directon
             digitalWrite(IN3,LOW);
             digitalWrite(IN4,HIGH);//setting motorB's directon
  }
              
  if (receivedChar == 'B') {

             // digitalWrite(ENA,LOW);
             // digitalWrite(ENB,LOW);// High is start driving car
              
              
                    
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,HIGH); 
             digitalWrite(IN2,LOW);//setting motorA's directon
             digitalWrite(IN3,HIGH);
             digitalWrite(IN4,LOW);//setting motorB's directon

                         
  }      
  if (receivedChar == 'C') 
  
              {

             // digitalWrite(ENA,LOW);
             // digitalWrite(ENB,LOW);// High is start driving car
  
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,HIGH); 
             digitalWrite(IN2,LOW);//setting motorA's directon
             digitalWrite(IN3,LOW);
             digitalWrite(IN4,HIGH);//setting motorB's directon

             

  } // if it is a 3 flash the LED

  if (receivedChar == 'D') {
  
              //digitalWrite(ENA,LOW);
              //digitalWrite(ENB,LOW);// High is start driving car
              
                          
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,LOW); 
             digitalWrite(IN2,HIGH);//setting motorA's directon
             digitalWrite(IN3,HIGH);
             digitalWrite(IN4,LOW);//setting motorB's directon


  } // if it is a 4 print out lots of sensor data


  if (receivedChar == 'E') {
  
             digitalWrite(ENA,HIGH);
             digitalWrite(ENB,HIGH);// High is start driving car
  
             digitalWrite(IN1,LOW); 
             digitalWrite(IN2,LOW);//setting motorA's directon
             digitalWrite(IN3,LOW);
             digitalWrite(IN4,LOW);//setting motorB's directon
  
  }
  
}