Dx pin --> reset to do a self reset

I would like to use an output pin to do a self reset.
That seems to not work as during power up the outputs go through a up/down cycle and do a reset immediately and also as soon as a digital pin is defined as OUTPUT it goes to 0 that then would trigger the reset…

In other words I like to connect e.g. D6 to reset. And if the chip’s WiFi looses connection I like to do a reset.

KW

actually this should work:

you set D6 to INPUT in setup()

then in loop set it to OUTPUT and set to LOW

then when you want to do a reset set D6 to HIGH.

not tested, but should work

KW

Have you seen this thread

http://community.spark.io/t/can-the-spark-core-trigger-the-reset-pin/2693

There you’ll find a way for a soft-reset without the need to sacrifice a GPIO, and an elaborate discussion about internal and external watchdog reset.

NVIC_SystemReset();

resets the spark in the loop(), thanks

That has helped a bit, but it still hangs i.e. no loop activity

KW

Since you don’t give any clue what your code is, one could only guess which of the many possible reasons triggers your problem.
And you didn’t mention anything about hanging before, so I think this would call for a new thread, that reflects your actual problem :wink:

BTW: Did you search the forum for possible reasons/solutions? There is a wealth of knowledge in these threads already.

Yes I have read ~ 200++ posts. especially in the web server, WiFi … sections.
All the logic works. It just hangs after some minutes with traffic, thought I could do a reset if there is not WiFi activity.

And since you ask, here the code. Any advice welcome. thx
KW

don’t know how to post code. just paste seems to create badly formatted text… here a link:
spark code

Hi @kw123

I fixed the markup in your post for code (which I see you have moved now, ok)–for future reference the markup is:

  ```cpp
  <code here>

It doesn’t look like I can download your code without a dropbox account–is it in your public folder?

Maybe a gist at github would be easier for folks.

ok here the code

/* Web_Parms_1.pde - very simple Webduino example of parameter passing and parsing */

/* can set pins to INPUT OUTPUT can read and write pins 
 *
 * how to use, examples:
 *  curl -d "SET=D1,INPUT"  http://192.168.x.y/parsed.html
 *  curl -d "SET=D1,OUTPUT"  http://192.168.x.y/parsed.html
 *  curl -d "READ=D1,1"  http://192.168.x.y/parsed.html
 *  curl -d "WRITE=D1,1"  http://192.168.x.y/parsed.html
*
 *  curl -d "SET=A1,INPUT"  http://192.168.x.y/parsed.html
 *  curl -d "SET=A1,OUTPUT"  http://192.168.x.y/parsed.html
 *  curl -d "READ=A1,1"  http://192.168.x.y/parsed.html
 *  curl -d "WRITE=A1,55"  http://192.168.x.y/parsed.html
 *
 *  curl -d "WRITE=A1,55&READ=A2,1&WRITE=D2,0"  http://192.168.x.y/parsed.html
 *           as many & as you like..
 
 * returns eg: 
 * {"1":{"cmd":"WRITE","type":"D,"pin":"D1","value":"0","status":"OK"},{"2":{"cmd":"READ","type":"A,"pin":"A1","value":"55","status":"OK"}}
 *
 * This invokes parsedCmd,  which displays the "raw" parameter string,
 * but also uses the "nexyURLparam" routine to parse out the individual
 * parameters, and display them.
 */


#define WEBDUINO_FAIL_MESSAGE "<h1>Request Failed</h1>"
//#include "avr/pgmspace.h" // new include
#include "WebServer/WebServer.h"

#define VERSION_STRING "0.1"

// ROM-based messages used by the application
// These are needed to avoid having the strings use up our limited
//    amount of RAM.

// not used anymore, should be deleted:
/*
P(Page_start) = "<html><head><title>Web_Parms_1 Version " VERSION_STRING "</title></head><body>\n";
P(Page_end) = "</body></html>";
P(Get_head) = "<h1>GET from ";
P(Post_head) = "<h1>POST to ";
P(Default_head) = "unidentified URL requested.</h1><br>\n";
P(Raw_head) = "raw.html requested.</h1><br>\n";
P(Parsed_head) = "parsed.html requested.</h1><br>\n";
P(Good_tail_begin) = "URL tail = '";
P(Bad_tail_begin) = "INCOMPLETE URL tail = '";
P(Tail_end) = "'<br>\n";
P(Parsed_tail_begin) = "URL parameters:<br>\n";
P(Parsed_item_separator) = " = '";
P(Params_end) = "End of parameters<br>\n";
P(Post_params_begin) = "Parameters sent by POST:<br>\n";
P(Line_break) = "<br>\n";
*/
P(Unknown_head) = "<h1>UNKNOWN request for ";


extern char* itoa(int a, char* buffer, unsigned char radix);

// variables used in code
    char    modeOUTPUT[8]   = "OUTPUT";
    char    modeINPUT[8]    = "INPUT";
    char    commandRead[8]  = "READ";
    char    commandWrite[8] = "WRITE"; 
    char    commandSet[8]   = "SET";
    bool    printLineDebug  = false;
    int     valRead         = 0;
    char    valReadChar[10] ;
    char    pinNum[3]       = "0";
    int     pinNumber       = 0;
    int     pinValueInt     = 0;
    char    pinName[3];
    char    pinValue[8];
    char    theString[100];
    char    theCountChar[8];
    char    failedCMD[16]  ;
    bool    isDigital       = false;
    char    isOK[3]          ="OK";
    int     loopCounter     = 0;
    char    test[10]        = "   ";
    int     resetCounter    = 0;
    char    buff1[10];

/* This creates an instance of the webserver.  By specifying a prefix
 * of "", all pages will be at the root of the server. */
#define PREFIX ""
WebServer webserver(PREFIX, 80);



#define NAMELEN 64
#define VALUELEN 64

void parsedCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete){
//    Serial.println("starting parse ");
    URLPARAM_RESULT rc;
    char name[NAMELEN];
    char value[VALUELEN];
    resetCounter=0;  // tell loop, we are still alive

    if (type != WebServer::POST ){
        server.printP(Unknown_head);
        return; 
    }


    int theCount        =0;    
    server.print("{");

///  loop over  & parameters    
    while (server.readPOSTparam(name, NAMELEN, value, VALUELEN)){
        theCount        +=1;
        strcpy(failedCMD,isOK);
        itoa(theCount,theCountChar,10);
        if (theCount >1)    strcpy(theString,",\"");
        else                strcpy(theString,"\"");
        if (printLineDebug) Serial.println(theCountChar);
        strcat(theString,theCountChar);
        strcat(theString,"\":{");
        char *theComma = strchr(value,',');
        if (theComma == NULL) {
            if (printLineDebug) Serial.println("bad input");
            strcat(theString,"\"badinput\":\"");
            strcat(theString,name);
            strcat(theString,"=");
            strcat(theString,value);
            strcat(theString,"\"}");
        }
        else {
            strcat(theString,"\"cmd\":\"");
            strcat(theString,name);
            strcat(theString,"\"");
            strcat(theString,",\"type\":\"");
            if (value[0] == 'D') {
                isDigital = true;
                strcat(theString,"D");
            }
            else {
                isDigital = false;
                strcat(theString,"A");
            }

            strncpy(pinNum,value+1,(int)(theComma-value-1));
            if (printLineDebug) Serial.println("pinName");
            if (printLineDebug) Serial.println(pinName);
            if (printLineDebug) Serial.println(pinNum);
            pinNumber = atoi(pinNum);
            if (pinNumber< 0 || pinNumber >7) {
                strcat(theString,",\"badinput\":\"");
                strcat(theString,value);
                strcat(theString,"\"}");
            }
            else{
                // get function = name   and value = second part after ","
                strncpy(pinName,value,2);
                strcpy(pinValue,theComma+1);
                pinValueInt = atoi(pinValue);
                strcat(theString,",\"pin\":\"");
                strcat(theString,pinName);
                strcat(theString,"\"");   
                
                
                // read from pins
                if (strcmp( name, commandRead ) == 0 ){
                    if (isDigital){
                        if (PIN_MAP[pinNumber].pin_mode == INPUT){
                            valRead = digitalRead(pinNumber) ;
                        }    
                        else{ 
                            strcpy(failedCMD,"NotReadMode");
                            valRead=-1;
                        }   
                    }
                    else{
                        if (PIN_MAP[pinNumber+10].pin_mode == INPUT){
                            valRead = analogRead(pinNumber+10);
                        }
                        else{
                            strcpy(failedCMD,"NotReadMode");
                            valRead=-1;
                        }
                    }   
                        
                    itoa(valRead,valReadChar,10);
                    Serial.println("valReadChar");
                    Serial.println(valReadChar);
                    strcat(theString,",\"value\":\"");
                    strcat(theString,valReadChar); 
                    strcat(theString,"\"");
                }
                
                
                // write to pins
                if (strcmp( name, commandWrite ) == 0 ){
                    if (pinValueInt<0)      pinValueInt=0;
                    if (isDigital) {
                        if (PIN_MAP[pinNumber].pin_mode == OUTPUT) {
                            if (pinValueInt>1)  pinValueInt=1;
                            digitalWrite(pinNumber, pinValueInt);
                        }
                        else strcpy(failedCMD,"NotWriteMode");
                    }
                    else{
                        if (PIN_MAP[pinNumber+10].pin_mode == OUTPUT) {
                            if (pinValueInt>255) pinValueInt=255;
                            analogWrite(pinNumber+10, pinValueInt);
                        }
                        else strcpy(failedCMD,"NotWriteMode");
                    }
                    itoa(pinValueInt,valReadChar,10);
                    strcat(theString,",\"value\":\"");
                    strcat(theString,valReadChar); 
                    strcat(theString,"\"");
                }
                
                // set pin functions
                if (strcmp( name, commandSet ) == 0 ){
                    strcat(theString,",\"value\":\"");
                    strcat(theString,pinValue); 
                    strcat(theString,"\"");
                    if  (strcmp(pinValue,modeINPUT)==0){
                        if (isDigital) pinMode(pinNumber, INPUT); 
                        else           pinMode(pinNumber+10, INPUT); 
                    }    
                    if  (strcmp(pinValue,modeOUTPUT)==0){
                        if (isDigital) pinMode(pinNumber, OUTPUT); 
                        else           pinMode(pinNumber+10, OUTPUT); 
                    }    
                }
                
                
            }
        }
        strcat(theString,",\"status\":\"");
        if ( strcmp( failedCMD, isOK ) != 0)   {
             strcat(theString,"error-");
             strcat(theString,failedCMD);
        }    
        else strcat(theString,"OK");
        strcat(theString,"\"}");
        Serial.println(theString);
        server.print(theString);
    }    
    server.print("}");
} 






void setup()
{
  webserver.addCommand("parsed.html", &parsedCmd);
    for (int i = 0; i < 8; i++)     pinMode(i, INPUT) ;
    for (int i = 10; i < 18; i++)   pinMode(i, INPUT) ;

  /* start the webserver */
    webserver.begin();
    Serial.begin(9600);
    Serial.println("starting");
    loopCounter =0;
}

void loop()
{
    char buff[64];
    int len = 64;

  /* process incoming connections one at a time forever */
//    Serial.println("loop");

    delay(1);
    webserver.processConnection(buff, &len);
    resetCounter +=1;
    if (resetCounter > 3000){  // this is about 18 seconds
        resetCounter =0;
        strcpy(theString,"reset: ");
        itoa( millis(), buff1,10);
        strcat(theString,buff1);
        strcpy(theString,"(mSec) after start");
        Serial.println(theString);
        delay (100);
        NVIC_SystemReset();  // this resets the system
    }

    loopCounter+=1;
    if ( loopCounter > 300) {  // log heatbeat into serial connecting for testing only
        strcpy(theString,"Loop ");
        itoa( millis(), buff1,10);
        strcat(theString,buff1);
        strcat(theString,"(mSec),  " );
        strcat(theString,", resetCount= " );
        itoa( resetCounter, buff1,10);
        strcat(theString,buff1);
        Serial.println(theString);
        loopCounter=0;
    }
    
}
>