Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This is an initial sketch to be used as a "blueprint" to create apps which can be used with IOTappstory.com infrastructure
- Your code can be filled wherever it is marked.
- Copyright (c) [2016] [Andreas Spiess]
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- virginSoilBasic V2.1.2
- */
- #define COMPDATE __DATE__ __TIME__
- #define MODEBUTTON 0 // Button pin on the esp for selecting modes. D3 for the Wemos!
- #define EEPROM_SIZE 4096
- #include <IOTAppStory.h> // IotAppStory.com library
- #include "BluetoothSerial.h"
- #include <EEPROM.h>
- #include "persistant.h"
- IOTAppStory IAS(COMPDATE, MODEBUTTON); // Initialize IotAppStory
- BluetoothSerial SerialBT;
- persistantVar persistantArray[25];
- String command;
- String parameter1;
- String parameter2;
- String parameter3;
- String parameter4;
- char array[100];
- char commandComp[100];
- byte persistantArrayIndex;
- void varsToCmndPrmtrs(char *usrPromptArray){
- byte var = 0;
- command = "";
- parameter1 = "";
- parameter2 = "";
- parameter3 = "";
- parameter4 = "";
- char *currentWord = NULL;
- currentWord = strtok(usrPromptArray, " ");
- while(currentWord != NULL){
- if (var == 0){
- command = currentWord;
- } else if (var == 1){
- parameter1 = currentWord;
- } else if (var == 2){
- parameter2 = currentWord;
- } else if (var == 3){
- parameter3 = currentWord;
- } else if (var == 4){
- parameter4 = currentWord;
- }
- var++;
- currentWord = strtok(NULL, " ");
- }
- }
- void divider(String usrString){
- char usrArray[255];
- usrString.toCharArray(usrArray, 255);
- char *currentWord = NULL;
- currentWord = strtok(usrArray, ";");
- while(currentWord != NULL){
- SerialBT.println(currentWord);
- currentWord = strtok(NULL, ";");
- }
- }
- // ================================================ SETUP ================================================
- void setup() {
- /*
- IAS.onFirstBoot([]() {
- IAS.eraseEEPROM('P'); // Optional! What to do with EEPROM on First boot of the app? 'F' Fully erase | 'P' Partial erase
- });
- */
- IAS.begin(); // Run IOTAppStory
- IAS.setCallHome(true);
- IAS.setCallHomeInterval(60);
- //-------- Your Setup starts from here ---------------
- SerialBT.begin("ESP32");
- Serial.println("ready to pair");
- if (!EEPROM.begin(EEPROM_SIZE)){
- Serial.println(F("Failed to initialise EEPROM!"));
- }
- persistantArrayIndex = EEPROM.read(0);
- loadPersistantArrayFromEEPROM(persistantArray);
- }
- // ================================================ LOOP =================================================
- void loop() {
- IAS.loop(); // this routine handles the calling home functionality,
- // reaction of the MODEBUTTON pin. If short press (<4 sec): update of sketch, long press (>7 sec): Configuration
- // reconnecting WiFi when the connection is lost,
- // and setting the internal clock (ESP8266 for BearSSL)
- //-------- Your Sketch starts from here ---------------
- if (SerialBT.available()){
- String usrInput = SerialBT.readString();
- usrInput.replace("\n","");
- usrInput.replace("\r","");
- usrInput.toCharArray(array, 100);
- varsToCmndPrmtrs(array);
- command.toCharArray(commandComp, 100);
- if (command == "save"){
- persistantArrayIndex = savePersistantVar(parameter1, parameter2, parameter3, persistantArray, persistantArrayIndex);
- savePersistantArrayToEEPROM(persistantArray, persistantArrayIndex);
- SerialBT.println("successfully saved " + parameter1);
- } else if (command == "load"){
- if (parameter1 != ""){
- String toPrint = "";
- toPrint = getPersistantVar(parameter1, persistantArray, persistantArrayIndex);
- divider(toPrint);
- } else {
- SerialBT.println("please, write the variable you want to load");
- }
- } else if (command == "edit"){
- if (parameter1 != ""){
- editPersistantVar(parameter1, parameter2, parameter3, parameter4, persistantArray, persistantArrayIndex);
- savePersistantArrayToEEPROM(persistantArray, persistantArrayIndex);
- SerialBT.println("successfully edited " + parameter1);
- } else {
- SerialBT.println("please, write the variable you want to edit");
- }
- } else if (command == "remove"){
- persistantArrayIndex = removePersistantVar(parameter1, persistantArray, persistantArrayIndex);
- SerialBT.println("successfully removed " + parameter1);
- } else if (command == "help"){
- if (parameter1 == "save"){
- SerialBT.println("write 3 parameters (name. type, value) to save them as new");
- } else if (parameter1 == "load"){
- SerialBT.println("load by using name as a parameter");
- } else if (parameter1 == "edit"){
- SerialBT.println("edit the variables using the name as the first parameter, and the new variables as the next 3 parameters");
- } else if (parameter1 == "list"){
- SerialBT.println("get a list of currently available created variables");
- } else if (parameter1 == "clear"){
- SerialBT.println("remove all created variables");
- } else if (parameter1 == "get"){
- SerialBT.println("get only the value of a variable by name");
- } else if (parameter1 == "remove"){
- SerialBT.println("to remove a variable by specifying its name");
- } else {
- SerialBT.println("save");
- SerialBT.println("load");
- SerialBT.println("edit");
- SerialBT.println("help");
- SerialBT.println("list");
- SerialBT.println("clear");
- SerialBT.println("get");
- SerialBT.println("remove");
- SerialBT.println("to get help on a command, specify the command after help, ex: help edit");
- }
- } else if (command == "list"){
- String toPrint = "";
- toPrint = listPersistantVar(persistantArray, persistantArrayIndex);
- divider(toPrint);
- } else if (command == "clear"){
- persistantArrayIndex = 0;
- EEPROM.write(0, persistantArrayIndex);
- EEPROM.commit();
- SerialBT.println("all variables have been cleared");
- } else if (command == "get"){
- SerialBT.println("value of " + parameter1 + " is: " + getValueString(parameter1, persistantArray, persistantArrayIndex));
- } else {
- SerialBT.println("write help to get a list of available commands");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement