Pages: 1 [2]   Go Down
Author Topic: Reading values from mySQL (located on a Pi) to arduino mega using python?  (Read 2303 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've finally got some time to work further on this project.

I have this so far

Code:
if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
       
    if (inData != NULL){
      index = 0;
      array[index] = strtok(inData, ",");
      started = true;
      ended = false;
    }
   
    else if (inData == NULL){
      ended =true;
    }
   
    else {
      index++;
    }
   
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }

What I'm trying to accomplish here is to break up inData into an array, separated by a delimiter (comma in this case). The array position is incremented by index.

I receive an error saying "invalid conversion from "char* to int". Any suggestions as what I'm missing?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 152
Posts: 6189
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this the line throwing the error?
Code:
      array[index] = strtok(inData, ",");
If array[] is an integer array, then that would be the error I would expect. strtok returns a character pointer, not an integer index to the array.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50890
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
strtok returns a character pointer
If the token being pointed to represents an integer, you can use atoi() to convert the token to an int, to store in the array.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help!!

Code:
if (inData != NULL){
      index = 0;
      array[index] = atoi(strtok(inData, ","));
      started = true;
      ended = false;
    }
   
    else if (inData == NULL){
      ended =true;
    }
   
    else {
      index++;
    }

When I use this code, array[0] position works but anything after that gives me a 5 digit number which makes me think Im not splitting the string correctly. I think it has to do with the way I handle NULL.

Code:
array[index] = atoi(strtok(inData, ","));
Is this correct. The way I see it; I point to the token with strtok() then convert to int with atoi(). Do I need a line under that that handles NULL? This is so confusing, or do I need a nested if statement in there somewhere, and what would it say?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50890
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When I use this code, array[0] position works but anything after that gives me a 5 digit number which makes me think Im not splitting the string correctly. I think it has to do with the way I handle NULL.
You are only populating the 1st element of the array. Any other element that you print will not be meaningful.

It's generally a good idea to make sure that strtok() returned a valid pointer before trying to dereference it (that it what atoi() does).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't understand. The 0th element is fine,  but the following element is populating and so on...
How do I check that strtok () is infact pointing correctly as you suggest?
The arduino mountain is getting steeper   smiley-mad
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50890
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but the following element is populating and so on...
Where? There is only ONE call like this:
Code:
      array[index] = atoi(strtok(inData, ","));
That gets just the first token and stores it in just the 1st element of the array.

To get additional tokens, and store them in the rest of the array, you need something like:
Code:
   if (strlen(inData) > 0)
   {
      char *token = strtok(inData, ",");
      if(token)
      {
         index = 0;
         array[index] = atoi(token);

         while(token = strtok(NULL, ",")
         {
            array[index++] = atoi(token);
         }
      }

      started = true;
      ended = false;
    }
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks PaulS

Turns out that code works perfectly as it is. Im not particularly interested in the 1st token anyway (i.e. the id).

Regards,
Damian
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, my project is coming along well. I recently encountered a problem with serial communication. The below code works great and does exactly what I want, receiving data from python.  I have a problem with sending data from arduino to python, I find that bytes are being lost along the way, not consistently but ever so often, Communicating from python to arduino works great but communicating from arduino to python is buggy. Am I correct in thinking its a python code problem? I do understand this is an arduino forum and Not python, I just want to make sure it is actually a python problem and not an arduino send communication issue. I feel I need an equivalent of the below code from PaulS on the python??? How I don't know, can't find much info

This code works perfectly receiving data from python, thanks PaulS  smiley
Code:
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Here is the arduino code which sends out to python which I'm not sure is correct for my purpose
Quote
/*----( SENSORS: READS SENSOR INPUTS )----*/
void Sensors()
{
  sensor1.requestTemperatures();  //Get TEMPERATURE from probe
  DHT.read22(DHT22);             //Get TEMP/HUMIDITY from DHT22
  setTime(hour(), minute(), second(), day(), month(), year());
  Serial.print(year());
  Serial.print('/');
  Serial.print(month());
  Serial.print('/');
  Serial.print(day());
  Serial.print(",");
  Serial.print(hour());
  Serial.print(':');
  Serial.print(minute());
  Serial.print(':');
  Serial.print(second());
  Serial.print(",");
  Serial.print(analogRead(photopin));
  Serial.print(",");
  Serial.print(sensor1.getTempCByIndex(0));
  Serial.print(",");
  Serial.print(DHT.humidity, 1);
  Serial.print(",");
  Serial.println(DHT.temperature, 1);
}

Logged

Pages: 1 [2]   Go Up
Jump to: