GPIO control via GUI

I changed to flashing via usb for this reason, its all done in 20 seconds :slight_smile: and with the CLI its simple.

download the bin file by clicking the little cloud next to the filename (it saves and compiles for you)
open a cmd prompt and go to where you saved the file
put the core in DFU mode
type spark flash --usb firmware.bin

then if you make a change to the firmware in build, re-download overwriting the old file, put the core in DFU, push up in the cmd prompt and hit enter… takes 3 seconds to do then 20 seconds to flash… it takes longer for the core to re-connect to the cloud than to update it!

For some things like my doorbell i still have to do OTA flashes, but if the core is next to me USB is the way to go.

Hi @mala,

Sorry about that, the longer flash time might be our fault. We increased the chunk size during OTA, but we got reports of problems, so we reverted it back. I’m guessing you got the speed boost while it was deployed, and then are back at normal speeds now. We’re still working on speeding things up, and firmware compiled with the latest version should use the bigger chunk size.

Thanks,
David

thanks for the info @Dave, I’m not sure what was going on but it’s now back to flashing quicker again :smile:

I have now found what was causing the problem with the spark function that I’m using to write the delays to the core.
If I have a delay that is more then 999ms then the core will time out ( gives me a few flashes of the cyan led just before that- relevant ?)
I think I have enough room assigned in the array and buffer being used to store the sent string of delays, so what could be going on here ?

I have changed this line:

if(now>=1000)  // max time *update*

just in case, but as this is part of the playback and not the delay saving I would have thought it would make no difference to saving the string.

Has anyone got any ideas why my code (post 18 above) fails if I try to write a delay larger than 999ms ?
Cheers,
mala

Hi @mala

I think you will do better if you treat Spark.fuction()'s similar to interrupt handlers and use the Spark.function() to set a flag for some action to be taken the next time your loop() code runs. So that means having a flag like runPlayback that is set in the Spark.function and then in loop you would have if (runPlayback) { ... }. Having the Spark function block for a long time (1 sec) might be causing you trouble.

Hi @bko
Thanks for the reply, what you say makes sense to me in terms of tidying up my code and making things more effecient.
but could this be the cause of this particular problem ? when everything is fine with the core saving the sent number 999 but not a 1000 ?

I will though take your advice and rework my code as suggested and see if it helps !
Thanks
mala

1 Like

Hi @bko

so before I followed your suggestion I have been stripping out all the code until I got down to the last part causing the problem, this part is

char delayVariable [100]; //array to store sent string of delays 


void setup()
{
    Spark.function("setdelays", saveDelays);
 }

int saveDelays(String command)
{
    command.toCharArray(delayVariable,100);
    return 1;
}

the command sent from terminal is the 16 delays comma separated,as below(token&ID removed)

curl https://api.spark.io/v1/devices/#################/setdelays \
     -d access_token=############################ \
     -d "args=999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999"

the above works fine until I change one of those 999 to 1000, then it will just time out.
The only thing I can think of is it’s something to do with command.CharArray ???
I had changed the array size from 85 to 100 wondering if that would make any difference…but no.

It looks like you are crossing through 64 characters for your function call string and that is the limit. Your string with all 9's has 63 characters now, if you add any character, you will have too many.

1 Like

Doh!
Ok, guess I need to find another way to do this then…back to the drawingboard.
Any suggestions ? :wink:

can you pass the index of the array and the value in, instead of just passing 16 values? so you would pass ā€œ0,999ā€ and then ā€œ1,999ā€, etc. the down side is you would have to call the function 16 times, but you shouldn’t violate the 64 character limit.

alternatively, you could make it so that you could pass multiple ā€˜index,value’ pairs to cut down on the function calls. so set up the parser to take multiple key/value pairs and then you could call it with something like ā€œ0,999,3,995,8,500ā€ or whatever, only changing the values that you need to.

1 Like

This is probably silly, but I wanted to be sure a delay(1000) would work in a function call, so I wrote up this quick app which seems to work well:

double foop = 0;
void setup() {
    pinMode(D7, OUTPUT);
    Spark.variable("Foo", &foop, DOUBLE);
    Spark.function("test", test);
} 

void loop() 
{
  foop++;
  digitalWrite(D7, HIGH);   // Turn ON the LED
  delay(500);               // Wait for 1000mS = 1 second
  digitalWrite(D7, LOW);    // Turn OFF the LED
  delay(500);               // Wait for 1 second
}

int test(String command)
{
    Spark.publish("testFn", "Started");
    delay(1000);
    Spark.publish("testFn", "Ended");
    return 1;
}

Thanks,
David

1 Like

@Dave Thanks, I’m not using the delays like that but I do appreciate the test :smile:
I have done a test setting only 14 delayed outputs and found that using a delay larger than 999 in my code was not a problem.
The problem is as @bko pointed out, that I’m violating the 64 character limit in the sent string for the variable.

@eely22 thanks for the suggestions I will have a think and experiment with this.
To be honest this problem combined with the other topic i posted here https://community.spark.io/t/need-more-inputs-solved/6807/11
Is making me think I should just maybe pare the number of outputs down to 14 and keep things simple.
As I still have to figure out all the GUI side of stuff I want to build as well.

Cheers,
mala

2 Likes

Ok, well I drew up a quick example to do this, if you wanted to do it this way. From the UI, it should be pretty easy, just call the function for the delays that changed when they change. I briefly tested it out (on my SparkLE even!) and it is working:

#define MAX_NUM_DELAYS  8
int delays[MAX_NUM_DELAYS];

int saveDelays (String command);

void setup ()
{
    Spark.function ("setdelays", saveDelays);
}

int saveDelays (String command)
{
    char buffer[command.length()];
    command.toCharArray(buffer, command.length());
    
    int index, value;
    bool save = false;
    char* token = strtok (buffer,",");
    while (token != NULL)
    {
        if (save) {
            value = atoi(token);
            if (index < 0 ||index >= MAX_NUM_DELAYS) {
                //error stuff happens here, index is out of bounds
            } else {
                delays[index] = value;
            }
        } else {
            index = atoi(token);
        }
        save = !save;
        token = strtok (NULL, ",");
    }
}

void loop() {

}
2 Likes

Thanks! @eely22
I’m already several beers into a Friday night so will have a look at this tomorrow.
I am very interested in your SparkLE by the way and it’s a great name!

To reduce the size of the string, you could encode the values as HEX. They will then never be more than 3 digits (assuming values < 4096)

See String(val, HEX) for encoding (int to String), and strtoul(string.cstr(), 0, 16) to decode (String to int)

Thanks for suggestions, for now I’m going to try and get some form of GUI with 12 or so outputs working, once I have my head round that I think I will come back and look at increasing this number.
Will be having a play with this to start:
https://community.spark.io/t/tutorial-spark-variable-and-function-on-one-web-page/4181

But also as I have no experience of html,java, web type things!
I have rather a general question to do with the GUI I’d like to build, rather than have 12 to 16 individual buttons for each output with a field next to each one for the delay value I was wondering if there is any way I could make an interface similar to this: (User data field from a 3D program I use)

Each point on the spline representing an output and the Y axis being the delay, the user can click and drag a point up/down to set delay time(with exact time being shown in the little"Delay" field.
Now of course I’m light years away from being able to build this GUI, but I would like to know if anyone can think of a an app,plug in, method or whatever that may be able to create this kind of interface, that could be hosted on a web page.
This to me is a more interesting way to interact with the application I hope to build and if it could be done, then perhaps even preset ā€œspline waves/shapesā€ could applied to outputs.

Cheers,
mala