432 views
in System Integration by

I am trying to read analog sensor data from my arduino via raspberry pi serial USB. I have a python script which reads the data and prints it every second which I know works. 

I am struggling to integrate this data into my GUI. I don't know how to write the code in C to put it in the device driver file, so I have been using the native {system("myPyScript.py")}. I don't know how to extract the data from this script in the code editor in EW and update the property so the widget changes. I've spent days trying to figure out the best way to do it. 

At the moment, it's just one sensor however in the future I will be adding more sensors to the arduino.

Can anyone help? I don't really know what to try next! 

python code just for interest;

#!/usr/bin/env python
import time
import serial

ser = serial.Serial(
        port='/dev/ttyACM0',
        baudrate = 9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1
)

int WaterLevel;

while 1:
        x=ser.readline()
        print x;
        WaterLevel = x

It's the "WaterLevel" figure I am trying to use as a property to alter the widget. 

 

1 Answer

+1 vote
by

You can use the wiringPi library for this purpose.

All the necessary documentation is available here: http://wiringpi.com

Code examples are on github and available at this link.: https://github.com/WiringPi/WiringPi/tree/master/examples

 

1.Install the WiringPi library by typing in the console

sudo apt-get install wiringpi

2. Below I put a code example in C.


#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <wiringPi.h>
#include <wiringSerial.h>

int main ()
{
  int fd ;

  if ((fd = serialOpen ("/dev/ttyACM0", 9600)) < 0)
  {
    fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    return 1 ;
  }

  if (wiringPiSetup () == -1)
  {
    fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
    return 1 ;
  }


  for (;; )
  {
   
    while (serialDataAvail (fd))
    {
      printf ("%d \n",serialGetchar (fd)) ;
      fflush (stdout) ;
    }
  }

return 0 ;
}

Remember to include the wiringpi library when generating the code

gcc -o test test.c -lwiringPi

 

3. Adapt the code to your needs and place it in DeviceDriver.c and configure reading data from gui.

by

I'm definitely getting closer but still not working properly. 

The widget on the screen is now showing some data and I have placed a second widget with a "value". Unfortunately, the widgets are showing garbage values. They are fluctuating between 10 and 53 at regular intervals despite the value on the pi's terminal showing as a steady 545. 

When I assign a static number to the property in the device driver it works fine so there is obviously an error in the code reading the serial. This is the section of code I have in the device driver. 

Can anyone see a problem?   Thanks all. 

 

#include <wiringPi.h>
#include <wiringSerial.h>
....

 

int fd;

int WaterLevel;

 

void DeviceDriver_Initialize( void )

{

#ifdef _ApplicationDeviceClass_
 

  wiringPiSetup();

  fd = serialOpen ("/dev/ttyACM0", 9600 );

...

 

 

int DeviceDriver_ProcessData( void )

{
  int needUpdate = 0;

while (serialDataAvail (fd))
  {
    WaterLevel = (serialGetchar (fd)) ;
    fflush (stdout) ;
  }

 #ifdef _ApplicationDeviceClass__UpdateWaterLevel_

ApplicationDeviceClass__UpdateWaterLevel( DeviceObject, WaterLevel);
      needUpdate = 1;
  #endif


return needUpdate;
}

by

so I've realised its actually splitting the string. 

I've added in the following, which works well when the string is 3 digits however when the sensor puts out anything over 1000, it loses the formatting again and splits the string. 

 

int fd;
    int i = 0;
    char aString[4];
    int WaterLevel = 0;
   

.....

for (i = 0; i <=4; i++)
    {

    aString[i] = putchar(serialGetchar(fd));
    WaterLevel= atoi(aString);
     }
 

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...