Пример #1
0
void loop() {
    static int disconnected;
    if (millis() - lastCheck > 3000) {
        
        if (networkStatus()) {
            digitalWrite(RED_LED,LED_OFF);
            disconnected=0;
        } else {
            digitalWrite(RED_LED,LED_ON);
            if (++disconnected > 10) {
                char tempssid[40];
                strcpy(tempssid,networkSSID);
                digitalWrite(RED_LED,networkJoin(tempssid)?LED_OFF:LED_ON);
            }
        }
        digitalWrite(GRN_LED,SerialUSB.isConnected()?HIGH:LOW);
        lastCheck=millis();
    }
        
    while (device.gotLine()) {
        handleDeviceInput();
    }
    while (console.gotLine()) {
        digitalWrite(GRN_LED,LOW);
        handleConsoleInput();
        digitalWrite(GRN_LED,HIGH);
    }
    
    Watchdog_Reset();

}
Пример #2
0
int networkStatus() {
    int retval=0;
    if (!hasWifiModule) return retval;
    radio.discard();
	radio.printf("\r\n");
	radio.discard();
	radio.printf("\r\nAT+WSTATUS\r\n");
	while (!radio.gotLine()); // and some timeout.
	while (!(retval=isErrorOrOK(radio.line()))){
        if ((radio.line()[0]=='N') && (radio.line()[1] == 'O') && (radio.line()[2] == 'T')) {
            //if (strncmp("NOT ASSOCIATED",radio.line(),10) ==0){ //wtf????
            //console.debug("NOT ASSOCIATED\r\n");
            return(0);
        } else {
            //            console.debug("%s",radio.line());
        }
		while (!radio.gotLine()) {
			// and some timeout!!!
            ;	
		}
	}
	//console.puts(radio.line());
	return(retval>=1);
	
}
Пример #3
0
int networkJoin(char *ssid) {
    
    int retval=0;
    if (!hasWifiModule) return retval;
    strcpy(networkSSID, ssid);
    console.debug("joining: %s\r\n",networkSSID);
    Watchdog_Reset();
	radio.discard();
	radio.printf("\r\nATE0\r\nAT+WA=%s\r\n",ssid);
	while (!radio.gotLine()); // and some timeout.
	while (!(retval=isErrorOrOK(radio.line()))){
		//console.puts(radio.line());
		while (!radio.gotLine()) { // timeout?
			;	
		}		
	}
    Watchdog_Reset();
	radio.printf("\r\nAT+ndhcp=1\r\n");
    delay(1000); // this should be an idle.
	while (!radio.gotLine()); // and some timeout.
	while (!(isErrorOrOK(radio.line()))){
		console.debug("%s",radio.line());
		while (!radio.gotLine()) {
			;	
		}		
	}
    //console.debug("join %s (%d)\r\n",networkSSID,retval=networkStatus());
	return(networkStatus());	
}
Пример #4
0
int networkJoin(char *ssid) {

    int retval=0;

    strcpy(networkSSID, ssid);

	radio.discard();
	radio.printf("\r\nATE0\r\nAT+WA=%s\r\n",ssid);
	while (!radio.gotLine()); // and some timeout.
	while (!(retval=isErrorOrOK(radio.line()))){
		//console.puts(radio.line());
		while (!radio.gotLine()) { // timeout?
			;	
		}		
	}
	radio.printf("\r\nAT+ndhcp=1\r\n");
    delay(1000); // this should be an idle.
	while (!radio.gotLine()); // and some timeout.
	while (!(isErrorOrOK(radio.line()))){
		console.printf("DBG: %s",radio.line());
		while (!radio.gotLine()) {
			;	
		}		
	}
    //console.printf("\r\nDBG: join %s (%d)\r\n",networkSSID,retval=networkStatus());
	return(networkStatus());	
}
void loop() {
    static int disconnected;
	console.printf("HELLO?\r\n");
    if (millis() - lastCheck > 3000) {
        digitalWrite(RED_LED,LED_OFF);
        if ((! hasWifiModule) || networkStatus()) {
            digitalWrite(RED_LED,LED_OFF);
            disconnected=0;
        } else {
            digitalWrite(RED_LED,LED_ON);
            if (++disconnected > 10) {
                char tempssid[40];
                strcpy(tempssid,networkSSID);
                digitalWrite(RED_LED,networkJoin(tempssid)?LED_OFF:LED_ON);
            }
        }
        digitalWrite(GRN_LED,SerialUSB.isConnected()?HIGH:LOW);
        lastCheck=millis();
    }
        
    
    while (console.gotLine()) {
        digitalWrite(GRN_LED,LOW);
        handleConsoleInput();
        digitalWrite(GRN_LED,HIGH);
    }
    clearStowedValues();
    Watchdog_Reset();

}
Пример #6
0
/*------------------------------------------------------------setupRadioModule()
 * 
 *----------------------------------------------------------------------------*/
void setupRadioModule(void) {
    long int mark=millis();
    console.printf("DBG: >>SetupRadioModule()\r\n");
	radio.printf("\r\n");
	radio.discard();
    //radio.printf("ATE0\r\nAT&K0\r\nAT&R0\r\nAT+WD\r\nAT+NCLOSEALL\r\n");
    radio.printf("ATE0\r\nATH\r\n");
	while (!radio.gotLine()); 
	while ((millis()-mark<1000)&&(!(hasWifiModule=isErrorOrOK(radio.line())))){
		//console.debug("%s\r\n",radio.line());
		while ((millis()-mark<1000)&&!radio.gotLine()) {
			;	
		}		
	}
    radio.discard();
    console.printf("DBG: >>SetupRadioModule()\r\n");
}
Пример #7
0
int networkSetPassword(char *pwd) {
    int retval=0;	
    //    strncpy(networkPassword, pwd, MAX_NETWORK_PASSWORD_LENGTH-1);
    strcpy(networkPassword, pwd);
	radio.discard();
	radio.printf("\r\nAT+WWPA=%s\r\n",pwd);
	while (!radio.gotLine()); // and some timeout.
	while (!(retval=isErrorOrOK(radio.line()))){
		//console.puts(radio.line());
		while (!radio.gotLine()) {
			;	
		}
		
	}
    //console.debug("Password should be: %s (%d)\r\n",networkPassword,retval);
	return(retval>=1);
	
}
/*------------------------------------------------------------setupRadioModule()
 * 
 *----------------------------------------------------------------------------*/
void setupRadioModule(void) {
    long int mark=millis();
	radio.printf("\r\n");
	radio.discard();
    //radio.printf("ATE0\r\nAT&K0\r\nAT&R0\r\nAT+WD\r\nAT+NCLOSEALL\r\n");
    radio.printf("ATE0\r\nATH\r\n");
	while (!radio.gotLine()); 
	while ((millis()-mark<1000)&&(!(hasWifiModule=isErrorOrOK(radio.line())))){
		console.printf("DBG: %s\r\n",radio.line());
		while (!radio.gotLine()) {
			;	
		}		
	}
    radio.discard();
    networkSetPassword(networkPassword);
    Watchdog_Reset();
    networkJoin(networkSSID);

}
time_t radioDate() {
    time_t retTime=0L;
    if (!hasWifiModule) return retTime;
	radio.discard();
	radio.printf("\r\nAT+GETTIME=?\r\n");
	while (!radio.gotLine()); 
	while (!(isErrorOrOK(radio.line()))){
                    //012345678901234567890q123456789012
		if (!strncmp("Current Time in msec since epoch=",radio.line(),32)) {
            console.printf("DBG: epoch as a string=%s\r\n",radio.line()+33);
            retTime=(time_t)(atoll(radio.line()+33)/1000L);
            console.printf("DBG: Epoch=%lu \r\n",retTime);
        }
		while (!radio.gotLine());	
	}
	
	radio.discard();
	return(retTime);
	
}
Пример #10
0
bool setRadioDate(char * datebuffer) { 
    bool retval;
    radio.printf("AT+SETTIME=%s\r\n",datebuffer);
    while (!(retval=isErrorOrOK(radio.line()))){
        
        while (!radio.gotLine()) {
            ;	
        }		
    }
    delay(100);
    radioDate();
    
}
Пример #11
0
/*-----------------------------------------------------------------pachubeDate()
 * pull date from pachube server
 *----------------------------------------------------------------------------*/
