Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.80 KB | None | 0 0
  1. /*
  2. Cayenne DS18B20 Example
  3.  
  4. This sketch shows how to send DS18B20 Sensor data to the Cayenne Dashboard.
  5.  
  6. The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
  7.  
  8. Steps:
  9. 1. Install the OneWire library (http://www.pjrc.com/teensy/td_libs_OneWire.html) from the Arduino Library Manager.
  10. 2. Install the DallasTemperature library (http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library#Code.2FLibrary) from the Arduino Library Manager.
  11. 3. Attach a DS18B20 to a digital pin on your Arduino.
  12. Schematic:
  13. [Ground] -- [DS18B20] -- [4.7k resistor] -- [5V]
  14. |______________|
  15. |
  16. Digital Pin
  17. 4. Set the SENSOR_PIN value below to the pin number you used when connecting the sensor.
  18. 5. Set the VIRTUAL_CHANNEL value below to a free virtual channel (or the virtual channel of a DS18B20 Sensor widget you have added) in the Dashboard.
  19. 6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  20. 7. Compile and upload this sketch.
  21. 8. Once the Arduino connects to the Dashboard it should automatically create a temporary display widget (or update the DS18B20 Sensor widget you have added) with data.
  22. To make a temporary widget permanent click the plus sign on the widget.
  23. */
  24.  
  25. #define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
  26. #include <OneWire.h>
  27. #include <DallasTemperature.h>
  28. #include <CayenneMQTTEthernet.h>
  29.  
  30. // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
  31. char username[] = "";
  32. char password[] = "";
  33. char clientID[] = "";
  34.  
  35. #define VIRTUAL_CHANNEL1 1
  36. #define VIRTUAL_CHANNEL2 2
  37. #define VIRTUAL_CHANNEL3 3
  38. #define VIRTUAL_CHANNEL4 4
  39. #define VIRTUAL_CHANNEL5 5
  40. #define VIRTUAL_CHANNEL6 6
  41. #define VIRTUAL_CHANNEL7 7
  42. #define VIRTUAL_CHANNEL8 8
  43. // #define VIRTUAL_CHANNEL9 9
  44. // #define VIRTUAL_CHANNEL10 10
  45.  
  46. #define ACTUATOR_PIN1 40
  47. #define ACTUATOR_PIN2 41
  48. #define ACTUATOR_PIN3 42
  49. #define ACTUATOR_PIN4 43
  50. #define ACTUATOR_PIN5 44
  51. #define ACTUATOR_PIN6 45
  52. #define ACTUATOR_PIN7 46
  53. #define ACTUATOR_PIN8 47
  54. //#define ACTUATOR_PIN9 48
  55.  
  56. #define SENSOR_PIN22 22
  57. #define SENSOR_PIN23 23
  58. #define SENSOR_PIN24 24
  59. #define SENSOR_PIN25 25
  60. #define SENSOR_PIN26 26
  61. #define SENSOR_PIN27 27
  62. #define SENSOR_PIN28 28
  63. // #define SENSOR_PIN29 29
  64. // #define SENSOR_PIN30 30
  65. // #define SENSOR_PIN31 31
  66. // #define SENSOR_PIN32 32
  67. // #define SENSOR_PIN33 33
  68. // #define SENSOR_PIN34 34
  69. // #define SENSOR_PIN35 35
  70. // #define SENSOR_PIN36 36
  71. // #define SENSOR_PIN37 37
  72.  
  73. #define VIRTUAL_CHANNEL22 22
  74. #define VIRTUAL_CHANNEL23 23
  75. #define VIRTUAL_CHANNEL24 24
  76. #define VIRTUAL_CHANNEL25 25
  77. #define VIRTUAL_CHANNEL26 26
  78. #define VIRTUAL_CHANNEL27 27
  79. #define VIRTUAL_CHANNEL28 28
  80. // #define VIRTUAL_CHANNEL29 29
  81. // #define VIRTUAL_CHANNEL30 30
  82. // #define VIRTUAL_CHANNEL31 31
  83. // #define VIRTUAL_CHANNEL32 32
  84. // #define VIRTUAL_CHANNEL33 33
  85. // #define VIRTUAL_CHANNEL34 34
  86. // #define VIRTUAL_CHANNEL35 35
  87. // #define VIRTUAL_CHANNEL36 36
  88. // #define VIRTUAL_CHANNEL37 37
  89.  
  90. OneWire oneWire22(SENSOR_PIN22);
  91. OneWire oneWire23(SENSOR_PIN23);
  92. OneWire oneWire24(SENSOR_PIN24);
  93. OneWire oneWire25(SENSOR_PIN25);
  94. OneWire oneWire26(SENSOR_PIN26);
  95. OneWire oneWire27(SENSOR_PIN27);
  96. OneWire oneWire28(SENSOR_PIN28);
  97. // OneWire oneWire29(SENSOR_PIN29);
  98. // OneWire oneWire30(SENSOR_PIN30);
  99. // OneWire oneWire31(SENSOR_PIN31);
  100. // OneWire oneWire32(SENSOR_PIN32);
  101. // OneWire oneWire33(SENSOR_PIN33);
  102. // OneWire oneWire34(SENSOR_PIN34);
  103. // OneWire oneWire35(SENSOR_PIN35);
  104. // OneWire oneWire36(SENSOR_PIN36);
  105. // OneWire oneWire37(SENSOR_PIN37);
  106.  
  107. DallasTemperature sensors22(&oneWire22);
  108. DallasTemperature sensors23(&oneWire23);
  109. DallasTemperature sensors24(&oneWire24);
  110. DallasTemperature sensors25(&oneWire25);
  111. DallasTemperature sensors26(&oneWire26);
  112. DallasTemperature sensors27(&oneWire27);
  113. DallasTemperature sensors28(&oneWire28);
  114. // DallasTemperature sensors29(&oneWire29);
  115. // DallasTemperature sensors30(&oneWire30);
  116. // DallasTemperature sensors31(&oneWire31);
  117. // DallasTemperature sensors32(&oneWire32);
  118. // DallasTemperature sensors33(&oneWire33);
  119. // DallasTemperature sensors34(&oneWire34);
  120. // DallasTemperature sensors35(&oneWire35);
  121. // DallasTemperature sensors36(&oneWire36);
  122. // DallasTemperature sensors37(&oneWire37);
  123. void setup()
  124. {
  125. Serial.begin(9600);
  126. pinMode(ACTUATOR_PIN1, OUTPUT);
  127. pinMode(ACTUATOR_PIN2, OUTPUT);
  128. pinMode(ACTUATOR_PIN3, OUTPUT);
  129. pinMode(ACTUATOR_PIN4, OUTPUT);
  130. pinMode(ACTUATOR_PIN5, OUTPUT);
  131. pinMode(ACTUATOR_PIN6, OUTPUT);
  132. pinMode(ACTUATOR_PIN7, OUTPUT);
  133. pinMode(ACTUATOR_PIN8, OUTPUT);
  134. // pinMode(ACTUATOR_PIN9, OUTPUT);
  135. // pinMode(ACTUATOR_PIN10, OUTPUT);
  136.  
  137. Cayenne.begin(username, password, clientID);
  138. sensors22.begin();
  139. sensors23.begin();
  140. sensors24.begin();
  141. sensors25.begin();
  142. sensors26.begin();
  143. sensors27.begin();
  144. sensors28.begin();
  145. // sensors29.begin();
  146. // sensors30.begin();
  147. // sensors31.begin();
  148. // sensors32.begin();
  149. // sensors33.begin();
  150. // sensors34.begin();
  151. // sensors35.begin();
  152. // sensors36.begin();
  153. // sensors37.begin();
  154. }
  155.  
  156. void loop()
  157. {
  158. Cayenne.loop();
  159. }
  160. CAYENNE_IN(VIRTUAL_CHANNEL1)
  161. {
  162. // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  163. if (getValue.asInt() == 0) {
  164. digitalWrite(ACTUATOR_PIN1, HIGH);
  165. }
  166. else {
  167. digitalWrite(ACTUATOR_PIN1, LOW);
  168. }
  169. }
  170. CAYENNE_IN(VIRTUAL_CHANNEL2)
  171. {
  172. // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  173. if (getValue.asInt() == 0) {
  174. digitalWrite(ACTUATOR_PIN2, HIGH);
  175. }
  176. else {
  177. digitalWrite(ACTUATOR_PIN2, LOW);
  178. }
  179. }
  180. CAYENNE_IN(VIRTUAL_CHANNEL3)
  181. {
  182. // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  183. if (getValue.asInt() == 0) {
  184. digitalWrite(ACTUATOR_PIN3, HIGH);
  185. }
  186. else {
  187. digitalWrite(ACTUATOR_PIN3, LOW);
  188. }
  189. }
  190. CAYENNE_IN(VIRTUAL_CHANNEL4)
  191. {
  192. // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  193. if (getValue.asInt() == 0) {
  194. digitalWrite(ACTUATOR_PIN4, HIGH);
  195. }
  196. else {
  197. digitalWrite(ACTUATOR_PIN4, LOW);
  198. }
  199. }
  200. CAYENNE_IN(VIRTUAL_CHANNEL5)
  201. {
  202. // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  203. if (getValue.asInt() == 0) {
  204. digitalWrite(ACTUATOR_PIN5, HIGH);
  205. }
  206. else {
  207. digitalWrite(ACTUATOR_PIN5, LOW);
  208. }
  209. }
  210. CAYENNE_IN(VIRTUAL_CHANNEL6)
  211. {
  212. // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  213. if (getValue.asInt() == 0) {
  214. digitalWrite(ACTUATOR_PIN6, HIGH);
  215. }
  216. else {
  217. digitalWrite(ACTUATOR_PIN6, LOW);
  218. }
  219. }
  220. CAYENNE_IN(VIRTUAL_CHANNEL7)
  221. {
  222. // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  223. if (getValue.asInt() == 0) {
  224. digitalWrite(ACTUATOR_PIN7, HIGH);
  225. }
  226. else {
  227. digitalWrite(ACTUATOR_PIN7, LOW);
  228. }
  229. }
  230. CAYENNE_IN(VIRTUAL_CHANNEL8)
  231. {
  232. // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  233. if (getValue.asInt() == 0) {
  234. digitalWrite(ACTUATOR_PIN8, HIGH);
  235. }
  236. else {
  237. digitalWrite(ACTUATOR_PIN8, LOW);
  238. }
  239. }
  240. /* CAYENNE_IN(VIRTUAL_CHANNEL9)
  241. {
  242. // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  243. if (getValue.asInt() == 0) {
  244. digitalWrite(ACTUATOR_PIN9, HIGH);
  245. }
  246. else {
  247. digitalWrite(ACTUATOR_PIN9, LOW);
  248. }
  249. }
  250. CAYENNE_IN(VIRTUAL_CHANNEL10)
  251. {
  252. // Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
  253. if (getValue.asInt() == 0) {
  254. digitalWrite(ACTUATOR_PIN10, HIGH);
  255. }
  256. else {
  257. digitalWrite(ACTUATOR_PIN10, LOW);
  258. }
  259. }*/
  260. // This function is called at intervals to send sensor data to Cayenne.
  261. CAYENNE_OUT(VIRTUAL_CHANNEL22)
  262. {
  263. sensors22.requestTemperatures();
  264. Cayenne.celsiusWrite(VIRTUAL_CHANNEL22, sensors22.getTempCByIndex(0));
  265.  
  266. }
  267. CAYENNE_OUT(VIRTUAL_CHANNEL23)
  268. {
  269. sensors23.requestTemperatures();
  270. Cayenne.celsiusWrite(VIRTUAL_CHANNEL23, sensors23.getTempCByIndex(0));
  271.  
  272. }
  273. CAYENNE_OUT(VIRTUAL_CHANNEL24)
  274. {
  275. sensors24.requestTemperatures();
  276. Cayenne.celsiusWrite(VIRTUAL_CHANNEL24, sensors24.getTempCByIndex(0));
  277.  
  278. }
  279. CAYENNE_OUT(VIRTUAL_CHANNEL25)
  280. {
  281. sensors25.requestTemperatures();
  282. Cayenne.celsiusWrite(VIRTUAL_CHANNEL25, sensors25.getTempCByIndex(0));
  283. }
  284. CAYENNE_OUT(VIRTUAL_CHANNEL26)
  285. {
  286. // Send the command to get temperatures.
  287. sensors26.requestTemperatures();
  288. // This command writes the temperature in Celsius to the Virtual Channel.
  289. Cayenne.celsiusWrite(VIRTUAL_CHANNEL26, sensors26.getTempCByIndex(0));
  290.  
  291. }
  292. CAYENNE_OUT(VIRTUAL_CHANNEL27)
  293. {
  294. // Send the command to get temperatures.
  295. sensors27.requestTemperatures();
  296. // This command writes the temperature in Celsius to the Virtual Channel.
  297. Cayenne.celsiusWrite(VIRTUAL_CHANNEL27, sensors27.getTempCByIndex(0));
  298.  
  299. }
  300. CAYENNE_OUT(VIRTUAL_CHANNEL28)
  301. {
  302. // Send the command to get temperatures.
  303. sensors28.requestTemperatures();
  304. // This command writes the temperature in Celsius to the Virtual Channel.
  305. Cayenne.celsiusWrite(VIRTUAL_CHANNEL28, sensors28.getTempCByIndex(0));
  306.  
  307. }
  308. /* CAYENNE_OUT(VIRTUAL_CHANNEL29)
  309. {
  310. // Send the command to get temperatures.
  311. sensors29.requestTemperatures();
  312. // This command writes the temperature in Celsius to the Virtual Channel.
  313. Cayenne.celsiusWrite(VIRTUAL_CHANNEL29, sensors29.getTempCByIndex(0));
  314.  
  315. }
  316. CAYENNE_OUT(VIRTUAL_CHANNEL30)
  317. {
  318. // Send the command to get temperatures.
  319. sensors30.requestTemperatures();
  320. // This command writes the temperature in Celsius to the Virtual Channel.
  321. Cayenne.celsiusWrite(VIRTUAL_CHANNEL30, sensors30.getTempCByIndex(0));
  322.  
  323. }
  324. CAYENNE_OUT(VIRTUAL_CHANNEL31)
  325. {
  326. // Send the command to get temperatures.
  327. sensors31.requestTemperatures();
  328. // This command writes the temperature in Celsius to the Virtual Channel.
  329. Cayenne.celsiusWrite(VIRTUAL_CHANNEL31, sensors31.getTempCByIndex(0));
  330.  
  331. }
  332. CAYENNE_OUT(VIRTUAL_CHANNEL32)
  333. {
  334. // Send the command to get temperatures.
  335. sensors32.requestTemperatures();
  336. // This command writes the temperature in Celsius to the Virtual Channel.
  337. Cayenne.celsiusWrite(VIRTUAL_CHANNEL32, sensors32.getTempCByIndex(0));
  338.  
  339. }
  340. CAYENNE_OUT(VIRTUAL_CHANNEL33)
  341. {
  342. // Send the command to get temperatures.
  343. sensors33.requestTemperatures();
  344. // This command writes the temperature in Celsius to the Virtual Channel.
  345. Cayenne.celsiusWrite(VIRTUAL_CHANNEL33, sensors33.getTempCByIndex(0));
  346.  
  347. }
  348. CAYENNE_OUT(VIRTUAL_CHANNEL34)
  349. {
  350. // Send the command to get temperatures.
  351. sensors34.requestTemperatures();
  352. // This command writes the temperature in Celsius to the Virtual Channel.
  353. Cayenne.celsiusWrite(VIRTUAL_CHANNEL34, sensors34.getTempCByIndex(0));
  354.  
  355. }
  356. CAYENNE_OUT(VIRTUAL_CHANNEL35)
  357. {
  358. // Send the command to get temperatures.
  359. sensors35.requestTemperatures();
  360. // This command writes the temperature in Celsius to the Virtual Channel.
  361. Cayenne.celsiusWrite(VIRTUAL_CHANNEL35, sensors35.getTempCByIndex(0));
  362.  
  363. }
  364. CAYENNE_OUT(VIRTUAL_CHANNEL36)
  365. {
  366. // Send the command to get temperatures.
  367. sensors36.requestTemperatures();
  368. // This command writes the temperature in Celsius to the Virtual Channel.
  369. Cayenne.celsiusWrite(VIRTUAL_CHANNEL36, sensors36.getTempCByIndex(0));
  370.  
  371. }
  372. CAYENNE_OUT(VIRTUAL_CHANNEL37)
  373. {
  374. // Send the command to get temperatures.
  375. sensors37.requestTemperatures();
  376. // This command writes the temperature in Celsius to the Virtual Channel.
  377. Cayenne.celsiusWrite(VIRTUAL_CHANNEL37, sensors37.getTempCByIndex(0));
  378.  
  379. } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement