Loading...
Logo
Processing Forum
prosjektandoya's Profile
2 Posts
0 Responses
0 Followers

Activity Trend

Last 30 days
Show:
Private Message

    Hello!

    I've been working on a project where i want to send a set of variables from an Arduino to an Android device, and have found this code for receiving the data on the Android:
    1.   this.server.addListener(new AbstractServerListener() {

    2.     @Override
    3.       public void onReceive(Client client, byte[] data)
    4.     {

    5.       sensorValueTemp = data[0];
    6.       sensorValueTrykk1 = (data[1]);
    7.       sensorValueTrykk2 = (data[2]);
    8.       sensorValueTrykk3 = (data[3]);
    9.       sensorValueCPS = (data[4]);
    10.       
    11.     };
    12.   }

    And this code for sending the bytes on the Arduino:
    1. if (millis()-lastTime > 1000)
    2.   {
    3.     
    4.    byte data[5] = {temperature, trykk1, trykk2, trykk3, CPS};
    5.    connection->write(5,(uint8_t*)&data);

    6.     lastTime = millis();
    7.   }

    I'm trying to show the data on the screen of the phone, but nothing shows up, so something is obviously wrong...
    1. textAlign(CENTER, TOP);
    2.   
    3.    text(
    4.        "temp: " + (sensorValueTemp) + "\n" +
    5.        "trykk1: " + (sensorValueTrykk1) + "\n" +
    6.        "trykk2: " + (sensorValueTrykk2) + "\n" +
    7.        "trykk3: " + (sensorValueTrykk3) + "\n" +
    8.        "cps: " + (sensorValueCPS), 160, 180);

    All of my variables' values should be within the range from 0 to 255, and I've tried to Serial.print them and they show up perfectly there. Have anybody tried something similar before, or see something wrong in the code?
    Hello!

    We have a school project where we want to build a CanSat using some of the sensors found on an Android device, such as accelerometer data and GPS-coordinates, together with some on an Arduino board (pressure, temperature and a geiger counter). We bought the Sony Tipo ST21i and Arduino Mega ADK, and learned that the Tipo only supports ADB mode. 

    We don't have much programming experience, but found many great tutorials online and source codes from other projects which we tried to merge together. Unfortunately the USB communication between the Tipo and the Arduino doesn't work in this code. We don't know wether the problem lies on the Android or the Arduino side, but here are both codes:

    (Android code is to collect accelerometer data and GPS coordinates and send them using ADB)

    1. public final byte COMMAND_SEND_TRUE = 1;    // Allow data transfer

    2. import java.io.IOException;
    3. import android.util.Log;

    4. // Imports
    5. import android.content.Context;
    6. import android.location.Location;
    7. import android.location.LocationManager;
    8. import android.location.LocationListener;
    9. import android.location.GpsStatus.Listener;
    10. import android.os.Bundle;

    11. AccelerometerManager accel;
    12. float ax, ay, az;


    13. String[] fontList;
    14. PFont androidFont;

    15. LocationManager locationManager;
    16. MyLocationListener locationListener;

    17. // Variables to hold the current GPS data
    18. float currentLatitude  = 0;
    19. float currentLongitude = 0;
    20. float currentAccuracy  = 0;
    21. String currentProvider = "";




    22. private Server server;
    23. private int sensorValue;

    24. void setup()
    25. {    
    26.   
    27.   accel = new AccelerometerManager(this);
    28.   orientation(PORTRAIT);
    29.   size(displayWidth, displayHeight);
    30.   fontList = PFont.list();
    31.   androidFont = createFont(fontList[4], 35, true);
    32.   textFont(androidFont);
    33.   noLoop();
    34.   
    35.   // Create TCP server
    36.   server = null;
    37.   try
    38.   {
    39.     server = new Server(4567);
    40.     server.start();
    41.   } 
    42.   catch (IOException e)
    43.   {
    44.     println(e.toString());
    45.   } 

    46.   this.server.addListener(new AbstractServerListener() {

    47.     @Override
    48.       public void onReceive(Client client, byte[] data)
    49.     {

    50.       if (data.length<2) return;

    51.       sensorValue = (data[0] & 0xff) | ((data[1] & 0xff) << 8);
    52.     };
    53.   }
    54.   );
    55. }

    56. void draw() 
    57. {
    58.   background(0);
    59.  fill(255);
    60.   textSize(20);
    61.   
    62.   textAlign(CENTER, BOTTOM);
    63.   
    64.   text("x: " + nf(ax, 1, 2) + "\n" + 
    65.        "y: " + nf(ay, 1, 2) + "\n" + 
    66.        "z: " + nf(az, 1, 2), 160, 180);
    67.        
    68.   textAlign(CENTER, TOP);
    69.   
    70.   text("Latitude: "+(currentLatitude) + "\n" +
    71.        "Longitude: "+(currentLongitude) + "\n" +
    72.        "Accuracy: "+(currentAccuracy) + "\n" +
    73.        "Provider: "+(currentProvider), 160, 300);
    74.     
    75. }

    76.   byte ux = byte(ax);
    77.   byte uy = byte(ay);
    78.   byte uz = byte(az);
    79.  
    80.  public void send() { 
    81.    try {
    82.  server.send(new byte[] {
    83.        (byte) (ux)
    84.        ,(byte) (uy)
    85.        ,(byte) (uz)
    86.  }
    87.  );
    88.  }
    89.       catch (IOException e)
    90.     {
    91.       println("We got problems");
    92.     }
    93.     
    94.     try {
    95.       Thread.sleep(500);
    96.     catch(InterruptedException ex) {
    97.       println("Oh no");
    98. }

    99.    }
    100.      

    101.  
    102. public void resume() {
    103.   if (accel != null) {
    104.     accel.resume();
    105.   }
    106. }

    107.     
    108. public void pause() {
    109.   if (accel != null) {
    110.     accel.pause();
    111.   }
    112. }


    113. public void shakeEvent(float force) {
    114.   println("shake : " + force);
    115. }


    116. public void accelerationEvent(float x, float y, float z) {
    117. //  println("acceleration: " + x + ", " + y + ", " + z);
    118.   ax = x;
    119.   ay = y;
    120.   az = z;
    121.   redraw();
    122. }

    123. void onResume() {
    124.   super.onResume();
    125.   // Build Listener
    126.   locationListener = new MyLocationListener();
    127.   // Acquire a reference to the system Location Manager
    128.   locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    129.   // Register the listener with the Location Manager to receive location updates
    130.   locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    131. }

    132. void onPause() {
    133.   super.onPause();
    134. }

    135. //-----------------------------------------------------------------------------------------

    136. // Define a listener that responds to location updates
    137. class MyLocationListener implements LocationListener {
    138.    void onLocationChanged(Location location) {
    139.       // Called when a new location is found by the network location provider.
    140.       currentLatitude  = (float)location.getLatitude();
    141.       currentLongitude = (float)location.getLongitude();
    142.       currentAccuracy  = (float)location.getAccuracy();
    143.       currentProvider  = location.getProvider();
    144.     }
    145.     void onProviderDisabled (String provider) { 
    146.       currentProvider = "";
    147.     }

    148.     void onProviderEnabled (String provider) { 
    149.       currentProvider = provider;
    150.     }

    151.     void onStatusChanged (String provider, int status, Bundle extras) {
    152.       // Nothing yet...
    153.     }
    154.     
    155. }

    (The Arduino code is to receive the data sent from the Tipo, and write it to serial monitor)


    1. #include <SoftModem.h>
    2. #include <ctype.h>
    3. #include <Wire.h>
    4. #include <LiquidCrystal.h>

    5. #include <SPI.h>

    6. #include <Adb.h>
    7. #define MAX_RESET 8


    8. #define BMP085_ADDRESS 0x77  // I2C address of BMP085

    9. // Threshold values for the led bar
    10. #define TH1 45
    11. #define TH2 95
    12. #define TH3 200
    13. #define TH4 400
    14. #define TH5 600

    15. // Conversion factor - CPM to uSV/h
    16. #define CONV_FACTOR 0.00812

    17. // initialize the library with the numbers of the interface pins
    18. LiquidCrystal lcd(3,4,5,6,7,8);

    19. const unsigned char OSS = 0;  // Oversampling Setting

    20. // Add these to the top of your program
    21. const float p0 = 101860;     // Pressure at sea level (Pa)
    22. float altitude;

    23. // Calibration values
    24. int ac1;
    25. int ac2; 
    26. int ac3; 
    27. unsigned int ac4;
    28. unsigned int ac5;
    29. unsigned int ac6;
    30. int b1; 
    31. int b2;
    32. int mb;
    33. int mc;
    34. int md;

    35. // Variables
    36. int ledArray [] = {10,11,12,13,9};
    37. int geiger_input = 2;
    38. long count = 0;
    39. long countPerSecond = 0;
    40. long timePrevious = 0;
    41. long timePreviousMeassure = 0;
    42. long time = 0;
    43. long countPrevious = 0;
    44. float radiationValue = 0.0;

    45. // b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
    46. // so ...Temperature(...) must be called before ...Pressure(...).
    47. long b5; 

    48. short temperature;
    49. long pressure;

    50. SoftModem modem;

    51. Connection * connection;

    52. word ttemp;
    53. word ttrykk;
    54. word hhoyde;
    55. word ccps;

    56. long uusb;

    57. // Elapsed time for ADC sampling
    58. long lastTime;
    59. boolean r = true;


    60. void setup()
    61. {
    62.   Serial.begin(9600);
    63.   Wire.begin();
    64.   bmp085Calibration();
    65.   
    66.   pinMode(geiger_input, INPUT);
    67.   digitalWrite(geiger_input,HIGH);
    68.   for (int i=0;i<5;i++){
    69.     pinMode(ledArray[i],OUTPUT);
    70.   
    71.   modem.begin();
    72.   
    73.   pinMode(3,OUTPUT);
    74.   pinMode(3, HIGH);
    75.   
    76.    // Note start time
    77.   lastTime = millis();


    78.   // Initialise the ADB subsystem.  
    79.   ADB::init();

    80.   // Open an ADB stream to the phone's shell. Auto-reconnect
    81.   connection = ADB::addConnection("tcp:4567", true, adbEventHandler);  
    82. }

    83.   lcd.begin(16, 2);
    84.   lcd.clear();
    85.   lcd.setCursor(0, 0);
    86.   lcd.print("Prosjekt Andoya");
    87.   lcd.setCursor(0,1);
    88.   lcd.print("Oh yeah!");  
    89.   delay(500);

    90.   lcd.clear();  
    91.   lcd.setCursor(0, 0);
    92.   lcd.print("CPS=");
    93.   lcd.setCursor(4,0);
    94.   lcd.print(6*count);
    95.   lcd.setCursor(0,1);
    96.   lcd.print(radiationValue);

    97.   attachInterrupt(0,countPulse,FALLING);

    98. }

    99. // Event handler for the shell connection. 
    100. void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t * data)
    101. {
    102.   byte i;

    103.   switch (event)
    104.         {
    105.         case (ADB_CONNECTION_RECEIVE):
    106.           Serial.println(data[i]);

    107.         default:
    108.            break;
    109.         }

    110. }
    111.  


    112. void loop()
    113. {
    114.   unsigned long uusb = Serial.read();

    115.   temperature = bmp085GetTemperature(bmp085ReadUT());
    116.   pressure = bmp085GetPressure(bmp085ReadUP());
    117.   altitude = (float)44330 * (1 - pow(((float) pressure/p0), 0.190295));
    118.   
    119.   /*
    120.   Serial.print("Temperatur: ");
    121.   Serial.print(temperature, DEC);
    122.   Serial.println(" *0.1 deg C");
    123.   Serial.print("Lufttrykk: ");
    124.   Serial.print(pressure, DEC);
    125.   Serial.println(" Pa");
    126.   // Add this into loop(), after you've calculated the pressure
    127.   Serial.print("Moh.: ");
    128.   Serial.print(altitude, 2);
    129.   Serial.println();
    130.   Serial.println(uusb);
    131.   delay(3000);
    132.   Serial.println();
    133.   */
    134.   
    135.   if (millis()-timePreviousMeassure > 1000){
    136.     countPerSecond = count;
    137.     /*
    138.     radiationValue = countPerMinute * CONV_FACTOR;
    139.     */
    140.     timePreviousMeassure = millis();
    141.     Serial.print("cps = "); 
    142.     Serial.println(countPerSecond,DEC);
    143.     /*
    144.     Serial.print(" - ");
    145.     Serial.print("uSv/h = ");
    146.     Serial.println(radiationValue,4); 
    147. */    
    148.     lcd.clear();    
    149.     lcd.setCursor(0, 0);
    150.     lcd.print("CPS=");
    151.     lcd.setCursor(4,0);
    152.     lcd.print(countPerSecond);
    153.     lcd.setCursor(7,0);
    154.     lcd.print("p=");
    155.     lcd.setCursor(9,0);
    156.     lcd.print(pressure, DEC);
    157.     lcd.setCursor(0,1);
    158.     lcd.print("a=");
    159.     lcd.setCursor(2,1);
    160.     lcd.print(altitude, 2);
    161.     lcd.setCursor(10,1);
    162.     lcd.print("t=");
    163.     lcd.setCursor(12,1);
    164.     lcd.print(temperature, DEC);
    165.     /*
    166.     lcd.setCursor(0,1);
    167.     lcd.print(radiationValue,4);
    168.     lcd.setCursor(6,1);
    169.     lcd.print(" uSv/h");
    170.     */
    171.     ttrykk = pressure;
    172.     ttemp = temperature;
    173.     hhoyde = altitude;
    174.     ccps = countPerSecond;
    175.     
    176.     modem.write(ttrykk);
    177.     modem.write(ttemp);
    178.     modem.write(hhoyde);
    179.     modem.write(ccps);
    180.     modem.write(uusb);

    181.     //led var setting  
    182.     if(countPerSecond <= TH1) ledVar(0);
    183.     if((countPerSecond <= TH2)&&(countPerSecond>TH1)) ledVar(1);
    184.     if((countPerSecond <= TH3)&&(countPerSecond>TH2)) ledVar(2);
    185.     if((countPerSecond <= TH4)&&(countPerSecond>TH3)) ledVar(3);
    186.     if((countPerSecond <= TH5)&&(countPerSecond>TH4)) ledVar(4);
    187.     if(countPerSecond>TH5) ledVar(5);

    188.     count = 0;
    189.   }
    190.     ADB::poll();
    191.   
    192. }



    193. void countPulse(){
    194.   detachInterrupt(0);
    195.   count++;
    196.   while(digitalRead(2)==0){
    197.   }
    198.   attachInterrupt(0,countPulse,FALLING);
    199. }

    200. void ledVar(int value){
    201.   if (value > 0){
    202.     for(int i=0;i<=value;i++){
    203.       digitalWrite(ledArray[i],HIGH);
    204.     }
    205.     for(int i=5;i>value;i--){
    206.       digitalWrite(ledArray[i],LOW);
    207.     }
    208.   }
    209.   else {
    210.     for(int i=5;i>=0;i--){
    211.       digitalWrite(ledArray[i],LOW);
    212.     }
    213.   }
    214. }


    215. // Stores all of the bmp085's calibration values into global variables
    216. // Calibration values are required to calculate temp and pressure
    217. // This function should be called at the beginning of the program
    218. void bmp085Calibration()
    219. {
    220.   ac1 = bmp085ReadInt(0xAA);
    221.   ac2 = bmp085ReadInt(0xAC);
    222.   ac3 = bmp085ReadInt(0xAE);
    223.   ac4 = bmp085ReadInt(0xB0);
    224.   ac5 = bmp085ReadInt(0xB2);
    225.   ac6 = bmp085ReadInt(0xB4);
    226.   b1 = bmp085ReadInt(0xB6);
    227.   b2 = bmp085ReadInt(0xB8);
    228.   mb = bmp085ReadInt(0xBA);
    229.   mc = bmp085ReadInt(0xBC);
    230.   md = bmp085ReadInt(0xBE);
    231. }

    232. // Calculate temperature given ut.
    233. // Value returned will be in units of 0.1 deg C
    234. short bmp085GetTemperature(unsigned int ut)
    235. {
    236.   long x1, x2;
    237.   
    238.   x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
    239.   x2 = ((long)mc << 11)/(x1 + md);
    240.   b5 = x1 + x2;

    241.   return ((b5 + 8)>>4);  
    242. }

    243. // Calculate pressure given up
    244. // calibration values must be known
    245. // b5 is also required so bmp085GetTemperature(...) must be called first.
    246. // Value returned will be pressure in units of Pa.
    247. long bmp085GetPressure(unsigned long up)
    248. {
    249.   long x1, x2, x3, b3, b6, p;
    250.   unsigned long b4, b7;
    251.   
    252.   b6 = b5 - 4000;
    253.   // Calculate B3
    254.   x1 = (b2 * (b6 * b6)>>12)>>11;
    255.   x2 = (ac2 * b6)>>11;
    256.   x3 = x1 + x2;
    257.   b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
    258.   
    259.   // Calculate B4
    260.   x1 = (ac3 * b6)>>13;
    261.   x2 = (b1 * ((b6 * b6)>>12))>>16;
    262.   x3 = ((x1 + x2) + 2)>>2;
    263.   b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
    264.   
    265.   b7 = ((unsigned long)(up - b3) * (50000>>OSS));
    266.   if (b7 < 0x80000000)
    267.     p = (b7<<1)/b4;
    268.   else
    269.     p = (b7/b4)<<1;
    270.     
    271.   x1 = (p>>8) * (p>>8);
    272.   x1 = (x1 * 3038)>>16;
    273.   x2 = (-7357 * p)>>16;
    274.   p += (x1 + x2 + 3791)>>4;
    275.   
    276.   return p;
    277. }

    278. // Read 1 byte from the BMP085 at 'address'
    279. char bmp085Read(unsigned char address)
    280. {
    281.   unsigned char data;
    282.   
    283.   Wire.beginTransmission(BMP085_ADDRESS);
    284.   Wire.write(address);
    285.   Wire.endTransmission();
    286.   
    287.   Wire.requestFrom(BMP085_ADDRESS, 1);
    288.   while(!Wire.available())
    289.     ;
    290.     
    291.   return Wire.read();
    292. }

    293. // Read 2 bytes from the BMP085
    294. // First byte will be from 'address'
    295. // Second byte will be from 'address'+1
    296. int bmp085ReadInt(unsigned char address)
    297. {
    298.   unsigned char msb, lsb;
    299.   
    300.   Wire.beginTransmission(BMP085_ADDRESS);
    301.   Wire.write(address);
    302.   Wire.endTransmission();
    303.   
    304.   Wire.requestFrom(BMP085_ADDRESS, 2);
    305.   while(Wire.available()<2)
    306.     ;
    307.   msb = Wire.read();
    308.   lsb = Wire.read();
    309.   
    310.   return (int) msb<<8 | lsb;
    311. }

    312. // Read the uncompensated temperature value
    313. unsigned int bmp085ReadUT()
    314. {
    315.   unsigned int ut;
    316.   
    317.   // Write 0x2E into Register 0xF4
    318.   // This requests a temperature reading
    319.   Wire.beginTransmission(BMP085_ADDRESS);
    320.   Wire.write(0xF4);
    321.   Wire.write(0x2E);
    322.   Wire.endTransmission();
    323.   
    324.   // Wait at least 4.5ms
    325.   delay(5);
    326.   
    327.   // Read two bytes from registers 0xF6 and 0xF7
    328.   ut = bmp085ReadInt(0xF6);
    329.   return ut;
    330. }

    331. // Read the uncompensated pressure value
    332. unsigned long bmp085ReadUP()
    333. {
    334.   unsigned char msb, lsb, xlsb;
    335.   unsigned long up = 0;
    336.   
    337.   // Write 0x34+(OSS<<6) into register 0xF4
    338.   // Request a pressure reading w/ oversampling setting
    339.   Wire.beginTransmission(BMP085_ADDRESS);
    340.   Wire.write(0xF4);
    341.   Wire.write(0x34 + (OSS<<6));
    342.   Wire.endTransmission();
    343.   
    344.   // Wait for conversion, delay time dependent on OSS
    345.   delay(2 + (3<<OSS));
    346.   
    347.   // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
    348.   Wire.beginTransmission(BMP085_ADDRESS);
    349.   Wire.write(0xF6);
    350.   Wire.endTransmission();
    351.   Wire.requestFrom(BMP085_ADDRESS, 3);
    352.   
    353.   // Wait for data to become available
    354.   while(Wire.available() < 3)
    355.     ;
    356.   msb = Wire.read();
    357.   lsb = Wire.read();
    358.   xlsb = Wire.read();
    359.   
    360.   up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
    361.   
    362.   return up;
    363. }

    Got any suggestions?..