Advertisement
Guest User

Untitled

a guest
May 15th, 2013
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "floatToString.h"
  3. #include "EmonLib.h" // Include Emon Library
  4. #include <DHT.h>
  5.  
  6. #define DHTPIN A0 // what pin we're connected to
  7. #define DHTTYPE DHT22 // DHT 22 (AM2302)
  8.  
  9. EnergyMonitor emon1; // Create an instance
  10. EnergyMonitor emon2; // Create an instance
  11. EnergyMonitor emon3; // Create an instance
  12. DHT dht(DHTPIN, DHTTYPE);
  13.  
  14. int switchPins[] = {7, 6, 5, 4, 3, 2};
  15. int switchStatus = 0;
  16. char StatusText[11];
  17. char x, tmp[5], hmd[5], o[5], m[5];
  18. char n[5];
  19. char cc[15];
  20. char th[11];
  21. char sn[26];
  22.  
  23. void setup()
  24. {
  25. emon1.current(1, 111.1); // Current: input pin, calibration.
  26. emon2.current(2, 111.1); // Current: input pin, calibration.
  27. emon3.current(3, 111.1); // Current: input pin, calibration.
  28. Serial.begin(9600);
  29. int i=0;
  30. for(i=0; i<6; i++)
  31. {
  32. pinMode(switchPins[i], OUTPUT);
  33. }
  34.  
  35. Wire.begin(4); // join i2c bus with address #4
  36. Wire.onReceive(receiveEvent); // register event
  37. Wire.onRequest(requestEvent); // register event
  38. dht.begin();
  39. }
  40.  
  41. void loop()
  42. {
  43. getSensorValue();
  44. delay(100);
  45. }
  46.  
  47. // function that executes whenever data is received from master
  48. // this function is registered as an event, see setup()
  49. void receiveEvent(int howMany)
  50. {
  51. x = Wire.read();
  52. if(x=='1')
  53. {
  54. switching(switchPins[0]);
  55. }
  56. else if(x=='2')
  57. {
  58. switching(switchPins[1]);
  59. }
  60. else if(x=='3')
  61. {
  62. switching(switchPins[2]);
  63. }
  64. else if(x=='4')
  65. {
  66. switching(switchPins[3]);
  67. }
  68. else if(x=='5')
  69. {
  70. switching(switchPins[4]);
  71. }
  72. else if(x=='6')
  73. {
  74. switching(switchPins[5]);
  75. }
  76. }
  77.  
  78. void requestEvent()
  79. {
  80. if(x=='c')
  81. {
  82. Wire.write(cc); // respond with message of 6
  83. }
  84. else if(x=='n')
  85. {
  86. Wire.write(sn); // respond with message of 6
  87. }
  88. else
  89. {
  90. checkingStatus(6);
  91. Wire.write(StatusText); // respond with message of 6
  92. }
  93. }
  94.  
  95. void switching(int switchPin)
  96. {
  97. switchStatus = digitalRead(switchPin); // Read Check Status Pin
  98. if(switchStatus==HIGH){ digitalWrite(switchPin, LOW); } else{ digitalWrite(switchPin, HIGH); }
  99. }
  100.  
  101. void checkingStatus(int numberofSwitch)
  102. {
  103. int i=0;
  104. for(i=0; i<numberofSwitch; i++)
  105. {
  106. switchStatus = digitalRead(switchPins[i]); // Read Check Status Pin
  107. if(switchStatus==HIGH)
  108. {
  109. StatusText[i]='1';
  110. }
  111. else
  112. {
  113. StatusText[i]='0';
  114. }
  115. }
  116. }
  117.  
  118. void getSensorValue()
  119. {
  120. sn[0]='\0';
  121. getCurrent();
  122. getDHT();
  123. strncat(sn,th,11);
  124. strncat(sn,"|",26);
  125. strncat(sn,cc,26);
  126. }
  127.  
  128. void getDHT()
  129. {
  130. th[0]='\0';
  131. float t = dht.readTemperature();
  132. float h = dht.readHumidity();
  133. floatToString(tmp,t,2);
  134. strncat(th,tmp,20);
  135. strncat(th,"|",20);
  136. floatToString(hmd,h,2);
  137. strncat(th,hmd,20);
  138. }
  139.  
  140. void getCurrent()
  141. {
  142. cc[0]='\0'; o[0]='\0'; m[0]='\0'; n[0]='\0';
  143. float IrmsR = emon1.calcIrms(1480); // Calculate Irms only
  144. float IrmsS = emon2.calcIrms(1480); // Calculate Irms only
  145. float IrmsT = emon3.calcIrms(1480); // Calculate Irms only
  146. floatToString(o,IrmsR,2);
  147. floatToString(m,IrmsS,2);
  148. floatToString(n,IrmsT,2);
  149. strncat(cc,o,20);
  150. strncat(cc,"|",20);
  151. strncat(cc,m,20);
  152. strncat(cc,"|",20);
  153. strncat(cc,n,20);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement