Guest User

Arduino 433 sensor

a guest
Apr 12th, 2017
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.01 KB | None | 0 0
  1. /*
  2. * connectingStuff, Oregon Scientific v2.1 Emitter
  3. * http://www.connectingstuff.net/blog/encodage-protocoles-oregon-scientific-sur-arduino/
  4. *
  5. * Copyright (C) 2013 [email protected]
  6. * Modified by Jonathan Martin <[email protected]>, August 2015
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. *
  22. * 2017 04 11
  23. * LeOS modification to get data from DHT11 sensor connected to PIN7
  24. * Tested working great on Domoticz environment
  25. */
  26.  
  27. #include <dht11.h>
  28. dht11 DHT;
  29. #define DHT11_PIN 7
  30.  
  31. #define MODE_0 0 // Temperature only [THN132N]
  32. #define MODE_1 1 // Temperature + Humidity [THGR2228N]
  33. #define MODE_2 2 // Temperature + Humidity + Baro() [BTHR918N]
  34.  
  35. #define MODE MODE_1
  36.  
  37. const byte TX_PIN = 6;
  38.  
  39. const unsigned long TIME = 512;
  40. const unsigned long TWOTIME = TIME*2;
  41.  
  42. #define SEND_HIGH() digitalWrite(TX_PIN, HIGH)
  43. #define SEND_LOW() digitalWrite(TX_PIN, LOW)
  44.  
  45. // Buffer for Oregon message
  46. #if MODE == MODE_0
  47. byte OregonMessageBuffer[8];
  48. #elif MODE == MODE_1
  49. byte OregonMessageBuffer[9];
  50. #elif MODE == MODE_2
  51. byte OregonMessageBuffer[11];
  52. #else
  53. #error mode unknown
  54. #endif
  55.  
  56. /**
  57. * \brief Send logical "0" over RF
  58. * \details azero bit be represented by an off-to-on transition
  59. * \ of the RF signal at the middle of a clock period.
  60. * \ Remenber, the Oregon v2.1 protocol add an inverted bit first
  61. */
  62. inline void sendZero(void)
  63. {
  64. SEND_HIGH();
  65. delayMicroseconds(TIME);
  66. SEND_LOW();
  67. delayMicroseconds(TWOTIME);
  68. SEND_HIGH();
  69. delayMicroseconds(TIME);
  70. }
  71.  
  72. /**
  73. * \brief Send logical "1" over RF
  74. * \details a one bit be represented by an on-to-off transition
  75. * \ of the RF signal at the middle of a clock period.
  76. * \ Remenber, the Oregon v2.1 protocol add an inverted bit first
  77. */
  78. inline void sendOne(void)
  79. {
  80. SEND_LOW();
  81. delayMicroseconds(TIME);
  82. SEND_HIGH();
  83. delayMicroseconds(TWOTIME);
  84. SEND_LOW();
  85. delayMicroseconds(TIME);
  86. }
  87.  
  88. /**
  89. * Send a bits quarter (4 bits = MSB from 8 bits value) over RF
  90. *
  91. * @param data Source data to process and sent
  92. */
  93.  
  94. /**
  95. * \brief Send a bits quarter (4 bits = MSB from 8 bits value) over RF
  96. * \param data Data to send
  97. */
  98. inline void sendQuarterMSB(const byte data)
  99. {
  100. (bitRead(data, 4)) ? sendOne() : sendZero();
  101. (bitRead(data, 5)) ? sendOne() : sendZero();
  102. (bitRead(data, 6)) ? sendOne() : sendZero();
  103. (bitRead(data, 7)) ? sendOne() : sendZero();
  104. }
  105.  
  106. /**
  107. * \brief Send a bits quarter (4 bits = LSB from 8 bits value) over RF
  108. * \param data Data to send
  109. */
  110. inline void sendQuarterLSB(const byte data)
  111. {
  112. (bitRead(data, 0)) ? sendOne() : sendZero();
  113. (bitRead(data, 1)) ? sendOne() : sendZero();
  114. (bitRead(data, 2)) ? sendOne() : sendZero();
  115. (bitRead(data, 3)) ? sendOne() : sendZero();
  116. }
  117.  
  118. /******************************************************************/
  119. /******************************************************************/
  120. /******************************************************************/
  121.  
  122. /**
  123. * \brief Send a buffer over RF
  124. * \param data Data to send
  125. * \param size size of data to send
  126. */
  127. void sendData(byte *data, byte size)
  128. {
  129. for(byte i = 0; i < size; ++i)
  130. {
  131. sendQuarterLSB(data[i]);
  132. sendQuarterMSB(data[i]);
  133. }
  134. }
  135.  
  136. /**
  137. * \brief Send an Oregon message
  138. * \param data The Oregon message
  139. */
  140. void sendOregon(byte *data, byte size)
  141. {
  142. sendPreamble();
  143. //sendSync();
  144. sendData(data, size);
  145. sendPostamble();
  146. }
  147.  
  148. /**
  149. * \brief Send preamble
  150. * \details The preamble consists of 16 "1" bits
  151. */
  152. inline void sendPreamble(void)
  153. {
  154. byte PREAMBLE[]={0xFF,0xFF};
  155. sendData(PREAMBLE, 2);
  156. }
  157.  
  158. /**
  159. * \brief Send postamble
  160. * \details The postamble consists of 8 "0" bits
  161. */
  162. inline void sendPostamble(void)
  163. {
  164. #if MODE == MODE_0
  165. sendQuarterLSB(0x00);
  166. #else
  167. byte POSTAMBLE[]={0x00};
  168. sendData(POSTAMBLE, 1);
  169. #endif
  170. }
  171.  
  172. /**
  173. * \brief Send sync nibble
  174. * \details The sync is 0xA. It is not use in this version since the sync nibble
  175. * \ is include in the Oregon message to send.
  176. */
  177. inline void sendSync(void)
  178. {
  179. sendQuarterLSB(0xA);
  180. }
  181.  
  182. /******************************************************************/
  183. /******************************************************************/
  184. /******************************************************************/
  185.  
  186. /**
  187. * \brief Set the sensor type
  188. * \param data Oregon message
  189. * \param type Sensor type
  190. */
  191. inline void setType(byte *data, byte* type)
  192. {
  193. data[0] = type[0];
  194. data[1] = type[1];
  195. }
  196.  
  197. /**
  198. * \brief Set the sensor channel
  199. * \param data Oregon message
  200. * \param channel Sensor channel (0x10, 0x20, 0x30)
  201. */
  202. inline void setChannel(byte *data, byte channel)
  203. {
  204. data[2] = channel;
  205. }
  206.  
  207. /**
  208. * \brief Set the sensor ID
  209. * \param data Oregon message
  210. * \param ID Sensor unique ID
  211. */
  212. inline void setId(byte *data, byte ID)
  213. {
  214. data[3] = ID;
  215. }
  216.  
  217. /**
  218. * \brief Set the sensor battery level
  219. * \param data Oregon message
  220. * \param level Battery level (0 = low, 1 = high)
  221. */
  222. void setBatteryLevel(byte *data, byte level)
  223. {
  224. if(!level) data[4] = 0x0C;
  225. else data[4] = 0x00;
  226. }
  227.  
  228. /**
  229. * \brief Set the sensor temperature
  230. * \param data Oregon message
  231. * \param temp the temperature
  232. */
  233. void setTemperature(byte *data, float temp)
  234. {
  235. // Set temperature sign
  236. if(temp < 0)
  237. {
  238. data[6] = 0x08;
  239. temp *= -1;
  240. }
  241. else
  242. {
  243. data[6] = 0x00;
  244. }
  245.  
  246. // Determine decimal and float part
  247. int tempInt = (int)temp;
  248. int td = (int)(tempInt / 10);
  249. int tf = (int)round((float)((float)tempInt/10 - (float)td) * 10);
  250.  
  251. int tempFloat = (int)round((float)(temp - (float)tempInt) * 10);
  252.  
  253. // Set temperature decimal part
  254. data[5] = (td << 4);
  255. data[5] |= tf;
  256.  
  257. // Set temperature float part
  258. data[4] |= (tempFloat << 4);
  259. }
  260.  
  261. /**
  262. * \brief Set the sensor humidity
  263. * \param data Oregon message
  264. * \param hum the humidity
  265. */
  266. void setHumidity(byte* data, byte hum)
  267. {
  268. data[7] = (hum/10);
  269. data[6] |= (hum - data[7]*10) << 4;
  270. }
  271.  
  272. /**
  273. * \brief Set the sensor temperature
  274. * \param data Oregon message
  275. * \param temp the temperature
  276. */
  277. void setPressure(byte *data, float pres)
  278. {
  279. if ((pres > 850) && (pres < 1100)) {
  280. data[8] = (int)round(pres) - 856;
  281. data[9] = 0xC0;
  282. }
  283. }
  284.  
  285. /**
  286. * \brief Sum data for checksum
  287. * \param count number of bit to sum
  288. * \param data Oregon message
  289. */
  290. int Sum(byte count, const byte* data)
  291. {
  292. int s = 0;
  293.  
  294. for(byte i = 0; i<count;i++)
  295. {
  296. s += (data[i]&0xF0) >> 4;
  297. s += (data[i]&0xF);
  298. }
  299.  
  300. if(int(count) != count)
  301. s += (data[count]&0xF0) >> 4;
  302.  
  303. return s;
  304. }
  305.  
  306. /**
  307. * \brief Calculate checksum
  308. * \param data Oregon message
  309. */
  310. void calculateAndSetChecksum(byte* data)
  311. {
  312. #if MODE == MODE_0
  313. int s = ((Sum(6, data) + (data[6]&0xF) - 0xa) & 0xff);
  314.  
  315. data[6] |= (s&0x0F) << 4; data[7] = (s&0xF0) >> 4;
  316. #elif MODE == MODE_1
  317. data[8] = ((Sum(8, data) - 0xa) & 0xFF);
  318. #else
  319. data[10] = ((Sum(10, data) - 0xa) & 0xFF);
  320. #endif
  321. }
  322.  
  323. /******************************************************************/
  324. /******************************************************************/
  325. /******************************************************************/
  326.  
  327. void setup()
  328. {
  329. pinMode(TX_PIN, OUTPUT);
  330.  
  331. //Serial.begin(9600);
  332. //Serial.println("\n[Oregon V2.1 encoder]");
  333.  
  334. SEND_LOW();
  335.  
  336. #if MODE == MODE_0
  337. // Create the Oregon message for a temperature only sensor (THN132N)
  338. byte ID[] = {0xEA,0x4C};
  339. #elif MODE == MODE_1
  340. // Create the Oregon message for a temperature/humidity sensor (THGR2228N)
  341. byte ID[] = {0x1A,0x2D};
  342. #else
  343. // Create the Oregon message for a temperature/humidity/barometer sensor (BTHR918N)
  344. byte ID[] = {0x5A, 0x6D};
  345. #endif
  346.  
  347. setType(OregonMessageBuffer, ID);
  348. setChannel(OregonMessageBuffer, 0x20);
  349. setId(OregonMessageBuffer, 0xCB);
  350. }
  351.  
  352. void loop()
  353. {
  354. // Get Temperature, humidity and battery level from sensors
  355. // (ie: 1wire DS18B20 for température, ...)
  356. setBatteryLevel(OregonMessageBuffer, 1); // 0 : low, 1 : high
  357. float temp=(DHT.temperature,1);
  358. setTemperature(OregonMessageBuffer, temp);
  359.  
  360.  
  361. #if MODE != MODE_0
  362. // Set Humidity
  363. //setHumidity(OregonMessageBuffer, 52);
  364. float hum=(DHT.humidity,1);
  365. setHumidity(OregonMessageBuffer, hum);
  366. #endif
  367.  
  368. #if MODE == MODE_2
  369. // Set Pressure
  370. setPressure(OregonMessageBuffer, 1013);
  371. #endif
  372.  
  373. // Calculate the checksum
  374. calculateAndSetChecksum(OregonMessageBuffer);
  375.  
  376. // Show the Oregon Message
  377. //Serial.println("Oregon -> ");
  378. //for (byte i = 0; i < sizeof(OregonMessageBuffer); ++i) {
  379. // Serial.print(OregonMessageBuffer[i] >> 4, HEX);
  380. // Serial.print(OregonMessageBuffer[i] & 0x0F, HEX);
  381. //}
  382. //Serial.println();
  383.  
  384. // Send the Message over RF
  385. sendOregon(OregonMessageBuffer, sizeof(OregonMessageBuffer));
  386. // Send a "pause"
  387. SEND_LOW();
  388. delayMicroseconds(TWOTIME*8);
  389. // Send a copie of the first message. The v2.1 protocol send the
  390. // message two time
  391. sendOregon(OregonMessageBuffer, sizeof(OregonMessageBuffer));
  392.  
  393. // Wait for 30 seconds before send a new message
  394. SEND_LOW();
  395. delay(30000);
  396. }
Advertisement
Add Comment
Please, Sign In to add comment