Advertisement
Guest User

arduino test

a guest
Jun 9th, 2021
119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   This is an initial sketch to be used as a "blueprint" to create apps which can be used with IOTappstory.com infrastructure
  3.   Your code can be filled wherever it is marked.
  4.  
  5.   Copyright (c) [2016] [Andreas Spiess]
  6.  
  7.   Permission is hereby granted, free of charge, to any person obtaining a copy
  8.   of this software and associated documentation files (the "Software"), to deal
  9.   in the Software without restriction, including without limitation the rights
  10.   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.   copies of the Software, and to permit persons to whom the Software is
  12.   furnished to do so, subject to the following conditions:
  13.  
  14.   The above copyright notice and this permission notice shall be included in all
  15.   copies or substantial portions of the Software.
  16.  
  17.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.   SOFTWARE.
  24.  
  25.   virginSoilBasic V2.1.2
  26. */
  27.  
  28. #define COMPDATE __DATE__ __TIME__
  29. #define MODEBUTTON 0                                        // Button pin on the esp for selecting modes. D3 for the Wemos!
  30. #define EEPROM_SIZE 4096
  31.  
  32. #include <IOTAppStory.h>                                // IotAppStory.com library
  33. #include "BluetoothSerial.h"
  34. #include <EEPROM.h>
  35. #include "persistant.h"
  36.  
  37. IOTAppStory IAS(COMPDATE, MODEBUTTON);  // Initialize IotAppStory
  38. BluetoothSerial SerialBT;
  39. persistantVar persistantArray[25];
  40.  
  41. String command;
  42. String parameter1;
  43. String parameter2;
  44. String parameter3;
  45. String parameter4;
  46.  
  47. char array[100];
  48. char commandComp[100];
  49.  
  50. byte persistantArrayIndex;
  51.  
  52. void varsToCmndPrmtrs(char *usrPromptArray){
  53.     byte var = 0;
  54.     command = "";
  55.     parameter1 = "";
  56.     parameter2 = "";
  57.     parameter3 = "";
  58.     parameter4 = "";
  59.     char *currentWord = NULL;
  60.     currentWord = strtok(usrPromptArray, " ");
  61.     while(currentWord != NULL){
  62.         if (var == 0){
  63.             command = currentWord;
  64.         } else if (var == 1){
  65.             parameter1 = currentWord;
  66.         } else if (var == 2){
  67.             parameter2 = currentWord;
  68.         } else if (var == 3){
  69.             parameter3 = currentWord;
  70.         } else if (var == 4){
  71.             parameter4 = currentWord;
  72.         }
  73.         var++;
  74.         currentWord = strtok(NULL, " ");
  75.     }
  76. }
  77.  
  78. void divider(String usrString){
  79.     char usrArray[255];
  80.     usrString.toCharArray(usrArray, 255);
  81.     char *currentWord = NULL;
  82.     currentWord = strtok(usrArray, ";");
  83.     while(currentWord != NULL){
  84.         SerialBT.println(currentWord);
  85.         currentWord = strtok(NULL, ";");
  86.     }
  87. }
  88.  
  89. // ================================================ SETUP ================================================
  90. void setup() {
  91.   /*
  92.   IAS.onFirstBoot([]() {
  93.     IAS.eraseEEPROM('P');                   // Optional! What to do with EEPROM on First boot of the app? 'F' Fully erase | 'P' Partial erase
  94.   });
  95.   */
  96.  
  97.   IAS.begin();                          // Run IOTAppStory
  98.     IAS.setCallHome(true);
  99.   IAS.setCallHomeInterval(60);
  100.  
  101.   //-------- Your Setup starts from here ---------------
  102.     SerialBT.begin("ESP32");
  103.     Serial.println("ready to pair");
  104.     if (!EEPROM.begin(EEPROM_SIZE)){
  105.       Serial.println(F("Failed to initialise EEPROM!"));
  106.     }
  107.     persistantArrayIndex = EEPROM.read(0);
  108.     loadPersistantArrayFromEEPROM(persistantArray);
  109. }
  110.  
  111.  
  112.  
  113. // ================================================ LOOP =================================================
  114. void loop() {
  115.   IAS.loop();   // this routine handles the calling home functionality,
  116.                 // reaction of the MODEBUTTON pin. If short press (<4 sec): update of sketch, long press (>7 sec): Configuration
  117.                 // reconnecting WiFi when the connection is lost,
  118.                 // and setting the internal clock (ESP8266 for BearSSL)
  119.  
  120.  
  121.   //-------- Your Sketch starts from here ---------------
  122.     if (SerialBT.available()){
  123.         String usrInput = SerialBT.readString();
  124.         usrInput.replace("\n","");
  125.         usrInput.replace("\r","");
  126.         usrInput.toCharArray(array, 100);
  127.         varsToCmndPrmtrs(array);
  128.         command.toCharArray(commandComp, 100);
  129.         if (command == "save"){
  130.             persistantArrayIndex = savePersistantVar(parameter1, parameter2, parameter3, persistantArray, persistantArrayIndex);
  131.             savePersistantArrayToEEPROM(persistantArray, persistantArrayIndex);
  132.             SerialBT.println("successfully saved " + parameter1);
  133.         } else if (command == "load"){
  134.             if (parameter1 != ""){
  135.                 String toPrint = "";
  136.                 toPrint = getPersistantVar(parameter1, persistantArray, persistantArrayIndex);
  137.                 divider(toPrint);
  138.             } else {
  139.                 SerialBT.println("please, write the variable you want to load");
  140.             }
  141.         } else if (command == "edit"){
  142.             if (parameter1 != ""){
  143.                 editPersistantVar(parameter1, parameter2, parameter3, parameter4, persistantArray, persistantArrayIndex);
  144.                 savePersistantArrayToEEPROM(persistantArray, persistantArrayIndex);
  145.                 SerialBT.println("successfully edited " + parameter1);
  146.             } else {
  147.                 SerialBT.println("please, write the variable you want to edit");
  148.             }
  149.         } else if (command == "remove"){
  150.             persistantArrayIndex = removePersistantVar(parameter1, persistantArray, persistantArrayIndex);
  151.             SerialBT.println("successfully removed " + parameter1);
  152.         } else if (command == "help"){
  153.             if (parameter1 == "save"){
  154.                 SerialBT.println("write 3 parameters (name. type, value) to save them as new");
  155.             } else if (parameter1 == "load"){
  156.                 SerialBT.println("load by using name as a parameter");
  157.             } else if (parameter1 == "edit"){
  158.                 SerialBT.println("edit the variables using the name as the first parameter, and the new variables as the next 3 parameters");
  159.             } else if (parameter1 == "list"){
  160.                 SerialBT.println("get a list of currently available created variables");
  161.             } else if (parameter1 == "clear"){
  162.                 SerialBT.println("remove all created variables");
  163.             } else if (parameter1 == "get"){
  164.                 SerialBT.println("get only the value of a variable by name");
  165.             } else if (parameter1 == "remove"){
  166.                 SerialBT.println("to remove a variable by specifying its name");
  167.             } else {
  168.                 SerialBT.println("save");
  169.                 SerialBT.println("load");
  170.                 SerialBT.println("edit");
  171.                 SerialBT.println("help");
  172.                 SerialBT.println("list");
  173.                 SerialBT.println("clear");
  174.                 SerialBT.println("get");
  175.                 SerialBT.println("remove");
  176.                 SerialBT.println("to get help on a command, specify the command after help, ex: help edit");
  177.             }
  178.         } else if (command == "list"){
  179.             String toPrint = "";
  180.             toPrint = listPersistantVar(persistantArray, persistantArrayIndex);
  181.             divider(toPrint);
  182.         } else if (command == "clear"){
  183.             persistantArrayIndex = 0;
  184.             EEPROM.write(0, persistantArrayIndex);
  185.             EEPROM.commit();
  186.             SerialBT.println("all variables have been cleared");
  187.         } else if (command == "get"){
  188.             SerialBT.println("value of " + parameter1 + " is: " + getValueString(parameter1, persistantArray, persistantArrayIndex));
  189.         } else {
  190.             SerialBT.println("write help to get a list of available commands");
  191.         }
  192.     }
  193. }
Advertisement
RAW Paste Data Copied
Advertisement