Shop OBEX P1 Docs P2 Docs Learn Events
RF Communication using BS2sx — Parallax Forums

RF Communication using BS2sx

chazbrazilchazbrazil Posts: 27
edited 2006-08-18 16:40 in BASIC Stamp
I'm currently working on a project that uses the BS2sx to take temperature readings with the Sensirion SHT1x module and relaying that information through RF modules to a home base. Ideally, the home base can support an "indefinate" amount of transmitters.

Code for the reciever, or base unit, is:

' {$STAMP BS2sx}
' {$PBASIC 2.5}
x       VAR   Word
y       VAR   Word
tF      VAR   Word
tC      VAR   Word
rhTrue  VAR   Word
rhLin   VAR   Word
name    VAR   Byte
inbyte  VAR   Byte
SerBaud CON 240 + 16384  ' 9600 Baud, 'inverted'
SerPort CON 16
MyData  VAR Byte
MyPin   VAR IN4
LED     CON 13
name = "A"
DO
GIVEUP:
  DEBUG "Preparing... ", name, CR
  PULSOUT 2, 3000
  SEROUT 2, 17405, [noparse][[/noparse]name]
  DEBUG "Request sent", CR
  SERIN 0, 17405, 5000, GIVEUP1, [noparse][[/noparse]WAIT(name), x.HIGHBYTE, x.LOWBYTE, y.HIGHBYTE, y.LOWBYTE]
  PAUSE 200
  GOTO CONTINUE
GIVEUP1:
  DEBUG "Gave up once... trying again.", CR
  DEBUG "Preparing... ", name, CR
  PULSOUT 2, 3000
  SEROUT 2, 17405, [noparse][[/noparse]name]
  DEBUG "Request sent", CR
  SERIN 0, 17405, 5000, GIVEUP2, [noparse][[/noparse]WAIT(name), x.HIGHBYTE, x.LOWBYTE, y.HIGHBYTE, y.LOWBYTE]
  PAUSE 200
CONTINUE:
  tf = x
  rhtrue = y
  DEBUG name
  SEROUT SerPort, SerBaud, [noparse][[/noparse]"T", DEC tf] ' Output as "0" or "1"
  PAUSE 500
  HIGH 13
  SEROUT SerPort, SerBaud, [noparse][[/noparse]"H", DEC rhtrue] ' Output as "0" or "1"
  PAUSE 500                            ' Wait 1000 mSec
  LOW 13
  DEBUG CR

  GIVEUP2:
  PAUSE 10000
  'name = name + 1
  'IF name = 67 THEN
  '  name = 65
  'ENDIF
LOOP

Note: Please forgive the disorganized manner.
Also, here is the code (much longer) for the transmitter. It's an adaptation of one of the modules posted on the parallax website:
' ==============================================================================
' {$PBASIC 2.5}
'
'   File...... SHT11.BS2
'   Purpose... Interface to Sensirion SHT11 temperature/humidity sensor
'   E-mail.... [url=mailto:stamptech@parallax.com]stamptech@parallax.com[/url]
'   Date...... 12/30/02
'
'   {$STAMP BS2sx}
'
' ==============================================================================

' ------------------------------------------------------------------------------
' Program Description
' ------------------------------------------------------------------------------
'
' This program demonstrates the interface to the Sht1X and displays the raw data
' ------------------------------------------------------------------------------
' I/O Definitions
' ------------------------------------------------------------------------------
ShtData         CON     3                       ' bi-directional data
Clock           CON     1

' ------------------------------------------------------------------------------
' Constants
' ------------------------------------------------------------------------------
ShtTemp         CON     %00011                  ' read temperature
ShtHumi         CON     %00101                  ' read humidity
ShtStatW        CON     %00110                  ' status register write
ShtStatR        CON     %00111                  ' status register read
ShtReset        CON     %11110                  ' soft reset (wait 11 ms after)
Ack             CON     0
NoAck           CON     1
No              CON     0
Yes             CON     1
MoveTo          CON     2                       ' for DEBUG control
ClrRt           CON     11                      ' clear DEBUG line to right
DegSym          CON     186                     ' degrees symbol for DEBUG

' ------------------------------------------------------------------------------
' Variables
' ------------------------------------------------------------------------------
ioByte          VAR     Byte                    ' data from/to SHT1x
ackBit          VAR     Bit                     ' ack/nak from/to SHT1x
toDelay         VAR     Byte                    ' timeout delay timer
timeOut         VAR     Bit                     ' timeout status
soT             VAR     Word                    ' temp counts from SHT1x
soRH            VAR     Word                    ' humidity counts from SHT1x
rhLin           VAR     Word                    ' humidity; linearized
rhTrue          VAR     Word                    ' humidity; temp compensated
status          VAR     Byte                    ' SHT1x status byte
x               VAR     Word
y               VAR     Word
tF               VAR     Word
tC               VAR     Word
adjTemp          VAR     Byte
adjHumi          VAR     Byte
signTemp         VAR     Bit
signHumi         VAR     Bit
BtnWk            VAR     Byte
txpin         CON   7
Baud9600      CON   110
  HIGH txpin
  PAUSE 100
  SEROUT txpin, Baud9600, [noparse][[/noparse]17]
  adjTemp = 0
  adjHumi = 0
  signTemp  = 0
  signHumi = 0

