Help With Command Line

Hi all.

I have a PH Sensor and inside the example it states

"type enterph into the serial command line to start calibration"

I want to get this to work from a button meaning when I press input 5 for example I want to send "enterph" to the serial command. I have tried a few things but not having any luck.

What is the correct method for this.

Thank You

Please post your best attempt (using code tags when you do) and describe what is wrong

Which Arduino board are you using ?

You might post a link to the manual for this device too.

DFRobot_PH.zip (415.0 KB)

Client2.ReadArea(S7AreaDB,DBNum,34,1,Buffer20); // Buffer 20 is array of bool in CPU

              bit1 = S7.BitAt(Buffer20,0,0); Serial.print("Testing Read Bit 1_"); Serial.println(bit1); // Check Bit 1 Status
              bit2 = S7.BitAt(Buffer20,0,1); Serial.print("Testing Read Bit 2_"); Serial.println(bit2); // Check Bit 2 Status

              String cmd = ("enterph");
              if (bit1 == 1) {Serial.print(cmd);}

Hi all, I have attached my code that i want to write to serial command. I have also attached the correct library.

When I use serial command and type in enterph it works, i just want this function when bit1 is high the command is sent.

Using Uno R3 or R4 board.

Thank You

Hi,

I have it connected to a siemens cpu so sending bits to the arduino. I would like when a certain bit goes high (which i already have working i believe) I need the command to work but the command only works when directly typing in with the serial box.

Thank you for the reply

Hi,

sorry its been one of them days. Everything sensor wise and comms works from arduino to siemens and vice versa. The PH sensor states write in the serial monitor text box "enterph" to start the calibration. I would like to when I send a high bol signal from siemens to arduino (bit1) which i have working it writes enterph into the serial monitor box to activate the command.

file DFRobot_PH_Test.h
//  brief This is the sample code for Gravity: Analog pH Sensor / Meter Kit V2, SKU:SEN0161-V2.
//  In order to guarantee precision, a temperature sensor such as DS18B20 is needed, to execute automatic temperature compensation.
//  You can send commands in the serial monitor to execute the calibration.
//
//    Serial Commands:
//    enterph -> enter the calibration mode
//    calph   -> calibrate with the standard buffer solution, two buffer solutions(4.0 and 7.0) will be automaticlly recognized
//    exitph  -> save the calibrated parameters and exit from calibration mode

```cpp
//----------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------
//
//  file DFRobot_PH_Test.h
//  brief This is the sample code for Gravity: Analog pH Sensor / Meter Kit V2, SKU:SEN0161-V2.
//  In order to guarantee precision, a temperature sensor such as DS18B20 is needed, to execute automatic temperature compensation.
//  You can send commands in the serial monitor to execute the calibration.
//
//    Serial Commands:
//    enterph -> enter the calibration mode
//    calph   -> calibrate with the standard buffer solution, two buffer solutions(4.0 and 7.0) will be automaticlly recognized
//    exitph  -> save the calibrated parameters and exit from calibration mode
//
//----------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------


#include <Platform.h>
#include <Settimino.h>

#include <TimerOne.h>
#include <DHT22.h>

#include <DFRobot_PH.h>
#include <EEPROM.h>


// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 4

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);




#define _NORMAL


#define PH_PIN A1
float voltage,phValue,temperature = 25;
DFRobot_PH ph;

byte mac[] = {0xA1, 0x10, 0x0A, 0xAA, 0x00, 0x40 }; // Must be unique for every ethernet shield
IPAddress Local(192,168,0,140); // Local Address


IPAddress PLC(192,168,0,5);   // PLC Address
IPAddress Gateway(192, 168, 0, 1);
IPAddress Subnet(255, 255, 255, 0);

int DBNum = 961;      // The number of data block we want to exchange the data

byte Buffer1[1];       // WErite byte To CPU - 1 Byte split into 8 bits - used for comms check in Siemens

float Buffer2[4];      // Buffer For Sensor 
float Buffer3[4];      // Buffer for Sensor 

float Buffer4[4];      // Buffer for Sensor 
float Buffer5[4];      // Buffer for Sensor 

float Buffer6[4];      // Buffer for Sensor 
float Buffer7[4];      // Buffer for Sensor 

float Buffer8[4];      // Buffer for Sensor 
float Buffer9[4];      // Buffer for Sensor 

float Buffer10[4];     // Buffer for Sensor 
float Buffer11[4];     // Buffer for Sensor 

byte Buffer20[1];      // Read byte from CP



int Size = 1; //the number of bytes that we want to get from PLC only used in read function

bool bit1 = 0; // Buffer 20 Bit 1
bool bit2 = 0; // Buffer 20 Bit 2


S7Client Client2;


void setup() {
              Serial.begin(115200);
              EthernetInit(mac, Local);
              delay(2000); 
              Serial.println("");
              Serial.println("Cable connected");  
              Serial.print("Local IP address : ");
              Serial.println(Ethernet.localIP());
              Timer1.initialize(1000000);
              Timer1.attachInterrupt(PLC_Read_Write); 


                  ph.begin();
                  sensors.begin();

             }


bool Connect()
              {
               int Result=Client2.ConnectTo(PLC,0,0); // Slot (see the doc.)
               Serial.print("Connecting to ");Serial.println(PLC);  
               if (Result==0){Serial.print("You are connected with Your PLC");}
               else{Serial.println("Connection error");}
               return Result==0;
              }



void loop() {
             while (!Client2.Connected){if (!Connect()){delay(250); // Need to see what this delay does - originally 500
             }           
                                        
            }
}



//----------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------
//            PLC WRITE / READ DATA
//----------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------



void PLC_Read_Write()
{
              
              Client2.ReadArea(S7AreaDB,DBNum,34,1,Buffer20); // Buffer 20 is array of bool in CPU

              bit1 = S7.BitAt(Buffer20,0,0); Serial.print("Testing Read Bit 1_"); Serial.println(bit1); // Check Bit 1 Status
              bit2 = S7.BitAt(Buffer20,0,1); Serial.print("Testing Read Bit 2_"); Serial.println(bit2); // Check Bit 2 Status

              String cmd = ("enterph");
              if (bit1 == 1) {Serial.print(cmd);}
              
         
               bitWrite(Buffer1[0],0, true); // Bit in BD to Check Comms is Good
               bitWrite(Buffer1[0],1, bit1);
               bitWrite(Buffer1[0],2, bit2);
            // bitWrite(Buffer1[0],3, true);
            // bitWrite(Buffer1[0],4, true);
            // bitWrite(Buffer1[0],5, true);
            // bitWrite(Buffer1[0],6, true);
            // bitWrite(Buffer1[0],7, true);

            Client2.WriteArea(S7AreaDB,DBNum,0,1,Buffer1); // Writes the above bits into DB15 Byte 0 - 0.0 to 0.7
            
            loop_PH_Sensor();

                                    
         }
}


void loop_PH_Sensor()
{



  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  // After we got the temperatures, we can print them here.
  // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  float tempC = sensors.getTempCByIndex(0);

  // Check if reading was successful
  if(tempC != DEVICE_DISCONNECTED_C) 
  {
    Serial.print("Temperature for the device 1 (index 0) is: ");
    Serial.println(tempC);
  } 
  else
  {
    Serial.println("Error: Could not read temperature data");
  }

          Buffer3[0] = tempC; // Write Float number to Buffer
          Client2.WriteArea(S7AreaDB,DBNum,6,4,Buffer3);


    
    static unsigned long timepoint = millis();
    if(millis()-timepoint>1000U){                  //time interval: 1s
        timepoint = millis();
        temperature = tempC;   // read your temperature sensor to execute temperature compensation
        voltage = analogRead(PH_PIN)/1024.0*5000;  // read the voltage
        phValue = ph.readPH(voltage,temperature);  // convert voltage to pH with temperature compensation
        Serial.print("temperature:");
        Serial.print(temperature,1);
        Serial.print("^C  pH:");
        Serial.println(phValue,2);
    }
    ph.calibration(voltage,temperature);           // calibration process by Serail CMD


          Buffer2[0] = phValue; // Write Float number to Buffer
          Client2.WriteArea(S7AreaDB,DBNum,2,4,Buffer2);

}



I have everything near enough working apart from in void PLC_Read_Write when bit1 goes high i want it to send the serial command enterph. bit1 goes high from the plc which is working but the serial command isnt being registered by the arduino. If i type in enterph manually it works.

It looks like you're using serial for debugging messages and Client2 to talk to the Siemens device over ethernet. Is that correct?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.