Go Down

Topic: Gobetwino code for temperature measurement (Read 5194 times) previous topic - next topic

golfmon

Hi , all

I tried to use Gobetwino for collecting data in Txt. file by combining code for arduino with code of Gobetwino, but it didn't work. So hope anyone could help me with this please. It is my final year project.

#include <string.h> //Use the string Library
#include <ctype.h>
#include <EEPROM.h>

#define PINEN 7 //Mux Enable pin
#define PINA0 4 //Mux Address 0 pin
#define PINA1 5 //Mux Address 1 pin
#define PINA2 6 //Mux Address 2 pin
#define PINSO 12 //TCAmp Slave Out pin (MISO)
#define PINSC 13 //TCAmp Serial Clock (SCK)
#define PINCS 9  //TCAmp Chip Select Change this to match the position of the Chip Select Link

unsigned int Temp[8], SensorFail[8];
unsigned int Mask;
//char data[16];
char i, j, NumSensors =1, UpdateDelay;
char Rxchar, Rxenable, Rxptr, Cmdcomplete, R;
char Rxbuf[15];
char adrbuf[3], cmdbuf[3], valbuf[12];
int val = 0, Param;     
unsigned long time;
int serInLen = 25;
char serInString[25];
void setup()   
{     
  Serial.begin(9600); 
  Serial.println("TCMUX");
  if (EEPROM.read(511)==1)
  {
    NumSensors = EEPROM.read(0);
    UpdateDelay = EEPROM.read(1);
  }
  pinMode(PINEN, OUTPUT);     
  pinMode(PINA0, OUTPUT);   
  pinMode(PINA1, OUTPUT);   
  pinMode(PINA2, OUTPUT);   
  pinMode(PINSO, INPUT);   
  pinMode(PINCS, OUTPUT);   
  pinMode(PINSC, OUTPUT);   
 
  digitalWrite(PINEN, HIGH);   // enable on
  digitalWrite(PINA0, LOW); // low, low, low = channel 1
  digitalWrite(PINA1, LOW);
  digitalWrite(PINA2, LOW);
  digitalWrite(PINSC, LOW); //put clock in low
 
Serial.println("#S|CPTEST|[]#");

}

void loop()                     
{
  if (millis() > (time + ((unsigned int)UpdateDelay*1000)))
  {
    time = millis();
    //for(j=0;j<NumSensors;j++)
    //{
    if (j<(NumSensors-1)) j++;
    else j=0;
     
      switch (j) //select channel
      {
        case 0:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, LOW);
        break;
        case 1:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, LOW);
        break;
        case 2:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, LOW);
        break;
        case 3:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, LOW);
        break;
        case 4:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, HIGH);
        break;
        case 5:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, HIGH);
        break;
        case 6:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, HIGH);
        break;
        case 7:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, HIGH);
        break;
      }
     
      delay(5);
      digitalWrite(PINCS, LOW); //stop conversion
      delay(5);
      digitalWrite(PINCS, HIGH); //begin conversion
      delay(250);  //wait 250 ms for conversion to complete
      digitalWrite(PINCS, LOW); //stop conversion, start serial interface
      delay(1);
      Temp[j] = 0;
   
      for (i=15;i>=0;i--)
      {
          digitalWrite(PINSC, HIGH);
          delay(1);
         
          /*if (digitalRead(PINSO)==1)
          {
            //Temp[j] += Mask;
            Serial.print("1");
          }
          else
          {
            Serial.print("0");
          }*/
        if ((i<15) && (i>2))
        {
         
          Mask = 1<<(i-3);
          if (digitalRead(PINSO)==1)
          {
            Temp[j] += Mask;
            //Serial.print("1");
          }
          else
          {
           // Serial.print("0");
          }
        }
        if (i==2)
        {
          SensorFail[j] = digitalRead(PINSO);
        }
        digitalWrite(PINSC, LOW);
        delay(1);
        //delay(1);
      }
      //Serial.println();
   
      //Serial.println(Temp,BIN);
      //Serial.print("#");
      //Serial.print(j+1,DEC);
     // Serial.print(": ");
      if (SensorFail[j] == 1)
      {
        Serial.println("FAIL");
      }
      else
      {
        Serial.print(Temp[j]>>2);
        Serial.print(".");
        if ((Temp[j] & 0b11) == 0b00)
        {
          Serial.print("00");
        }
        if ((Temp[j] & 0b11) == 0b01)
        {
          Serial.print("25");
        }
        if ((Temp[j] & 0b11) == 0b10)
        {
          Serial.print("50");
        }
        if ((Temp[j] & 0b11) == 0b11)
        {
          Serial.print("75");
        }
        Serial.println(176,BYTE);
      //}
        //delay(1000);
    }//end reading sensors
  }//end time
  if (Serial.available() > 0)    // Is a character waiting in the buffer?
  {
    Rxchar = Serial.read();      // Get the waiting character

    if (Rxchar == '@')      // Can start recording after @ symbol
    {
      if (Cmdcomplete != 1)
      {
        Rxenable = 1;
        Rxptr = 1;
      }//end cmdcomplete
    }//end rxchar
    if (Rxenable == 1)           // its enabled so record the characters
    {
      if ((Rxchar != 32) && (Rxchar != '@')) //dont save the spaces or @ symbol
      {
        Rxbuf[Rxptr] = Rxchar;
        //Serial.println(Rxchar);
        Rxptr++;
        if (Rxptr > 13)
        {
          Rxenable = 0;
        }//end rxptr
      }//end rxchar
      if (Rxchar == 13)
      {
        Rxenable = 0;
        Cmdcomplete = 1;
      }//end rxchar
    }//end rxenable

  }// end serial available


   
  if (Cmdcomplete == 1)
  {
    Cmdcomplete = 0;
     cmdbuf[0] = toupper(Rxbuf[1]); //copy and convert to upper case
     cmdbuf[1] = toupper(Rxbuf[2]); //copy and convert to upper case
     cmdbuf[2] = 0; //null terminate        Command = Chr(rxbuf(3)) + Chr(rxbuf(4))
     //   Command = Ucase(command)
  //Serial.println(cmdbuf);
     valbuf[0] = Rxbuf[3]; //        Mystr = Chr(rxbuf(5))
        R = Rxptr - 1;
            for (i = 4 ; i <= R ; i++)//For I = 6 To R
            {
                valbuf[i-3] = Rxbuf; //Mystr = Mystr + Chr(rxbuf(i))
            }
     valbuf[R+1] = 0; //null terminate
     Param = atoi(valbuf);//   Param = Val(mystr)

     //Serial.println(Param); //   'Print "Parameter: " ; Param

       
              if (strcmp(cmdbuf,"NS")==0)       //NumSensors
              {
                   //'Print "command was ON"
                   if ((Param <= 8) && (Param > 0))
                   {
                      NumSensors = Param;                   
                   }
                   
              }
              if (strcmp(cmdbuf,"UD")==0)       //UpdateDelay
              {
                   //'Print "command was ON"
                   if ((Param <= 60) && (Param >= 0))
                   {
                      UpdateDelay = Param;                   
                   }
                   
              }
              if (strcmp(cmdbuf,"SV")==0)       //Save
              {
                   EEPROM.write(0,NumSensors);
                   EEPROM.write(1,UpdateDelay);
                   EEPROM.write(511,1);
              }
         }
}


void logData( int Temp)
{
   char buffer[5];
 
   Serial.print("#S|GOGETTEMP|[");
   Serial.print(itoa((Temp), buffer, 10));
   Serial.print(";");
      Serial.println("]#");
   readSerialString(serInString,1000);
 
}


{
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray = Serial.read();
      i++;
   }
}


when i executed this code with Gobetwino, the result shown as in the picture i attached. Data is not stored in txt. file at all.



golfmon

arduino board that i used is http://www.oceancontrols.com.au/KTA-259.html
I am really really the beginner of this technology, please help me out.

robtillaart


Please modify your post, select the code and press the # button so the code gets properly tagged. looks so much better.

Advice, start working with the gobetwino samples before mixing it with you code.

this line of code is WRONG (sorry for shouting)
if (millis() > (time + ((unsigned int)UpdateDelay*1000)))
==>
if (millis() - (UpdateDelay*1000) > time) 

and make     unsigned long UpdateDelay;  that makes the math 100% unsigned long (less chance for mistakes)

millis() will overflow  and subtraction is overflow proof, addition not. (why? do the math on paper with a pencil and you will see)

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

PaulS

Code: [Select]
Serial.println("#S|CPTEST|[]#");
   Serial.print("#S|GOGETTEMP|[");

What, on the PC, is supposed to respond to this data?

Quote
when i executed this code with Gobetwino, the result shown as in the picture i attached.

Pictures "attached" using post-it notes don't work. I saw no attachments.
The art of getting good answers lies in asking good questions.

golfmon

Sorry about the picture

golfmon

Code: [Select]

#include <string.h> //Use the string Library
#include <ctype.h>
#include <EEPROM.h>

#define PINEN 7 //Mux Enable pin
#define PINA0 4 //Mux Address 0 pin
#define PINA1 5 //Mux Address 1 pin
#define PINA2 6 //Mux Address 2 pin
#define PINSO 12 //TCAmp Slave Out pin (MISO)
#define PINSC 13 //TCAmp Serial Clock (SCK)
#define PINCS 9  //TCAmp Chip Select Change this to match the position of the Chip Select Link

unsigned int Temp[8], SensorFail[8];
unsigned int Mask;
//char data[16];
char i, j, NumSensors =1, UpdateDelay;
char Rxchar, Rxenable, Rxptr, Cmdcomplete, R;
char Rxbuf[15];
char adrbuf[3], cmdbuf[3], valbuf[12];
int val = 0, Param;     
unsigned long time;
int serInLen = 25;
char serInString[25];
void setup()   
{     
  Serial.begin(9600); 
  Serial.println("TCMUX");
  if (EEPROM.read(511)==1)
  {
    NumSensors = EEPROM.read(0);
    UpdateDelay = EEPROM.read(1);
  }
  pinMode(PINEN, OUTPUT);     
  pinMode(PINA0, OUTPUT);   
  pinMode(PINA1, OUTPUT);   
  pinMode(PINA2, OUTPUT);   
  pinMode(PINSO, INPUT);   
  pinMode(PINCS, OUTPUT);   
  pinMode(PINSC, OUTPUT);   
 
  digitalWrite(PINEN, HIGH);   // enable on
  digitalWrite(PINA0, LOW); // low, low, low = channel 1
  digitalWrite(PINA1, LOW);
  digitalWrite(PINA2, LOW);
  digitalWrite(PINSC, LOW); //put clock in low
 
Serial.println("#S|CPTEST|[]#");

}

void loop()                     
{
  if (millis() > (time + ((unsigned int)UpdateDelay*1000)))
  {
    time = millis();
    //for(j=0;j<NumSensors;j++)
    //{
    if (j<(NumSensors-1)) j++;
    else j=0;
     
      switch (j) //select channel
      {
        case 0:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, LOW);
        break;
        case 1:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, LOW);
        break;
        case 2:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, LOW);
        break;
        case 3:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, LOW);
        break;
        case 4:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, HIGH);
        break;
        case 5:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, LOW);
          digitalWrite(PINA2, HIGH);
        break;
        case 6:
          digitalWrite(PINA0, LOW);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, HIGH);
        break;
        case 7:
          digitalWrite(PINA0, HIGH);
          digitalWrite(PINA1, HIGH);
          digitalWrite(PINA2, HIGH);
        break;
      }
     
      delay(5);
      digitalWrite(PINCS, LOW); //stop conversion
      delay(5);
      digitalWrite(PINCS, HIGH); //begin conversion
      delay(250);  //wait 250 ms for conversion to complete
      digitalWrite(PINCS, LOW); //stop conversion, start serial interface
      delay(1);
      Temp[j] = 0;
   
      for (i=15;i>=0;i--)
      {
          digitalWrite(PINSC, HIGH);
          delay(1);
         
          /*if (digitalRead(PINSO)==1)
          {
            //Temp[j] += Mask;
            Serial.print("1");
          }
          else
          {
            Serial.print("0");
          }*/
        if ((i<15) && (i>2))
        {
         
          Mask = 1<<(i-3);
          if (digitalRead(PINSO)==1)
          {
            Temp[j] += Mask;
            //Serial.print("1");
          }
          else
          {
           // Serial.print("0");
          }
        }
        if (i==2)
        {
          SensorFail[j] = digitalRead(PINSO);
        }
        digitalWrite(PINSC, LOW);
        delay(1);
        //delay(1);
      }
      //Serial.println();
   
      //Serial.println(Temp,BIN);
      //Serial.print("#");
      //Serial.print(j+1,DEC);
     // Serial.print(": ");
      if (SensorFail[j] == 1)
      {
        Serial.println("FAIL");
      }
      else
      {
        Serial.print(Temp[j]>>2);
        Serial.print(".");
        if ((Temp[j] & 0b11) == 0b00)
        {
          Serial.print("00");
        }
        if ((Temp[j] & 0b11) == 0b01)
        {
          Serial.print("25");
        }
        if ((Temp[j] & 0b11) == 0b10)
        {
          Serial.print("50");
        }
        if ((Temp[j] & 0b11) == 0b11)
        {
          Serial.print("75");
        }
        Serial.println(176,BYTE);
      //}
        //delay(1000);
    }//end reading sensors
  }//end time
  if (Serial.available() > 0)    // Is a character waiting in the buffer?
  {
    Rxchar = Serial.read();      // Get the waiting character

    if (Rxchar == '@')      // Can start recording after @ symbol
    {
      if (Cmdcomplete != 1)
      {
        Rxenable = 1;
        Rxptr = 1;
      }//end cmdcomplete
    }//end rxchar
    if (Rxenable == 1)           // its enabled so record the characters
    {
      if ((Rxchar != 32) && (Rxchar != '@')) //dont save the spaces or @ symbol
      {
        Rxbuf[Rxptr] = Rxchar;
        //Serial.println(Rxchar);
        Rxptr++;
        if (Rxptr > 13)
        {
          Rxenable = 0;
        }//end rxptr
      }//end rxchar
      if (Rxchar == 13)
      {
        Rxenable = 0;
        Cmdcomplete = 1;
      }//end rxchar
    }//end rxenable

  }// end serial available


   
  if (Cmdcomplete == 1)
  {
    Cmdcomplete = 0;
     cmdbuf[0] = toupper(Rxbuf[1]); //copy and convert to upper case
     cmdbuf[1] = toupper(Rxbuf[2]); //copy and convert to upper case
     cmdbuf[2] = 0; //null terminate        Command = Chr(rxbuf(3)) + Chr(rxbuf(4))
     //   Command = Ucase(command)
  //Serial.println(cmdbuf);
     valbuf[0] = Rxbuf[3]; //        Mystr = Chr(rxbuf(5))
        R = Rxptr - 1;
            for (i = 4 ; i <= R ; i++)//For I = 6 To R
            {
                valbuf[i-3] = Rxbuf[i]; //Mystr = Mystr + Chr(rxbuf(i))
            }
     valbuf[R+1] = 0; //null terminate
     Param = atoi(valbuf);//   Param = Val(mystr)

     //Serial.println(Param); //   'Print "Parameter: " ; Param

       
              if (strcmp(cmdbuf,"NS")==0)       //NumSensors
              {
                   //'Print "command was ON"
                   if ((Param <= 8) && (Param > 0))
                   {
                      NumSensors = Param;                   
                   }
                   
              }
              if (strcmp(cmdbuf,"UD")==0)       //UpdateDelay
              {
                   //'Print "command was ON"
                   if ((Param <= 60) && (Param >= 0))
                   {
                      UpdateDelay = Param;                   
                   }
                   
              }
              if (strcmp(cmdbuf,"SV")==0)       //Save
              {
                   EEPROM.write(0,NumSensors);
                   EEPROM.write(1,UpdateDelay);
                   EEPROM.write(511,1);
              }
         }
}
// Send the LOGTEST command to Gobetwino the 3 random values are seperated by semicolons

void logData( int Temp)
{
   char buffer[5];
 
   Serial.print("#S|GOGETTEMP|[");
   Serial.print(itoa((Temp), buffer, 10));
   Serial.print(";");
      Serial.println("]#");
   readSerialString(serInString,1000);
 
}


void readSerialString (char *strArray,long timeOut)
{
   long startTime=millis();
   int i;

   while (!Serial.available()) {
      if (millis()-startTime >= timeOut) {
         return;
      }
   }
   while (Serial.available() && i < serInLen) {
      strArray[i] = Serial.read();
      i++;
   }
}


Thanks very much

golfmon

here' s the picture

golfmon

Hello,

Pauls, that code is from Gobetwino entirely and it works for the sample code with PC. However, when i mixed it with the other code, for collecting temperature, it didn't work.

robtillaart, that line i also got it from the company code for collecting temperature data. Therefore, i am not sure that i will be a wrong code because when i purely executed this code without mixing it with Gobetwino code, it works.

The problem is Gobetwino show a message " The string received:  32.8? is not well formed command string"

Could anyone please help me

Thank you

MikMo

#8
Aug 25, 2011, 08:54 am Last Edit: Aug 25, 2011, 09:07 am by MikMo Reason: 1
Do you have anything connected to pins 0 and 1 on the Arduino board ?

These pins are the serial RX and TX pins and if you try to use them at the ame time as sending serial over the USB you can get strange problems.

EDIT:

Also what is this line in your Void setup doing:
Serial.println("TCMUX");

It is confusing GoBetwino, and so isall the other serial.print stuff that is not GoBetwino commands, like "FAIL"
When using GoBetwino you should not send anything but GoBetwino commands over ther serial connection.

It also looks like you are trying to use the serial monitor at the same time as GoBetwino, this is not possible.

golfmon

Quote
Also what is this line in your Void setup doing:
Serial.println("TCMUX");


It is all the code from a company, so i am not sure if i should change it since the pure code for getting temperature does work properly but when i mixed it with Gobetwino code, data isn't stored in .txt file but in arduino serial monitor everything still work.

Please Help , don't know what to change anymore :smiley-zipper:

MikMo

#10
Sep 05, 2011, 06:33 pm Last Edit: Sep 05, 2011, 06:35 pm by MikMo Reason: 1
You can not use the serial monitor and GoBetwino at the same time. It is not a limitation in GoBetwino, but simply because  two different processes can not open the same serial port at the same time

GoBetwino will not work if you send anything else than GoBetwino commands over the serial line.

Only one device can be connected to the other end of the serial connection.


MikMo

Try to remove all the code that sends data over the serial line, that is not GoBetwino related and see what happens.

golfmon

Quote
Try to remove all the code that sends data over the serial line, that is not GoBetwino related and see what happens.


I am so sorry i am very very beginner with this, so i don't know what is the "serial line"

please tell me

thx u for ur time

MikMo

The USB port on Arduino is really a serial port.

So when i say "the serial line" i mean the connection between your Arduino and your PC. It looks like a USB connectin, but it really is a serial connection.
On the PC it show up as a serial port you can use as any other serial port.

So what i meant in my previous post is remove all "serial.print" or "serial.println" statements that is not there to send  a GoBetwino command.

golfmon

I just put // in front of every "serial.print" and "serial.println" for the arduino code, then i uploaded and executed it.
When I open the Gobetwino file, this time nothing came out; there was only one line ' "9/9/2011 8:56:05 AM   Serial port : COM5 opened at 9600 baud"

Did i fix the right thing?

thx

Go Up