Pages: [1]   Go Down
Author Topic: Arduino duemillanove, Adafruit gps shield and bmp085 synchronization  (Read 276 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am doing a scientific project and I need my device to measure gps (position, time, height, etc), temperature and pressure and save it on microSD in txt. Can anyone help me? I followed the tutorial and examples, but I just can't make it work.

I have working codes for showing temperature, pressure and gps but in two different files.


* gps.ino (2.26 KB - downloaded 3 times.)
* teplota_tlak.ino (5.05 KB - downloaded 3 times.)
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26618
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's better if you post the "it" that you've got but that doesn't work.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

bmp085
Code:
#include <Wire.h>

#define BMP085_ADDRESS 0x77 

const unsigned char OSS = 0; 

int ac1;
int ac2;
int ac3;
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1;
int b2;
int mb;
int mc;
int md;

long b5;

short temperature;
long pressure;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  bmp085Calibration();
 

 
}

void loop()
{
  temperature = bmp085GetTemperature(bmp085ReadUT());
  pressure = bmp085GetPressure(bmp085ReadUP());


  Serial.print("Temperature: ");
  Serial.print(temperature, DEC);
  Serial.println(" *0.1 deg C");
  Serial.print("Pressure: ");

Serial.print(pressure, DEC);
 Serial.println(" Pa");
   delay(1000);
}

void bmp085Calibration()
{
  ac1 = bmp085ReadInt(0xAA);
  ac2 = bmp085ReadInt(0xAC);
  ac3 = bmp085ReadInt(0xAE);
  ac4 = bmp085ReadInt(0xB0);
  ac5 = bmp085ReadInt(0xB2);
  ac6 = bmp085ReadInt(0xB4);
  b1 = bmp085ReadInt(0xB6);
  b2 = bmp085ReadInt(0xB8);
  mb = bmp085ReadInt(0xBA);
  mc = bmp085ReadInt(0xBC);
  md = bmp085ReadInt(0xBE);
}

short bmp085GetTemperature(unsigned int ut)
{
  long x1, x2;
 
  x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  x2 = ((long)mc << 11)/(x1 + md);
  b5 = x1 + x2;

  return ((b5 + 8)>>4); 
}

/long bmp085GetPressure(unsigned long up)
{
  long x1, x2, x3, b3, b6, p;
  unsigned long b4, b7;
 
  b6 = b5 - 4000;
  x1 = (b2 * (b6 * b6)>>12)>>11;
  x2 = (ac2 * b6)>>11;
  x3 = x1 + x2;
  b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
 
  // Calculate B4
  x1 = (ac3 * b6)>>13;
  x2 = (b1 * ((b6 * b6)>>12))>>16;
  x3 = ((x1 + x2) + 2)>>2;
  b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
 
  b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  if (b7 < 0x80000000)
    p = (b7<<1)/b4;
  else
    p = (b7/b4)<<1;
   
  x1 = (p>>8) * (p>>8);
  x1 = (x1 * 3038)>>16;
  x2 = (-7357 * p)>>16;
  p += (x1 + x2 + 3791)>>4;
 
  return p;
}

char bmp085Read(unsigned char address)
{
  unsigned char data;
 
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();
 
  Wire.requestFrom(BMP085_ADDRESS, 1);
  while(!Wire.available())
    ;
   
  return Wire.read();
}

int bmp085ReadInt(unsigned char address)
{
  unsigned char msb, lsb;
 
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();
 
  Wire.requestFrom(BMP085_ADDRESS, 2);
  while(Wire.available()<2)
    ;
  msb = Wire.read();
  lsb = Wire.read();
 
  return (int) msb<<8 | lsb;
}

unsigned int bmp085ReadUT()
{
  unsigned int ut;
 
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF4);
  Wire.write(0x2E);
  Wire.endTransmission();
 
  delay(5);
 
  ut = bmp085ReadInt(0xF6);
  return ut;
}

unsigned long bmp085ReadUP()
{
  unsigned char msb, lsb, xlsb;
  unsigned long up = 0;
 
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF4);
  Wire.write(0x34 + (OSS<<6));
  Wire.endTransmission();
 
  delay(2 + (3<<OSS));
 
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF6);
  Wire.endTransmission();
  Wire.requestFrom(BMP085_ADDRESS, 3);
 
  while(Wire.available() < 3)
    ;
  msb = Wire.read();
  lsb = Wire.read();
  xlsb = Wire.read();
 
  up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
 
  return up;
}


and GPS
Code:
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 7);

Adafruit_GPS GPS(&mySerial);

#define GPSECHO  true

boolean usingInterrupt = false;
void useInterrupt(boolean);

void setup() 
{
   
  Serial.begin(115200);
  Serial.println("Adafruit GPS library basic test!");

  GPS.begin(9600);
 
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);

  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   
 
  GPS.sendCommand(PGCMD_ANTENNA);

  useInterrupt(true);

  delay(5000);
 
  mySerial.println(PMTK_Q_RELEASE);
}


SIGNAL(TIMER0_COMPA_vect) {
  char c = GPS.read();
#ifdef UDR0
  if (GPSECHO)
    if (c) UDR0 = c; 
   
#endif
}

void useInterrupt(boolean v) {
  if (v) {
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } else {
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
}

uint32_t timer = millis();
void loop()                     
{
  if (! usingInterrupt) {
    char c = GPS.read();
    if (GPSECHO)
      if (c) Serial.print(c);
  }
 
  if (GPS.newNMEAreceived()) {

    if (!GPS.parse(GPS.lastNMEA()))
      return; 
  }

  if (timer > millis())  timer = millis();

  if (millis() - timer > 5000) {
    timer = millis();
   
    Serial.print("\nTime: ");
    Serial.print(GPS.hour, DEC); Serial.print(':');
    Serial.print(GPS.minute, DEC); Serial.print(':');
    Serial.print(GPS.seconds, DEC); Serial.print('.');
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC); Serial.print('/');
    Serial.print(GPS.month, DEC); Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); Serial.print((int)GPS.fix);
    Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", ");
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
     
      Serial.print("Speed (knots): "); Serial.println(GPS.speed);
      Serial.print("Angle: "); Serial.println(GPS.angle);
      Serial.print("Altitude: "); Serial.println(GPS.altitude);
      Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
    }
  }
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26618
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I forget to say, around here
Quote
but I just can't make it work
doesn't really cut it.
What does it do that you don't want it to, and what doesn't it do that you want it to?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3080
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your arduino will only one one program at a time.

So you need to write one program to do both tasks.   Have you done that yet ?

Combining two program which each do one separate task,   can be a tricky job but
it is not impossible,  if you understand what the project is supposed to do.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Of course I tried, but the result was that nothing was writen on microSD.

Code:
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SD.h>
const int chipSelect = 10;
#include <avr/sleep.h>

SoftwareSerial mySerial(8, 7);
Adafruit_GPS GPS(&mySerial);
#define GPSECHO  true
#define LOG_FIXONLY false 

#define chipSelect 10
#define ledPin 13
File logfile;
#include <SD.h>

#include <Wire.h>
 
#define ADR 0x77
 
short ac1 = 0;
short ac2 = 0;
short ac3 = 0;
short b1 = 0;
short b2 = 0;
short mb = 0;
short mc = 0;
short md = 0;
unsigned short ac6, ac4, ac5;
long UT, b5;
 
// 0 až 3 (0 nejmenší)
byte OSS = 1;
 
File myFile;
uint8_t parseHex(char c) {
  if (c < '0')
    return 0;
  if (c <= '9')
    return c - '0';
  if (c < 'A')
    return 0;
  if (c <= 'F')
    return (c - 'A')+10;
}

void error(uint8_t errno) {
  while(1) {
    uint8_t i;
    for (i=0; i<errno; i++) {
      digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(1000);
    }
    for (i=errno; i<10; i++) {
      delay(200);
    }
  }
}
void setup()
{
  Serial.begin(9600);
  Wire.begin();
 
  ac1 = GetShort(0xAA, 0xAB);
  ac2 = GetShort(0xAC, 0xAD);
  ac3 = GetShort(0xAE, 0xAF);
  ac4 = GetShort(0xB0, 0xB1);
  ac5 = GetShort(0xB2, 0xB3);
  ac6 = GetShort(0xB4, 0xB5);
  b1 = GetShort(0xB6, 0xB7);
  b2 = GetShort(0xB8, 0xB9);
  mb = GetShort(0xBA, 0xBB);
  mc = GetShort(0xBC, 0xBD);
  md = GetShort(0xBE, 0xBF);
   while (!Serial) {
  }


  Serial.print("Initializing SD card...");
   pinMode(10, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  myFile = SD.open("test.txt", FILE_WRITE);
 
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("start");
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening test.txt");
  }
  pinMode(ledPin, OUTPUT);

  pinMode(10, OUTPUT);
 
  if (!SD.begin(chipSelect)) {     
    Serial.println("Card init. failed!");
    error(2);
  }
  char filename[15];
  strcpy(filename, "test.TXT");
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = '0' + i/10;
    filename[7] = '0' + i%10;
    if (! SD.exists(filename)) {
      break;
    }
  }

  logfile = SD.open(filename, FILE_WRITE);
  if( ! logfile ) {
    Serial.print("Couldnt create "); Serial.println(filename);
    error(3);
  }
  Serial.print("Writing to "); Serial.println(filename);
 
  GPS.begin(9600);

  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   

  GPS.sendCommand(PGCMD_NOANTENNA);
 
  Serial.println("Ready!");
}


void loop()
{
 
  char c = GPS.read();
  if (GPSECHO)
     if (c)   Serial.print(c);

  if (GPS.newNMEAreceived()) {
       
    if (!GPS.parse(GPS.lastNMEA())) 
      return; 
   
 
    Serial.println("OK");
    if (LOG_FIXONLY && !GPS.fix) {
        Serial.print("No Fix");
        return;
    }

   
    Serial.println("Log");
   
    char *stringptr = GPS.lastNMEA();
    uint8_t stringsize = strlen(stringptr);
    if (stringsize != logfile.write((uint8_t *)stringptr, stringsize))   
      error(4);
    if (strstr(stringptr, "RMC"))   logfile.flush();
    Serial.println();
  }
UT =  GetUT();
short teplota = GetTemperature(UT); 
myFile.println("T:      ");
myFile.println((float)teplota/10); 
myFile.println(" 'C");

long tlak = GetPressure(ReadUP());
myFile.println("p:         ");
myFile.println((float)tlak/100);
myFile.println(" hPa");
 

delay (1000);
  myFile = SD.open("test.txt", FILE_WRITE);
 
  if (myFile) {
    Serial.print("Writing to test.txt...");
   
    UT =  GetUT();
    short teplota = GetTemperature(UT); 
    myFile.println("T:      ");
    myFile.println((float)teplota/10); 
    myFile.println(" 'C");
    long tlak = GetPressure(ReadUP());
    myFile.println("p:         ");
    myFile.println((float)tlak/100);
    myFile.println(" hPa");
    delay (1000);
   
    myFile.close();
    Serial.println("done.");
  } else {
    Serial.println("error opening test.txt");
  }
 
Serial.println();
}
 
 
 
short GetShort(byte adr1, byte adr2)
{
  byte b1, b2;
  Wire.beginTransmission(ADR);
  Wire.write(adr1); 
  Wire.endTransmission();
  Wire.requestFrom(ADR,1);
  if (Wire.available()) b1 = Wire.read();
 
  Wire.beginTransmission(ADR);
  Wire.write(byte(adr2)); 
  Wire.endTransmission();
  Wire.requestFrom(ADR,1);
  if (Wire.available()) b2 = Wire.read();
  short r = b2 + (unsigned short)(b1 << 8);
  return r;
}
 
long GetUT()
{
  byte b1, b2, b3;
 
  Wire.beginTransmission(ADR);
  Wire.write(0xF4);
  Wire.write(0x2E); 
  Wire.endTransmission();
 
  delay(5);
 
  Wire.beginTransmission(ADR);
  Wire.write(0xF6);
  Wire.endTransmission();
  Wire.requestFrom(ADR,1);
  if (Wire.available()) b1 = Wire.read();
   
  Wire.beginTransmission(ADR);
  Wire.write(0xF7);
  Wire.endTransmission();
  Wire.requestFrom(ADR,1);
  if (Wire.available()) b2 = Wire.read();
   
  long ut =  ((long)b1<<8) + b2;
  return ut;
}
 
unsigned long ReadUP()
{
  unsigned char msb, lsb, xlsb;
  unsigned long up = 0;
 
  Wire.beginTransmission(ADR);
  Wire.write(0xF4);
  Wire.write(0x34 + (OSS<<6));
  Wire.endTransmission();
 
  delay(2 + (3<<OSS));
   
  Wire.beginTransmission(ADR);
  Wire.write(0xF6);
  Wire.endTransmission();
  Wire.requestFrom(ADR, 3);
 
  while(Wire.available() < 3)
    ;
  msb = Wire.read();
  lsb = Wire.read();
  xlsb = Wire.read();
 
  up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
 
  return up;
}
 
 
short GetTemperature(unsigned int ut)
{
  long x1, x2;
  x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  x2 = ((long)mc << 11)/(x1 + md);
  b5 = x1 + x2;
  return ((b5 + 8)>>4); 
}
 
long GetPressure(unsigned long up)
{
  long x1, x2, x3, b3, b6, p;
  unsigned long b4, b7;
 
  b6 = b5 - 4000;
  x1 = (b2 * (b6 * b6)>>12)>>11;
  x2 = (ac2 * b6)>>11;
  x3 = x1 + x2;
  b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
 
  x1 = (ac3 * b6)>>13;
  x2 = (b1 * ((b6 * b6)>>12))>>16;
  x3 = ((x1 + x2) + 2)>>2;
  b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
 
  b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  if (b7 < 0x80000000)
    p = (b7<<1)/b4;
  else
    p = (b7/b4)<<1;
   
  x1 = (p>>8) * (p>>8);
  x1 = (x1 * 3038)>>16;
  x2 = (-7357 * p)>>16;
  p += (x1 + x2 + 3791)>>4;
 
  return p;
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 650
Posts: 50833
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Of course I tried, but the result was that nothing was writen on microSD.
What serial output did you get?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No text in serial monitor.
Logged

Pages: [1]   Go Up
Jump to: