Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. #include <SPI.h> // needed for Arduino versions later than 0018
  2. #include <Ethernet_STM.h>
  3. #include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
  4. #include <LiquidCrystal_I2C.h>
  5. #include <EmonLib.h>
  6.  
  7. //write(0x00, 0x05, 128); // Software reset the W5500 chip
  8. //line 333 in w5500.h __SOCKET_REGISTER16(SnRX_WR, 0x002A) // RX Write Pointer (supported?)
  9.  
  10.  
  11. #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
  12. ;
  13. #else
  14. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  15. #endif
  16. IPAddress ip(192, 168, 0, 200);
  17. IPAddress mydns(192,168,0,1);
  18. unsigned int localPort = 8888; // local port to listen on
  19.  
  20.  
  21. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  22.  
  23.  
  24. EthernetUDP Udp;
  25. EnergyMonitor enMonitor0, enMonitor1;
  26. LiquidCrystal_I2C lcd(0x3F, 16, 2);
  27.  
  28. const int acVoltage0 = PA0;
  29. const int acVoltage1 = PA1;
  30. const int analogIn = PA3;
  31. const int currentPin = PA2;
  32. const int statusPin = PC13;
  33. const int relay1 = PB0;
  34. const int relay2 = PB1;
  35. const int relay3 = PB3;
  36. const int relay4 = PB4;
  37. const float r_divider = 0.048122;
  38. double V_AC0, V_AC1, Amps, voltageDC;
  39. bool auth = false;
  40. bool entering = false;
  41. bool pchange = false;
  42. char *key = "asdf";
  43.  
  44. void setup()
  45. {
  46. #if defined(WIZ550io_WITH_MACADDRESS)
  47. Ethernet.begin(ip);
  48. #else
  49. Ethernet.begin(mac,ip, mydns);
  50. #endif
  51. Udp.begin(localPort);
  52.  
  53. Serial.begin(9600);
  54. Serial.println("STARTED");
  55. disableDebugPorts();
  56. lcd.begin();
  57. lcd.backlight();
  58. pinMode(PB11, OUTPUT);
  59. pinMode(relay1, OUTPUT);
  60. pinMode(relay2, OUTPUT);
  61. pinMode(relay3, OUTPUT);
  62. pinMode(relay4, OUTPUT);
  63. pinMode(statusPin, OUTPUT);
  64. pinMode(analogIn, INPUT_ANALOG);
  65. pinMode(currentPin, INPUT_ANALOG);
  66. digitalWrite(relay4, LOW);
  67. enMonitor0.voltage(acVoltage0, 227, 1.7);
  68. enMonitor1.voltage(acVoltage1, 227, 1.7);
  69. lcd.clear();
  70. lcd.setCursor(0, 0);
  71. lcd.print(" -++=EDISON=++- ");
  72. Serial.println("END of setup");
  73. //nvic_sys_reset(); //Reset STM32F103C
  74. }
  75.  
  76.  
  77.  
  78. double measureVoltage_AC0()
  79. {
  80. double V_AC;
  81. enMonitor0.calcVI(200, 2000);
  82. V_AC = enMonitor0.Vrms * 1.51;
  83. return V_AC;
  84. }
  85.  
  86. double measureVoltage_AC1()
  87. {
  88. double V_AC;
  89. enMonitor1.calcVI(200, 2000);
  90. V_AC = enMonitor1.Vrms * 1.51;
  91. return V_AC;
  92. }
  93.  
  94. double measureVoltage_DC()
  95. {
  96. uint16_t adcin;
  97. double voltage;
  98. adcin = analogRead(analogIn);
  99. voltage = 3.3 / 4095 * adcin;
  100. voltage = voltage * 20;
  101. return voltage;
  102. }
  103.  
  104. double measureCurrent()
  105. {
  106. uint16_t RawValue = 0;
  107. double Voltage = 0;
  108. double Amps = 0;
  109. RawValue = analogRead(currentPin);
  110. Voltage = (RawValue / 4095.0) * 5000; // Gets you mV
  111. Amps = ((Voltage - 600) / 60);
  112. if (RawValue < 50) Amps = 0.0;
  113. return Amps;
  114. }
  115.  
  116. void loop()
  117. {
  118. Serial.println("loop");
  119. digitalWrite(PB11, LOW);
  120. delay(100);
  121. digitalWrite(PB11, HIGH);
  122. delay(100);
  123. main:
  124. Serial.println("mAIN");
  125. V_AC0 = measureVoltage_AC0();
  126. V_AC1 = measureVoltage_AC1();
  127. voltageDC = measureVoltage_DC();
  128. Amps = measureCurrent();
  129. Serial.println("lcd.printing");
  130. lcd.clear();
  131. lcd.setCursor(0, 0);
  132. lcd.print(V_AC0);
  133. lcd.setCursor(7, 0);
  134. lcd.print("V");
  135. lcd.setCursor(0, 1);
  136. lcd.print(V_AC1);
  137. lcd.setCursor(7, 1);
  138. lcd.print("V");
  139. lcd.setCursor(10, 0);
  140. lcd.print(voltageDC);
  141. lcd.setCursor(15, 0);
  142. lcd.print("V");
  143. lcd.setCursor(10, 1);
  144. lcd.print(Amps);
  145. lcd.setCursor(15, 1);
  146. lcd.print("A");
  147. Serial.println(" end lcd.printing");
  148. // if there's data available, read a packet
  149. int packetSize = Udp.parsePacket();
  150. Serial.println(packetSize);
  151. if (packetSize)
  152. {
  153. IPAddress remote = Udp.remoteIP();
  154. Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  155. if (auth == false && entering == false)
  156. {
  157. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  158. Udp.write("Please Enter KEY");
  159. Udp.endPacket();
  160. entering = true;
  161. }
  162. else if (!auth && entering)
  163. {
  164. if (strstr(packetBuffer, key))
  165. {
  166. auth = true;
  167. entering = false;
  168. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  169. Udp.write("Auth OK");
  170. Udp.endPacket();
  171. }
  172. }
  173. else if (auth)
  174. {
  175. if (strstr(packetBuffer, "R1=ON"))
  176. {
  177. digitalWrite(relay1, HIGH);
  178. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  179. Udp.write("K1 turned ON");
  180. Udp.endPacket();
  181. }
  182. else if (strstr(packetBuffer, "R1=OFF"))
  183. {
  184. digitalWrite(relay1, LOW);
  185. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  186. Udp.write("K1 turned OFF");
  187. Udp.endPacket();
  188. }
  189. else if (strstr(packetBuffer, "R2=ON"))
  190. {
  191. digitalWrite(relay2, HIGH);
  192. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  193. Udp.write("K2 turned ON");
  194. Udp.endPacket();
  195. }
  196. else if (strstr(packetBuffer, "R2=OFF"))
  197. {
  198. digitalWrite(relay2, LOW);
  199. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  200. Udp.write("K2 turned OFF");
  201. Udp.endPacket();
  202. }
  203. else if (strstr(packetBuffer, "R3=ON"))
  204. {
  205. digitalWrite(relay3, HIGH);
  206. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  207. Udp.write("K3 turned ON");
  208. Udp.endPacket();
  209. }
  210. else if (strstr(packetBuffer, "R3=OFF"))
  211. {
  212. digitalWrite(relay3, LOW);
  213. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  214. Udp.write("K3 turned OFF");
  215. Udp.endPacket();
  216. }
  217. else if (strstr(packetBuffer, "R4=ON"))
  218. {
  219. digitalWrite(relay4, HIGH);
  220. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  221. Udp.write("K4 turned ON");
  222. Udp.endPacket();
  223. }
  224. else if (strstr(packetBuffer, "R4=OFF"))
  225. {
  226. digitalWrite(relay4, LOW);
  227. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  228. Udp.write("K4 turned OFF");
  229. Udp.endPacket();
  230. }
  231. else if (strstr(packetBuffer, "MEASURES"))
  232. {
  233. char ac1[10], ac2[10], dc[10], amps[10];
  234. dtostrf(V_AC0, 4, 2, ac1);
  235. dtostrf(V_AC1, 4, 2, ac2);
  236. dtostrf(voltageDC, 4, 2, dc);
  237. dtostrf(Amps, 4, 2, amps);
  238. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  239. Udp.write("VAC1 = ");
  240. Udp.write(ac1);
  241. Udp.write(" ");
  242. Udp.write("VAC2 = ");
  243. Udp.write(ac2);
  244. Udp.write(" ");
  245. Udp.write("VDC = ");
  246. Udp.write(dc);
  247. Udp.write(" ");
  248. Udp.write("A = ");
  249. Udp.write(amps);
  250. Udp.endPacket();
  251. }
  252. else if (strstr(packetBuffer, "CHPASS"))
  253. {
  254. pchange = true;
  255. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  256. Udp.write("Enter new KEY");
  257. Udp.endPacket();
  258. while (pchange)
  259. {
  260. for (int i = 0; i < 24; i++)
  261. {
  262. packetBuffer[i] = 0;
  263. }
  264. int tmp = Udp.parsePacket();
  265. if (tmp)
  266. {
  267. Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  268. int len = 0;
  269. for (int i = 0; i < 24; i++)
  270. {
  271. if (packetBuffer[i] > 0)
  272. len++;
  273. }
  274. Serial.println(len);
  275. if (len != 4)
  276. {
  277. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  278. Udp.write("Only 4 digit");
  279. Udp.endPacket();
  280. }
  281. else
  282. {
  283. key = packetBuffer;
  284. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  285. Udp.write("New KEY is: ");
  286. Udp.write(key);
  287. Udp.endPacket();
  288. pchange = false;
  289. goto main;
  290. }
  291. }
  292. }
  293. }
  294. else if (strstr(packetBuffer, "RESTART"))
  295. {
  296. nvic_sys_reset();
  297. }
  298. else if (strstr(packetBuffer, "LOGOUT"))
  299. {
  300. auth = false;
  301. entering = false;
  302. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  303. Udp.write("Log out OK");
  304. Udp.endPacket();
  305. }
  306. for (int i = 0; i < 24; i++)
  307. {
  308. packetBuffer[i] = 0;
  309. }
  310. }
  311. }
  312. delay(10);
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement