/* ===== pfod Command for Menu_1 ==== pfodApp msg {.} --> {,<+7>~Led Control`0~V7|A<+4>`0~Led is ~~Off\On~|B<+4>~Voltage Plot} */ // Using Arduino Nano 33 IoT via WiFi // You need to modify the WLAN_SSID, WLAN_PASS settings below // to match your network settings /* Code generated by pfodDesignerV3 V3.0.3773 */ /* (c)2014-2020 Forward Computing and Control Pty. Ltd. NSW Australia, www.forward.com.au This code is not warranted to be fit for any purpose. You may only use it at your own risk. This generated code may be freely used for both private and commercial use provided this copyright is maintained. */ #include #include #define DEBUG // Install SafeString library from the Arduino IDE Library Manager // SafeString tutorial at https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html #include "SafeString.h" // Download pfodWiFiGenericBufferedClient library from http://www.forward.com.au/pfod/pfodParserLibraries/index.html // pfodWiFiGenericBufferedClient.zip contains pfodWiFiGenericBufferedClient and pfodWiFiGenericUtils #include #include #include "millisDelay.h" // download the millisDelay library from https://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html pfodWiFiGenericBufferedClient bufferedClient; #define WLAN_SSID "myNetwork" // cannot be longer than 32 characters! #define WLAN_PASS "myPassword" const int portNo = 23; // What TCP port to listen on for connections. const char staticIP[] = ""; // set this the static IP you want, e.g. "10.1.1.200" or leave it as "" for DHCP. DHCP is not recommended. WiFiServer server(portNo); WiFiClient client; boolean alreadyConnected = false; // whether or not the client was connected previously const int Led_pin = 13; // name the output pin for 'Led is' millisDelay plotDataTimer; // plot data timer unsigned long PLOT_DATA_INTERVAL = 1000;// mS == 1 sec, edit this to change the plot data interval void setup() { pinMode(Led_pin, OUTPUT); // output for 'Led is' is initially LOW, digitalWrite(Led_pin, 0); // set output #ifdef DEBUG Serial.begin(115200); for (int i = 10; i > 0; i--) { Serial.print(i); Serial.print(' '); delay(500); } Serial.println(); #endif // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { #ifdef DEBUG Serial.println("Communication with WiFi module failed!"); #endif // don't continue while (true); } #ifdef DEBUG createSafeString(fv, 10); fv = WiFi.firmwareVersion(); Serial.println(fv); Serial.println(WIFI_FIRMWARE_LATEST_VERSION); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println(F("Please upgrade the firmware")); while (true); } #endif if (*staticIP != '\0') { IPAddress ip(pfodWiFiGenericUtils::ipStrToNum(staticIP)); IPAddress gateway(ip[0], ip[1], ip[2], 1); // set gatway to ... 1 #ifdef DEBUG Serial.print(F("Setting gateway/dns to: ")); Serial.println(gateway); #endif IPAddress subnet(255, 255, 255, 0); WiFi.config(ip, gateway, gateway, subnet); } WiFi.begin(WLAN_SSID, WLAN_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); #ifdef DEBUG Serial.print("."); #endif } #ifdef DEBUG Serial.println(); Serial.println(F("WiFi connected")); #endif // Start the server server.begin(); #ifdef DEBUG Serial.println(F("Server started")); #endif // Print the IP address #ifdef DEBUG Serial.println(WiFi.localIP()); #endif // initialize client client = server.available(); // evaluates to false if no connection plotDataTimer.start(PLOT_DATA_INTERVAL); // start plot timer // <<<<<<<<< Your extra setup code goes here } const size_t maxCmdLength = 5; // make SafeStrings at least large enough to hold longest cmd // Use SafeStrings for the commands as comparing two SafeStrings is generally faster as the lengths can be compared first. createSafeString(onCmdStr, maxCmdLength, "on"); createSafeString(offCmdStr, maxCmdLength, "off"); // input must be large enough to hold longest cmd + 1 delimiter createSafeString(input, maxCmdLength + 1); // to read input cmd + 1 delimiter createSafeString(token, maxCmdLength + 1); // for parsing capacity >= input.capacity() char delimiters[] = " .,\r\n"; // space dot comma CR NL are cmd delimiters millisDelay timeout; unsigned long TIMEOUT_MS = 300; // 0.3sec // the loop routine runs over and over again forever: void loop() { if (!client) { // see if a client is available client = server.available(); // evaluates to false if no connection } else { // have client if (!client.connected()) { if (alreadyConnected) { // client closed so clean up closeConnection(); } } else { // have connected client if (!alreadyConnected) { bufferedClient.connect(&client); // sets new io stream to read from and write to alreadyConnected = true; } if (input.read(bufferedClient)) { // read from Serial, returns true if at least one character was added to SafeString input timeout.start(TIMEOUT_MS); // restart a 0.3sec timer every time something is read } if (input.nextToken(token, delimiters)) { // process at most one token per loop does not return tokens longer than input.capacity() if (token == onCmdStr) { digitalWrite(Led_pin, 1); // set output bufferedClient.println("Led on"); } else if (token == offCmdStr) { digitalWrite(Led_pin, 0); // set output bufferedClient.println("Led off"); } else { // not a valid cmd ignore bufferedClient.println("Commands are on,off"); } } if (timeout.justFinished()) { // nothing received for 0.3secs, terminated last chars so token will be processed. input += delimiters[0]; // any delimiter will do } } // sendData(); } // <<<<<<<<<<< Your other loop() code goes here } void closeConnection() { // add any special code here to force connection to be dropped alreadyConnected = false; bufferedClient.stop(); // clears client reference client.stop(); // cleans up memory client = server.available(); // evaluates to false if no connection } void sendData() { if (plotDataTimer.justFinished()) { plotDataTimer.repeat(); // restart plot data timer, without drift // assign values to plot variables from your loop variables or read ADC inputs int plot_1_var = analogRead(A0); // read input to plot // send plot data in CSV format bufferedClient.print(millis());// time in milliseconds bufferedClient.print(','); bufferedClient.print(plot_1_var); bufferedClient.println(); // end of CSV data record } }