General

Help with my code rs485 and sensor Digital (With stainless)

userHead FernandoHT 2013-11-28 11:39:13 10397 Views8 Replies
Good afternoon guys i try to communicate my Digital Temperature & humidity sensor (With Stainless Steel Probe) with my RS485 sensor node v1.0 but SHT1x code in that example is different to my Digital Temperature & humidity sensor (With Stainless Steel Probe).
I am using the same code but i think i am wrong

/*
# The Sample code for test the data of Analogue_Test and SHT1X Module

# Editor : Phoebe
# Date : 2012.10.25
# Ver : 0.2
# Product: Analogue_Test and SHT1X Module
# SKU :

# Description:
# Read the Analog value and the data of humidity & temperature

# Hardwares:
1. Arduino UNO
2. IO Expansion Shield V5
3. Analogue_Test and SHT1X Module

# Interface: RS485
# Note: Connect the Analogue_Test and SHT1X Module with IO Expansion Shield V5 through RS485
Set the address of the module in manual,range from 0x02 to 0x7F,take effect after 30 seconds
*/

#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
#define addr 0x02 //set address of the device for 0x02
uchar cmd[50];
uchar receive_ACK[100];
int EN=2;

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#define printByte(args) Serial.write(args)
#define printlnByte(args) Serial.write(args),Serial.println()
#else
#include "WProgram.h"
#define printByte(args) Serial.print(args,BYTE)
#define printlnByte(args) Serial.println(args,BYTE)
#endif

void setup(){
Serial.begin(9600);
pinMode(2,OUTPUT);// TTL -> RS485 chip driver pin
}

void loop(){
static ulong timepoint=0;
if(millis()-timepoint> 500){
read_command();
timepoint=millis();
}
if (Serial.available() > 0) data_return();

delay(1000);

}

/************************Send command to Analogue_Test and SHT1X Module*************************/
void read_command()
{
int i;
char sum_cmd = 0;
digitalWrite(EN,HIGH);// Turn the drvier pin to HIGH -> Turn on code transmitting mode for the RS485 interface
// Turn the driver pin to LOW -> Turn on reading mode for the RS485 interface
delay(10);
cmd[0] = 0x55;
cmd[1] = 0xaa;
cmd[2] = addr;
cmd[3] = 0x00;
cmd[4] = 0x21;
for(i = 0;i < 5;i++){
sum_cmd += cmd;
}
cmd[5] = sum_cmd;
for(i = 0;i < 6;i++){
printByte(cmd);// command send to device
#if defined(ARDUINO) && ARDUINO >= 100
Serial.flush();// complete the transmission of outgoing serial data
#endif
delay(10);
}
digitalWrite(EN,LOW);
}
/************Feedback data of the Analog value and humidity & temperature ************/\

void data_return()
{
digitalWrite(EN,LOW);// Turn the driver pin to LOW -> Turn on reading mode for the RS485 interface
delay(10);
for(int j = 0; j < 26; j++)
{
if (Serial.available() > 0)
{
receive_ACK[j]= Serial.read();
delay(20);
Serial.print( receive_ACK[j],HEX); // return command
Serial.print( " ");
if(receive_ACK[0] != 0x55 || receive_ACK[1] != 0xAA && j > 1) break;//detect the head byte of feedback datas
}
}
Serial.println(" ");
show_0x21_command();//Get datas from the module
digitalWrite(EN,HIGH);
}

void show_0x21_command(void)
{
sht1x_data();
Analog_test_data();

}

/************Deal with datas from Sht1x humidity & temperature sensor************/

void sht1x_data()
{
uint humidity;
uint temperature;
humidity = receive_ACK[7] * 256 + receive_ACK[8];
temperature= receive_ACK[9] * 256 + receive_ACK[10];
Serial.print("H:");
Serial.print(humidity/10,DEC);
Serial.print(" ");
Serial.print("T:");
Serial.println(temperature/10,DEC);
}

/********************Deal with datas from 6 Analog Sensors****************/
void Analog_test_data()
{
char register_addr;
uint Analog_data;
register_addr=13;
Serial.print("Analog Value:");
for(int n=1;n< 7;n++){
Analog_data =receive_ACK[register_addr] *256 + receive_ACK[register_addr+1];
register_addr=register_addr+2;
Serial.print(Analog_data,DEC);
Serial.print(" ");
}
Serial.println(" ");
delay(1000);
}

Best regards
2013-12-12 09:43:04 i dont know why but i disconect gnd sensor (blue wire) and works!!!
thank you so much

