10 Replies Latest reply on Mar 21, 2016 10:41 AM by DiegoV_Intel

    Arduino e-Health code to Python

    HE

      Hi,

       

      I've an e-Health kit of Cooking Hacks and I would like to use is together with the Intel Edison board (Arduino breakout). I tried some of the sensors already, things are working like expected. I used the Cooking Hacks library for Intel Galileo. Now, actually I prefer to write Python code instead of the Arduino language.

       

      A lot of the code is easy to port, but I'm lost with the getting data out of the blood pressure sensor. It works with a serial connection. Here is the Arduino code (included in the library): [C++] /* * eHealth sensor platform for Arduino and Raspberry from Cooking-hacks. - Pastebin.com

       

      The code for the blood pressure sensor is implemented in the readBloodPressureSensor function.

       

      I'm currently having the following lines of code:

      u = mraa.Uart(0) u.setBaudRate(19200) u.setMode(8, mraa.UART_PARITY_NONE, 1) u.setFlowcontrol(False, False) print (u.getDevicePath()) u.write(bytearray.fromhex("aa")) usleep(1) u.write(bytearray.fromhex("55")) usleep(1) u.write(bytearray.fromhex("88")) time.sleep(2.5)

       

      if u.dataAvailable(100):

         print(u.readStr(250))

      Unfortunately, I don't get anything useful. It would be nice if someone could help me a little bit.

        • 1. Re: Arduino e-Health code to Python
          DiegoV_Intel

          Hi HE,

           

          I recommend you to check the serial communication first. The following MRAA examples might be helpful for this purpose. Try to test them just to be sure the UART is working:

           

          If those examples work, we can focus on the readBloodPressureSensor function.

           

          Regards,

          Diego

          • 2. Re: Arduino e-Health code to Python
            HE

            DiegoV_Intel, I just did a little test. Connected an Arduino 101 to the serial pins, loaded a program that prints out the received data and sends back a message. On the Edison side, I uses the code provided on GitHub. It works like a charm, so it should be possible to use the blood pressure sensor.

            • 3. Re: Arduino e-Health code to Python
              DiegoV_Intel

              Hi HE,

               

              Yes, it should be possible. I've checked the readBloodPressureSensor() function and it's composed by some write() and read() methods. What kind of issue did you have when you tried to port it to Python?

               

              I recommend you to divide the function in small parts. Check the code below, it is the readBloodPressureSensor() function you mentioned in your first post. I added some comments to divide the function in small parts. Try to port each part one by one and check it with your Arduino 101 in the same way you did with the previous test. If you are able to test one part successfully, then proceed with the next one until you have ported the complete function.

               

              void eHealthClass::readBloodPressureSensor(void)
                  {  
              /********** PART 1 - Start *********/
                      unsigned char _data;
                      int ia=0;
                      length=0;
              
                      Serial1.begin(19200);
                      Serial1.write(0xAA);
                      delayMicroseconds(1);
                      Serial1.write(0x55);
                      delayMicroseconds(1);
                      Serial1.write(0x88);
                      delay(2500);
              
                      Serial1.print("\n");
              /********** PART 1 - End *********/
              /********** PART 2 - Start *********/
                      if (Serial1.available() > 0) { // The protocol sends the measures
              
                          for (int i = 0; i<4; i++){ // Read four dummy data 
                              Serial1.read();
                          }
               /********** PART 2 - End *********/
               /********** PART 3 - Start *********/         
                          while(_data != 0xD1){
                              if (ia==0){
                              _data = Serial1.read();
                              }
               /********** PART 3 - End *********/
               /********** PART 4 - Start *********/
                          bloodPressureDataVector[length].year = swap(_data);
                          bloodPressureDataVector[length].month = swap(Serial1.read());
                          bloodPressureDataVector[length].day = swap(Serial1.read());
                          bloodPressureDataVector[length].hour = swap(Serial1.read());
                          bloodPressureDataVector[length].minutes = swap(Serial1.read());
                          bloodPressureDataVector[length].systolic = swap(Serial1.read());
                          bloodPressureDataVector[length].diastolic = swap(Serial1.read());
                          bloodPressureDataVector[length].pulse = swap(Serial1.read());
                          length++;
                          ia=1;
               /********** PART 4 - End *********/
              /********** PART 5 - Start *********/
              
                          for (int i = 0; i<4; i++){ // CheckSum 1
                              Serial1.read();
                          }
                         
                          _data = Serial1.read();
                          }
               /********** PART 5 - End *********/
               /********** PART 6 - Start *********/
                          for (int i = 0; i<3; i++){ // CheckSum 2
                              Serial1.read();
                          }  
               /********** PART 6 - End *********/         
                      }
                  }
              

               

               

              Regards,

              Diego

              • 4. Re: Arduino e-Health code to Python
                HE

                Well, the problem is that I don't know exactly how to port everything.

                 

                First of all, the first part I translated into:

                usleep = lambda x: time.sleep(x / 1000000.0)
                
                u = mraa.uart(0)
                
                u.setBaudRate(19200)
                u.setMode(8, mraa.UART_PARITY_NONE, 1)
                u.setFlowcontrol(False, False)
                
                u.write(bytearray.fromhex("aa"))
                usleep(1)
                u.write(bytearray.fromhex("55"))
                usleep(1)
                u.write(bytearray.fromhex("88"))
                time.sleep(2.5)
                
                u.writeStr("\n")
                

                 

                First of all, I'm not sure if I'm opening the connection in the right way. Also, I'm not sure about how to send the hex vars in a proper way. Anyway, I don't get any errors, so maybe it is behaving like it should.

                 

                About the second part, I don't have a real idea how to port this piece of code. I suppose I could use if u.dataAvailable():  followed by a for. But what about the read?

                • 5. Re: Arduino e-Health code to Python
                  DiegoV_Intel

                  Hi HE,

                   

                  Your code looks good. I tested the way you are sending the hex values and it works. Another way could be the following: u.write(bytearray([0xAA]))

                   

                  Regarding the second part, you could use the method dataAvailable(). For the read part, there is also a method for reading values that you can use. I recommend you to check the documentation of the MRAA API in the following site: Welcome to mraa’s documentation! — mraa v0.9.6 documentation. You can check the description of each method and see how you can use them in your code.

                   

                  Regards,

                  Diego

                  • 6. Re: Arduino e-Health code to Python
                    HE

                    Hi Diego,

                     

                    Thanks for the answer! I think it must be something like this:

                     

                    if u.dataAvailable():
                        for i in range(0, 4):
                              test = u.read(1)
                              print (binascii.hexlify(bytearray(test)))
                    
                        _data = ''
                        while _data != 'd1':
                              if ia == 0
                                  _data = u.read(1)
                                  _data = binascii.hexlify(bytearray(_data))
                                  print (_data)
                    
                    
                    
                    
                    

                     

                    So far, so good, it looks like this is working. But I'm actually not sure about the read(1) statement. I checked out the Arduino documentation, the read() methods is inherited from the Stream class. In the documentation they say it reads the first byte of incoming data available. If not data is available -1 is returned. (I removed the checks in the above code, just to keep it readable.) The MRAA documentation says that the read function required one parameter, which is the lenght. I suppose this means the amount of bytes that are read. And as you can see, I converted the data to hex values. Is this right?

                     

                    By the way, here is a list of the hex values that are printed: c1 c2 c3 c4 fe ef 0f 0f 9d a7 d8 aa ff f0 f1 f2 fe df 5e be - Pastebin.com

                     

                    I think if the above code is right, I now should port the swap function. Looks like this:

                    char eHealthClass::swap(char _data)
                    {
                         char highBits = (_data & 0xF0) / 16; 
                         char lowBits =  (_data & 0x0F) * 16; 
                         return ~(highBits + lowBits);
                    }
                    
                    • 7. Re: Arduino e-Health code to Python
                      DiegoV_Intel

                      Hi HE,

                       

                      According to the MRAA documentation, the parameter used in the read() method is the buffer pointer. You could use the readStr() method instead. The parameter used in this case is the number of bytes to read. Check how it is used in this example: mraa/uart_receiver.py at master · intel-iot-devkit/mraa · GitHub

                       

                      I recommend you to check the data received by the read() and readStr() methods to be sure that the data received is correct. Test it with your Arduino 101. If you know what the data sent by the Arduino 101 is, you will know if the data received by Edison is the correct one.

                       

                      Once you have confirmed that you are able to send and receive the correct data, you can proceed with the rest.

                       

                      Regards,

                      Diego

                      • 8. Re: Arduino e-Health code to Python
                        HE

                        I checked it out, so far I got the exact same data: http://prntscr.com/ahykfp .

                        • 9. Re: Arduino e-Health code to Python
                          HE

                          Do you have any idea how to implement the swap method in Python?

                          • 10. Re: Arduino e-Health code to Python
                            DiegoV_Intel

                            Hi HE ,

                             

                            The swap method is composed by a couple of logic operations. These operations can be done in Python in the same way. I recommend you to check the following site for additional details about bitwise operators: https://wiki.python.org/moin/BitwiseOperators

                             

                            Regards,

                            Diego