Снятие показаний с погодного датчика Oregon THN132N
(Погодная станция Oregon Scientific Bar339P)
Краткое описание работы
- Считываем показание с датчика
- Формируем строку для передачи на сервер вида: deviceid=EA4C108122234030&channel=1&temperature=22&battery=90, где:
- deviceid - ID температурного датчика
- channel - Канал, на котором настроен датчик. Выбирается при помощи тумблера на устройстве.
- temperature - Темпаратура
- battery - Текущий заряд батареи
- Отправляем данные в БД сайта
Исходные компоненты
- Погодная станция с температурным датчиком (433Mhz) (https://market.yandex.ru/product--meteostantsiia-oregon-scientific-bar339p/7328708)
- Радио приёмник-передатчик (RF-kit) (https://www.seeedstudio.com/433mhz-rf-link-kit-p-127.html)
- Arduino Uno (https://www.seeedstudio.com/Arduino-Uno-Rev3-p-694.html)
- Arduino Ethernet Shield (https://www.arduino.cc/en/Guide/ArduinoEthernetShield)
- BreadBoard (http://amperka.ru/product/breadboard)
Подключение
Получение данных от датчика и последующая передача в БД
[oregon.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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
// Oregon V2 decoder modfied - Olivier Lebrun // Oregon V2 decoder added - Dominique Pierre // New code to decode OOK signals from weather sensors, etc. // 2010-04-11 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php // $Id: ookDecoder.pde 5331 2010-04-17 10:45:17Z jcw $ #include <SPI.h> #include <util.h> #include <Ethernet.h> // Ethernet options byte mac[] = { 0x90, 0xA2, 0x0D, 0x0D, 0xA2, 0xCC }; // Arduino Mac IPAddress ip(192,168,1,6); // Arduino IP IPAddress gateway(192,168,1,1); // WAN GateWay IPAddress subnet(255,255,255,0); // Subnet char pageName[] = "/handlers/SetSensorTemperature.ashx"; // Function to fill temperature data char serverName[] = "www.silvergate.ru"; // Server int serverPort = 80; // Port // insure params is big enough to hold your variables char params[255]; EthernetClient client; boolean lastConnected = false; // state of the connection last time through the main loop class DecodeOOK { protected: byte total_bits, bits, flip, state, pos, data[25]; virtual char decode(word width) = 0; public: enum { UNKNOWN, T0, T1, T2, T3, OK, DONE }; DecodeOOK () { resetDecoder(); } bool nextPulse(word width) { if (state != DONE) switch (decode(width)) { case -1: resetDecoder(); break; case 1: done(); break; } return isDone(); } bool isDone () const { return state == DONE; } const byte* getData (byte& count) const { count = pos; return data; } void resetDecoder() { total_bits = bits = pos = flip = 0; state = UNKNOWN; } // add one bit to the packet data buffer virtual void gotBit (char value) { total_bits++; byte *ptr = data + pos; *ptr = (*ptr >> 1) | (value << 7); if (++bits >= 8) { bits = 0; if (++pos >= sizeof data) { resetDecoder(); return; } } state = OK; } // store a bit using Manchester encoding void manchester(char value) { flip ^= value; // manchester code, long pulse flips the bit gotBit(flip); } // move bits to the front so that all the bits are aligned to the end void alignTail(byte max =0) { // align bits if (bits != 0) { data[pos] >>= 8 - bits; for (byte i = 0; i < pos; ++i) data[i] = (data[i] >> bits) | (data[i+1] << (8 - bits)); bits = 0; } // optionally shift bytes down if there are too many of 'em if (max > 0 && pos > max) { byte n = pos - max; pos = max; for (byte i = 0; i < pos; ++i) data[i] = data[i+n]; } } void reverseBits() { for (byte i = 0; i < pos; ++i) { byte b = data[i]; for (byte j = 0; j < 8; ++j) { data[i] = (data[i] << 1) | (b & 1); b >>= 1; } } } void reverseNibbles() { for (byte i = 0; i < pos; ++i) data[i] = (data[i] << 4) | (data[i] >> 4); } void done() { while (bits) gotBit(0); // padding state = DONE; } }; class OregonDecoderV2 : public DecodeOOK { public: OregonDecoderV2() {} // add one bit to the packet data buffer virtual void gotBit (char value) { if(!(total_bits & 0x01)) { data[pos] = (data[pos] >> 1) | (value ? 0x80 : 00); } total_bits++; pos = total_bits >> 4; if (pos >= sizeof data) { if (Serial.available()) Serial.println("sizeof data"); resetDecoder(); return; } state = OK; } virtual char decode(word width) { if (200 <= width && width < 1200) { byte w = width >= 700; switch (state) { case UNKNOWN: if (w != 0) { // Long pulse ++flip; } else if (w == 0 && 24 <= flip) { // Short pulse, start bit flip = 0; state = T0; } else { // Reset decoder return -1; } break; case OK: if (w == 0) { // Short pulse state = T0; } else { // Long pulse manchester(1); } break; case T0: if (w == 0) { // Second short pulse manchester(0); } else { // Reset decoder return -1; } break; } } else if (width >= 2500 && pos >= 8) { return 1; } else { return -1; } return 0; } }; OregonDecoderV2 orscV2; volatile word pulse; void ext_int_1(void) { static word last; // determine the pulse length in microseconds, for either polarity pulse = micros() - last; last += pulse; } float temperature(const byte* data) { int sign = (data[6]&0x8) ? -1 : 1; float temp = ((data[5]&0xF0) >> 4)*10 + (data[5]&0xF) + (float)(((data[4]&0xF0) >> 4) / 10.0); return sign * temp; } byte humidity(const byte* data) { return (data[7]&0xF) * 10 + ((data[6]&0xF0) >> 4); } // Ne retourne qu'un apercu de l'etat de la baterie : 10 = faible byte battery(const byte* data) { return (data[4] & 0x4) ? 10 : 90; } byte channel(const byte* data) { byte channel; switch (data[2]) { case 0x10: channel = 1; break; case 0x20: channel = 2; break; case 0x40: channel = 3; break; } return channel; } String reportSerial(const char* s, class DecodeOOK& decoder) { byte pos; const byte* data = decoder.getData(pos); if (Serial.available()) { Serial.print(s); Serial.print(' '); } String vTemperatureData = ""; for (byte i = 0; i < pos; ++i) { if (Serial.available()) { Serial.print(data[i] >> 4, HEX); Serial.print(data[i] & 0x0F, HEX); } } // Outside/Water Temp : THN132N,... if(data[0] == 0xEA && data[1] == 0x4C) { float tempC = temperature(data); if (Serial.available()) { Serial.print("[THN132N,...] Id:"); Serial.print(data[3], HEX); Serial.print(", Channel:"); Serial.print(channel(data)); Serial.print(", temp:"); Serial.print(tempC); Serial.print(", bat:"); Serial.print(battery(data)); Serial.println(); } char temp[5]; dtostrf(tempC,5,1,temp); //char * vTempData; //dtostrf(temperature(data), 5, 2, vTempData); sprintf(params,"deviceid=%s&channel=%i&temperature=%s&battery=%i","EA4C1083322340A3",channel(data),temp,battery(data)); vTemperatureData = params; if (Serial.available()) Serial.println("vTemperatureData: " + vTemperatureData); } else if(data[0] == 0x1A && data[1] == 0x2D) { // Inside Temp-Hygro : THGR228N,... if (Serial.available()) { Serial.print("[THGR228N,...] Id:"); Serial.print(data[3], HEX); Serial.print(" ,Channel:"); Serial.print(channel(data)); Serial.print(" ,temp:"); Serial.print(temperature(data)); Serial.print(" ,hum:"); Serial.print(humidity(data)); Serial.print(" ,bat:"); Serial.print(battery(data)); Serial.println(); } } decoder.resetDecoder(); return vTemperatureData; } byte sendData (char* domainBuffer, int thisPort, char* page, String thisData) { if (client.connected()) client.stop(); int inChar; char outBuf[64]; if (Serial.available()) { Serial.print(F("connecting...")); Serial.println(domainBuffer); } if (client.connect(serverName,serverPort)) { if (Serial.available()) Serial.println(F("connected")); // send the header //String data = "deviceid=EA4C108122234030&channel=1&temperature=22&battery=90"; String data = thisData; if (Serial.available()) Serial.println("Sending to Server: "); client.println("POST /handlers/SetSensorTemperature.ashx HTTP/1.1"); if (Serial.available()) Serial.print("POST /handlers/SetSensorTemperature.ashx HTTP/1.1"); client.println("Host: www.silvergate.ru"); client.println("Content-Type: application/x-www-form-urlencoded"); client.println("Connection: close"); client.println("User-Agent: Arduino/1.0"); client.print("Content-Length: "); client.println(data.length()); client.println(); client.print(data); client.println(); if (Serial.available()) Serial.println("data uploaded"); } else { if (Serial.available()) Serial.println(F("connection failed")); } } void setup () { Serial.begin(115200); delay(500); // start the Ethernet connection: if (Ethernet.begin(mac) == 0) { if (Serial.available()) Serial.println("Failed to configure Ethernet using DHCP"); // no point in carrying on, so do nothing forevermore: // try to congifure using IP address instead of DHCP: Ethernet.begin(mac, ip); } // give the Ethernet shield a second to initialize: delay(2000); attachInterrupt(1, ext_int_1, CHANGE); } void PostTemperatureDataToServer(String vData) { sendData(serverName,serverPort,pageName,vData); // if there's no net connection, but there was one last time // through the loop, then stop the client: if (!client.connected() && lastConnected) { if (Serial.available()) { Serial.println(); Serial.println("disconnecting."); } client.stop(); } lastConnected = client.connected(); } void loop() { static int i = 0; cli(); word p = pulse; pulse = 0; sei(); if (p != 0) { if (orscV2.nextPulse(p)) { String vData = reportSerial("OSV2", orscV2); if (vData != "") { PostTemperatureDataToServer(vData); } } } } |