-
Notifications
You must be signed in to change notification settings - Fork 10
/
aprswxbox.ino
247 lines (225 loc) · 5.02 KB
/
aprswxbox.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
APRS Weather Station
coded by Yang Lei
20120412
Beta 1.8
*/
#include <Adafruit_BMP085.h>
#include <DHT.h>
#include <Wire.h>
#include <SPI.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#define DHTTYPE DHT21
#define DHTPIN 3
//****************************************************************************
//Some network settings below.
byte mac[] = {0xDE, 0xAD, 0xBE, 0x00, 0xFE, 0x00};//Set your MAC Address here.
char SVR_NAME[] = "hangzhou.aprs2.net";
#define SVR_PORT 14580
// define your callsign, passcode and location below
#define callsign "yourcall-13"
#define passcode "your passcode"
#define location "0000.00N/00000.00E"
#define VER "2.0"
#define SVR_PROMPT "javAPRSSrvr"
#define SVR_VERIFIED "verified"
int REPORT_INTERVAL = 10;
#define TO_LINE 10000
//****************************************************************************
EthernetClient client;//Create a client
#define DHTTYPE DHT22
#define DHTPIN 3
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BMP085 bmp;
void setup()
{
Serial.begin(9600);
delay(2000);
Serial.println();
Serial.println("APRS WX Station");
bmp.begin();
initNet();
}
void loop()
{
float h =0;
float t =0;
float b =0;
boolean sent = false;
if ( dht.read() )
{
h = dht.readHumidity();
t = dht.readTemperature(true);
b = bmp.readPressure();
Serial.print(h);
Serial.print(" ");
Serial.print(t);
Serial.print(" ");
Serial.println(b);
if ( client.connect(SVR_NAME, SVR_PORT) )
{
Serial.println("Server connected");
if ( wait4content(&client, SVR_PROMPT, 11) )
{
Serial.println("Prompt ok");
client.print("user ");
client.print(callsign);
client.print(" pass ");
client.print(passcode);
client.print(" vers APRSduino ");
client.println(VER);
if ( wait4content(&client, SVR_VERIFIED, 8) )
{
Serial.println("Login ok");
client.print(callsign);
client.print(">APRS,TCPIP*:");
client.print("!");
client.print(location);
client.print("_");
client.print("000/000g000t");
sendTemp(t);
client.print("r000p000P000h");
sendHumi(h);
client.print("b");
sendBaro(b);
client.print("APRSWXBox ");//Sending comments
client.println(VER);
Serial.println("Data sent ok");
delay(5000);
client.stop();
Serial.println("Server disconnected");
delay((long)REPORT_INTERVAL*60L*1000L);
//delay(30*1000L);
sent = true;
} // if login
else
{
Serial.println("Login failed.");
}
} // if prompt
else
{
Serial.println("No prompt from the server.");
}
} // if connect
else
{
Serial.println("Can not connect to the server.");
}
if ( !sent )
{
initNet();
}
} // if dht.read()
else
{
Serial.println("DHT fail");
}
delay(5000);
}
void initNet()
{
Serial.println("Initiating net");
do {
} while ( Ethernet.begin(mac) == 0 );
delay(1000);//wait for the Ethernet Shield for 1 second
Serial.print("Net inited, IP:");
Ethernet.localIP().printTo(Serial);
Serial.println();
}
void sendTemp(float t)
{
int i=t+0.5;
if ( i<0 ) {
client.print("-");
i = -i;
if ( i>99 )
i=99;
else if ( i<10 )
client.print("0");
} else {
if ( i<10 )
client.print("00");
else if ( i<100 )
client.print("0");
}
client.print(i);
}
void sendHumi(float h)
{
int i = h+0.5;
if ( i>99 ) i=99;
if ( i<10 )
client.print("0");
client.print(i);
}
void sendBaro(float b)
{
int i = b/10;
if (i > 99999){
i = 99999;
client.print(i);}
if (i > 10000 && i <= 99999){
client.print(i);}
if (i < 10000 && i >= 1000){
client.print(0);
client.print(i);}
if (i < 1000 && i > 0){
client.print(00);
client.print(i);}
if(i<0){
client.print(00000);}
}
boolean wait4content(Stream* stream, char *target, int targetLen)
{
size_t index = 0; // maximum target string length is 64k bytes!
int c;
boolean ret = false;
unsigned long timeBegin;
delay(50);
timeBegin = millis();
while ( true )
{
// wait and read one byte
while ( !stream->available() )
{
if ( millis() - timeBegin > TO_LINE )
{
break;
}
delay(2);
}
if ( stream->available() ) {
c = stream->read();
// judge the byte
if ( c == target[index] )
{
index ++;
if ( !target[index] )
// return true if all chars in the target match
{
ret = true;
break;
}
}
else if ( c>=0 )
{
index = 0; // reset index if any char does not match
} else // timed-out for one byte
{
break;
}
}
else // timed-out
{
break;
}
}
return ret;
}