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 sendData( char nozzle, char distance ) { vw_wait_tx(); char buf[2]; if( nozzle == 0 ) nozzle = 1; if( distance == 0 ) distance = 1; buf[0] = nozzle; buf[1] = distance; vw_send((uint8_t *)buf, strlen(buf)); }
////////////////////// // // tx_send_packet: transmit a data packet // // length: length of raw packet including header // pkt: pointer to packet // void tx_send_pkt(pkt_t *pkt, uint8_t length) { #if defined(RADIO_DEBUG) log_packet('T', pkt, length); #endif vw_wait_tx(); // wait for tx idle vw_send((uint8_t *) pkt, length); // todo: handle error here tx_packet_count++; }
// Transmit data// void DataTX() { digitalWrite(DataRadioSwitch,HIGH); struct roverRemoteData payload; payload.TX_ID = 3; payload.Sensor1Data = BattValue; vw_send((uint8_t *)&payload, sizeof(payload)); vw_wait_tx(); digitalWrite(DataRadioSwitch,LOW); }
void TransmitRFData(RFData outDataSeq) { //Call the VirtualWire library using outDataSeq.s[] as argument if(sizeof(outDataSeq.s) <= 27) //Max allowable packet size in VirtualWire library is 27 bytes { vw_send((uint8_t*)outDataSeq.s, sizeof(outDataSeq.s)); vw_wait_tx(); } }
/* Transmit a message. Used in the loop() method. Maximum number of characters that can be transmitted is 27 including the handshake. */ void RFEasy::transmit(String msg) { if(_type == transmitter_type) { msg = msg + _handshake; if(msg.length() > 27) { Serial.println("Message is more than 27 characters and cannot be transmitted"); } else { char transmitCharArr[msg.length()]; msg.toCharArray(transmitCharArr, msg.length() + 1); vw_send((uint8_t *)transmitCharArr, strlen(transmitCharArr)); vw_wait_tx(); } } else { Serial.println("Is not initialized as a transmitter. Please call init_transmitter first."); //DEBUG } }
int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer //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(3/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; } }*/ }
// Wait until transmitter is available and encode and queue the message // into vw_tx_buf // The message is raw bytes, with no packet structure imposed // It is transmitted preceded a byte count and followed by 2 FCS bytes unsigned char vw_send(unsigned char* buf, unsigned char len) { uint8_t i; uint8_t index = 0; uint16_t crc = 0xffff; uint8_t *p = vw_tx_buf + VW_HEADER_LEN; // start of the message area uint8_t count = len + 3; // Added byte count and FCS to get total number of bytes if (len > VW_MAX_PAYLOAD) return false; // Wait for transmitter to become available vw_wait_tx(); // Encode the message length crc = _crc_ccitt_update(crc, count); p[index++] = symbols[count >> 4]; p[index++] = symbols[count & 0xf]; // Encode the message into 6 bit symbols. Each byte is converted into // 2 6-bit symbols, high nybble first, low nybble second for (i = 0; i < len; i++) { crc = _crc_ccitt_update(crc, buf[i]); p[index++] = symbols[buf[i] >> 4]; p[index++] = symbols[buf[i] & 0xf]; } // Append the fcs, 16 bits before encoding (4 6-bit symbols after encoding) // Caution: VW expects the _ones_complement_ of the CCITT CRC-16 as the FCS // VW sends FCS as low byte then hi byte crc = ~crc; p[index++] = symbols[(crc >> 4) & 0xf]; p[index++] = symbols[crc & 0xf]; p[index++] = symbols[(crc >> 12) & 0xf]; p[index++] = symbols[(crc >> 8) & 0xf]; // Total number of 6-bit symbols to send vw_tx_len = index + VW_HEADER_LEN; // Start the low level interrupt handler sending symbols vw_tx_start(); return true; }
int radio_send_state_update(uint8_t sensor_type, uint8_t sensor_index, uint16_t value) { radio_state_update_frame frame; uint8_t *buffer = (uint8_t *)&frame; memset(&frame, 0, sizeof(frame)); frame.header.source = state.uid; frame.header.dest = DEST_MASTER; frame.header.msg_type = SENSOR_MSG_STATE_UPDATE; frame.sensor_type = sensor_type; frame.sensor_index = sensor_index; frame.value = value; xor_data(buffer, sizeof(frame)); vw_send(buffer, sizeof(frame)); vw_wait_tx(); }
void Temp_Hum() { char msg[5]; hum = dht.readHumidity(); delay(1000); temp = dht.readTemperature(); // check if returns are valid, if they are NaN (not a number) then something went wrong! if (isnan(temp) || isnan(hum)) { Serial.println("Failed to read from DHT"); } else { // RF transmission //Humidity vw_send((uint8_t *)"$", 1); vw_wait_tx(); // Wait until the whole message is gone vw_send((uint8_t *)"H", 1); vw_wait_tx(); // Wait until the whole message is gone if (hum < 10) { vw_send((uint8_t *)"00", 2); vw_wait_tx(); // Wait until the whole message is gone dtostrf(hum, 2, 2, msg); vw_send((uint8_t *)msg, strlen(msg)); vw_wait_tx(); // Wait until the whole message is gone } else if(hum<100) { vw_send((uint8_t *)"0", 1); vw_wait_tx(); // Wait until the whole message is gone dtostrf(hum, 2, 2, msg); vw_send((uint8_t *)msg, strlen(msg)); vw_wait_tx(); // Wait until the whole message is gone } else { dtostrf(hum, 2, 2, msg); vw_send((uint8_t *)msg, strlen(msg)); vw_wait_tx(); // Wait until the whole message is gone } vw_send((uint8_t *)"\r", 1); vw_wait_tx(); // Wait until the whole message is gone // Temperature vw_send((uint8_t *)"$", 1); vw_wait_tx(); // Wait until the whole message is gone vw_send((uint8_t *)"T", 1); vw_wait_tx(); // Wait until the whole message is gone if (temp<0) { vw_send((uint8_t *)"-", 1); vw_wait_tx(); // Wait until the whole message is gone } else { vw_send((uint8_t *)"+", 1); vw_wait_tx(); // Wait until the whole message is gone } if (temp < 10) { vw_send((uint8_t *)"00", 2); vw_wait_tx(); // Wait until the whole message is gone dtostrf(temp, 3, 2, msg); vw_send((uint8_t *)msg, strlen(msg)); vw_wait_tx(); // Wait until the whole message is gone } else if(temp<100) { vw_send((uint8_t *)"0", 1); vw_wait_tx(); // Wait until the whole message is gone dtostrf(temp, 3, 2, msg); vw_send((uint8_t *)msg, strlen(msg)); vw_wait_tx(); // Wait until the whole message is gone } else { dtostrf(temp, 3, 2, msg); vw_send((uint8_t *)msg, strlen(msg)); vw_wait_tx(); // Wait until the whole message is gone } vw_send((uint8_t *)"\r", 1); vw_wait_tx(); // Wait until the whole message is gone /* Serial.print("\n>Humidity: "); Serial.print(hum); Serial.print(" %\t"); Serial.print("\n>Temperature: "); Serial.print(temp); Serial.println(" ºC"); */ } }