Arduino CanBus Shield Python and Android

Arduino CanBus Shield Python and Android

Postby capu81 » Fri Nov 21, 2014 5:14 pm

Hi, i'm trying to use python (SL4a) for read bluetooth serial event from Arduino with CanBus Shield.

I'm able to receive data from arduino. If i use Blueterm on Android i'm able to read code

I've made a simple python program to read code from android but arduino string is very very strange......

This is the Arduino sketch
Code: Select all
#include <SPI.h>
#include "mcp_can.h"
#include <SoftwareSerial.h>

INT32U canId = 0x000;

unsigned char len = 0;
unsigned char buf[8];
char str[20];
int CanMessage=0;
int HeadLightsDetected=0;
const int rxPin = 7; //SoftwareSerial RX pin, connect to JY-MCY TX pin
const int txPin = 4; //SoftwareSerial TX pin, connect to JY-MCU RX pin
                     // level shifting to 3.3 volts may be needed

SoftwareSerial mySerial(rxPin, txPin); // RX, TX

void setup()
{
    mySerial.begin(9600);

START_INIT:

    if(CAN_OK == CAN.begin(CAN_100KBPS))
    {
        mySerial.println("CAN BUS Shield init ok!");
    }
    else
    {
        mySerial.println("CAN BUS Shield init fail");
        mySerial.println("Init CAN BUS Shield again");
        delay(100);
        goto START_INIT;
    }
}


void loop()
{
    if(CAN_MSGAVAIL == CAN.checkReceive()) 
    {
        CAN.readMsgBuf(&len, buf);
        CanMessage=0;
        canId = CAN.getCanId();
        switch (canId) {
          case 1473:
            //comandi al volante
            for(int i = 0; i<len; i++)
            {
              CanMessage = CanMessage + buf[i];
            }
            //mySerial.println(CanMessage);
            switch (CanMessage) {
              case 102:
              mySerial.println("vol+");
              delay(300);
              break;
              case 103:
              mySerial.println("vol-");
              delay(300);
              break;
              case 99:
              mySerial.println("prev");
              delay(300);
              break;
              case 98:
              mySerial.println("next");
              delay(300);
              break;
              case 122:
              mySerial.println("phone");
              delay(300);
              break;
              case 139:
              mySerial.println("mic");
              delay(300);
              break;
              case 137:
              mySerial.println("back");
              delay(300);
              break;
              case 136:
              mySerial.println("ok");
              delay(300);
              break;
            }
            break;
        }
    }
}


and this is the python code
Code: Select all
import android
from sys import stdout
BT_DEVICE_ID = '20:13:05:06:47:19'
droid = android.Android()
droid.toggleBluetoothState(True)  # turn on bluetooth
print droid.bluetoothConnect('00001101-0000-1000-8000-00805F9B34FB', BT_DEVICE_ID)
print droid.bluetoothGetConnectedDeviceName()
droid.ttsSpeak('Interfaccia arduino comandi al volante pronta')
while True:
    message = droid.bluetoothRead().result
    #message = 'prev'
    print message
    print ("-------")
    print message[0].encode("utf-8")
    print ("-------")
    print message.encode("utf-8")
    print ("----------------------")
    if message == 'rev\r\n':
        droid.makeToast(getMediaVolume().result)
        droid.ttsSpeak('volume: ' + getMediaVolume().result)
droid.exit()



If i press volume+ on steering wheel arduino send string "vol+" but in python if i use repr(message) i receive:
u'v'
u'ol+\r\n'

There is a way to receive the original "vol+" string?
I think python make an Array:
message[0] is "v"
message[1] is "ol+"

I must modify arduino sketch or the python code?
capu81
 
Posts: 13
Joined: Sat Oct 18, 2014 4:57 pm

Re: Arduino CanBus Shield Python and Android

Postby capu81 » Fri Nov 21, 2014 9:00 pm

I've found the error

i've change
Code: Select all
message = droid.bluetoothRead().result


with

Code: Select all
message = droid.bluetoothReadLine().result
capu81
 
Posts: 13
Joined: Sat Oct 18, 2014 4:57 pm


Return to OBD2 & CAN-BUS Projects

Who is online

Users browsing this forum: No registered users and 1 guest

cron