output formatted data
This commit is contained in:
79
main.go
79
main.go
@@ -9,14 +9,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const Timeout = math.MaxInt32
|
const Timeout = math.MaxInt32
|
||||||
const TimeoutErrInitial string = "Timed out waiting for sensor's initial %s reading!"
|
const TimeoutErrInitial string = "timed out waiting for sensor's initial %s reading"
|
||||||
|
|
||||||
var oneSecondInCycles = (func() int {
|
var oneSecondInCycles = (func() int {
|
||||||
result := cyclesInAMillisecond()
|
result := cyclesInAMillisecond()
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
result += cyclesInAMillisecond()
|
result += cyclesInAMillisecond()
|
||||||
}
|
}
|
||||||
fmt.Println(result / 100)
|
|
||||||
return result / 100
|
return result / 100
|
||||||
})()
|
})()
|
||||||
|
|
||||||
@@ -41,65 +40,39 @@ func main() {
|
|||||||
|
|
||||||
pinNumber := gpio.GPIO4
|
pinNumber := gpio.GPIO4
|
||||||
pin := gpio.NewPin(pinNumber)
|
pin := gpio.NewPin(pinNumber)
|
||||||
result, err := readDataFromDHT(pin)
|
dhtData, err := readDataFromDHT(pin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(fmt.Sprintf("Bad read! Reason: %s", err.Error()))
|
fmt.Println(fmt.Sprintf("Bad read! Reason: %s!", err.Error()))
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(result)
|
fmt.Println(convertToRhAndTemp(dhtData))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func readDataFromDHT(pin *gpio.Pin) ([5]byte, error) {
|
func readDataFromDHT(pin *gpio.Pin) ([5]byte, error) {
|
||||||
receivedInput := [5]byte{0, 0, 0, 0, 0}
|
|
||||||
// Setup
|
// Setup
|
||||||
pin.PullUp()
|
pin.PullUp()
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
// Send start signal
|
// Send start signal
|
||||||
pin.Low()
|
pin.Low()
|
||||||
pin.Output()
|
pin.Output()
|
||||||
time.Sleep(1100 * time.Microsecond)
|
time.Sleep(1100 * time.Microsecond)
|
||||||
pin.Input()
|
pin.Input()
|
||||||
time.Sleep(40 * time.Microsecond)
|
time.Sleep(40 * time.Microsecond)
|
||||||
|
// Wait for high low signal
|
||||||
// Get Data
|
|
||||||
if cyclesForReading(pin, gpio.Low) == Timeout {
|
if cyclesForReading(pin, gpio.Low) == Timeout {
|
||||||
return receivedInput, errors.New(fmt.Sprintf(TimeoutErrInitial, "low"))
|
return [5]byte{}, errors.New(fmt.Sprintf(TimeoutErrInitial, "low"))
|
||||||
}
|
}
|
||||||
if cyclesForReading(pin, gpio.High) == Timeout {
|
if cyclesForReading(pin, gpio.High) == Timeout {
|
||||||
return receivedInput, errors.New(fmt.Sprintf(TimeoutErrInitial, "high"))
|
return [5]byte{}, errors.New(fmt.Sprintf(TimeoutErrInitial, "high"))
|
||||||
}
|
}
|
||||||
|
pulsesAsCycles := readPulsesAsCycles(pin)
|
||||||
// Store cycle number for each low and high pulse
|
receivedInput, err := storeCycleCountsAsBinarySequence(&pulsesAsCycles)
|
||||||
cycles := [80]int{}
|
|
||||||
for i := 0; i < 80; i += 2 {
|
|
||||||
cycles[i] = cyclesForReading(pin, gpio.Low)
|
|
||||||
cycles[i + 1] = cyclesForReading(pin, gpio.High)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := storeCycleCountsAsBinarySequence(&receivedInput, &cycles)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return receivedInput, err
|
return [5]byte{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return receivedInput, nil
|
return receivedInput, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func storeCycleCountsAsBinarySequence(store *[5]byte, cycles *[80]int) error {
|
|
||||||
for i := 0; i < 40; i++ {
|
|
||||||
lowCycles := cycles[2 * i]
|
|
||||||
highCycles := cycles[2 * i + 1]
|
|
||||||
if (lowCycles == Timeout) || (highCycles == Timeout) {
|
|
||||||
return errors.New("Timed out waiting for sensor pulse!")
|
|
||||||
}
|
|
||||||
store[i / 8] <<= 1
|
|
||||||
if highCycles > lowCycles {
|
|
||||||
store[i / 8] |= 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func cyclesForReading(pin *gpio.Pin, level gpio.Level) int {
|
func cyclesForReading(pin *gpio.Pin, level gpio.Level) int {
|
||||||
count := 0
|
count := 0
|
||||||
for pin.Read() == level {
|
for pin.Read() == level {
|
||||||
@@ -110,3 +83,35 @@ func cyclesForReading(pin *gpio.Pin, level gpio.Level) int {
|
|||||||
}
|
}
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readPulsesAsCycles(pin *gpio.Pin) [80]int {
|
||||||
|
cycles := [80]int{}
|
||||||
|
for i := 0; i < 80; i += 2 {
|
||||||
|
cycles[i] = cyclesForReading(pin, gpio.Low)
|
||||||
|
cycles[i + 1] = cyclesForReading(pin, gpio.High)
|
||||||
|
}
|
||||||
|
return cycles
|
||||||
|
}
|
||||||
|
|
||||||
|
func storeCycleCountsAsBinarySequence(cycles *[80]int) ([5]byte, error) {
|
||||||
|
data := [5]byte{}
|
||||||
|
for i := 0; i < 40; i++ {
|
||||||
|
lowCycles := cycles[2 * i]
|
||||||
|
highCycles := cycles[2 * i + 1]
|
||||||
|
if (lowCycles == Timeout) || (highCycles == Timeout) {
|
||||||
|
return [5]byte{}, errors.New("timed out waiting for sensor pulse")
|
||||||
|
}
|
||||||
|
data[i / 8] <<= 1
|
||||||
|
if highCycles > lowCycles {
|
||||||
|
data[i / 8] |= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertToRhAndTemp(data [5]byte) (float32, float32) {
|
||||||
|
rh := float32(data[0]) + float32(int(data[1]) << 4)
|
||||||
|
temp := float32(data[2]) + float32(int(data[3]) << 4)
|
||||||
|
return rh/10, temp/10
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user