/
_297SchemaTest
146 lines (120 loc) · 3.06 KB
/
_297SchemaTest
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
#include <stdint.h>
#include <SHT1x.h>
#include <XBee.h>
#include <SFE_BMP180.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
#define DEBUG
#define dataPin 10
#define clockPin 11
//SCHEMA 296 Basic
typedef struct {
uint16_t schema = 297; //Set to 296 (@)
uint16_t address = 1; //Address of particular node, assigned by lab (@)
uint32_t uptime_ms; //Uptime in ms, how long has microcontroller been on for (@)
uint16_t batt_mv[6]; //Battery voltage in millivolts (@)
uint16_t panel_mv[6]; //Panel voltage in millivolts (@)
uint32_t bmp185_press_pa; //Pressure in pascals (@)
int16_t bmp185_temp_decic; //Temperature in deci-celsius (@)
uint16_t humidity_centi_pct; //Humidity in centi percents (@)
uint16_t solar_irr_w_m2[20]; //Solar irradiance (@)
} schema_296_full;
//Initialize schema
schema_296_full schema;
//Initialize Sensors
SHT1x sht1x(dataPin, clockPin);
XBee xbee = XBee();
Adafruit_BMP085 bmp;
void setup() {
//Open serial connection
Serial.begin(9600);
delay(2500);
}
void loop() {
//Single point readings
sampleSHT1x();
sampleBMP180();
sampleUptime();
//Multiple point readings
for(int i = 0; i < 20; i++){
sampleApogee(i);
if(i % 3 == 0){
sampleBattery((i/3));
samplePanel((i/3));
}
delay(3000);
}
//Create and send packet
constructPacket();
}
void sampleSHT1x() {
//Places in schema
schema.humidity_centi_pct = sht1x.readHumidity();
#ifndef DEBUG
Serial.print("Humidity: ");
Serial.println(schema.humidity_centi_pct);
#endif
}
void sampleBMP180() {
//Places in schema
bmp.begin();
schema.bmp185_press_pa = bmp.readPressure();
schema.bmp185_temp_decic = bmp.readTemperature();
#ifndef DEBUG
Serial.print("Pressure: ");
Serial.println(schema.bmp185_press_pa);
Serial.print("Temperature: ");
Serial.println(schema.bmp185_temp_decic);
#endif
}
void sampleUptime() {
schema.uptime_ms = millis();
#ifndef DEBUG
Serial.print("Uptime: ");
Serial.println(schema.uptime_ms);
#endif
}
void sampleApogee(int i) {
schema.solar_irr_w_m2[i] = (analogRead(A2))/0.004;
#ifndef DEBUG
Serial.print("Solar Irradiance: ");
Serial.println(schema.solar_irr_w_m2[i]);
#endif
}
void sampleBattery(int i) {
schema.batt_mv[i] = (analogRead(A0)*(5000/1023));
#ifndef DEBUG
Serial.print("Battery Voltage: ");
Serial.println(schema.batt_mv[i]);
#endif
}
void samplePanel(int i) {
schema.panel_mv[i] = (analogRead(A1)*(5000/1023)+70);
#ifndef DEBUG
Serial.print("Panel Voltage: ");
Serial.println(schema.panel_mv[i]);
#endif
}
void constructPacket() {
int len = 0;
uint8_t payload[80];
//Packet construction
memset(payload, '\0', sizeof(payload));
len = sizeof(schema);
memcpy(payload, &schema, len);
/*
Serial.print(("BIN Length is: "));
Serial.print(len);
Serial.print("\n");
int i;
for(i = 0; i < 80; i++)
{
Serial.println(payload[i]);
Serial.print("\n");
}
*/
//Transfer Packet
XBeeAddress64 addr64 = XBeeAddress64(0x00, 0x00);
ZBTxRequest zbTx = ZBTxRequest(addr64, payload, len);
xbee.send(zbTx);
}