' ------------------------------------------------------------------------------
' EEPROM Data
' ------------------------------------------------------------------------------

' ------------------------------------------------------------------------------
' Initialization
' ------------------------------------------------------------------------------
Initialize:
  GOSUB SHT_Connection_Reset                    ' reset device connection
  PAUSE 250                                     ' let DEBUG window open

' ------------------------------------------------------------------------------
' Program Code
' ------------------------------------------------------------------------------
Sensor_Demo:
  GOSUB SHT_Measure_Temp

Main:
  DEBUG CLS
  DEBUG "SHT1x Demo", CR
  DEBUG "----------", CR
Main2:
  GOSUB SHT_Measure_Temp
  DEBUG MoveTo, 0, 3
  DEBUG "Raw Data Temp: "
  DEBUG DEC soT, ClrRt, CR

  GOSUB SHT_Measure_Humidity
 ' DEBUG MoveTo, 0, 7
  DEBUG "Raw Data Humidity: "
  DEBUG DEC soRH, ClrRt, CR
  x = soT
  y = soRH

  BUTTON 15, 0, 1,1, BtnWk, 0, NoPress1
  IF signTemp = 0 THEN
    adjTemp = adjTemp + 1
  ELSE
    IF adjTemp = 1 THEN
      signTemp = 0
      adjTemp = 0
    ELSE
      adjTemp = adjTemp - 1
    ENDIF
  ENDIF
  GOTO MAIN2
  NoPress1:
  BUTTON 14, 0, 1,1, BtnWk, 0, NoPress2
  IF signTemp = 0 THEN
    IF adjTemp = 0 THEN
      signTemp = 1
      adjTemp = 1
    ELSE
      adjTemp = adjTemp - 1
    ENDIF
  ELSE
    adjTemp = adjTemp + 1
  ENDIF
  GOTO MAIN2

  NoPress2:
  BUTTON 13, 0, 1,1, BtnWk, 0, NoPress3
  IF signHumi = 0 THEN
    adjHumi = adjHumi + 1
  ELSE
    IF adjHumi = 1 THEN
      signHumi = 0
      adjHumi = 0
    ELSE
      adjHumi = adjHumi - 1
    ENDIF
  ENDIF
  GOTO MAIN2
  NoPress3:
  BUTTON 12, 0, 1,1, BtnWk, 0, NoPress4
  IF signHumi = 0 THEN
    IF adjHumi = 0 THEN
      signHumi = 1
      adjHumi = 1
    ELSE
      adjHumi = adjHumi - 1
    ENDIF
  ELSE
    adjHumi = adjHumi + 1
  ENDIF
  GOTO MAIN2
 
  NoPress4:
  tF = x ** 11796 - 400
  tC = x / 10 - 400
  'DEBUG CLS, DEC (tF / 10), ".", DEC1 tF, CR
  rhLin = (y ** 26542)
  rhLin = rhLin - ((y ** 3468) * (y ** 3468) + 50 / 100)
  rhLin = rhLin - 40
  rhTrue = ((tC / 10 - 25) * (y ** 524 + 1) + (rhLin * 10)) + 5 / 10
  'DEBUG DEC (rhTrue / 10), ".", DEC1 rhTrue, CR
  IF signTemp = 0 THEN
    tF = tF + adjTemp
  ELSE
    tF = tF - adjTemp
  ENDIF
  IF signHumi = 0 THEN
    rhTrue = rhTrue + adjHumi
  ELSE
    rhTrue = rhTrue - adjHumi
  ENDIF
 
  DEBUG "Begin waiting...", CR
  SERIN 9, 17405, [noparse][[/noparse]WAIT("A")]
  DEBUG "End waiting...", CR
  PAUSE 300
  PULSOUT 5, 3000
  SEROUT 5, 17405, [noparse][[/noparse]"A", tF.HIGHBYTE, tF.LOWBYTE, rhTrue.HIGHBYTE, rhTrue.LOWBYTE]

  HIGH txpin
  PAUSE 100
  SEROUT txpin, Baud9600, [noparse][[/noparse]22]
  SEROUT txpin, Baud9600, [noparse][[/noparse]17]
  'PAUSE 100
  'SEROUT txpin, Baud9600, [noparse][[/noparse]"SHOVE IT!"]
  'PAUSE 10
  SEROUT txpin, Baud9600, [noparse][[/noparse]13]
  'PAUSE 10
  SEROUT txpin, Baud9600, [noparse][[/noparse]"   Temp: ", SDEC (tF / 10), ".", SDEC1 tF, " F"]
  'PAUSE 10
  SEROUT txpin, Baud9600, [noparse][[/noparse]13]
  'PAUSE 10
  SEROUT txpin, Baud9600, [noparse][[/noparse]"   Humi: ", SDEC (rhTrue / 10), ".", SDEC1 rhTrue, " %"]
  'PAUSE 10
  SEROUT txpin, Baud9600, [noparse][[/noparse]128]
  'PAUSE 100
 
 
  PAUSE 1000                                    ' minimum delay between readings
  GOTO Main2
 

' ------------------------------------------------------------------------------
' Subroutines
' ------------------------------------------------------------------------------
' connection reset: 9 clock cyles with ShtData high, then start sequence
'
SHT_Connection_Reset:
  SHIFTOUT ShtData, Clock, LSBFIRST, [noparse][[/noparse]$FFF\9]
'
SHT_Start:
  INPUT ShtData                                 ' let pull-up take line high
  LOW Clock
  HIGH Clock
  LOW ShtData
  LOW Clock
  HIGH Clock
  INPUT ShtData
  LOW Clock
  RETURN

SHT_Measure_Temp:
  GOSUB SHT_Start                               ' alert device
  ioByte = ShtTemp                              ' temperature command
  GOSUB SHT_Write_Byte                          ' send command
  GOSUB SHT_Wait                                ' wait until measurement done
  ackBit = Ack                                  ' another read follows
  GOSUB SHT_Read_Byte                           ' get MSB
  soT.HIGHBYTE = ioByte
  ackBit = NoAck                                ' last read
  GOSUB SHT_Read_Byte                           ' get LSB
  soT.LOWBYTE = ioByte
  ' Note: Conversion factors are multiplied by 10 to return the
  '       temperature values in tenths of degrees
  RETURN

' measure humidity
'
SHT_Measure_Humidity:
  GOSUB SHT_Start                               ' alert device
  ioByte = ShtHumi                              ' humidity command
  GOSUB SHT_Write_Byte                          ' send command
  GOSUB SHT_Wait                                ' wait until measurement done
  ackBit = Ack                                  ' another read follows
  GOSUB SHT_Read_Byte                           ' get MSB
  soRH.HIGHBYTE = ioByte
  ackBit = NoAck                                ' last read
  GOSUB SHT_Read_Byte                           ' get LSB
  soRH.LOWBYTE = ioByte
  RETURN
 
SHT_Write_Status:
  GOSUB SHT_Start                               ' alert device
  ioByte = ShtStatW                             ' write to status reg command
  GOSUB SHT_Write_Byte                          ' send command
  ioByte = status
  GOSUB SHT_Write_Byte
  RETURN
 
SHT_Read_Status:
  GOSUB SHT_Start                               ' alert device
  ioByte = ShtStatW                             ' write to status reg command
  GOSUB SHT_Read_Byte                           ' send command
  ackBit = NoAck                                ' only one byte to read
  GOSUB SHT_Read_Byte
  RETURN
 
SHT_Write_Byte:
  SHIFTOUT ShtData, Clock, MSBFIRST, [noparse][[/noparse]ioByte]   ' send byte
  SHIFTIN  ShtData, Clock, LSBPRE, [noparse][[/noparse]ackBit\1]   ' get ack bit
  RETURN
 
SHT_Read_Byte:
  SHIFTIN  ShtData, Clock, MSBPRE, [noparse][[/noparse]ioByte]     ' get byte
  SHIFTOUT ShtData, Clock, LSBFIRST, [noparse][[/noparse]ackBit\1] ' send ack bit
  INPUT ShtData                                 ' release data line
  RETURN

SHT_Wait:
  INPUT ShtData                                 ' data line is input
  FOR toDelay = 1 TO 250                        ' give ~1/4 second to finish
    timeOut = INS.LOWBIT(ShtData)               ' scan data line
    IF (timeOut = No) THEN SHT_Wait_Done        ' if low, we're done
    PAUSE 1
  NEXT
SHT_Wait_Done:
  RETURN

SHT_Soft_Reset:
  GOSUB SHT_Connection_Reset                    ' reset the connection
  ioByte = ShtReset                             ' reset command
  ackBit = NoAck                                ' only one byte to send
  GOSUB SHT_Write_Byte                          ' send it
  PAUSE 11                                      ' wait at least 11 ms
  RETURN

·The data I'm recieving looks like this:
debug_screen.JPG

Where "A" is the name for the transmitter. Now values like "T789H472" make sense because it translates to "temperature: 78.9 and humidity: 47.2"... But the other data values like "T32378H65310" make no sense. Any clues?

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-08-18 16:40
    Hello,

    ·· What transmitters and receivers are you using?· It sounds like what is happening is that multiple transmitters are sending data at the same time and the receiver is getting the extra data mixed in sometimes.· Ideally to have multiple transmitters you would need to set up a polled-system where each unit actually has a transmitter and receiver.· Thebase would send out the address of the first station and only that station would reply.· Then it would poll/query the second station and so on.· In this manner only one system is transmitting at any given time.

    Your other option is to use something like the EB500 which is addressable.· That way you could specify the connections by MAC Address.· I hope this helps.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
Sign In or Register to comment.