Removed obsolete files
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
.idea
|
||||
node_modules
|
||||
/node_modules
|
||||
climate-server
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type HandlerFuncWithError = func (http.ResponseWriter, *http.Request) error
|
||||
|
||||
type HttpMethod string
|
||||
|
||||
const(
|
||||
HttpGet HttpMethod = "GET"
|
||||
HttpPost HttpMethod = "POST"
|
||||
)
|
||||
|
||||
type RequestContext struct {
|
||||
ResponseWriter http.ResponseWriter
|
||||
Request *http.Request
|
||||
MethodType HttpMethod
|
||||
Pattern string
|
||||
}
|
||||
|
||||
type Router struct {
|
||||
patterns map[string] *PatternRouter
|
||||
}
|
||||
|
||||
type PatternRouter struct {
|
||||
Get HandlerFuncWithError
|
||||
Post HandlerFuncWithError
|
||||
}
|
||||
|
||||
func (p *Router) MethodFuncForPattern(methodType HttpMethod, pattern string) (HandlerFuncWithError, error) {
|
||||
var methodFunc HandlerFuncWithError
|
||||
switch methodType {
|
||||
case HttpGet:
|
||||
methodFunc = p.patterns[pattern].Get
|
||||
case HttpPost:
|
||||
methodFunc = p.patterns[pattern].Post
|
||||
}
|
||||
if methodFunc == nil {
|
||||
return nil, fmt.Errorf("method %s not defined on pattern %s", methodType, pattern)
|
||||
}
|
||||
return methodFunc, nil
|
||||
}
|
||||
|
||||
func (p *Router) setGet(pattern string, cb HandlerFuncWithError) {
|
||||
if _, ok := p.patterns[pattern]; ok {
|
||||
p.patterns[pattern].Get = cb
|
||||
} else {
|
||||
p.patterns[pattern] = &PatternRouter{cb, nil}
|
||||
http.HandleFunc(pattern, getPatternHandler(pattern))
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Router) setPost(pattern string, cb HandlerFuncWithError) {
|
||||
if _, ok := p.patterns[pattern]; ok {
|
||||
p.patterns[pattern].Post = cb
|
||||
} else {
|
||||
p.patterns[pattern] = &PatternRouter{nil, cb}
|
||||
http.HandleFunc(pattern, getPatternHandler(pattern))
|
||||
}
|
||||
}
|
||||
|
||||
var MainRouter = Router{make(map[string] *PatternRouter)}
|
||||
|
||||
func getPatternHandler(pattern string) http.HandlerFunc {
|
||||
return func (w http.ResponseWriter, r *http.Request) {
|
||||
cb, err := MainRouter.MethodFuncForPattern(HttpMethod(r.Method), pattern)
|
||||
if err == nil {
|
||||
err = cb(w, r)
|
||||
}
|
||||
if err != nil {
|
||||
internalErrorOnErr(err, w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/warthog618/gpio"
|
||||
"math"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const Timeout = math.MaxInt32
|
||||
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()
|
||||
}
|
||||
return result / 100
|
||||
})()
|
||||
|
||||
func cyclesInAMillisecond() int {
|
||||
count := 0
|
||||
start := time.Now().UnixNano()
|
||||
for {
|
||||
count++
|
||||
if time.Now().UnixNano() - start >= 1000000 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := gpio.Open()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer gpio.Close()
|
||||
|
||||
pin := gpio.NewPin(gpio.GPIO4)
|
||||
dhtData, err := readDataFromDHT(pin)
|
||||
if err != nil {
|
||||
fmt.Println(fmt.Sprintf("Bad read! Reason: %s!", err.Error()))
|
||||
} else {
|
||||
fmt.Println(
|
||||
strconv.FormatInt(int64(dhtData[0]), 2),
|
||||
strconv.FormatInt(int64(dhtData[1]), 2),
|
||||
strconv.FormatInt(int64(dhtData[2]), 2),
|
||||
strconv.FormatInt(int64(dhtData[3]), 2),
|
||||
strconv.FormatInt(int64(dhtData[4]), 2))
|
||||
fmt.Println(convertToRhAndTemp(dhtData))
|
||||
}
|
||||
}
|
||||
|
||||
func sendStartSignal(pin *gpio.Pin) {
|
||||
pin.PullUp()
|
||||
time.Sleep(time.Millisecond)
|
||||
pin.Output()
|
||||
pin.Low()
|
||||
time.Sleep(1100 * time.Microsecond)
|
||||
pin.PullUp()
|
||||
pin.Input()
|
||||
time.Sleep(55 * time.Microsecond)
|
||||
}
|
||||
|
||||
func readDataFromDHT(pin *gpio.Pin) ([5]byte, error) {
|
||||
cycles := [80]int{}
|
||||
sendStartSignal(pin)
|
||||
if cyclesForReading(pin, gpio.Low) == Timeout {
|
||||
return [5]byte{}, errors.New(fmt.Sprintf(TimeoutErrInitial, "low"))
|
||||
}
|
||||
if cyclesForReading(pin, gpio.High) == Timeout {
|
||||
return [5]byte{}, errors.New(fmt.Sprintf(TimeoutErrInitial, "high"))
|
||||
}
|
||||
for i := 0; i < 80; i += 2 {
|
||||
cycles[i] = cyclesForReading(pin, gpio.Low)
|
||||
cycles[i + 1] = cyclesForReading(pin, gpio.High)
|
||||
}
|
||||
receivedInput, err := storeCycleCountsAsBinarySequence(&cycles)
|
||||
if err != nil {
|
||||
return [5]byte{}, err
|
||||
}
|
||||
return receivedInput, nil
|
||||
}
|
||||
|
||||
func cyclesForReading(pin *gpio.Pin, level gpio.Level) int {
|
||||
count := 0
|
||||
for pin.Read() == level {
|
||||
count++
|
||||
if count >= oneSecondInCycles {
|
||||
return Timeout
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
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]
|
||||
fmt.Println(lowCycles, highCycles)
|
||||
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(int(data[0]) << 8) + float32(data[1])
|
||||
temp := float32(int(data[2]) << 8) + float32(data[3])
|
||||
return rh/10, temp/10
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user