-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEl Termostato_APRS_a_mimir.ino
276 lines (234 loc) · 7.38 KB
/
El Termostato_APRS_a_mimir.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Adafruit_BMP280.h>
#include <WifiClient.h>
#include <DHT.h>
#define MAX_SRV_CLIENTS 1
#define MAX_TIME_INACTIVE 60000000
// Set WiFi credentials
#define WIFI_SSID "Torre_Mesh"
#define WIFI_PASS "Q8REWQLB7HRFE6"
// Definir Sensores
#define SEALEVELPRESSURE_HPA (1036.6)
#define DHTPIN D6 // define en que puerto va conectado el DHT
#define DHTTYPE DHT11 // modelo de DHT (DHT11 o DHT22)
// Definir el tiempo que dormirá (en segundos)
#define SLEEP_TIME 300
// Declarar Sensores
Adafruit_BMP280 bmp; //I2C
DHT dht(DHTPIN, DHTTYPE);
// Declarar Variables
float tempC, tempF, hum, pres, alt = -99; // puede que no sirva, revisar y eliminar más tarde
float temperatura, humedad, presion, altitud;
// Create a new web server
ESP8266WebServer webserver(80);
unsigned long ElapsedTime = 0;
// Configuración conexion a Telnet APRS server
/* Programa para convertir coordenadas: https://www.qsl.net/on6mu/tinylocator.htm */
const String USER = "EA4HVF"; //write your aprs callsign
const String PAS = "21677"; // write your aprs password
const String LAT = "4034.75N"; //write your latitude
const String LON = "00357.39W"; //write your longitute
const String COMMENT = "EA4HVF Torrelodones WX Weather Station (g)"; //write some comment
//const String SERVER = "spain.d2g.com";
const String SERVER = "Spain.aprs2.net"; // write the address of the aprs server
const int PORT = 14580; //write the aprs server port
// Fin configuracion TELNET
// Handle Root
void rootPage() {
webserver.send(200, "text/plain", "Torrelodones WX Station !");
}
// Handle 404
void notfoundPage(){
webserver.send(404, "text/plain", "404: Nada por aquí...");
}
void setup()
{
// Setup serial port
Serial.begin(115200);
Serial.println();
bmp.begin(0x76);
dht.begin();
delay(10);
//Begin WiFi
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) { delay(100); }
// WiFi Connected
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
// Start Web Server
webserver.on("/", rootPage);
webserver.onNotFound(notfoundPage);
webserver.begin();
}
// Listen for HTTP requests
void loop(void){
webserver.handleClient();
sendAPRSPacketEvery(120000); //Envía datos cada 2 minutos
yield();
// Se duerme a los 5 minutos
if (millis() >= 300000) {
ESP.deepSleep(SLEEP_TIME * 1000000, WAKE_RF_DEFAULT);
}
}
void handle_0nConnect(){
temperatura = bmp.readTemperature();
humedad = dht.readHumidity();
altitud = bmp.readAltitude(SEALEVELPRESSURE_HPA);
presion = bmp.readPressure()/100;
}
void getDataFromBMP(){
int count = 0;
tempC = bmp.readTemperature();
tempF = ((tempC *9)/5 +32);
hum = dht.readHumidity();
pres = bmp.readPressure();
alt = bmp.readAltitude();
Serial.println("Read tempC="+String(tempC)+" tempF="+String(tempF)+" hum="+String(hum)+" pres="+String(pres)+" alt="+String(alt));
while ((isnan(tempC) || isnan(tempF) || isnan(pres) || isnan(hum) || isnan(alt) || hum == 0) && count < 1000){
// Debug.println("Read (isnan(tempC) || isnan(tempF) || isnan(pres) || isnan(hum) || isnan(alt) || hum == 0) && count < 1000");
tempC = bmp.readTemperature();
tempF = ((tempC *9)/5 +32);
hum = dht.readHumidity();
pres = bmp.readPressure();
alt = bmp.readAltitude();
Serial.println("Trying read again tempC="+String(tempC)+" tempF="+String(tempF)+" hum="+String(hum)+" pres="+String(pres)+" alt="+String(alt)+" count="+String(count));
delay(2);
count++;
}
}
String getTemp(float pTemp){
String strTemp;
int intTemp;
intTemp = (int)pTemp;
strTemp = String(intTemp);
//strTemp.replace(".", "");
switch (strTemp.length()){
case 1:
return "00" + strTemp;
break;
case 2:
return "0" + strTemp;
break;
case 3:
return strTemp;
break;
default:
return "-999";
}
}
String getHum(float pHum){
String strHum;
int intHum;
intHum = (int)pHum;
strHum = String(intHum);
switch (strHum.length()){
case 1:
return "0" + strHum;
break;
case 2:
return strHum;
break;
case 3:
if (intHum == 100){
return "00";
}else {
return "-99";
}
break;
default:
return "-99";
}
}
String getPres(float pPress){
String strPress;
int intPress = 0;
intPress = (int)(pPress/10);
strPress = String(intPress);
//strPress.replace(".", "");
///strPress = strPress.substring(0,5);
//return strPress;
switch (strPress.length()){
case 1:
return "0000" + strPress;
break;
case 2:
return "000" + strPress;
break;
case 3:
return "00" + strPress;
break;
case 4:
return "0" + strPress;
break;
case 5:
return strPress;
break;
default:
return "-99999";
}
}
void sendAPRSPacketEvery(unsigned long t){
unsigned long currentTime;
currentTime = millis();
if (currentTime < ElapsedTime){
Serial.println("Run: (currentTime = millis()) < ElapsedTime.\ncurrentTime ="+String(currentTime)+"\nElapsedTime="+String(ElapsedTime));
ElapsedTime = 0;
Serial.println("Set ElapsedTime=0");
}
if ((currentTime - ElapsedTime) >= t){
Serial.println("Tried : (currentTime - ElapsedTime) >= t.\ncurrentTime ="+String(currentTime)+"\nElapsedTime="+String(ElapsedTime));
clientConnectTelNet();
ElapsedTime = currentTime;
Serial.println("Set ElapsedTime = currentTime");
}
}
void clientConnectTelNet(){
WiFiClient client;
int count = 0;
String packet, aprsauth, tempStr, humStr, presStr;
Serial.println("Run clientConnectTelNet()");
getDataFromBMP();
while (!client.connect(SERVER.c_str(), PORT) && count <20){
Serial.println("Didn't connect with server: "+String(SERVER)+" Port: "+String(PORT));
delay (1000);
client.stop();
client.flush();
Serial.println("Run client.stop");
Serial.println("Trying to connect with server: "+String(SERVER)+" Port: "+String(PORT));
count++;
Serial.println("Try: "+String(count));
}
if (count == 20){
Serial.println("Tried: "+String(count)+" don't send the packet!");
}else{
Serial.println("Connected with server: "+String(SERVER)+" Port: "+String(PORT));
tempStr = getTemp(tempF);
humStr = getHum(hum);
presStr = getPres(pres);
Serial.println("Leu tempStr="+tempStr+" humStr="+humStr+" presStr="+presStr);
while (client.connected()){ //there is some problem with the original code from WiFiClient.cpp on procedure uint8_t WiFiClient::connected()
// it don't check if the connection was close, so you need to locate and change the line below:
//if (!_client ) to:
//if (!_client || _client->state() == CLOSED)
delay(1000);
Serial.println("Run client.connected()");
if(tempStr != "-999" || presStr != "-99999" || humStr != "-99"){
aprsauth = "user " + USER + " pass " + PAS + "\n";
client.write(aprsauth.c_str());
delay(500);
Serial.println("Send client.write="+aprsauth);
packet = USER + ">APRMCU,TCPIP*,qAC,T2SPAIN:=" + LAT + "/" + LON +
"_.../...g...t" + tempStr +
"r...p...P...h" + humStr +
"b" + presStr + COMMENT + "\n";
client.write(packet.c_str());
delay(500);
Serial.println("Send client.write="+packet);
client.stop();
client.flush();
Serial.println("Telnet client disconnect ");
}
}
}
}