HC-06 data receiving

Discussion in 'Embedded Systems and Microcontrollers' started by electruc254, Sep 17, 2015.

  1. electruc254

    Thread Starter New Member

    Sep 17, 2015
    1
    0
    Hello!
    I've wrote a script in python which sends data through PC's bluetooth to HC-06 on arduino.

    "state" is integer from 0 to 1000 (it is controlled through potentiometer)
    ...
    gaugeSocket.send(str(state))
    ...

    I have a problem, because I can't get right character to integer conversion on arduino (from HC-06 serial).

    Code (Text):
    1.  
    2. SoftwareSerial mySerial(10, 11); // RX, TX
    3. char buf[2];
    4. int i=0;
    5. int data;
    6. int ledPin = 9;
    7.  
    8. void setup() {
    9.     pinMode(ledPin, OUTPUT);
    10.   // Open serial communications and wait for port to open:
    11.   Serial.begin(57600);
    12.   while (!Serial) {
    13.     ; // wait for serial port to connect. Needed for Leonardo only
    14.   }
    15.  
    16.   mySerial.begin(9600);
    17. }
    18.  
    19. void loop() { // run over and over
    20.   if (mySerial.available()) {
    21.     for(;i<2; ){
    22.       buf[I] = mySerial.read();
    23.       i++;
    24.     }
    25.     i = 0;
    26.     Serial.print(buf);
    27.     data = atoi(buf);
    28.   }
    29.  
    30.     analogWrite(ledPin, data/10); //To see how code behaves on LED
    31. }
    32.  
    Serial print gives me this (if I turn potentiometer from 0 to 1000):
    0ÿ(0ÿ(0ÿ(0ÿ(0ÿ(0ÿ(0ÿ(60(1ÿ(00(16(0ÿ(23(0ÿ(30(0ÿ(3ÿ(60(43(0ÿ(49(0ÿ(53(0ÿ(5ÿ(90(69(0ÿ(88(0ÿ(10(00(10(00(10(

    What am I missing?

    Thank you[/I]
     
Loading...