-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
233 lines (199 loc) · 4.62 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"encoding/json"
"machine"
"time"
"tinygo.org/x/drivers/at24cx"
"tinygo.org/x/drivers/wifinina"
"tinygo.org/x/drivers/bme280"
"tinygo.org/x/drivers/ds3231"
"tinygo.org/x/drivers/vl6180x"
)
var (
dirPin machine.Pin
stepPin machine.Pin
sleepPin machine.Pin
relay [4]machine.Pin
)
var (
distanceSensor vl6180x.Device
rtc ds3231.Device
temperatureSensor bme280.Device
eeprom at24cx.Device
distanceSensorEnabled bool
temperatureSensorEnabled bool
rtcEnabled bool
eepromEnabled bool
sensorState SensorState
relayState RelayState
data []byte
err error
distance uint16
dt time.Time
temp int32
n int
eepromData []byte
)
const (
Alarm1 = 0
LastAlarm1 = 4
NextAlarm1 = 12
Quantity1 = 20
Alarm2 = 24
LastAlarm2 = 28
NextAlarm3 = 36
Quantity4 = 44
NextRecord = 48
)
const (
DISTANCE = iota
DISTANCE_RAW
MEMORY
TEMPERATURE
PRESSURE
HUMIDITY
RTC
)
func main() {
time.Sleep(5 * time.Second)
println("online")
// SETUP RELAY
relay = [4]machine.Pin{
machine.D5,
machine.D4,
machine.D3,
machine.D2,
}
for i := 0; i < 4; i++ {
relay[i].Configure(machine.PinConfig{Mode: machine.PinOutput})
relay[i].Low()
}
// SETUP THE MOTOR
dirPin = machine.D10
stepPin = machine.D9
sleepPin = machine.D8
dirPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
stepPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
sleepPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
sleepPin.High()
machine.I2C0.Configure(machine.I2CConfig{})
// SETUP DISTANCE SENSOR
distanceSensor = vl6180x.New(machine.I2C0)
distanceSensorEnabled = distanceSensor.Connected()
if !distanceSensorEnabled {
println("VL6180X device not found")
} else {
distanceSensor.Configure(true)
}
// SETUP BME280
temperatureSensor = bme280.New(machine.I2C0)
temperatureSensor.Configure()
// SETUP RTC
rtc = ds3231.New(machine.I2C0)
rtc.Configure()
valid := rtc.IsTimeValid()
if !valid {
println("DATE IS NOT VALID")
date := time.Date(2023, 05, 14, 15, 49, 07, 0, time.UTC)
rtc.SetTime(date)
}
rtcEnabled = rtc.IsRunning()
if !rtcEnabled {
err := rtc.SetRunning(true)
if err != nil {
println("Error configuring RTC")
} else {
rtcEnabled = true
}
}
// SETUP EEPROM
eeprom := at24cx.New(machine.I2C0)
eeprom.Configure(at24cx.Config{})
eepromEnabled = true // assume it's working
eepromData = make([]byte, 48)
// Configure SPI for 8Mhz, Mode 0, MSB First
spi.Configure(machine.SPIConfig{
Frequency: 8 * 1e6,
SDO: machine.NINA_SDO,
SDI: machine.NINA_SDI,
SCK: machine.NINA_SCK,
})
// Init esp8266/esp32
adaptor = wifinina.New(spi,
machine.NINA_CS,
machine.NINA_ACK,
machine.NINA_GPIO0,
machine.NINA_RESETN)
adaptor.Configure()
connectToAP()
connectToMQTT()
publishDiscovery()
// Let discovery message to be processed and other devices subscribe to it
time.Sleep(2 * time.Second)
for {
sendSensorStatus()
sendRelayStatus()
time.Sleep(time.Second * 60)
}
}
func sendSensorStatus() {
distance = distanceSensor.Read()
println("Distance:", distance)
sensorState.Distance = distance
dt, err = rtc.ReadTime()
if err != nil {
println("Error reading date:", err)
} else {
println(dt.Year(), dt.Month(), dt.Day(), dt.Hour(), dt.Minute(), dt.Second())
}
sensorState.Date = dt.Format(time.RFC3339)
/*temp, _ = rtc.ReadTemperature()
println("Temperature (RTC):", temp)
sensorState.Temperature = temp*/
temp, _ = temperatureSensor.ReadTemperature()
println("Temperature (BME280):", temp)
sensorState.Temperature = temp
temp, _ = temperatureSensor.ReadPressure()
println("Pressure (BME280):", temp)
sensorState.Pressure = temp
temp, _ = temperatureSensor.ReadHumidity()
println("Humidity (BME280):", temp)
sensorState.Humidity = temp
n, err = eeprom.Read(eepromData)
println(n, err)
for i := 0; i < 48; i++ {
print(eepromData[i])
}
println("==========")
sensorState.EEPROM = eepromData
data, err = json.Marshal(sensorState)
if err != nil {
println("ERROR MARSHALLING SENSOR DATA", err)
} else {
publishData(sensorStateTopic, &data)
}
}
func sendRelayStatus() {
relayState.Relay1 = "OFF"
relayState.Relay2 = "OFF"
relayState.Relay3 = "OFF"
relayState.Relay4 = "OFF"
if relay[0].Get() {
relayState.Relay1 = "ON"
}
if relay[1].Get() {
relayState.Relay2 = "ON"
}
if relay[2].Get() {
relayState.Relay3 = "ON"
}
if relay[3].Get() {
relayState.Relay4 = "ON"
}
data, err = json.Marshal(relayState)
if err != nil {
println("ERROR MARSHALLING RELAY DATA", err)
} else {
publishData(relayStateTopic, &data)
}
}