void loop() { uint8_t buf[VW_MAX_MESSAGE_LEN]; uint8_t buflen = VW_MAX_MESSAGE_LEN; // Si recibimos señal ejecutamos las acciones que deseemos if (vw_get_message(buf, &buflen)) { Serial.print("Recibimos un "); Serial.println(buf[0]); // Si recibimos un 1 es que hemos pulsado el botón de café if(buf[0] == '1') { // "Endendemos" el LED para mostrar que la señal se ha enviado digitalWrite(13,1); // Esperamos un segundo delay(1000); // Apagamos el LED digitalWrite(13,0); } // Si recibimos un 0 es que hemos pulsado el botón de té if(buf[0] == '0') { // "Endendemos" el LED para mostrar que la señal se ha enviado digitalWrite(13,1); // Esperamos un segundo delay(1000); // Apagamos el LED digitalWrite(13,0); } } }
void loop() { const char *msg = "hello"; uint8_t buf[VW_MAX_MESSAGE_LEN]; uint8_t buflen = VW_MAX_MESSAGE_LEN; // Wait for a message vw_wait_rx(); if (vw_get_message(buf, &buflen)) // Non-blocking { int i; const char *msg = "goodbye"; digitalWrite(13, true); // Flash a light to show received good message // Message with a good checksum received, dump it. Serial.print("Got: "); for (i = 0; i < buflen; i++) { Serial.print(buf[i], HEX); Serial.print(" "); } Serial.println(""); // Send a reply vw_send((uint8_t *)msg, strlen(msg)); digitalWrite(13, false); } }
////////////////////// // rx_fetch_pkt: read packet into buffer if available // // returns number of bytes transferred to pkt, 0 if no data available // byte rx_fetch_pkt(pkt_t *pkt) { // Does the radio have business for us? //if (!vw_have_message()) return 0; // "If a message is available (good checksum or not), copies up to *len octets to buf. // Returns true if there was a message and the checksum was good." byte length = RF_PACKET_SIZE; if (!vw_get_message((uint8_t *) pkt, &length)) { //sp("rx: bad chksum\r\n"); return 0; } // If the packet length is bigger than our buffer We Have A Situation. // Clip to our max packet size, silently discarding the excess. // if (length > RF_PACKET_SIZE) { // todo: log here length = RF_PACKET_SIZE; } rx_packet_count++; // got one? count it #if defined(RADIO_DEBUG) log_packet('R', pkt, length); #endif return length; }
void loop() { uint8_t msg[MSG_LEN]; uint8_t msglen = MSG_LEN; if ( vw_get_message( msg, &msglen ) ) { if ( msglen != MSG_LEN ) { // msglen has actuall number of bytes read. if less // than expected, we received either an incomplete message // or noise. make blinky error and ignore. digitalWrite( ledPin, HIGH ); delay( 100 ); digitalWrite( ledPin, LOW ); delay( 100 ); digitalWrite( ledPin, HIGH ); delay( 100 ); digitalWrite( ledPin, LOW ); digitalWrite( ledPin, state ); return; } // I went a little overboard in my message 'protocol'. don't // really need addr or message type, so ignore. uint8_t action = msg[ACTN_INDEX]; state = ( action == TURN_ON ? HIGH : LOW ); digitalWrite( relayPin, state ); digitalWrite( ledPin, state ); } }
void CommClass::update(bool clearLatest) { if (clearLatest) latestDataType = TYPE_NONE; uint8_t buf[VW_MAX_MESSAGE_LEN]; uint8_t buflen = VW_MAX_MESSAGE_LEN; if (vw_get_message(buf, &buflen)) // Non-blocking { //Authorize first if (buflen > sizeof(auth_t)) { auth_t authToken = *((auth_t*)buf); if (authToken == WIRELESS_AUTH) { latestDataType = (CommDataType)buf[sizeof(auth_t)]; switch (latestDataType) { case TYPE_COMMAND: memcpy(&latestData.command, buf, sizeof(CommCommand)); break; case TYPE_COMMAND8: memcpy(&latestData.command8, buf, sizeof(CommCommand8)); break; case TYPE_CONTROL: memcpy(&latestData.control, buf, sizeof(CommControl)); break; default: latestDataType = TYPE_NONE; } } } } }
void loop() { uint8_t buf[ VW_MAX_MESSAGE_LEN ]; uint8_t buflen = VW_MAX_MESSAGE_LEN; showTime(); if( vw_get_message( buf, &buflen )) // Non-blocking { // const char *fieldName = ""; buflen = buflen < ( VW_MAX_MESSAGE_LEN - 1) ? buflen : VW_MAX_MESSAGE_LEN - 1; buf[ buflen ] = '\0'; String msg(reinterpret_cast<char *>(buf)); String fieldValue = msg.substring(2); Serial.println( msg ); lcd.setCursor( 0, 1 ); if( msg.startsWith( "#T" )) { lcd.print( "Tmp: " + fieldValue ); } else if( msg.startsWith( "#H" )) { lcd.print( "Hmd: " + fieldValue ); } else if( msg.startsWith( "#P" )) { lcd.print( "Prs: " + fieldValue ); } else if( msg.startsWith( "#A" )) { lcd.print( "Alt: " + fieldValue ); } else if( msg.startsWith( "#L" )) { lcd.print( "Lgt: " + fieldValue ); } } delay( 1000 ); }
int main(void) { mcusr_save = MCUSR; MCUSR = 0; wdt_disable(); if (!(mcusr_save & WDRF)) { seqnum = 0; } #ifdef SN_TEMP_DDR // configure pin for analog SN_TEMP_DDR &= ~SN_TEMP_MASK; // set as input SN_TEMP_PORT &= ~SN_TEMP_MASK; // pull-ups off DIDR0 |= SN_TEMP_MASK; // disable digital input buffer #endif SN_LED_DDR |= SN_LED_MASK; // set as output SN_LED_PORT &= ~SN_LED_MASK; vw_setup(); #ifdef SN_RX_CAPABLE vw_rx_start(); #endif for (int i=0;i<3;i++) { send_status(SN_ADDRESS_BROADCAST); vw_wait_tx(); } for (;;) { #ifdef SN_RX_CAPABLE if (vw_have_message()) { uint8_t len = sizeof(msg); if (vw_get_message((uint8_t *)&msg, &len)) { if (msg.header.destination == SN_ADDRESS_ME|| msg.header.destination == SN_ADDRESS_BROADCAST) { process_msg(); } else { // TODO - consider routing here } } else { // bad message } } #endif #ifdef SN_TX_CAPABLE // Process any outgoing stuff if (millis() - last_status > STATUS_PERIOD) { send_status(SN_ADDRESS_BROADCAST); } if (millis() - last_sensor_data > SENSOR_PERIOD) { send_sensor_data(SN_ADDRESS_BROADCAST); } #endif // SN_TX_CAPABLE #ifdef USE_MILLIS //set_sleep_mode(SLEEP_MODE_IDLE); //sleep_enable(); //sleep_cpu(); //sleep_disable(); #endif } }
void loop2() { vw_wait_rx(); if (vw_get_message(buf, &buflen)) // Non-blocking { int port=parse(); switchState(port); Serial.print(port); Serial.println(); } }
String RFEasy::_getMessage() { uint8_t buflen = VW_MAX_MESSAGE_LEN; //Set buffer length to maximum for VirtualWire uint8_t buf[buflen]; //Initialise char array of size buffer length String msg = ""; //Variable for storing the received message (with handshake) in vw_wait_rx(); if(vw_get_message(buf, &buflen)) { //Get message from VirtualWire, scaling buffer length down to message size for(int i = 0; i < buflen; i++) { //Loop through all characters in the buffer char c = char(buf[i]); //Get character msg = msg + c; //Concatenate to string } } return msg; }
void loop() { if (vw_get_message((byte*) data, &msgLength)) { // Non-blocking { Serial.println("Got: "); if (msgLength == dataBytes) { lightUpLed1(data[0], data[1], data[2]); lightUpLed2(data[3], data[4], data[5]); } else { Serial.print("unexpected msg length of "); Serial.println(msgLength); } Serial.println(); delay(50); // give the circuit some rest, it deserves it ;-) } }
void loop() { uint8_t buf[VW_MAX_MESSAGE_LEN]; uint8_t buflen = VW_MAX_MESSAGE_LEN; if (vw_get_message(buf, &buflen)) // Non-blocking { int i; for (i = 0; i < buflen; i++) { Serial.print((char)buf[i]); } } }
int radio_receive_message(uint8_t *buffer, uint8_t *buflen) { if (vw_get_message(buffer, buflen)) // Non-blocking { // decrypt xor_data(buffer, *buflen); if( *buflen >= sizeof(radio_frame_header) ){ radio_frame_header *header = (radio_frame_header*) buffer; return header->msg_type; } else { // ignore invalid packets return 0; } } else { return 0; } }
void loop() { uint8_t buf[2]; uint8_t buflen = 2; if (vw_get_message(buf, &buflen)) // Non-blocking { digitalWrite( STATUS_LED, HIGH ); int i; // Message with a good checksum received, dump HEX Serial.print("Got: "); for (i = 0; i < buflen; i++) { Serial.print(buf[i], HEX); Serial.print(" "); } Serial.println(""); digitalWrite( STATUS_LED, LOW ); } }
bool RFLinkDataAvailable(uint8_t* buf, uint8_t* buflen) { if(vw_get_message(buf,buflen)) return true; else return false; }
int main(void) { /// setup init(); setup(); /// loop control for(frame=0; ; ++frame) { digitalWrite(13, HIGH); status.reset(); String new_msg = "Loop #"; new_msg.concat(frame); #ifdef PERIPHERAL_RTCC /// check time if (status.VALID == status.time_valid) { GetDatetimeString(rtc.now()); } /* TODO: port RTC.chipPresent() functionality over to RTClib if ( RTC.read(tm) ) { status.time_valid = status.VALID; } else { if ( RTC.chipPresent() ) { status.time_valid = status.INVALID; Serial.println("The DS1307 is stopped. Please set the RTC time."); Serial.println(); } else { status.time_valid = status.UNINSTALLED; Serial.println("DS1307 read error! Please check the circuitry."); Serial.println(); } } */ #endif /// Check interfaces for received messages // Serial, direct to the Command Line Interface (CLI) if(Serial.available() > 0) { char buff_console [8]; for(uint8_t len_console = 0x00; Serial.available() > 0; len_console++) { buff_console[len_console] = Serial.read(); CLI(buff_console, len_console); } } #ifdef INTERFACE_ASK_RX // RF (1-wire ASK, aka VirtualWire), print to console uint8_t buff_rf [VW_MAX_MESSAGE_LEN]; uint8_t len_rf = VW_MAX_MESSAGE_LEN; if(vw_get_message(buff_rf, &len_rf)) { #ifdef PERIPHERAL_RTCC // Prefix received messages with current date-time on console if (status.VALID == status.time_valid) { Serial.print(currentTime); Serial.write(" | "); } #endif //PERIPHERAL_RTCC Serial.print("RF Received : "); for(uint8_t i = 0; i < len_rf; i++) { Serial.print((char)buff_rf[i]); } Serial.println(); } #endif //INTERFACE_ASK_RX #ifdef ETHERNET_WEBSERVER EthernetClient client = server.available(); if (client) { Serial.println("new http client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println("Refresh: 60"); // refresh the page automatically every 60 sec client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); #ifdef PERIPHERAL_RTCC client.print("green-O-matic RTC Time : "); client.println(currentTime); #endif //PERIPHERAL_RTCC #ifdef INTERFACE_ASK_RX client.println("Most recently received 433MHz ASK Transmission : "); #endif //INTERFACE_ASK_RX client.println("</html>"); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(5); // close the connection client.stop(); Serial.println("client disconnected"); } #endif //ETHERNET_WEBSERVER digitalWrite(13, LOW); delay (LOOP_DELAY); }; return 0; }
int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer UCSCTL3 = SELREF_2; // Set DCO FLL reference = REFO UCSCTL4 |= SELA_2; // Set ACLK = REFO UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx // Loop until XT1,XT2 & DCO stabilizes - In this case only DCO has to stabilize do { UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG); // Clear XT2,XT1,DCO fault flags SFRIFG1 &= ~OFIFG; // Clear fault flags }while (SFRIFG1&OFIFG); // Test oscillator fault flag __bis_SR_register(SCG0); // Disable the FLL control loop UCSCTL1 = DCORSEL_5; // Select DCO range 16MHz operation UCSCTL2 |= 249; // Set DCO Multiplier for 8MHz // (N + 1) * FLLRef = Fdco // (249 + 1) * 32768 = 8MHz __bic_SR_register(SCG0); // Enable the FLL control loop // Worst-case settling time for the DCO when the DCO range bits have been // changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx // UG for optimization. // 32 x 32 x 8 MHz / 32,768 Hz = 250000 = MCLK cycles for DCO to settle __delay_cycles(250000); //P1DIR |= BIT0; // Set P1.0 to output direction //TA0CCTL0 = CCIE; // CCR0 interrupt enabled //TA0CTL = TASSEL_2 + MC_1 + TACLR + TAIE + ID_3; // SMCLK/8, upmode //TA0CCR0 = 0xffff; // 12.5 Hz vw_setup(1000); vw_rx_start(); P1DIR |= BIT4; /*uint8_t count = 1; while(1) { char msg[7] = {'h','e','l','l','o',' ','#'}; msg[6] = count; vw_send((uint8_t *)msg, 7); vw_wait_tx(); // Wait until the whole message is gone __delay_cycles(5/cycletime); count = count + 1; }*/ while(1) { uint8_t buf[VW_MAX_MESSAGE_LEN]; uint8_t buflen = VW_MAX_MESSAGE_LEN; if (vw_get_message(buf, &buflen)) // Non-blocking { //int i; P1OUT |= BIT4; // Flash a light to show received good message __delay_cycles(1/cycletime); // Message with a good checksum received, dump it. //Serial.print("Got: "); //for (i = 0; i < buflen; i++) //{ // Serial.print(buf[i], HEX); // Serial.print(' '); //} //Serial.println(); P1OUT &= ~BIT4; } } }