```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.