Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <SPI.h>
  3. #include <WiFiNINA.h>
  4.  
  5. char ssid[] = "***"; // House -- Network SSID (name)
  6. char pass[] = "***"; // House -- SSID password (WPA2-PSK)
  7.  
  8. #define MAX_CLIENTS 5
  9. #define MAX_LINE_LEN 50
  10. char inputs[MAX_CLIENTS][MAX_LINE_LEN] = {0}; // Instantiate 2D array of
  11. client/received data
  12. int msg_array[] = {0,0};
  13. int net_status = WL_IDLE_STATUS; // The Wifi radio's status
  14. WiFiServer server(80); // Create server object on port of WiFi
  15. // server, which will be used to
  16. // respond to commands
  17.  
  18. WiFiClient *clients[MAX_CLIENTS] = {NULL}; // Create client objects
  19. // limited to size of
  20. // MAX_CLIENTS
  21.  
  22.  
  23. byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED}; // Arduino's physical MAC
  24. byte ip[] = {192,168,9,25}; // IP of this controller
  25. byte gateway[] = {192,168,0,1}; // Router address (internet)
  26. byte subnet[] = {255,255,0,0}; // Subnet mask
  27.  
  28. unsigned long wd_Dside, wd_Cside = millis(); // Gather current tstamp
  29. unsigned long wd_Delap, wd_Celap = 0; // Create an elapsed time
  30. //counter
  31.  
  32.  
  33. void setup(){
  34.  
  35. pinMode(4, OUTPUT); // pin selected to control D SIDE
  36. pinMode(5, OUTPUT); // pin selected to control C SIDE
  37.  
  38. //enable serial data print
  39. Serial.begin(9600);
  40. while (!Serial){
  41. Serial.println("No serial.");
  42. }
  43.  
  44. Serial.println("Serial.begin was initiated.");
  45.  
  46.  
  47. if (WiFi.status() == WL_NO_SHIELD) { // check for the WiFi module:
  48. Serial.println("Wi-Fi: Communication with NINA has failed!");
  49. // do nothing here
  50. while (true);
  51. }
  52.  
  53. String fv = WiFi.firmwareVersion();
  54. if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
  55. Serial.println("Please upgrade the firmware");
  56. }
  57.  
  58. while (net_status != WL_CONNECTED) {
  59. Serial.print("Attempting to connect to WPA SSID: ");
  60. Serial.println(ssid);
  61.  
  62. // Connect to WPA/WPA2 network:
  63. net_status = WiFi.begin(ssid, pass); // Attempt to join this SSID
  64.  
  65. // wait 5 seconds for connection:
  66. delay(5000);
  67.  
  68. listNetworks();
  69.  
  70. server.begin(); // Start Wi-Fi server
  71. Serial.print("Server started on ");
  72. Serial.println(WiFi.localIP());
  73. }
  74. }
  75.  
  76.  
  77. void loop() {
  78.  
  79. delay(5);
  80.  
  81. WiFiClient newClient = server.available(); // Begin listening for
  82. // client messages
  83. if (newClient) {
  84. Serial.println("New client discovered."); // Could use this as
  85. // an ACK/handshake on
  86. // client-side host
  87. for (int i=0; i<MAX_CLIENTS; i++) {
  88. if (clients[i] == NULL) {
  89. clients[i] = new WiFiClient(newClient);
  90. break;
  91. }
  92. }
  93. }
  94.  
  95. for (int i=0; i<MAX_CLIENTS; i++){ // Check client for data
  96. if (clients[i] != NULL && clients[i]->available()){
  97. // If the data isn't garbage and the data is available..
  98.  
  99. char newChar = clients[i]->read();
  100. // Read the data
  101.  
  102. if (newChar == 'x') {
  103. // Check for a termination character for separation
  104.  
  105. // Now parse with strtok in order to get parameterization
  106. char delimiters[] = ",";
  107. char* valIndex;
  108. valIndex = strtok(inputs[i], delimiters);
  109. for (int j=0; j < 2; j++){
  110. // Expand later to largest anticipated character count
  111. msg_array[j] = atoi(valIndex);
  112. Serial.print(msg_array[j]);
  113. valIndex = strtok(NULL, delimiters);
  114. }
  115.  
  116. switch(msg_array[0]) {
  117. // Determine type of message received (Character 0 parsed as
  118. // Message Type)
  119.  
  120. case '3':
  121. // Examine string for client cmds to turn sides on or off.
  122. if(msg_array[1] == '1')
  123. // Msg type 3, Instruction 1: Turn D side ON
  124. {
  125. digitalWrite(4, HIGH);
  126. // Set pin 4 high
  127. Serial.println("Dside started.");
  128. wd_Delap = 0;
  129. // Reset elapsed time watchdog time value for D side.
  130. wd_Dside = millis();
  131. // Last good heartbeat from client device on D side.
  132. }
  133. if(msg_array[1] == '2')
  134. // Msg type 3, Instruction 2: Turn D side OFF
  135. {
  136. digitalWrite(4, LOW);
  137. // Set pin 4 low
  138. Serial.println("Dside stopped");
  139. wd_Delap = 0;
  140. // Reset elapsed time watchdog time value for D side.
  141. wd_Dside = millis();
  142. // Last good heartbeat from client device on D side.
  143. }
  144. if(msg_array[1] == '3')
  145. // Msg type 3, Instruction 3: Turn C side ON
  146. {
  147. digitalWrite(5, HIGH);
  148. // Set pin 5 high
  149. Serial.println("C side started.");
  150. wd_Celap = 0;
  151. // Reset elapsed time watchdog time value for C side.
  152. wd_Cside = millis();
  153. // Last good heartbeat from client device on C side.
  154. }
  155. if(msg_array[1] == '4')
  156. // Msg type 3, Instruction 4: Turn C side OFF
  157. {
  158. digitalWrite(5, LOW);
  159. // Set pin 5 low
  160. Serial.println("C side stopped.");
  161. wd_Celap = 0;
  162. // Reset elapsed time watchdog time value for C side.
  163. wd_Cside = millis();
  164. // Last good heartbeat from client device on C side.
  165. }
  166. break;
  167.  
  168. case '6':
  169.  
  170. // Examine string for another message type TBD
  171. break;
  172.  
  173. default:
  174. Serial.println("Default break. Received crap.");
  175. break;
  176.  
  177. }
  178.  
  179. inputs[i][0] = '';
  180. // Clear out this array element for the next message
  181. // clients[i]->stop();
  182. // By default, kill client session, just wait for new TCP open
  183. // delete clients[i];
  184. // Remove client element's data
  185. // clients[i] = NULL;
  186. // Reset to NULL
  187.  
  188. }
  189.  
  190. else {
  191.  
  192. if (strlen(inputs[i]) > MAX_LINE_LEN) {
  193. // First check for size compliance
  194. Serial.println("Max line length exceeded from client.");
  195. inputs[i][0] = '0';
  196. // Clear out this array element for the next message
  197. }
  198. else {
  199. **strcat(inputs[i], newChar);**
  200. // Add character to the received string
  201. }
  202.  
  203. Serial.println();
  204. Serial.println(newChar);
  205. }
  206. }
  207.  
  208. // Update watchdog timers
  209.  
  210. wd_Delap += (millis()-wd_Dside);
  211. wd_Celap += (millis()-wd_Cside);
  212.  
  213. if (wd_Delap>=60000)
  214. {
  215. Serial.print("Elapsed time on watchdog, D side: ");
  216. Serial.println(wd_Delap);
  217. Serial.print("Last message received: ");
  218. Serial.println(wd_Dside);
  219. digitalWrite(4, LOW); // set pin 4 low
  220. Serial.println("D side didn't hear from a client in 60s;
  221. defaulting to OFF.");
  222. }
  223.  
  224. if (wd_Celap>=60000)
  225. {
  226. Serial.print("Elapsed time on watchdog, C side: ");
  227. Serial.println(wd_Celap);
  228. Serial.print("Last message recieved: ");
  229. Serial.println(wd_Cside);
  230. digitalWrite(5, LOW); // set pin 5 low
  231. Serial.println("C side didn't hear from a client in 60s;
  232. defaulting to OFF.");
  233. }
  234.  
  235. }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement