diff --git a/main.go b/main.go index 9c7414f..febca74 100644 --- a/main.go +++ b/main.go @@ -9,14 +9,13 @@ import ( ) 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 { result := cyclesInAMillisecond() for i := 0; i < 100; i++ { result += cyclesInAMillisecond() } - fmt.Println(result / 100) return result / 100 })() @@ -41,65 +40,39 @@ func main() { pinNumber := gpio.GPIO4 pin := gpio.NewPin(pinNumber) - result, err := readDataFromDHT(pin) + dhtData, err := readDataFromDHT(pin) if err != nil { - fmt.Println(fmt.Sprintf("Bad read! Reason: %s", err.Error())) + fmt.Println(fmt.Sprintf("Bad read! Reason: %s!", err.Error())) } else { - fmt.Println(result) + fmt.Println(convertToRhAndTemp(dhtData)) } } func readDataFromDHT(pin *gpio.Pin) ([5]byte, error) { - receivedInput := [5]byte{0, 0, 0, 0, 0} // Setup pin.PullUp() time.Sleep(time.Second) - // Send start signal pin.Low() pin.Output() time.Sleep(1100 * time.Microsecond) pin.Input() time.Sleep(40 * time.Microsecond) - - // Get Data + // Wait for high low signal 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 { - return receivedInput, errors.New(fmt.Sprintf(TimeoutErrInitial, "high")) + return [5]byte{}, errors.New(fmt.Sprintf(TimeoutErrInitial, "high")) } - - // Store cycle number for each low and high pulse - 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) + pulsesAsCycles := readPulsesAsCycles(pin) + receivedInput, err := storeCycleCountsAsBinarySequence(&pulsesAsCycles) if err != nil { - return receivedInput, err + return [5]byte{}, err } - 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 { count := 0 for pin.Read() == level { @@ -109,4 +82,36 @@ func cyclesForReading(pin *gpio.Pin, level gpio.Level) int { } } return count -} \ No newline at end of file +} + +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 +} +