forked from PiSupply/pxt-iot-lora-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
174 lines (144 loc) · 5.5 KB
/
main.ts
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
/*
* pxt-iot-lora node, Micro:Bit library for IoTLoRaNode
* Copyright (C) 2018 Pi Supply
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
enum Channels {
One = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9
}
enum SpreadingFactors {
Seven = 5,
Eight = 4,
Nine = 3,
Ten = 2,
Eleven = 1,
Twelve = 0
}
//% weight=10 color=#8bc34a icon="\uf1eb"
namespace IotLoRaNode {
serial.redirect(SerialPin.P14, SerialPin.P15, BaudRate.BaudRate115200);
let payload = ""
//%blockId="IotLoRaNode_InitialiseRadio" block="Initialise LoRa Radio:|Device Address %deviceaddress|Network Session Key %netswk|App Session Key %appswk|SF %datarate"
//% blockGap=8
export function InitialiseRadio(devaddress: string, netswk: string, appswk: string, datarate: SpreadingFactors): void {
/**
* First we need to configure the serial port to use the pins and reset the radio
*/
pins.digitalWritePin(DigitalPin.P16, 1)
basic.pause(100)
pins.digitalWritePin(DigitalPin.P16, 0)
serial.readLine()
/**
* For this we are only going to use ABP & LoRa WAN Modes for now
*/
//Set to use LoRaWAN Mode
serial.writeString("at+mode=0\r\n");
serial.readLine()
//Set Device Address
serial.writeString("at+set_config=dev_addr:" + devaddress + "\r\n");
serial.readLine()
//Set the network session key
serial.writeString("at+set_config=nwks_key:" + netswk + "\r\n");
serial.readLine()
//Set the application session key
serial.writeString("at+set_config=apps_key:" + appswk + "\r\n");
serial.readLine()
//Set the data rate
serial.writeString("at+set_config=dr:" + datarate + "\r\n");
serial.readLine()
//"Join" the LoRaWAN Network in ABP Mode
serial.writeString("at+join=abp\r\n");
serial.readLine()
//Display on the screen that LoRa is ready.
basic.showString("LoRa Ready")
}
//%blockId="IotLoRaNode_DigitalValue"
//%block="Add Digital Value: %value on channel: %chanNum"
export function DigitalValue(value: boolean, chanNum: Channels): void {
/**
* Add digital value
*/
let intVal = value ? 1 : 0;
payload = payload + "0" + chanNum + "000" + intVal;
}
//%blockId="IotLoRaNode_AnalogueValue" block="Add Analogue Value: %value on channel: %chanNum"
//% value.min=0 value.max=254
export function AnalogueValue(value: number, chanNum: Channels): void {
/**
* Add analogue value
*/
let bufr = pins.createBuffer(2);
bufr.setNumber(NumberFormat.Int16BE, 0, (value * 100))
payload = payload + "0" + chanNum + "02" + bufr.toHex();
}
//%blockId="IotLoRaNode_tempertureValue" block="Add Temperature Value: $temperatureVal to channel: %id"
export function TempertureValue(temperatureVal: number, chanNum: Channels): void {
/**
* Add temperature value
*/
let bufr = pins.createBuffer(2);
bufr.setNumber(NumberFormat.Int16BE, 0, (temperatureVal * 10))
payload = payload + "0" + chanNum + "67" + bufr.toHex();
}
//%blockId="IotLoRaNode_HumidityValue" block="Add Humidity Value: $humidityVal to channel: %id"
//%advanced=true
export function HumidityValue(humidityVal: number, chanNum: Channels): void {
/**
* Add humidity value
*/
let bufr = pins.createBuffer(2);
bufr.setNumber(NumberFormat.Int16BE, 0, (humidityVal * 100))
payload = payload + "0" + chanNum + "68" + bufr.toHex();
}
/**
* //%blockId="IotLoRaNode_AccelorometerValue" block="Add Accelerometer Value: $accelVal to channel: %id"
*export function AccelorometerValue(accelVal: number, chanNum: channels): void {
* /**
* * Add accelorometer
*
* let bufr = pins.createBuffer(2);
* bufr.setNumber(NumberFormat.Int16BE, 0, (accelVal * 100))
*
* payload = payload + "0" + chanNum + "02" + bufr.toHex();
*
*}
**/
//%blockId="IotLoRaNode_LightValue" block="Add light Value: $lightVal on channel: %id"
export function LightValue(lightVal: number, chanNum: Channels): void {
/**
* Add light value
*/
let bufr = pins.createBuffer(2);
bufr.setNumber(NumberFormat.Int16BE, 0, (lightVal))
payload = payload + "0" + chanNum + "65" + bufr.toHex();
}
//%blockId="IotLoRaNode_TransmitMessage" block="Transmit LoRa Data"
export function loraTransmitPayload(): void {
/**
* Transmit Message
*/
serial.writeString("at+send=0,1," + payload + "\r\n");
basic.showString(serial.readUntil(serial.delimiters(Delimiters.NewLine)))
basic.showString(serial.readUntil(serial.delimiters(Delimiters.NewLine)))
payload = ""
}
}