int pachubeDate() {
    uint8_t n,index=0;
	int retval=0;
	char databuffer[36];
	char cid[4]="0";
    bool gotResponse=false;
    enum {CONNECT, ERROR, DISASSOCIATED} status=ERROR;//statii;
	uint8_t ch='0';
    
    if (!hasWifiModule) return retval;
    
	radio.discard();
	radio.printf("\r\n");
	radio.discard();
	radio.printf("\r\nAT+NCTCP=216.52.233.122,80\r\n");
	while (!radio.available()); // and some timeout.
    
	// ERROR
    // CONNECT <CID>
    // DISASSOCIATED
 	digitalWrite(ORN_LED,LOW);
    
	while (!gotResponse){ // this needs to catch errors.
		while (radio.available()) {
            databuffer[index]=ch=(char) radio.read();
            if (index<36) index++;
			//console.putChar(ch);
			if ((ch=='\n')||(ch=='\r')) {
                databuffer[index]='\0';
                if (strncmp("CONNECT", databuffer,6)==0) {
                    if (index>7) {
                        for (index=0, n=8; index<3 && databuffer[n];) {
                            cid[index++]=databuffer[n++]; // if isdigit????
                        }
                        cid[index]='\0';
                    }
                    status=CONNECT;
                    gotResponse=true;
                    console.debug("Connect CID=%s\r\n",cid);
                } else if (strncmp("ERROR", databuffer,5)==0) {
                    gotResponse=true;
                    status=ERROR;
                    
                } else if (strncmp("DISASSOCIATED",databuffer,index)==0) {
                    gotResponse=true;
                    status=DISASSOCIATED;
                }
                index=0;
            }
 		}
		
	}
	digitalWrite(ORN_LED,HIGH);
    
    
    if (status!=CONNECT) {
        console.debug("toPachube NOT CONNECTED! (%d) Exiting\r\n", status);
        radio.discard();
        return(0);
    }
	//snprintf(databuffer, 36, "%d,%s",key,value);
	radio.printf("\033S%dHEAD / HTTP/1.1\r\n",atoi(cid),pachubeFeed);
	radio.printf("User-Agent: SuspectDevices/Baco-matic5000 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.15\r\n");
	radio.printf("Host: api.pachube.com\r\nAccept: */*\r\nConnection: close\r\n");
	radio.printf("X-PachubeApiKey: %s\r\n",pachubeKey);
	radio.printf("Content-Length: 0\r\n\r\n\r\n\033E",strlen(databuffer),databuffer);
	//console.debug("Sending Head / request\r\n");
    
    //
    // sent data then wait for an OK
    //
	while (!radio.gotLine()); 
	while (!(retval=isErrorOrOK(radio.line()))){
		//console.debug("%s\r\n",radio.line());
		while (!radio.gotLine()) {
			;	
		}		
	}
    
	if (retval<1) {
        console.debug("NOT OK! %s\r\n",radio.line());
        radio.discard();
        return(0);
    }
    //
    // should get an escaped stream followed by a disconnect
    //
	index=0;
    gotResponse=false;
	digitalWrite(ORN_LED,LOW);
    
    while (!radio.gotLine());
    sprintf(databuffer,"%c%c%c%c%cS%dHTTP",0x1b,0x4f,0x1b,0x4f,0x1b,atoi(cid));
    if(!strncmp(databuffer, radio.line(),strlen(databuffer)) ) {
        databuffer[0]=radio.line()[16];
        databuffer[1]=radio.line()[17];
        databuffer[2]=radio.line()[18];
        databuffer[3]='\0';
        console.debug("[%d] %s\r\n",retval=atoi(databuffer),radio.line()+7);
    }
    
    while (!radio.gotLine()); 
    if(!strncmp("Date:",radio.line(),5)) {
        //Date: Sun, 03 Jun 2012 01:22:34 GMT
        //AT+SETTIME=<dd/mm/yyyy>,<HH:MM:SS>
        parseDate(radio.line(),databuffer); 
        retval=setRadioDate(databuffer);
    }
    digitalWrite(ORN_LED,HIGH);
    
    return(retval);
}
int toTheEthers(int key, char * value) {
    uint8_t n,index=0;
	int retval=0;
	char databuffer[36];
	char cid[4]="0";
    bool gotResponse=false;
    enum {CONNECT, ERROR, DISASSOCIATED} status=ERROR;//statii;
	uint8_t ch='0';

    if (!hasWifiModule) return retval;

	radio.discard();
	radio.printf("\r\n");
	radio.discard();
	radio.printf("\r\nAT+NCTCP=198.202.31.182,80\r\n");
	while (!radio.available()); // and some timeout.
    
	// ERROR
    // CONNECT <CID>
    // DISASSOCIATED
 	digitalWrite(ORN_LED,LOW);
    
	while (!gotResponse){ // this needs to catch errors.
		while (radio.available()) {
            databuffer[index]=ch=(char) radio.read();
            if (index<36) index++;
			//console.putChar(ch);
			if ((ch=='\n')||(ch=='\r')) {
                databuffer[index]='\0';
                if (strncmp("CONNECT", databuffer,6)==0) {
                    if (index>7) {
                        for (index=0, n=8; index<3 && databuffer[n];) {
                            cid[index++]=databuffer[n++]; // if isdigit????
                        }
                        cid[index]='\0';
                    }
                    status=CONNECT;
                    gotResponse=true;
                    //console.printf("DBG: Connect CID=%s\r\n",cid);
                } else if (strncmp("ERROR", databuffer,5)==0) {
                    gotResponse=true;
                    status=ERROR;
                    
                } else if (strncmp("NOT ASSOCIATED",databuffer,13)==0) {
                    gotResponse=true;
                    status=DISASSOCIATED;
                } else if (strncmp("DISASSOCIATED",databuffer,13)==0) {
                        gotResponse=true;
                        status=DISASSOCIATED;
                }
                index=0;
            }
 		}
		
	}
	digitalWrite(ORN_LED,HIGH);
    
    if (status!=CONNECT) {
        console.printf("DBG: toPachube() NOT CONNECTED! (%d) Exiting\r\n", status);
        radio.discard();
       return(0);
     }
	//snprintf(databuffer, 36, "%d,%s",key,value);
	
    radio.printf("\033S%dGET /art2013/swls/&state=%s HTTP/1.1\r\n",atoi(cid),value?"ON" : "OFF");
	//radio.printf("\033S%dPUT /art2013/swls/%s.csv HTTP/1.1\r\n",atoi(cid));
	radio.printf("User-Agent: SuspectDevices/Baco-matic5000 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.15\r\n");
	radio.printf("Host: suspectdevices.com\r\nAccept: */*\r\nConnection: close\r\n");
	radio.printf("X-PachubeApiKey: %s\r\n\r\n",pachubeKey);
	//radio.printf("Content-Length: %d\r\n\r\n%s\r\n\033E",strlen(databuffer),databuffer);
	//console.printf("DBG: Sending to feed %s: (%d)%s \r\n",pachubeFeed,strlen(databuffer),databuffer);
    
    //
    // sent data, wait for an OK
    //
	while (!radio.gotLine()); 
	while (!(retval=isErrorOrOK(radio.line()))){
		//console.printf("DBG: %s\r\n",radio.line());
		while (!radio.gotLine()) {
			;	
		}		
	}
    
	if (retval<1) {
        console.printf("DBG: NOT OK! %s\r\n",radio.line());
        radio.discard();
        return(0);
    }
    //
    // should get an escaped stream followed by a disconnect
    //
	index=0;
    gotResponse=false;
	digitalWrite(ORN_LED,LOW);
    
    while (!radio.gotLine());
    sprintf(databuffer,"%c%c%c%c%cS%dHTTP",0x1b,0x4f,0x1b,0x4f,0x1b,atoi(cid)); // the double escapeing I dont get.
    if(!strncmp(databuffer, radio.line(),strlen(databuffer)) ) {
        databuffer[0]=radio.line()[16];
        databuffer[1]=radio.line()[17];
        databuffer[2]=radio.line()[18];
        databuffer[3]='\0';
        //console.printf("DBG:[%d] %s\r\n",retval=atoi(databuffer),radio.line()+7);
    }
    while (!radio.gotLine()); 
    radio.discard();
    delay(500); // this is not so good.
	digitalWrite(ORN_LED,HIGH);
    return(retval);
}