https://gist.github.com/dmsherazi/8540303
#define F_CPU 8000000U #include <avr/io.h> #include <avr/interrupt.h> #include <inttypes.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <util/delay.h> #include <avr/eeprom.h> const char slashr[]="\r"; #include <avr/wdt.h> const char ERRORS[]="ERROR"; #define RX_BUFFER_SIZE 128 //***watch buffer overflow!!! #define Relay1on PORTB|=0x20;pgmst=1; #define Relay1off PORTB&=~0x20;pgmst=0; #define Relay2on PORTB|=0x10;pgmst2=1; #define Relay2off PORTB&=~0x10;pgmst2=0; #define chan1_on PORTB|=0x08; #define chan1_off PORTB&=~0x08; #define chan2_on PORTB|=0x04; #define chan2_off PORTB&=~0x04; #define Brown_high PORTB|=0x40; #define Brown_low PORTB&=~0x40; #define Brown_dash_high PORTB|=0x80; #define Brown_dash_low PORTB&=~0x80; #define outputLow(port,pin) port &= ~(1<<pin) #define outputHigh(port,pin) port |= (1<<pin) #define toggleOutput(port,pin) port ^= (1<<pin) #define setInput(portdir,pin) portdir &= ~(1<<pin) #define setOutput(portdir,pin) portdir |= (1<<pin) #define digitalRead(pinPort,pin) ((pinPort>>pin)& 1)//Eg PINB,PB4 #define ONTIME_INDEX_HR 0 #define OFFTIME_INDEX_HR 2 #define ONTIME_INDEX_MIN 1 #define OFFTIME_INDEX_MIN 3 #define ENABLED_INDEX 4 ////////////////////////////////////////////////////////////////////////// struct time { uint8_t min,hours; } tempTime,Timer1OnTime,Timer2OnTime,Timer1OffTime,Timer2OffTime; uint8_t EEMEM Timer1En; uint8_t EEMEM MINON; uint8_t EEMEM HOURSON; uint8_t EEMEM MIN; uint8_t EEMEM HOURS; uint8_t EEMEM Timer2En; uint8_t EEMEM MINON2; uint8_t EEMEM HOURSON2; uint8_t EEMEM MIN2; uint8_t EEMEM HOURS2; uint8_t Timers_db[9][6]; volatile uint64_t millis_count; char buffer[RX_BUFFER_SIZE]; char RS485buffer[RX_BUFFER_SIZE]; int head,headRS485=0; int tail,tailRS485=0; char* fullData; char* fullDataRS485; // full Simcom responce data, filled by catchSimcomData char* parsedData; char* parsedDataRS485; // parsed responce char message[160]; char address[6]; char relay[15]; char command[160]; char restmess[40]; char tempmessage[40]; ////////////////////////////////////////////////////////////////////////// // Functions Intializations const char* const catchSimcomData(uint32_t = 180000, bool = false, uint16_t= 300, uint32_t = 60);//Default time out for +COPS is 180 seconds const char* const catchRS485Data(uint32_t = 180000, uint16_t= 300, uint32_t = 60);//Default time out for +COPS is 180 seconds //time out //quickCheck //datasize //baudDelay const char* const parseData(const char* const,const char*,const char*); const char* const parseSplit(const char* const,const char*,uint16_t); bool parseFind(const char* const, const char*); void sendATCommand(const char*); //char* const sendRecQuickATCommand(const char*); //Used to send/get reply for a OK reply const char* const sendRecATCommand(const char*); //Main function that gets Simcom reply const char* const sendRecATCommandParse(const char*, const char* _start, const char* _end); //Sends AT command parses reply, between _start _end const char* const sendRecATCommandSplit(const char*, const char* _delimiters, uint16_t _field);//Sends AT command splits data according to delimeter //Hardware void USART0_Transmit(unsigned char data) { while (!(UCSR0A & (1<<UDRE0))) ; UDR0 = data; } void USART1_Transmit(unsigned char data) { while (!(UCSR1A & (1<<UDRE1))) ; UDR1 = data; } void write(const char *data) { while (*data) USART0_Transmit(*data++); } void write1(const char *data) { outputHigh(PORTD,4); while (*data) USART1_Transmit(*data++); while (!(UCSR1A & (1<<UDRE1))) ; _delay_us(1900); outputLow(PORTD,4); } inline void store_char(unsigned char c) { int i = (head + 1) % RX_BUFFER_SIZE; if (i != tail) { buffer[head] = c; head = i; } } inline const char* const getFullData() { return fullData; } inline const char* const getParsedData() { return parsedData; } inline const char* const getFullDataRS485() { return fullDataRS485; } inline const char* const getParsedDataRS485() { return parsedDataRS485; } ISR(USART0_RX_vect) //triggers when recieve buffer is full { unsigned char c = UDR0; store_char(c); PORTB^=1; } void store_char_RS485(unsigned char c) { int g = (headRS485 + 1) % RX_BUFFER_SIZE; if (g != tailRS485) { RS485buffer[headRS485] = c; headRS485 = g; } } ISR(USART1_RX_vect) //triggers when recieve buffer is full { unsigned char y = UDR1; store_char_RS485(y); } char read() { // if the head isn't ahead of the tail, we don't have any characters if (head == tail) { return 0; } else { char c = buffer[tail]; tail = (tail + 1) % RX_BUFFER_SIZE; return c; } } char read_RS485() { // if the head isn't ahead of the tail, we don't have any characters if (headRS485 == tailRS485) { return 0; } else { char c = RS485buffer[tailRS485]; tailRS485 = (tailRS485 + 1) % RX_BUFFER_SIZE; return c; } } uint8_t available() { return (RX_BUFFER_SIZE + head - tail) % RX_BUFFER_SIZE; } uint8_t available_RS485() { return (RX_BUFFER_SIZE + headRS485 - tailRS485) % RX_BUFFER_SIZE; } void flush() { head =tail; } void flush_RS485() { headRS485 =tailRS485; } void USART0_Init( uint32_t baud ) { uint16_t baud_rate = (F_CPU / 8 / baud - 1) / 2; UBRR0H = (uint8_t)(baud_rate >> 8); UBRR0L = (uint8_t)baud_rate; UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0); UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); } void USART1_Init( uint32_t baud ) { uint16_t baud_rate = (F_CPU / 8 / baud - 1) / 2; UBRR1H = (uint8_t)(baud_rate >> 8); UBRR1L = (uint8_t)baud_rate; UCSR1B = (1<<RXEN1)|(1<<TXEN1)|(1<<RXCIE1); UCSR1C = (1<<UCSZ11) | (1<<UCSZ10); } void timer0() { TCCR0A = (1<<WGM01); //sets prescaler clkIO/256 ***THIS MIGHT CAUSE ISSUES SETS FOR ALL CLOCKS**!!!! TCCR0B = (1<<CS02); //sets interrupt enable for OCF0A (TIMER_COMPA_vect) TIMSK0 = (1<<OCIE0A); //sets TOP counter match A at 31 OCR0A = 31; } uint32_t millis(); ISR(TIMER0_COMPA_vect) { ++millis_count; //OCR0A = 10; //sets upper breakpoint A } void sendATCommand(const char* theMessageMelleMel) { write(theMessageMelleMel); write(slashr); wdt_reset(); } //Sends AT command and checks for OK or ERROR good for commands that reply OK or ERROR const char* const sendRecQuickATCommand(const char* theMessageMelleMel) { write(theMessageMelleMel); write(slashr); wdt_reset(); return (catchSimcomData(2000,true)); // 2000 milli time out with a quickCheck return data } //Sends AT command and stores reply with the catch data function const char* const sendRecATCommand(const char* theMessageMelleMel) { write(theMessageMelleMel); write(slashr); wdt_reset(); return (catchSimcomData()); //return data } //Sends AT command parses reply, makes string from the passed in strings const char* const sendRecATCommandParse(const char* theMessageMelleMel,const char* _start,const char* _end) { write(theMessageMelleMel); write(slashr); wdt_reset(); return (parseData(const_cast<char*>(catchSimcomData(3000,false)),_start,_end)); //return data } //Sends AT command splits data according to delimeter const char* const sendRecATCommandSplit(const char* theMessageMelleMel,const char* _delimiters, uint16_t _field) { write(theMessageMelleMel); write(slashr); wdt_reset(); return (parseSplit(const_cast<char*>(catchSimcomData(1500,false,30,100)),_delimiters,_field)); //return data } //////////////////////////////////////////////////////////////////////SEND FUNCS* //////////////////////////////////////////////////////////////////////PARSE FUNCS //finds the objectOfDesire string in theString if it is ! a NULL pointer bool parseFind(const char* const theString,const char* objectOfDesire) { if (!theString) return 0; // If we get a NULL pointer bail return strstr(theString,objectOfDesire); } const char* const catchSimcomData(uint32_t _timeout,bool quickCheck,uint16_t dataSize,uint32_t baudDelay) { //memory allocation issue with realloc writes over itself and malloc fragments the SRAM with //too many calls I tried for a happy medium but unless i know your program flow this is the //best I can do. bool badMessage = false; free(fullData); fullData=NULL; if (quickCheck) dataSize=20; //If it is just a quick check max size is "/n/nERROR/n/n/0" wdt_reset(); char* storeData = (char*) malloc(sizeof(char) * (dataSize)); if (storeData == NULL)return 0; //if we get bad memory // block wait for reply uint64_t startTimeGlobal = millis(); //9600/1000= 9.6bits per milli 6 milles ~6 bytes while (available() < 1) { if ((millis() - startTimeGlobal) > _timeout) { return 0; // smallest message "<CR><LF>OK<CR><LF>" // timed out bad message } } wdt_reset(); uint16_t dataPos=0; uint64_t startTimeBaud; //if no data in 60 milli (baudDelay default) sendings done while (1) { if ((dataPos+1) >= dataSize) { dataPos = 0; //don't overflow buffer overwrite to clear buffer badMessage = true; //after return NULL to mark bad message } storeData[dataPos++] = read(); //Read out serial register ////////////////////////////////////////////////////////////////////////// /////USART1_Transmit(storeData[dataPos-1]); startTimeBaud = millis(); while (available() < 1) { if ((millis() - startTimeBaud) > baudDelay) //if no more data is coming { storeData[dataPos]= '\0'; //Add NULL for a string //If it is a small amount of data //we can afford to cut it down. if (dataPos < 500) //500 seems to be the threshold with nothing else running { free(fullData); fullData=NULL; fullData = (char*) malloc(sizeof(char) * (dataPos+1)); if (fullData == NULL) { return 0; } memcpy(fullData,storeData,dataPos+1); free(storeData); storeData=NULL; } else fullData=storeData; //ELSE we just copy over the whole thing goto doneReceive; } } //No data in x time goto doneReceive, based on baud delay } doneReceive: wdt_reset(); //DebugPort-> // write1(fullData); // write1("/n"); if (badMessage) return 0; if (quickCheck) { if (parseFind(fullData, "\r\nOK\r\n")) return fullData; //return fullData else if (parseFind(fullData,ERRORS)) return 0; //return NULL else return 0; } return fullData; } const char* const catchRS485Data(uint32_t _timeout,uint16_t dataSize,uint32_t baudDelay) { //memory allocation issue with realloc writes over itself and malloc fragments the SRAM with //too many calls I tried for a happy medium but unless i know your program flow this is the //best I can do. bool badMessage = false; free(fullDataRS485); fullDataRS485=NULL; wdt_reset(); char* storeData = (char*) malloc(sizeof(char) * (dataSize)); if (storeData == NULL)return 0; //if we get bad memory // block wait for reply uint64_t startTimeGlobal = millis(); //9600/1000= 9.6bits per milli 6 milles ~6 bytes while (available_RS485() < 1) { if ((millis() - startTimeGlobal) > _timeout) { return 0; // smallest message "<CR><LF>OK<CR><LF>" // timed out bad message } } uint16_t dataPos=0; uint64_t startTimeBaud; //if no data in 60 milli (baudDelay default) sendings done while (1) { if ((dataPos+1) >= dataSize) { dataPos = 0; //don't overflow buffer overwrite to clear buffer badMessage = true; //after return NULL to mark bad message } storeData[dataPos++] = read_RS485(); //Read out serial register ////////////////////////////////////////////////////////////////////////// /////USART1_Transmit(storeData[dataPos-1]); startTimeBaud = millis(); wdt_reset(); while (available_RS485() < 1) { if ((millis() - startTimeBaud) > baudDelay) //if no more data is coming { storeData[dataPos]= '\0'; //Add NULL for a string //If it is a small amount of data //we can afford to cut it down. if (dataPos < 500) //500 seems to be the threshold with nothing else running { free(fullDataRS485); fullDataRS485=NULL; fullDataRS485 = (char*) malloc(sizeof(char) * (dataPos+1)); if (fullDataRS485 == NULL) { return 0; } memcpy(fullDataRS485,storeData,dataPos+1); free(storeData); storeData=NULL; } else fullDataRS485=storeData; //ELSE we just copy over the whole thing goto doneReceive; } } //No data in x time goto doneReceive, based on baud delay } doneReceive: wdt_reset(); if (badMessage) return 0; return fullData; } const char* const parseData(const char* const theString,const char* start,const char* end) { if (!theString) return 0; // If we get a NULL pointer bail size_t startSize = strlen(start); // get size of string char* startP = strstr (theString,start); // looks for string gives pointer including look if (!startP) return 0; // If we didn't find begining of string *MEM LEAK IF TAKEN OUT* startP+=startSize; // offset (gets rid of delim) char* endP = strstr ((startP),end); // starts at startP looks for END string if (!endP) return 0; // We didn't find end *MEM LEAK IF TAKEN OUT* free(parsedData); // if it has been allocated, Free the memory parsedData=NULL; parsedData = (char*) malloc((size_t)(sizeof(char)*(endP-startP)+1)); // get memory if (parsedData == NULL) { return 0; } // if we couldn't get mem uint16_t dataPos=0; while ( startP != endP ) // grab between starP and endP { parsedData[dataPos++]= *startP++; } parsedData[dataPos]= '\0'; // NULL to make a proper string return parsedData; // gives back what it can. parsData has class scope. } //Splits data according to delimeter //EG //(THE FIELDS 0 1 2 3 4 ) //input string: "the, quick: brown, blah: blah," //_delimiters string ",:" //field: 2 //returns pointer to "brown" const char* const parseSplit(const char* const theString,const char* delimiters,uint16_t field) { if (!theString) return 0; // if not a NULL pointer char * temp; // you have to use a local scope char array, char deadStr[strlen(theString)+1]; // or you get a bad memory leak. strcpy(deadStr, theString); // don't change this unless you are careful. temp = strtok (deadStr,delimiters); // split here first for (uint16_t i=0; i<field; ++i) { temp = strtok (NULL,delimiters); // use NULL to keep spliting same string } free(parsedData); // if it has been allocated Free the memory parsedData=NULL; if (!temp)return 0; // if we didn't find anything return NULL parsedData = (char*) malloc(sizeof(char)* (strlen(temp)+1)); // get mem +'\0' if (parsedData == NULL) return 0; // If we get a NULL pointer strcpy(parsedData,temp); // copy to parsedData, it has class scope return parsedData; } //uint64_t okLED;bool okFLAg; unsigned char USART0_Receive(void) { while (!(UCSR0A & (1<<RXC0))) ; return UDR0; } uint32_t millis() { uint32_t mill; uint8_t oldSREG = SREG; //remember last value of interupts // disable interrupts while we read timer0_millis or we might get an // inconsistent value (e.g. in the middle of a write to timer0_millis) cli(); mill = millis_count; SREG = oldSREG; // rewrite reg value to turn back on interrupts return mill; } void Timer1_Init(void) { // Timer1 settings: ~ 39999488 ticks (4999936 us / 4999.936 ms / 4.999936 sec) TCCR1B = (1<<CS12) | (1<<CS10); // CLK/1024 TIMSK1 = (1 << TOIE1); // Timer/Counter1 Overflow Interrupt Enable TCNT1H = 103; TCNT1L = 106; } bool RS485FLAG=false; void checkRS485() { if (read_RS485()=='*') { wdt_reset(); catchRS485Data(1800,180,50); wdt_reset(); strcpy(message,parseData(fullDataRS485,"*","!!")); if (strcmp(message,"")) { strncpy(address,parseData(message,"#",":"),sizeof(address)/sizeof(address[0])); strncpy(relay,parseData(message,":",","),sizeof(relay)/sizeof(relay[0])); strncpy(command,parseData(message,",","-"),sizeof(command)/sizeof(command[0])); strncpy(restmess,parseData(message,"-",":"),sizeof(restmess)/sizeof(restmess[0])); RS485FLAG=true; } } } char bAdress[]="12"; char onOffTimeString[30]; void send_back_time() { strcpy(tempmessage,"\nTime: "); strcat(tempmessage,"["); char buff[21]; long unsigned x; x = millis() / 1000 ; long unsigned seconds = x % 60; x /= 60 ; long unsigned minutes = x % 60; x /= 60; long unsigned hours = x % 24 ; x /= 24 ; long unsigned days = x ; //sprintf(buff, "%06lu", millis()); //strcat(tempmessage,buff); strcat(tempmessage,""); sprintf(buff, "%lu", hours); strcat(tempmessage,buff); strcat(tempmessage,"."); sprintf(buff, "%lu", minutes); strcat(tempmessage,buff); strcat(tempmessage,"."); sprintf(buff, "%lu", seconds); strcat(tempmessage,buff); strcat(tempmessage,"]"); write1(tempmessage); } void timestringtoMillis() { bool err=0; Timer1OnTime.hours=( ( (onOffTimeString[0]-48) *10)+(onOffTimeString[1]-48)); if (Timer1OnTime.hours<0 || Timer1OnTime.hours>23) err=1; Timer1OnTime.min=( ( (onOffTimeString[3]-48) *10)+(onOffTimeString[4]-48)); if (Timer1OnTime.min<0 || Timer1OnTime.min>59) err=1; char buff[25]; Timer1OffTime.min=( ( (onOffTimeString[6]-48) *10)+(onOffTimeString[7]-48)); if (Timer1OffTime.min<0 || Timer1OffTime.min>59) err=1; uint32_t millis_count_temp; if (Timer1OffTime.min>32) { millis_count_temp=16000+(Timer1OffTime.min*1000)+(Timer1OnTime.min *60000)+(Timer1OnTime.hours *3600000 ); } else millis_count_temp=(Timer1OffTime.min*1000)+(Timer1OnTime.min *60000)+(Timer1OnTime.hours *3600000 ); cli(); millis_count=millis_count_temp; sei(); //send_back_time(); } uint64_t last_timer_check=0; uint64_t last_time_check=0; uint64_t last_onTime=0; uint64_t last_update_time=0; uint64_t last_check_time; uint8_t relayPos[]={6,7,5,4}; #define Debug int main(void) { // MCUSR &= ~(1 << WDRF); // wdt_disable(); MCUCR=(1<<JTD); MCUCR=(1<<JTD); wdt_enable(WDTO_8S); wdt_reset(); outputHigh(DDRD,4); DDRD=0xFA; DDRB=255; outputLow(PORTD,4); //REC on RS485 USART0_Init(4800); USART1_Init(38400); wdt_reset(); _delay_ms(500); //write1("\nStarted long string here boss"); sei(); timer0(); PORTB&=~3; _delay_ms(5000); write1("**#00:getData,"); write1(bAdress); write1("-:!!"); PORTB|=3; PORTD|=0b01100000; int getData=0; last_update_time=last_check_time=millis(); while (1) { exitloc: wdt_reset(); if (millis()%3000<100) { PORTB= PORTB & ~0x01; } else { PORTB|=1; } if (millis()-last_update_time>60000) { PORTD=0; last_update_time=millis(); PORTB &=0b11111101; write1("**#00:getData,"); write1(bAdress); write1("-:!!"); wdt_reset(); PORTD|=0b01100000; PORTB|=3; } if (millis()-last_check_time>20000) { write1("\r\n\r\n"); send_back_time(); last_check_time=millis(); last_timer_check=millis(); wdt_reset(); long unsigned x; x = millis() / 1000 ; long unsigned seconds = x % 60; x /= 60 ; long unsigned minutes = x % 60; x /= 60; long unsigned hours = x % 24 ; x /= 24 ; long unsigned days = x ; char buff[13]; tempTime.hours=hours; tempTime.min=minutes; //bool ondone,offdone=false; wdt_reset(); char tempStr[4]; for (int k=0;k<4;k++) { int x=k; itoa(x+1,tempStr,10); //write1(tempStr); if (Timers_db[k][ENABLED_INDEX]=='E') { write1("\r\n"); write1(tempStr); write1(" Enab "); if (tempTime.hours==Timers_db[k][ONTIME_INDEX_HR]) { write1(" ON HR "); if (tempTime.min==Timers_db[k][ONTIME_INDEX_MIN]) { write1(" ON MIN "); outputHigh(PORTB,relayPos[k]); //ondone=true; } } if (tempTime.hours==Timers_db[k][OFFTIME_INDEX_HR]) { write1(" OFF HR "); if (tempTime.min==Timers_db[k][OFFTIME_INDEX_MIN]) { write1(" OFF MIN "); outputLow(PORTB,relayPos[k]); //offdone=true; } } } // else // { // write1("\r\n"); // write1(tempStr); // write1("is disabled on hours is ");itoa(Timers_db[k][ONTIME_INDEX_HR],tempStr,10); // write1(tempStr); // } // write1(":\r\n index is :"); // USART1_Transmit(Timers_db[k][ENABLED_INDEX]); // write1(":\r\n"); } wdt_reset(); //if(ondone) last_onTime=millis(); //if(offdone) last_offTime=millis(); } int TimerNumber=0; checkRS485(); if (RS485FLAG==true) { wdt_reset(); RS485FLAG=false; if (!(strcmp(address,bAdress))) // Address matches board address { if (!(strcmp(relay,"setTime"))) { if (parseFind(command,"cT=")) { if (!parseData(command,"cT=",";;")) ; else { strncpy(onOffTimeString,parsedData,30); timestringtoMillis(); } } } else if (!(strncmp(relay,"setTimer",8))) { uint8_t TimerNumber=relay[8]-48; //write1("\r\nset timer rec"); if (parseFind(command,"cT=")) { if (parseData(command,"cT=",";;")) { strncpy(onOffTimeString,parsedData,30); timestringtoMillis(); } } if (parseFind(command,"En=1")) { wdt_reset(); if (!parseData(command,"En=1","//")) { goto exitloc; } else { //write1("\r\nenabled rec"); strncpy(onOffTimeString,parsedData,30); // write1("\r\r\r\n rce"); // write1(onOffTimeString); bool err=0; Timer1OnTime.hours=( ( (onOffTimeString[0]-48) *10)+(onOffTimeString[1]-48)); if (Timer1OnTime.hours<0 || Timer1OnTime.hours>23) err=1; Timer1OnTime.min=( ( (onOffTimeString[3]-48) *10)+(onOffTimeString[4]-48)); if (Timer1OnTime.min<0 || Timer1OnTime.min>59) err=1; Timer1OffTime.hours=( ( (onOffTimeString[6]-48) *10)+(onOffTimeString[7]-48)); if (Timer1OffTime.hours<0 || Timer1OffTime.hours>23) err=1; Timer1OffTime.min=( ( (onOffTimeString[9]-48) *10)+(onOffTimeString[10]-48)); if (Timer1OffTime.min<0 || Timer1OffTime.min>59) err=1; if (err==1) { goto exitloc; } else { // write1("\r\nno error rec"); Timers_db[TimerNumber][ENABLED_INDEX]='E'; Timers_db[TimerNumber][ONTIME_INDEX_HR]=Timer1OnTime.hours; Timers_db[TimerNumber][ONTIME_INDEX_MIN]=Timer1OnTime.min; Timers_db[TimerNumber][OFFTIME_INDEX_HR]=Timer1OffTime.hours; Timers_db[TimerNumber][OFFTIME_INDEX_MIN]=Timer1OffTime.min; } } } else if (parseFind(command,"En=0")) { Timers_db[TimerNumber][ENABLED_INDEX]='D'; } } else if (!(strcmp(relay,"haveData"))) { //uint8_t TimerNumber=relay[8]-48; //write1("\r\nhave data rec"); if (parseFind(command,"cT=")) { if (parseData(command,"cT=",";;")) { strncpy(onOffTimeString,parsedData,30); timestringtoMillis(); } } char starting[15]; char ending[15]; char disabledString[7]; strcpy(starting,"1En=1"); strcpy(ending,"one//"); strcpy(disabledString,"1En=0"); if (parseFind(command,starting)) { wdt_reset(); if (parseData(command,starting,ending)) { strncpy(onOffTimeString,parsedData,30); //write1("\r\nfound in one "); // write1(onOffTimeString); TimerNumber=0; bool err=0; Timer1OnTime.hours=( ( (onOffTimeString[0]-48) *10)+(onOffTimeString[1]-48)); if (Timer1OnTime.hours<0 || Timer1OnTime.hours>23) err=1; Timer1OnTime.min=( ( (onOffTimeString[3]-48) *10)+(onOffTimeString[4]-48)); if (Timer1OnTime.min<0 || Timer1OnTime.min>59) err=1; Timer1OffTime.hours=( ( (onOffTimeString[6]-48) *10)+(onOffTimeString[7]-48)); if (Timer1OffTime.hours<0 || Timer1OffTime.hours>23) err=1; Timer1OffTime.min=( ( (onOffTimeString[9]-48) *10)+(onOffTimeString[10]-48)); if (Timer1OffTime.min<0 || Timer1OffTime.min>59) err=1; if (err==1) { // write1("\r\r\r\n ERRORRRR"); // //goto exitloc2; } else { // write1("\r\r\r\n enabled written with E"); Timers_db[0][ENABLED_INDEX]='E'; Timers_db[0][ONTIME_INDEX_HR]=Timer1OnTime.hours; Timers_db[0][ONTIME_INDEX_MIN]=Timer1OnTime.min; Timers_db[0][OFFTIME_INDEX_HR]=Timer1OffTime.hours; Timers_db[0][OFFTIME_INDEX_MIN]=Timer1OffTime.min; //if (Timers_db[0][ENABLED_INDEX]=='E') // write1("\r\n one Enabled is "); } } // else write1("\r\r\nnot found in one "); } else if (parseFind(command,disabledString)) { //write1("\r\r\nfound disabled"); Timers_db[0][ENABLED_INDEX]='D'; } strcpy(starting,"2En=1"); strcpy(ending,"two//"); strcpy(disabledString,"2En=0"); wdt_reset(); if (parseFind(command,starting)) { wdt_reset(); if (parseData(command,starting,ending)) { strncpy(onOffTimeString,parsedData,30); // write1("\r\nfound in two "); // write1(onOffTimeString); TimerNumber=1; bool err=0; Timer1OnTime.hours=( ( (onOffTimeString[0]-48) *10)+(onOffTimeString[1]-48)); if (Timer1OnTime.hours<0 || Timer1OnTime.hours>23) err=1; Timer1OnTime.min=( ( (onOffTimeString[3]-48) *10)+(onOffTimeString[4]-48)); if (Timer1OnTime.min<0 || Timer1OnTime.min>59) err=1; Timer1OffTime.hours=( ( (onOffTimeString[6]-48) *10)+(onOffTimeString[7]-48)); if (Timer1OffTime.hours<0 || Timer1OffTime.hours>23) err=1; Timer1OffTime.min=( ( (onOffTimeString[9]-48) *10)+(onOffTimeString[10]-48)); if (Timer1OffTime.min<0 || Timer1OffTime.min>59) err=1; if (err==1) { //write1("\r\r\r\n ERRORRRR"); //goto exitloc2; } else { // write1("\r\r\r\n enabled written with E"); Timers_db[TimerNumber][ENABLED_INDEX]='E'; Timers_db[TimerNumber][ONTIME_INDEX_HR]=Timer1OnTime.hours; Timers_db[TimerNumber][ONTIME_INDEX_MIN]=Timer1OnTime.min; Timers_db[TimerNumber][OFFTIME_INDEX_HR]=Timer1OffTime.hours; Timers_db[TimerNumber][OFFTIME_INDEX_MIN]=Timer1OffTime.min; // if (Timers_db[0][ENABLED_INDEX]=='E') // write1("\r\n 2 Enabled is "); } } // else write1("\r\r\nnot found in 2 "); } else if (parseFind(command,disabledString)) { // write1("\r\r\nfound disabled"); Timers_db[TimerNumber][ENABLED_INDEX]='D'; } strcpy(starting,"3En=1"); strcpy(ending,"three//"); strcpy(disabledString,"3En=0"); wdt_reset(); if (parseFind(command,starting)) { wdt_reset(); if (parseData(command,starting,ending)) { strncpy(onOffTimeString,parsedData,30); // write1("\r\nfound in three "); // write1(onOffTimeString); TimerNumber=2; bool err=0; Timer1OnTime.hours=( ( (onOffTimeString[0]-48) *10)+(onOffTimeString[1]-48)); if (Timer1OnTime.hours<0 || Timer1OnTime.hours>23) err=1; Timer1OnTime.min=( ( (onOffTimeString[3]-48) *10)+(onOffTimeString[4]-48)); if (Timer1OnTime.min<0 || Timer1OnTime.min>59) err=1; Timer1OffTime.hours=( ( (onOffTimeString[6]-48) *10)+(onOffTimeString[7]-48)); if (Timer1OffTime.hours<0 || Timer1OffTime.hours>23) err=1; Timer1OffTime.min=( ( (onOffTimeString[9]-48) *10)+(onOffTimeString[10]-48)); if (Timer1OffTime.min<0 || Timer1OffTime.min>59) err=1; if (err==1) { // write1("\r\r\r\n ERRORRRR"); //goto exitloc2; } else { // write1("\r\r\r\n enabled written with E"); Timers_db[TimerNumber][ENABLED_INDEX]='E'; Timers_db[TimerNumber][ONTIME_INDEX_HR]=Timer1OnTime.hours; Timers_db[TimerNumber][ONTIME_INDEX_MIN]=Timer1OnTime.min; Timers_db[TimerNumber][OFFTIME_INDEX_HR]=Timer1OffTime.hours; Timers_db[TimerNumber][OFFTIME_INDEX_MIN]=Timer1OffTime.min; //if (Timers_db[0][ENABLED_INDEX]=='E') // write1("\r\n 3 Enabled is "); } } // else //write1("\r\r\nnot found in three "); } else if (parseFind(command,disabledString)) { // write1("\r\r\nfound disabled"); Timers_db[TimerNumber][ENABLED_INDEX]='D'; } strcpy(starting,"4En=1"); strcpy(ending,"four//"); strcpy(disabledString,"4En=0"); wdt_reset(); if (parseFind(command,"4En=1")) { wdt_reset(); if (parseData(command,"4En=1","four")) { strncpy(onOffTimeString,parsedData,30); // write1("\r\nfound in four "); // write1(onOffTimeString); TimerNumber=3; bool err=0; Timer1OnTime.hours=( ( (onOffTimeString[0]-48) *10)+(onOffTimeString[1]-48)); if (Timer1OnTime.hours<0 || Timer1OnTime.hours>23) err=1; Timer1OnTime.min=( ( (onOffTimeString[3]-48) *10)+(onOffTimeString[4]-48)); if (Timer1OnTime.min<0 || Timer1OnTime.min>59) err=1; Timer1OffTime.hours=( ( (onOffTimeString[6]-48) *10)+(onOffTimeString[7]-48)); if (Timer1OffTime.hours<0 || Timer1OffTime.hours>23) err=1; Timer1OffTime.min=( ( (onOffTimeString[9]-48) *10)+(onOffTimeString[10]-48)); if (Timer1OffTime.min<0 || Timer1OffTime.min>59) err=1; if (err==1) { // write1("\r\r\r\n ERRORRRR"); //goto exitloc2; } else { // write1("\r\r\r\n enabled written with E"); Timers_db[TimerNumber][ENABLED_INDEX]='E'; Timers_db[TimerNumber][ONTIME_INDEX_HR]=Timer1OnTime.hours; Timers_db[TimerNumber][ONTIME_INDEX_MIN]=Timer1OnTime.min; Timers_db[TimerNumber][OFFTIME_INDEX_HR]=Timer1OffTime.hours; Timers_db[TimerNumber][OFFTIME_INDEX_MIN]=Timer1OffTime.min; // if (Timers_db[0][ENABLED_INDEX]=='E') // write1("\r\n four Enabled is "); } } //else{ // write1("\r\r\nnot found in four "); // write1(command);} } else if (parseFind(command,disabledString)) { // write1("\r\r\nfound disabled"); Timers_db[TimerNumber][ENABLED_INDEX]='D'; } } else if (!(strcmp(relay,"G1"))) { if (!(strncmp(command,"ON",2))) { chan1_on _delay_ms(1000); chan1_off //write1("\r\n R1"); } else if (!(strncmp(command,"ST",2))) { uint8_t tempPortB=PORTB; if (tempPortB &= (1<<PB3)) { write1("**#00:ON:!!"); } else write1("**#00:OFF:!!"); } } else if (!(strcmp(relay,"D2"))) { if (!(strncmp(command,"ON",2))) { chan2_on wdt_reset(); _delay_ms(2000); chan2_off write1("\r\n R1"); } else if (!(strncmp(command,"ST",2))) { uint8_t tempPortB=PORTB; if (tempPortB &= (1<<PB2)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } } else if (!(strcmp(relay,"R1"))) { if (!(strncmp(command,"ON",2))) { outputHigh(PORTB,6); _delay_ms(50); uint8_t tempPortB=PORTB; if (tempPortB &= (1<<PB6)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } else if (!(strncmp(command,"OF",2))) { outputLow(PORTB,6); uint8_t tempPortB=PORTB; _delay_ms(50); if (tempPortB &= (1<<PB6)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } else if (!(strncmp(command,"ST",2))) { uint8_t tempPortB=PORTB; _delay_ms(50); if (tempPortB &= (1<<PB6)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } } else if (!(strcmp(relay,"R2"))) { if (!(strncmp(command,"ON",2))) { outputHigh(PORTB,7); uint8_t tempPortB=PORTB;_delay_ms(50); if (tempPortB &= (1<<PB7)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } else if (!(strncmp(command,"OF",2))) { outputLow(PORTB,7); uint8_t tempPortB=PORTB;_delay_ms(50); if (tempPortB &= (1<<PB7)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } else if (!(strncmp(command,"ST",2))) { uint8_t tempPortB=PORTB;_delay_ms(50); if (tempPortB &= (1<<PB7)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } } else if (!(strcmp(relay,"R3"))) { if (!(strncmp(command,"ON",2))) { outputHigh(PORTB,5); uint8_t tempPortB=PORTB;_delay_ms(50); if (tempPortB &= (1<<PB5)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } else if (!(strncmp(command,"OF",2))) { outputLow(PORTB,5); uint8_t tempPortB=PORTB;_delay_ms(50); if (tempPortB &= (1<<PB5)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } else if (!(strncmp(command,"ST",2))) { uint8_t tempPortB=PORTB;_delay_ms(50); if (tempPortB &= (1<<PB5)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } } else if (!(strcmp(relay,"R4"))) { if (!(strncmp(command,"ON",2))) { outputHigh(PORTB,4); uint8_t tempPortB=PORTB;_delay_ms(50); if (tempPortB &= (1<<PB4)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } else if (!(strncmp(command,"OF",2))) { outputLow(PORTB,4); uint8_t tempPortB=PORTB;_delay_ms(50); if (tempPortB &= (1<<PB4)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } else if (!(strncmp(command,"ST",2))) { uint8_t tempPortB=PORTB;_delay_ms(50); if (tempPortB &= (1<<PB4)) write1("**#00:ON:!!"); else write1("**#00:OFF:!!"); } } } } } }