[quote="Grey"]
Hello Fernando,
Here is sample code:
If your hardware is well, you could get the data in Serial port.
Code: Select all
/*
# The Sample code for test the data of Analogue_Test and SHT1X Module 
 
 # Editor : Lisper
 # Date   : 2013.12.9
 # Ver    : 1.3
 # Product: Analogue_Test and SHT1X Module
 # SKU    : DFR0233
 
 # Description:     
 # Read the Analog value and the data of humidity & temperature 
 
 # Hardwares:
 1. Arduino UNO
 2. IO Expansion Shield V5
 3. Analogue_Test and SHT1X Module 
 
 # Interface: RS485
 # Note: Connect the Analogue_Test and SHT1X Module with IO Expansion Shield V5 through RS485
 Set the address of the module in manual,range from 0x02 to 0x7F,take effect after 30 seconds
 */

#define uint    unsigned  int
#define uchar   unsigned  char
#define ulong   unsigned  long
#define addr  0x02     //set address of the device for 0x02
uchar cmd[50];
uchar receive_ACK[100];
int EN=2;

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#define printByte(args) Serial.write(args)
#define printlnByte(args)  Serial.write(args),Serial.println()
#else
#include "WProgram.h"
#define printByte(args) Serial.print(args,BYTE)
#define printlnByte(args)  Serial.println(args,BYTE)
#endif

void setup(){
        Serial.begin(9600);
        pinMode(2,OUTPUT);// TTL -> RS485 chip driver pin
}

void loop(){  
        static ulong timepoint=0;
        if(millis()-timepoint> 1000){    
                read_command();
                timepoint=millis();
        }
        if (Serial.available() > 0) data_return();  

        //  delay(1000);

}

/************************Send command to Analogue_Test and SHT1X Module*************************/
void read_command()
{
        int i;
        char sum_cmd = 0;
        digitalWrite(EN,HIGH);// Turn the drvier pin to HIGH -> Turn on code transmitting mode for the RS485 interface
        // Turn the driver pin to LOW  -> Turn on reading mode for the RS485 interface
        delay(10);
        cmd[0] = 0x55;
        cmd[1] = 0xaa;
        cmd[2] = addr;
        cmd[3] = 0x00;
        cmd[4] = 0x21;   
        for(i = 0;i < 5;i++){
                sum_cmd += cmd[i];
        }   
        cmd[5] = sum_cmd;   

        for(i = 0;i < 6;i++){

                printByte(cmd[i]);// command send to device
#if defined(ARDUINO) && ARDUINO >= 100
                Serial.flush();// complete the transmission of outgoing serial data 
#endif
                delay(10);
        }
        digitalWrite(EN,LOW);
}
/************Feedback data of the Analog value and humidity & temperature ************/\



void data_return()
{
        digitalWrite(EN,LOW);// Turn the driver pin to LOW  -> Turn on reading mode for the RS485 interface
        delay(10);
        int i = 0;

        unsigned long timer = millis();

        while(true){
                if(Serial.available()){
                        receive_ACK[i] = Serial.read();
                        i++;
                }
                if(millis() - timer > 100){
                        break;
                        Serial.println("Finish reading!");
                }
        }
        print_data () ;


       /*************************************************************************/
       
       
// Display the original data

//        for(int j = 0; j < 26; j++){
//                Serial.print(receive_ACK[j],HEX); // return command 
//                Serial.print(" ");
//        }
//        Serial.println(" ");
} 

void show_0x21_command(void)
{
        sht1x_data();
        Analog_test_data();

} 

/************Deal with datas from Sht1x humidity & temperature sensor************/

void sht1x_data()
{
        uint humidity;
        uint temperature;
        humidity =  receive_ACK[7] * 256 + receive_ACK[8];
        temperature= receive_ACK[9] * 256 + receive_ACK[10];
        Serial.print("H:");
        Serial.print(humidity/10,DEC); 
        Serial.print("  ");
        Serial.print("T:");
        Serial.println(temperature/10,DEC);
}

/********************Deal with datas from 6 Analog Sensors****************/
void Analog_test_data()
{
        char register_addr;
        uint  Analog_data; 
        register_addr=13;
        Serial.print("Analog Value:");
        for(int n=1;n< 7;n++){
                Analog_data =receive_ACK[register_addr] *256 + receive_ACK[register_addr**];
                register_addr=register_addr**;
                Serial.print(Analog_data,DEC);
                Serial.print(" ");
        }
        Serial.println(" ");
        delay(1000);
}

/*************************** by lisper *********************************/
//print humidity and temperature
void print_data () {
        if (checksum ()) {        // if check sum is right
                Serial.println ();
                float humidity = read_uint8_t (receive_ACK, 7) / 10.0;
                Serial.print ("humidity=");
                Serial.println (humidity, 2);
                
                float temperature =(read_uint8_t (receive_ACK, 9) / 10.0);
                Serial.print ("temperature=");
                Serial.println (temperature, 2);
        } 
        else {
                Serial.print ("\ncheck sum error! sum=");
                Serial.println (getsum_add (receive_ACK, 25), HEX);
        }
}

//if check sum is ok
boolean checksum () {
        uint8_t checksum = getsum_add (receive_ACK, 25);
        if (checksum == receive_ACK[25])
                return true;
        else 
                return false;
}

//read 2 byte to uint16_t
uint16_t read_uint8_t (uint8_t *buffer, uint8_t sub) {        // Big-Endian, first byte is high byte
        return ((uint16_t)(buffer[sub]) << 8) + buffer[sub**];
}

//get check sum, add from 0 to length-1
uint8_t getsum_add (uint8_t *buffer, uint8_t length) {
        uint8_t sum;
        for (int i=sum=0; i<length; i++) {
                sum += buffer[i];
        }
        return sum;
}
/*******************************************************************************/


[/quote]
userHeadPic FernandoHT
2013-12-10 02:41:21 Hello Fernando,
Here is sample code:
If your hardware is well, you could get the data in Serial port.
Code: Select all
/*
# The Sample code for test the data of Analogue_Test and SHT1X Module 
 
 # Editor : Lisper
 # Date   : 2013.12.9
 # Ver    : 1.3
 # Product: Analogue_Test and SHT1X Module
 # SKU    : DFR0233
 
 # Description:     
 # Read the Analog value and the data of humidity & temperature 
 
 # Hardwares:
 1. Arduino UNO
 2. IO Expansion Shield V5
 3. Analogue_Test and SHT1X Module 
 
 # Interface: RS485
 # Note: Connect the Analogue_Test and SHT1X Module with IO Expansion Shield V5 through RS485
 Set the address of the module in manual,range from 0x02 to 0x7F,take effect after 30 seconds
 */

#define uint    unsigned  int
#define uchar   unsigned  char
#define ulong   unsigned  long
#define addr  0x02     //set address of the device for 0x02
uchar cmd[50];
uchar receive_ACK[100];
int EN=2;

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#define printByte(args) Serial.write(args)
#define printlnByte(args)  Serial.write(args),Serial.println()
#else
#include "WProgram.h"
#define printByte(args) Serial.print(args,BYTE)
#define printlnByte(args)  Serial.println(args,BYTE)
#endif

void setup(){
        Serial.begin(9600);
        pinMode(2,OUTPUT);// TTL -> RS485 chip driver pin
}

void loop(){  
        static ulong timepoint=0;
        if(millis()-timepoint> 1000){    
                read_command();
                timepoint=millis();
        }
        if (Serial.available() > 0) data_return();  

        //  delay(1000);

}

/************************Send command to Analogue_Test and SHT1X Module*************************/
void read_command()
{
        int i;
        char sum_cmd = 0;
        digitalWrite(EN,HIGH);// Turn the drvier pin to HIGH -> Turn on code transmitting mode for the RS485 interface
        // Turn the driver pin to LOW  -> Turn on reading mode for the RS485 interface
        delay(10);
        cmd[0] = 0x55;
        cmd[1] = 0xaa;
        cmd[2] = addr;
        cmd[3] = 0x00;
        cmd[4] = 0x21;   
        for(i = 0;i < 5;i++){
                sum_cmd += cmd[i];
        }   
        cmd[5] = sum_cmd;   

        for(i = 0;i < 6;i++){

                printByte(cmd[i]);// command send to device
#if defined(ARDUINO) && ARDUINO >= 100
                Serial.flush();// complete the transmission of outgoing serial data 
#endif
                delay(10);
        }
        digitalWrite(EN,LOW);
}
/************Feedback data of the Analog value and humidity & temperature ************/\



void data_return()
{
        digitalWrite(EN,LOW);// Turn the driver pin to LOW  -> Turn on reading mode for the RS485 interface
        delay(10);
        int i = 0;

        unsigned long timer = millis();

        while(true){
                if(Serial.available()){
                        receive_ACK[i] = Serial.read();
                        i++;
                }
                if(millis() - timer > 100){
                        break;
                        Serial.println("Finish reading!");
                }
        }
        print_data () ;


       /*************************************************************************/
       
       
// Display the original data

//        for(int j = 0; j < 26; j++){
//                Serial.print(receive_ACK[j],HEX); // return command 
//                Serial.print(" ");
//        }
//        Serial.println(" ");
} 

void show_0x21_command(void)
{
        sht1x_data();
        Analog_test_data();

} 

/************Deal with datas from Sht1x humidity & temperature sensor************/

void sht1x_data()
{
        uint humidity;
        uint temperature;
        humidity =  receive_ACK[7] * 256 + receive_ACK[8];
        temperature= receive_ACK[9] * 256 + receive_ACK[10];
        Serial.print("H:");
        Serial.print(humidity/10,DEC); 
        Serial.print("  ");
        Serial.print("T:");
        Serial.println(temperature/10,DEC);
}

/********************Deal with datas from 6 Analog Sensors****************/
void Analog_test_data()
{
        char register_addr;
        uint  Analog_data; 
        register_addr=13;
        Serial.print("Analog Value:");
        for(int n=1;n< 7;n++){
                Analog_data =receive_ACK[register_addr] *256 + receive_ACK[register_addr**];
                register_addr=register_addr**;
                Serial.print(Analog_data,DEC);
                Serial.print(" ");
        }
        Serial.println(" ");
        delay(1000);
}

/*************************** by lisper *********************************/
//print humidity and temperature
void print_data () {
        if (checksum ()) {        // if check sum is right
                Serial.println ();
                float humidity = read_uint8_t (receive_ACK, 7) / 10.0;
                Serial.print ("humidity=");
                Serial.println (humidity, 2);
                
                float temperature =(read_uint8_t (receive_ACK, 9) / 10.0);
                Serial.print ("temperature=");
                Serial.println (temperature, 2);
        } 
        else {
                Serial.print ("\ncheck sum error! sum=");
                Serial.println (getsum_add (receive_ACK, 25), HEX);
        }
}

//if check sum is ok
boolean checksum () {
        uint8_t checksum = getsum_add (receive_ACK, 25);
        if (checksum == receive_ACK[25])
                return true;
        else 
                return false;
}

//read 2 byte to uint16_t
uint16_t read_uint8_t (uint8_t *buffer, uint8_t sub) {        // Big-Endian, first byte is high byte
        return ((uint16_t)(buffer[sub]) << 8) + buffer[sub**];
}

//get check sum, add from 0 to length-1
uint8_t getsum_add (uint8_t *buffer, uint8_t length) {
        uint8_t sum;
        for (int i=sum=0; i<length; i++) {
                sum += buffer[i];
        }
        return sum;
}
/*******************************************************************************/


userHeadPic Grey.CC
2013-12-04 23:14:20 Hi Fernando
To compare with the 6 Pin one, the  Digital Temperature & humidity sensor (With Stainless Steel Probe) has not the pull-up resistor. It makes the signal become not very well. So I suggest to add a resistor between DATA and VCC.
You can see the picture that I add it outside for the convenient reason.
You also could add it inside for the beautiful reason.
They will both work with sensor node.

By the way, how do check the output data? with our Arduino IDE sketch, or some other software on PC?
userHeadPic Grey.CC
2013-12-04 20:05:23 Real problem:
sensor node v1.0 and my sensor (http://www.dfrobot.com/index.php?route= ... p0MucRLNHA )

when i put the code including in dfrobot page isn´t compatible with my sensor before
(http://www.dfrobot.com/index.php?route= ... p0MucRLNHA )

why?? is the code or whats happened??

pdt. it works (my sensor node v1.0 and sht1x "6 pins"but doesn´t work with my sensor SHT1x "4 pins"

Thank you for helping me!!
userHeadPic FernandoHT
2013-12-04 02:54:44 Hello Fernando,
I check the schematic of the this sensor.
I find it has not a resistor between VCC and DATA. The output signal will be not stable.
I will attach a diagram about it. With the pull-up resistor, it will be fine.

And the code is not very well, we will work on it these days.
userHeadPic Grey.CC
2013-12-03 14:44:08 Thank you for helping Jose.

But mi problem is with this sensor

http://www.dfrobot.com/index.php?route= ... p0MucRLNHA

and my sensor node

Because i tried to make work with the same code that SHT1X (6 pin) but doesn´t work
and i don´t know why
userHeadPic FernandoHT
2013-11-30 02:12:48 Fernando, wiki page is updated.
Check it for Grey's new diagram

http://www.dfrobot.com/wiki/index.php/R ... DFR0233%29

Cheers^^
userHeadPic Jose
2013-11-29 23:09:40 Hello Fernando,
Sorry, the jumper wire of the old version diagram has something wrong.
That is for the old verson.
I modified it. It should work now
If you still can't, check the address. whether it is 0x02? Only NO.2 switch should be up.
userHeadPic Grey.CC