⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sensornodem.nc

📁 基于NesC/TinyOS的无线传感器网络测量节点源码
💻 NC
📖 第 1 页 / 共 2 页
字号:
/*
* Versions built for real field applications
* 2008.11.24
* By Zhen Li
*/

/* Data structure
*  databuffer[0]->day
*  databuffer[1]->measurement
*  databuffer[2,3]->vref [2]high [3]low
*  databuffer[4,5]->ADC0 [4]high [5]low
*  databuffer[6,7]->ADC1 [6]high [7]low
*  databuffer[8,9]->ADC2 [8]high [9]low
*  databuffer[10]->wc3
*  databuffer[11]->wc2
*  databuffer[12]->wc1
*  databuffer[13]->wc0
*  databuffer[14]->ec2
*  databuffer[15]->ec1
*  databuffer[16]->ec0
*  databuffer[17]->temp2
*  databuffer[18]->temp1*  databuffer[19]->temp0
*  databuffer[20,21]-> ADC3 [20]high [21]low (newly added for consistancy)
*/

// include sensorboard.h definitions from tos/mda300 directory
includes sensorboard;
includes sensorboardApp;

module SensorNodeM {
 provides {
    interface StdControl;
  }
  
 uses {
// Timer
	interface Timer as Timer1;
	interface Timer as Timer2;
	interface Timer as Timer3;
	
// Leds
	interface Leds;
	interface Power;
	
// communication
	interface StdControl as CommControl;
	interface SendMsg as Send;
	interface ReceiveMsg as Receive;
	
// ADC
	interface StdControl as IBADCcontrol;
	interface ADConvert as Soilsensor0;  //senosr0
	interface ADConvert as Soilsensor1;  //senosr1
	interface ADConvert as Soilsensor2;  //senosr2
	interface ADConvert as Soilsensor3;  //senosr3
	
// Battery    
    interface StdControl as BatteryControl;
    interface ADC as Battery;
	
//  UART0
    interface HPLUART;
	
//  Digital I/Os
	interface StdControl as DioControl;
    interface Dio as Dio0;
    interface Dio as Dio1;
    interface Dio as Dio2;
	interface Dio as Dio3;
	interface Dio as Dio4;
  }
} 

implementation {

  
  //The excitation circuits
  #define FIVE_VOLT_ON() TOSH_SET_PW5_PIN()
  #define FIVE_VOLT_OFF() TOSH_CLR_PW5_PIN()
    
  #define THREE_VOLT_ON()  TOSH_SET_PW3_PIN()
  #define THREE_VOLT_OFF() TOSH_CLR_PW3_PIN()

  #define TURN_VOLTAGE_BUFFER_ON() TOSH_SET_PW2_PIN()
  #define TURN_VOLTAGE_BUFFER_OFF() TOSH_CLR_PW2_PIN()

  #define VOLTAGE_BOOSTER_ON() TOSH_CLR_PW1_PIN()
  #define VOLTAGE_BOOSTER_OFF() TOSH_SET_PW1_PIN()

  //The instrumentation amplifier
  #define TURN_AMPLIFIERS_ON() TOSH_SET_PW6_PIN()
  #define TURN_AMPLIFIERS_OFF() TOSH_CLR_PW6_PIN() 
  
  #define EC_TEWAITINGTIME 1000
  #define MEASUREINTERVAL  20000*1.024  // For test, 20 seconds interval
  //#define MEASUREINTERVAL  5000*1.024  // For test, 5 seconds interval
  //#define MEASUREINTERVAL  7200000  // For field application, 2 hours interval

  enum {
      ASCII_CR=0x0d,
	  ASCII_SP=0x20,
	  ASCII_0 =0x30
  };
  
  enum {
	  NOECTE        = 0x00,
	  SOILSENSOR0EX = 0x01,
      SOILSENSOR0GD = 0x02, 
	  SOILSENSOR1EX = 0x03,
      SOILSENSOR1GD = 0x04, 
	  SOILSENSOR2EX = 0x05,
      SOILSENSOR2GD = 0x06,
      SOILSENSOR3EX = 0x07,
      SOILSENSOR3GD = 0x08, 
	  BATTERY       = 0x09
	};
  
  enum{
      DIGI0 = 0x00,
	  DIGI1 = 0x01
    };
	
  enum {
      MAX_DATUM_DIGITS = 4
  };
  
  enum {
      UART_BUF_MAXLEN = 15
  };
  
  TOS_Msg msg_buffer;    
  TOS_MsgPtr msg_ptr;
  XDataMsg *pack;
  
  uint8_t databuffer[DATA_RESTORE_BYTES];
  uint8_t measured = 0, day = 0;
  uint8_t uartBufCount = 0;
  uint8_t uartBuf[UART_BUF_MAXLEN-1];
  uint8_t state;
  //uint8_t digi_state = DIGI0;
  uint8_t datastore[12][DATA_RESTORE_BYTES];
  uint8_t rstIndex;
  
  bool EX_ON  = TRUE;
  bool EX_OFF = FALSE;
  bool Sampling_Finished = FALSE;
  bool sending_packet = FALSE;
  bool system_started = FALSE;
  bool bufData_tx = FALSE;
  bool renewData;
  bool fromExcitation = FALSE;
  bool inited = FALSE;
  bool ready = FALSE;
  
 /****************************************************************************
 * Task to send uart and rf message
 ****************************************************************************/
  
  static void Excitation_33(bool Ex_Status){
    if(Ex_Status == TRUE)
       {
	     TOSH_SET_PW7_PIN();
	     VOLTAGE_BOOSTER_ON();
	     TURN_VOLTAGE_BUFFER_ON();
		 THREE_VOLT_ON();
	   }
	else
	   {
	     VOLTAGE_BOOSTER_OFF();
	     TURN_VOLTAGE_BUFFER_OFF();
		 THREE_VOLT_OFF();
	   }
   } /* code done */
   
   static void Excitation_25(bool Ex_Status){
    if(Ex_Status == TRUE)
       {
	     TOSH_SET_PW7_PIN();
	     VOLTAGE_BOOSTER_ON();
	     TURN_VOLTAGE_BUFFER_ON();
		 
	   }
	else
	   {
	     VOLTAGE_BOOSTER_OFF();
	     TURN_VOLTAGE_BUFFER_OFF();
	   }
   } /* code done */
  
  task void send_msg(){
	atomic{
	  pack->xSensorHeader.day = databuffer[0];
	  pack->xSensorHeader.packet_id = databuffer[1];
	  pack->xData.soilData.vref = (((uint16_t)databuffer[2])<<8)+((uint16_t)databuffer[3]) ;
	  pack->xData.soilData.WC0 = databuffer[13];
	  pack->xData.soilData.WC1 = databuffer[12];
	  pack->xData.soilData.WC2 = databuffer[11];
	  pack->xData.soilData.WC3 = databuffer[10];
	  pack->xData.soilData.Temp0 = databuffer[19];
	  pack->xData.soilData.Temp1 = databuffer[18];
	  pack->xData.soilData.Temp2 = databuffer[17];
	  pack->xData.soilData.EC0 = databuffer[16];
	  pack->xData.soilData.EC1 = databuffer[15];
	  pack->xData.soilData.EC2 = databuffer[14];
	  pack->xData.soilData.adc0 = (((uint16_t)databuffer[4])<<8)+((uint16_t)databuffer[5]) ;
	  pack->xData.soilData.adc1 = (((uint16_t)databuffer[6])<<8)+((uint16_t)databuffer[7]) ;
	  pack->xData.soilData.adc2 = (((uint16_t)databuffer[8])<<8)+((uint16_t)databuffer[9]) ;
	  pack->xData.soilData.adc3 = (((uint16_t)databuffer[20])<<8)+((uint16_t)databuffer[21]) ;
	  if(bufData_tx){pack->xSensorHeader.target = 0;}
	  else {pack->xSensorHeader.target = 1;}
	}
	if (call Send.send(TOS_BCAST_ADDR,sizeof(XDataMsg),&msg_buffer) != SUCCESS)
	  sending_packet = FALSE;
    return;
    }
	
  task void dataProcess(){
    uint8_t position_EC0 = uartBufCount-4;
	uint8_t position_WC0;
	call Timer2.stop();
	Excitation_33(EX_OFF);
	atomic {
	// Rectrive Temp
	databuffer[19] = uartBuf[uartBufCount];
	databuffer[18] = uartBuf[uartBufCount-1];
	databuffer[17] = uartBuf[uartBufCount-2];
	
	// Rectrive EC
	databuffer[16] = uartBuf[position_EC0];
	if (uartBuf[position_EC0-1] != ASCII_SP) 
	  {
	    databuffer[15]=uartBuf[position_EC0-1];
		if (uartBuf[position_EC0-2] != ASCII_SP)
		  {
		    databuffer[14]=uartBuf[position_EC0-1];
			position_WC0 = position_EC0-4;
		  }
		else
		  {
		    position_WC0 = position_EC0-3;
			databuffer[14] = 0x30;
		  }
	  }
	else
	  {
	    position_WC0 = position_EC0-2;
		databuffer[15] = 0x30;
		databuffer[14] = 0x30;
	  }
	
	// Rectrive WC
	databuffer[13] = uartBuf[position_WC0];
	databuffer[12] = uartBuf[position_WC0-1];
	databuffer[11] = uartBuf[position_WC0-2];
	switch(position_WC0) 
	  {
	    case 3:  databuffer[10] = uartBuf[position_WC0-3];
		          break;
		case 2:  databuffer[10] = 0x30;
		          break;
		default: break;
	  } }
	
	
	atomic state = SOILSENSOR0EX;
	call Dio2.high();                        // Enable ADG0804
	call Timer2.start(TIMER_ONE_SHOT,20);
  }

  /*--------------------------   Routines   -----------------------------*/
  command result_t StdControl.init() {
	call Leds.init();                        // Init LED
    call CommControl.init();                 // Init communication
	call DioControl.init();                  // Init Digital I/Os
    call IBADCcontrol.init();                // Init ADC
	call BatteryControl.init();              // Init Battery monitoring
	call HPLUART.init();                     // Init the UART0
	atomic {
      pack = (XDataMsg *)&(msg_buffer.data); 
	  pack->xSensorHeader.node_id = TOS_LOCAL_ADDRESS;
	}
    return SUCCESS;
  }

  command result_t StdControl.start() {
	call CommControl.start();
	call IBADCcontrol.start();
	call DioControl.start();
	call BatteryControl.start();
	call Dio3.setparam(DIG_LOGIC|FALLING_EDGE);
	call Dio4.setparam(DIG_LOGIC|FALLING_EDGE);
	call Dio0.low();
	call Dio1.low();
	call Dio2.low();
	call Timer1.start(TIMER_ONE_SHOT, 10000);
    return SUCCESS;
  }

  command result_t StdControl.stop() {
    call CommControl.stop();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -