Hi,
I’m totally newbie for particle and IOT.
I just passed the getting started processes.
What I’m trying to achieve is to collect sensor output in the data log but I’m not quite sure how to do it. My sensor signal interface is Modbus RS-485 with 2 bare wires A and B. Now what I have is photon and related kits with max485 module (https://www.banggood.com/5V-MAX485-TTL-To-RS485-Converter-Module-Board-For-Arduino-p-912674.html?rmmds=search).
I tried to find useful articles in the forum (this is probably the most helpful article Photon and Modbus 485 RTU - Connections & interface requirements for newbie like me) but to be honest, I’m not sure how to wire or what else or any more necessary devices do I need. Do I need to use USB-RS485 cable? Do I need to include any library? I just want to test it first that sensor’s working before correctly collect the data.
Here are the steps I’ve done.
- Ground has been connected completely.
- I connected 5V power source to max485 module and my sensor
- I connected A and B port of sensor to A and B port of max485 module
- I connected RD/DI pins of max 485 to the Photon RX/TX pins.
- I connected DE/RE pins to Photon D3 pin and use the following code.
Apology for my stupidity, but I’m really new in this field.
String readString = "";
void setup() {
digitalWrite(D3, LOW);
digitalWrite(D0, HIGH);
Serial.begin(9600);
Serial1.begin(9600);
Particle.publish("Setup");
Particle.function("digitalread", tinkerDigitalRead);
Particle.function("digitalwrite", tinkerDigitalWrite);
Particle.function("analogread", tinkerAnalogRead);
Particle.function("analogwrite", tinkerAnalogWrite);
Particle.function("readsendor", readSensor);
}
void loop() {
while (Serial1.available()) // While receiving characters over serial...
{
delay(1000); // Necessary delay
char c = Serial1.read(); // Read the character
readString += c; // Add the character to the string
}
readString.trim();
if (readString.length() > 0) // If a string has been read...
{
// Serial.println("Received: " + readString); // Send the parsed string to Serial for debugging
Particle.publish("Read", readString);
readString = ""; // Clear the string
}
}
int readSensor(String x)
{
Particle.publish("readsensor start");
while (Serial1.available()) // While receiving characters over serial...
{
delay(1000); // Necessary delay
char c = Serial1.read(); // Read the character
readString += c; // Add the character to the string
}
readString.trim();
if (readString.length() > 0) // If a string has been read...
{
// Serial.println("Received: " + readString); // Send the parsed string to Serial for debugging
Particle.publish("Read", readString);
readString = ""; // Clear the string
}
Particle.publish("readsensor out");
return 1;
}
/*******************************************************************************
* Function Name : tinkerDigitalRead
* Description : Reads the digital value of a given pin
* Input : Pin
* Output : None.
* Return : Value of the pin (0 or 1) in INT type
Returns a negative number on failure
*******************************************************************************/
int tinkerDigitalRead(String pin)
{
//convert ascii to integer
int pinNumber = pin.charAt(1) - '0';
//Sanity check to see if the pin numbers are within limits
if (pinNumber< 0 || pinNumber >7) return -1;
if(pin.startsWith("D"))
{
pinMode(pinNumber, INPUT_PULLDOWN);
return digitalRead(pinNumber);
}
else if (pin.startsWith("A"))
{
pinMode(pinNumber+10, INPUT_PULLDOWN);
return digitalRead(pinNumber+10);
}
return -2;
}
/*******************************************************************************
* Function Name : tinkerDigitalWrite
* Description : Sets the specified pin HIGH or LOW
* Input : Pin and value
* Output : None.
* Return : 1 on success and a negative number on failure
*******************************************************************************/
int tinkerDigitalWrite(String command)
{
bool value = 0;
//convert ascii to integer
int pinNumber = command.charAt(1) - '0';
//Sanity check to see if the pin numbers are within limits
if (pinNumber< 0 || pinNumber >7) return -1;
if(command.substring(3,7) == "HIGH") value = 1;
else if(command.substring(3,6) == "LOW") value = 0;
else return -2;
if(command.startsWith("D"))
{
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, value);
return 1;
}
else if(command.startsWith("A"))
{
pinMode(pinNumber+10, OUTPUT);
digitalWrite(pinNumber+10, value);
return 1;
}
else return -3;
}
/*******************************************************************************
* Function Name : tinkerAnalogRead
* Description : Reads the analog value of a pin
* Input : Pin
* Output : None.
* Return : Returns the analog value in INT type (0 to 4095)
Returns a negative number on failure
*******************************************************************************/
int tinkerAnalogRead(String pin)
{
//convert ascii to integer
int pinNumber = pin.charAt(1) - '0';
//Sanity check to see if the pin numbers are within limits
if (pinNumber< 0 || pinNumber >7) return -1;
if(pin.startsWith("D"))
{
return -3;
}
else if (pin.startsWith("A"))
{
return analogRead(pinNumber+10);
}
return -2;
}
/*******************************************************************************
* Function Name : tinkerAnalogWrite
* Description : Writes an analog value (PWM) to the specified pin
* Input : Pin and Value (0 to 255)
* Output : None.
* Return : 1 on success and a negative number on failure
*******************************************************************************/
int tinkerAnalogWrite(String command)
{
//convert ascii to integer
int pinNumber = command.charAt(1) - '0';
//Sanity check to see if the pin numbers are within limits
if (pinNumber< 0 || pinNumber >7) return -1;
String value = command.substring(3);
if(command.startsWith("D"))
{
pinMode(pinNumber, OUTPUT);
analogWrite(pinNumber, value.toInt());
return 1;
}
else if(command.startsWith("A"))
{
pinMode(pinNumber+10, OUTPUT);
analogWrite(pinNumber+10, value.toInt());
return 1;
}
else return -2;
}