// SSR AC load switching project using Mediatek LinkitONE by Shivankur Pilania #include #include #include #include int RELAY1 = 5; int RELAY2 = 6; int RELAY3 = 7; void setup() { Serial.begin(9600); pinMode(RELAY1, INPUT); pinMode(RELAY2, INPUT); pinMode(RELAY3, INPUT); Scheduler.startLoop(loop2); } void loop() { digitalRead(RELAY1); delay(1000); digitalWrite(RELAY2); delay(1000); digitalRead(RELAY3) delay(1000); } { Serial.begin(9600); { delay(1000); } PROGMEM const char *string_table[]= { header, command_line, var }; #define BUFFER_SIZE 980 static uint8_t buf[BUFFER_SIZE+1]; typedef struct program_internal { uint8_t output; uint16_t wait; uint8_t nextstate; } statetable; #define NUM_STATES 4 statetable states[NUM_STATES]; static uint16_t wait_timer; static uint8_t current_state; uint16_t r; char relay_string[3]; uint8_t RELAY[3]={RELAY1, RELAY2, RELAY3}; static byte relay_status; byte run_status; void setup() { uint8_t i; Serial.begin(9600); Serial.println("System Reset"); for (i=0; i<3; i++) pinMode(RELAY[i], OUTPUT); relay_status=0; if(EEPROM.read(0) != 0x3a) { Serial.println("EEPROM unitialized"); EEPROM.write(0, 0x5a); EEPROM.write(1, 0x01); #define NUM_DEFAULT_STATES 4 states[0].output = B00101010; // start polling which relay is on states[0].wait = 1; // delay time in minutes states[0].nextstate = 1; // next state states[1].output = B00010101; states[1].wait = 1; states[1].nextstate = 2; states[2].output = B00100000; states[2].wait = 1; states[2].nextstate = 3; states[3].output = B00000001; states[3].wait = 1; states[3].nextstate = 0; for(i = 0; i < sizeof(statetable) * NUM_DEFAULT_STATES; i++) { EEPROM.write(2 + i, ((uint8_t *) states)[i]); } } else { Serial.println("Reading EEPROM"); run_status = EEPROM.read(1); for(i = 0; i < sizeof(statetable) * NUM_STATES; i++) ((uint8_t *) states)[i] = EEPROM.read(2 + i); } wait_timer=states[0].wait; current_state=0; relay_status=states[current_state].output; } void loop(){ uint16_t plen, dat_p; int8_t cmd, i; static uint16_t elapsed_minutes; static unsigned long mscounter; static uint16_t seconds; { if ( millis() - mscounter >999) { mscounter=millis(); seconds++; relay_status= states[current_state].output; for (i=0; i<2; i++) { if ((lowByte(relay_status) & 1<=1) relay_status^=1<<--rrly; EEPROM.write(1, run_status); for (i=0; i<3; i++) { if ((lowByte(relay_status) & 1<NUM_STATES) st=0; plen=edit_program_page(buf,st); { { uint8_t found=0; uint8_t i=0; char *kp; kp=key; while(*str && *str!=' ' && found==0){ if (*str == *kp){ kp++; if (*kp == '\0'){ str++; kp=key; if (*str == '='){ found=1; } } }else{ kp=key; } str++; } if (found==1){ // copy the value to a buffer and terminate it with '\0' while(*str && *str!=' ' && *str!='&' && i 0x2f){ // is a ASCII number, return it //r=(*line_buffer-0x30); r = atoi(line_buffer); } } return r; } uint16_t print_program_page(uint8_t *buf, uint8_t pagenum) { uint8_t tempcount=0, count=0, rlycnt=0; uint8_t start=0, end=0; uint16_t plen; strcpy_P(line_buffer, (char*)pgm_read_word(&(string_table[0]))); while (line_buffer[tempcount]) { buf[TCP_CHECKSUM_L_P+3+plen]=line_buffer[tempcount++]; plen++; } plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("

Ethernet Relay Controller")); tempcount=0; strcpy_P(line_buffer, (char*)pgm_read_word(&(string_table[1]))); while (line_buffer[tempcount]) { buf[TCP_CHECKSUM_L_P+3+plen]=line_buffer[tempcount++]; plen++; } plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("

Current Program ")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("

")); plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("")); for (count=start; count", prognum, states[prognum].wait, states[prognum].nextstate, relay_string[0],relay_string[1],relay_string[2]); tempcount=0; while (line_buffer[tempcount]) { buf[TCP_CHECKSUM_L_P+3+plen]=line_buffer[tempcount++]; plen++; } } } { tempcount=0; while (line_buffer[tempcount]) { buf[TCP_CHECKSUM_L_P+3+plen]=line_buffer[tempcount++]; plen++; } Serial.print("plen="); Serial.println(plen); return(plen); }
State

#

Wait

(seconds)

Next

State

Outputs

1 2 3 4 5 6

%dOFFToggle", count+1, count+1); } else { sprintf(line_buffer, "
%dONToggle", count+1, count+1); } tempcount=0; while (line_buffer[tempcount]) { buf[TCP_CHECKSUM_L_P+3+plen]=line_buffer[tempcount++]; plen++; } } uint8_t tempcount=0, count=0, rlycnt=0; uint8_t start=0, end=0; uint16_t plen; tempcount=0; strcpy_P(line_buffer, (char*)pgm_read_word(&(string_table[0]))); // get header string while (line_buffer[tempcount]) { buf[TCP_CHECKSUM_L_P+3+plen]=line_buffer[tempcount++]; plen++; } sprintf (relay_string, "123456"); for (rlycnt=0; rlycnt<=6; rlycnt++) { if ((lowByte(states[prognum].output) & 1<%u%u%u%c%c%c%c%c%c