-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarvin_gas.ino
216 lines (165 loc) · 5.6 KB
/
marvin_gas.ino
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
/* Marvin Piezo Vibration Sensor examples - bd
This basically sends a LoRA packet whenever the piezo sensor gets hit.
There's no sleep yet, nothing fancy, but it works.
*/
#include "CayenneLPP.h"
// set max size
int MAX_SIZE = 52;
CayenneLPP lpp(MAX_SIZE);
// Port to assign the type of lora data (any port can be used between 1 and 223)
int set_port = 1;
// Some standard ports that depend on the layout of the Marvin
long defaultBaudRate = 57600;
int reset_port = 5;
int RN2483_power_port = 6;
int led_port = 13;
//*** Set parameters here BEGIN ---->
// NOTE: Devices joining the network via ABP, what we're doing for the hackathon,
// only require DevADDR, NWKSKey, and APPSKey. DevEUI will be assigned by Network Server.
String set_devaddr = "01010101"; // Put your 8 hex char here
String set_nwkskey = "01010101010101010101010101010101"; // Put your 32 hex char here
String set_appskey = "01010101010101010101010101010101"; // Put your 32 hex char here
//*** <---- END Set parameters here
// Some global items
String reader = "";
// counter
int count = 1;
void setup() {
InitializeSerials(defaultBaudRate);
initializeRN2483(RN2483_power_port, reset_port);
Serial.println("STARTUP..." );
pinMode(A2, INPUT_PULLUP);
digitalWrite(A2, LOW);
pinMode(A3, INPUT_PULLUP);
digitalWrite(A3, LOW);
}
float MQ2() {
float R0 = 1.32; // baseline sample @ 0.36V
float sensor_volt;
float RS_gas; // Get value of RS in a GAS
float ratio; // Get ratio RS_GAS/RS_air
String sample;
int sensorValue = analogRead(A3);
sensor_volt=(float)sensorValue/1024*5.0;
RS_gas = (5.0-sensor_volt)/sensor_volt; // omit *RL
ratio = RS_gas/R0;
// sample = "MQ2 " + String(int(sensor_volt * 100)) + " " + String(int(ratio * 100));
return ratio;
}
float MQ5() {
float R0 = 1.99; // baseline sample @ 0.36V
float sensor_volt;
float RS_gas; // Get value of RS in a GAS
float ratio; // Get ratio RS_GAS/RS_air
// String sample;
int sensorValue = analogRead(A5);
sensor_volt=(float)sensorValue/1024*5.0;
RS_gas = (5.0-sensor_volt)/sensor_volt; // omit *RL
ratio = RS_gas/R0;
// sample = "MQ5 " + String(int(sensor_volt * 100)) + " " + String(int(ratio * 100));
return ratio;
}
float MQ9() {
float R0 = 1.91; // baseline sample @ 0.24V
float sensor_volt;
float RS_gas; // Get value of RS in a GAS
float ratio; // Get ratio RS_GAS/RS_air
// String sample;
int sensorValue = analogRead(A0);
sensor_volt=(float)sensorValue/1024*5.0;
RS_gas = (5.0-sensor_volt)/sensor_volt; // omit *RL
ratio = RS_gas/R0;
//sample = "MQ9 " + String(int(sensor_volt * 100)) + " " + String(int(ratio * 100));
return ratio;
}
void loop() {
//int buttonState = digitalRead(A3);
//int button2State = digitalRead(A2);
//if (buttonState == 1 || button2State == 1) {
char payload[MAX_SIZE] = "";
lpp.reset();
// Stub out payloads if you want.
// lpp.addDigitalInput(1, 1);
// lpp.addTemperature(12, 22.5);
// lpp.addBarometricPressure(2, 1073.21);
// lpp.addGPS(3, 52.37365, 4.88650, 2);
float reading = MQ2();
Serial.println(reading);
lpp.addAnalogInput(12,short(reading * 100));
lpp.addAnalogInput(15,short(MQ5() * 100));
lpp.addAnalogInput(19,short(MQ9() * 100));
uint8_t buff = *lpp.getBuffer();
Serial.print("Buffer size:" );
Serial.println(lpp.getSize());
for (int i = 0; i < lpp.getSize(); i++) {
char tmp[16];
sprintf(tmp, "%.2X",(lpp.getBuffer())[i]);
strcat(payload, tmp);
}
Serial.print("Buffer content:" );
Serial.println(payload);
send_LoRa_data(set_port, payload);
delay(500);
read_data_from_LoRa_Mod();
delay(10000);
Serial.println(count);
count = count + 1;
//} // end if
} // end loop()
void InitializeSerials(long baudrate) {
Serial.begin(baudrate);
Serial1.begin(baudrate);
delay(1000);
print_to_console("Serial ports initialised");
}
void initializeRN2483(int pwr_port, int rst_port) {
//Enable power to the RN2483
pinMode(pwr_port, OUTPUT);
digitalWrite(pwr_port, HIGH);
print_to_console("RN2483 Powered up");
delay(1000);
//Disable reset pin
pinMode(rst_port, OUTPUT);
digitalWrite(rst_port, HIGH);
//Configure LoRa module
send_LoRa_Command("sys reset");
read_data_from_LoRa_Mod();
send_LoRa_Command("mac set nwkskey " + set_nwkskey);
read_data_from_LoRa_Mod();
send_LoRa_Command("mac set appskey " + set_appskey);
read_data_from_LoRa_Mod();
send_LoRa_Command("mac set devaddr " + set_devaddr);
read_data_from_LoRa_Mod();
//For this commands some extra delay is needed.
send_LoRa_Command("mac set adr on");
//send_LoRa_Command("mac set dr 0"); //uncomment this line to fix the RN2483 on SF12 (dr=DataRate)
delay(1000);
read_data_from_LoRa_Mod();
send_LoRa_Command("mac save");
delay(1000);
read_data_from_LoRa_Mod();
send_LoRa_Command("mac join abp");
delay(1000);
read_data_from_LoRa_Mod();
send_LoRa_Command("radio set crc off");
delay(1000);
read_data_from_LoRa_Mod();
}
void print_to_console(String message) {
Serial.println(message);
}
void read_data_from_LoRa_Mod() {
if (Serial1.available()) {
String inByte = Serial1.readString();
Serial.println(inByte);
}
}
void send_LoRa_Command(String cmd) {
print_to_console("Now sending: " + cmd);
Serial1.println(cmd);
delay(500);
}
void send_LoRa_data(int tx_port, String rawdata) {
// send_LoRa_Command("mac tx uncnf " + String(tx_port) + String(" ") + rawdata);
send_LoRa_Command("mac tx cnf " + String(tx_port) + String(" ") + rawdata);
}