-
Notifications
You must be signed in to change notification settings - Fork 4
/
sample.go
68 lines (58 loc) · 1.17 KB
/
sample.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"github.com/matiasinsaurralde/go-mcu/nodemcu"
"github.com/matiasinsaurralde/go-mcu/nodemcu/gpio"
"log"
"os"
"time"
)
const (
logPrefix = "go-mcu "
)
func main() {
// Setup the port (replace the arguments accordingly):
node, err := nodemcu.NewNodeMCU("/dev/cu.usbserial-1410", 115200)
if err != nil {
panic(err)
}
// Set a custom logger
l := log.New(os.Stdout, logPrefix, log.LstdFlags)
node.SetLogger(l)
// Initialization
err = node.Sync()
if err != nil {
panic(err)
}
// Upload a file
err = node.SendFile("test.lua")
if err != nil {
panic(err)
}
// List files
files, err := node.ListFiles()
if err != nil {
panic(err)
}
fmt.Printf("Found %d files\n", len(files))
for i, file := range files {
fmt.Printf("File #%d, '%s' (%d bytes)\n", i, file.Name, file.Size)
}
// Invoke a file
err = node.Run("blink.lua")
if err != nil {
panic(err)
}
// Retrieve hardware info
hwInfo, err := node.HardwareInfo()
if err != nil {
panic(err)
}
fmt.Println(hwInfo)
// GPIO module
var pin = 4
node.GPIO.Mode(pin, gpio.Output)
node.GPIO.Mode(pin, gpio.High)
time.Sleep(1 * time.Second)
node.GPIO.Mode(pin, gpio.Low)
}