Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. /**************************************************************************
  2. Souliss - Android Control for RGB Led/Strip, FAN, Relay, TEMP DB18B20 & AC712
  3. for Current and Watt Sensor
  4.  
  5. This is using Arduino pro mini board connected to ESP8266 using hardware
  6. TX and RX
  7.  
  8. Run this code on one of the following boards:
  9. - Arduino with ESP8266 connected using hardware serial TX & RX
  10. - ESP TX to Arduino RX, ESP RX to Arduino TX
  11. - For Arduino 5V use Shift Leveling for TX/RX Pin
  12.  
  13. ***************************************************************************/
  14.  
  15. // Configure the framework
  16. #include "bconf/StandardArduino.h" // Use a standard Arduino
  17. #include "conf/usart.h" // USART for ESP8266
  18.  
  19. // Include framework code and libraries
  20. #include <SPI.h>
  21. #include "Souliss.h"
  22. #include "extras/extras.h"
  23. #include <OneWire.h>
  24. #include <DallasTemperature.h>
  25.  
  26.  
  27. // Define the network configuration according to your router settings
  28. #define myvNet_subnet 0xFF00
  29. #define Gateway_RS485 0xD001
  30. #define Peer_RS485 0xD002
  31.  
  32. //Define Slot Number
  33. #define LEDCONTROL 0 // This is the memory slot for the logic that handle the light
  34. #define LEDRED 1 // This is the memory slot for the logic that handle the light
  35. #define LEDGREEN 2 // This is the memory slot for the logic that handle the light
  36. #define LEDBLUE 3 // This is the memory slot for the logic that handle the light
  37. #define MYLEDLOGIC 4 // This is the memory slot for the logic that handle the light
  38. #define FAN 5 // This is the memory slot for the logic that handle the light
  39. #define TEMPERATURE 7
  40. #define TEMPERATURE1 9
  41. #define CURRENT 11
  42. #define WATTS 13
  43. #define DEADBAND 0.01 // Deadband value 5%
  44. #define DEADBAND1 0.01 // Deadband value 5%
  45.  
  46. //Arduino Pin, use pin 7
  47. #define ONE_WIRE_BUS 7
  48. OneWire oneWire(ONE_WIRE_BUS);
  49. // Pass our oneWire reference to Dallas Temperature.
  50. DallasTemperature sensors(&oneWire);
  51. DeviceAddress Ujung = {0x28, 0xFF, 0x01, 0x33, 0x02, 0x15, 0x04, 0x78};
  52. DeviceAddress Deket = {0x28, 0xFF, 0xCB, 0x2C, 0x02, 0x15, 0x04, 0x45};
  53.  
  54. //Variable for Current & Watts
  55. const int analogIn = A0;
  56. int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
  57. int RawValue= 0;
  58. int ACSoffset = 2500;
  59. float Voltage = 0;
  60. float VDC = 0;
  61. float Amps = 0;
  62.  
  63. void setup()
  64. {
  65. // Serial.begin(115200);
  66.  
  67. Initialize();
  68.  
  69. // Set network parameters
  70. SetAddress(Peer_RS485, myvNet_subnet, Gateway_RS485);
  71.  
  72. // Set a logic to control a RGB LED, Relay, Fan, Temp, Current & Watts
  73. Set_LED_Strip(LEDCONTROL);
  74. Set_SimpleLight(MYLEDLOGIC);
  75. Set_DimmableLight(FAN);
  76. Souliss_SetT52(memory_map, TEMPERATURE);
  77. Souliss_SetT52(memory_map, TEMPERATURE1);
  78. Set_Current(CURRENT);
  79. Set_Power(WATTS);
  80.  
  81. pinMode(2, INPUT); // Hardware pulldown required (button)
  82. pinMode(9, OUTPUT); // Power the LED Red
  83. pinMode(5, OUTPUT); // Power the LED Green
  84. pinMode(6, OUTPUT); // Power the LED Blue
  85. pinMode(10, OUTPUT); // Power the LED (relay)
  86. pinMode(3, OUTPUT); // Power the Fan
  87.  
  88. // Start up the DALLAS library
  89. sensors.begin();
  90. sensors.setResolution(Ujung, 10);
  91. sensors.setResolution(Deket, 10);
  92. }
  93.  
  94. void loop()
  95. {
  96. // Here we start to play
  97. EXECUTEFAST() {
  98. UPDATEFAST();
  99.  
  100. // Read every 10ms the input state and send it to the other board
  101. FAST_10ms() {
  102.  
  103. // Retreive data from the MaCaco communication channel
  104. Souliss_CommunicationData(memory_map, &data_changed);
  105.  
  106. // Compare the acquired input with the stored one, send the new value to the
  107. // user interface if the difference is greater than the deadband
  108.  
  109. Souliss_Logic_T52(memory_map, TEMPERATURE, DEADBAND, &data_changed);
  110. Souliss_Logic_T52(memory_map, TEMPERATURE1, DEADBAND1, &data_changed);
  111.  
  112. // Use Pin2 as command
  113. if (DigIn(2, Souliss_T1n_ToggleCmd, LEDCONTROL))
  114. mInput(MYLEDLOGIC) = mInput(LEDCONTROL);
  115.  
  116. // Execute the logic that handle the LED
  117. Logic_LED_Strip(LEDCONTROL);
  118.  
  119. // Use the output values to control the PWM
  120. analogWrite(9, mOutput(LEDRED));
  121. analogWrite(5, mOutput(LEDGREEN));
  122. analogWrite(6, mOutput(LEDBLUE));
  123.  
  124. Logic_SimpleLight(MYLEDLOGIC);
  125. DigOut(10, Souliss_T1n_Coil,MYLEDLOGIC);
  126.  
  127. Logic_DimmableLight(FAN);
  128. analogWrite(3, mOutput(FAN+1));
  129.  
  130. }
  131.  
  132. FAST_510ms() {
  133. Logic_Current(CURRENT);
  134. Logic_Power(WATTS);
  135. }
  136.  
  137. // Here we handle here the communication with Android, commands and notification
  138. FAST_PeerComms();
  139.  
  140. }
  141.  
  142. EXECUTESLOW()
  143. {
  144. UPDATESLOW();
  145.  
  146. SLOW_10s() {
  147. //Serial.print("Requesting temperatures...");
  148. sensors.requestTemperatures(); // Send the command to get temperatures
  149. //Serial.println("DONE");
  150. //Serial.print("Temperature for Device Ujung is: ");
  151. // float temperature = sensors.getTempCByIndex(0);
  152. float temperature = sensors.getTempC(Ujung);
  153. //Serial.println (temperature);
  154. Souliss_ImportAnalog(memory_map, TEMPERATURE, &temperature);
  155.  
  156. // float temperature1 = sensors.getTempCByIndex(1);
  157. //Serial.print("Temperature for Device Dekat is: ");
  158. float temperature1 = sensors.getTempC(Deket);
  159. //Serial.println (temperature1);
  160. Souliss_ImportAnalog(memory_map, TEMPERATURE1, &temperature1);
  161.  
  162. // The timer handle timed-on states
  163. Timer_LED_Strip(LEDCONTROL);
  164.  
  165. RawValue = analogRead(analogIn);
  166. Voltage = (RawValue / 1023.0) * 5000; // Gets you mV
  167. VDC = ((RawValue / 1023.0) * 5000/2)/100; // Gets you mV
  168. Amps = ((Voltage - ACSoffset) / mVperAmp);
  169. float watt = VDC * Amps;
  170. // Serial.print("\t mV = "); // shows the voltage measured
  171. // Serial.print(VDC); // the '3' after voltage allows you to display 3 digits after decimal point
  172. // Serial.print("\t Amps = "); // shows the voltage measured
  173. // Serial.println(Amps); // the '3' after voltage allows you to display 3 digits after decimal point
  174. // delay(2500);
  175. Souliss_ImportAnalog(memory_map, CURRENT, &Amps);
  176. Souliss_ImportAnalog(memory_map, WATTS, &watt);
  177. }
  178. }
  179.  
  180. START_PeerJoin(); //tell gateway that i am exist
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement