Ejemplo n.º 1
0
void main(void)
{
	char opt;
	
	// Make sure nCS is high before doing anything
	nCS_HIGH;
	
	// Enable user module interrupts
	SleepTimer_EnableInt();

	// Enable global interrutps
	M8C_EnableGInt;

	// Start the user modules
	UART_Start(UART_PARITY_NONE);
	UART_PutCRLF();
	SPIM_Start(SPIM_SPIM_MODE_0 | SPIM_SPIM_MSB_FIRST);
	SleepTimer_Start();
	DAC8_Start(DAC8_FULLPOWER);
	UART_CPutString("Synthesiszing waveforms\r\n");
	WriteBlock(0);
	WriteBlock(1);
	WriteBlock(2);
	WriteBlock(3);
	while(1) 
	{
		UART_CPutString("Synthetic wave output is on Port0[4]\r\nCowabunga Dude! Time to catch some waves.\r\n\r\n0. Play block 0\r\n1. Play block 1\r\n2. Play block 2\r\n3. Play block 3\r\n4. Test status register\r\n5. Test byte mode\r\n6. Test sequential mode\r\n");
		
		opt = GetNumber(0, 6);
		switch (opt)
		{
			case 4:
				while(SPIRAM_StatusRegisterTest()) 
					SleepTimer_SyncWait(4, SleepTimer_WAIT_RELOAD);
				break;
			
			case 5:
				while(SPIRAM_ByteModeTest()) 
					SleepTimer_SyncWait(4, SleepTimer_WAIT_RELOAD);	
				break;
			
			case 6:
				while(SPIRAM_SequentialModeTest()) 
					SleepTimer_SyncWait(4, SleepTimer_WAIT_RELOAD);
				break;
			
			default:
				PlayBlock(opt);
				break;	
		}
	}
}
Ejemplo n.º 2
0
void main(void)
{
	int result;
	float voltage;
	int status;
	
	M8C_EnableGInt ; // Uncomment this line to enable Global Interrupts
	// Insert your main routine code here.
	
	//Start PGA in high power mode
	PGA_Start(PGA_HIGHPOWER);
	
	//Start ADCINC in high power mode
	ADCINC_Start(ADCINC_HIGHPOWER);
	
	//Start LCD
	LCD_Start();
	
	//Run the ADC continuously
	ADCINC_GetSamples(0);
	
	SleepTimer_Start();
   	SleepTimer_SetInterval(SleepTimer_1_HZ);
   	SleepTimer_EnableInt();
	
	while (1)
	{
		SleepTimer_SyncWait(1, SleepTimer_WAIT_RELOAD);
		
		// Wait for data to be ready
		while (ADCINC_fIsDataAvailable() == 0);
		
		// Get Data and clear flag
		result=ADCINC_iClearFlagGetData();
		voltage = result * SCALE_FACTOR;
		
		LCD_Position(0, 0);
		LCD_PrCString("                ");
		LCD_Position(0, 0);
		LCD_PrHexInt(result);
		
		LCD_Position(1, 0);
		LCD_PrCString("                ");
		LCD_Position(1, 0);
		LCD_PrString(ftoa(voltage, &status));
	}
}
Ejemplo n.º 3
0
void main(void)
{
	//Variables
	char lcdFirstLine[LCD_LENGTH], lcdSecondLine[LCD_LENGTH];
	unsigned int displaymode = 3; // FIXME: sollte 0 sein!
	
	/** init **/
	
	// interrupt and SleepTimer init
	M8C_EnableGInt ;                            // Turn on interrupts
	SleepTimer_Start();
    SleepTimer_SetInterval(SleepTimer_8_HZ);    // Set interrupt to a
    SleepTimer_EnableInt();                     // 8 Hz rate
	
	// init PGA and SAR6 for sun and rain sensor
	PGA_sun_SetGain(PGA_sun_G5_33);  // gain of 5,33
    PGA_sun_Start(PGA_sun_HIGHPOWER);
	//PGA_rain_SetGain(PGA_rain_G8_00);
    //PGA_rain_Start(PGA_rain_MEDPOWER);
	SAR6_sun_Start(SAR6_sun_HIGHPOWER);
	//SAR6_rain_Start(SAR6_sun_MEDPOWER);

	// LCD init
	LCD_Init();
	
	// print welcome screen to LCD
	csprintf(lcdFirstLine,"   Welcome to   ");
	csprintf(lcdSecondLine, " Weatherstation ");
	LCD_Position(0,0);
	LCD_PrString(lcdFirstLine);
	LCD_Position(1,0);
	LCD_PrString(lcdSecondLine);
	
	while(1) {
		// get temp and humidity here
		
		switch(displaymode) {
			case 0:
				// overview();
				break;
				
			case 1:
				// temp();
				break;
				
			case 2:
				// humidity();
				break;
				
			case 3:
				sunsensor(lcdFirstLine, lcdSecondLine);
				break;
				
			case 4:
				// wind();
				break;
		
			default:
				displaymode = 0;
				csprintf(lcdFirstLine,"     Error      ");
				csprintf(lcdSecondLine,"                ");
		}
		
		// lets see what we've got
		LCD_Position(0,0);
		LCD_PrString(lcdFirstLine);
		LCD_Position(1,0);
		LCD_PrString(lcdSecondLine);
		
		// lets sleep for a while
		SleepTimer_SyncWait(8, SleepTimer_WAIT_RELOAD);
	}
}
Ejemplo n.º 4
0
void main(void)
{

	//Variables
	char lcdFirstLine[LCD_LENGTH], lcdSecondLine[LCD_LENGTH];
	int displaymode = 1;
 	int temperature[5];
	int humidity[5];
	
	
	
	/** init **/
	
	// interrupt and SleepTimer init
	M8C_EnableGInt ;                            // Turn on interrupts
	SleepTimer_Start();
    SleepTimer_SetInterval(SleepTimer_8_HZ);    // Set interrupt to a
    SleepTimer_EnableInt();                     // 8 Hz rate

	// LCD init
	LCD_Init();
	
	// print welcome screen to LCD
	csprintf(lcdFirstLine,"   Welcome to   ");
	csprintf(lcdSecondLine, " Weatherstation ");
	LCD_Position(0,0);
	LCD_PrString(lcdFirstLine);
	LCD_Position(1,0);
	LCD_PrString(lcdSecondLine);


	while(1) {
	
		I2Cm_Start();						//Initialize I2C
		I2Cm_fSendStart( 0x28, 0);			//Send Measuring Request	
		measuring(temperature, humidity); 	//measuring temperature and humidity
	
		
		switch(displaymode) {
			case 0:
				// overview();
				break;
				
			case 1:
				printtemp(lcdFirstLine, lcdSecondLine, temperature); //write temp in the variable for the lcd
				break;
				
			case 2:
				printhum(lcdFirstLine, lcdSecondLine, humidity); //wirte humidity in the variable for the lcd
				break;
				
			case 3:
				// rain();
				break;
				
			case 4:
				// wind();
				break;
		
			default:
				displaymode = 0;
				csprintf(lcdFirstLine,"     Error      ");
				csprintf(lcdSecondLine,"                ");
		}
		
		// lets see what we've got
		LCD_Position(0,0);
		LCD_PrString(lcdFirstLine);
		LCD_Position(1,0);
		LCD_PrString(lcdSecondLine);
		
		// lets sleep for a while
		SleepTimer_SyncWait(8, SleepTimer_WAIT_RELOAD);
		
	}
	}
Ejemplo n.º 5
0
void main(void)
{
	unsigned long temp_ulong;
	INT temp_int, temp_int2;
	BYTE temp_byte;
	AMUX4_0_InputSelect(AMUX4_0_PORT0_1);        
   	AMUX4_1_InputSelect(AMUX4_1_PORT0_0);
   	INSAMP_Start(INSAMP_LOWPOWER); 
    ADCINC_Start(ADCINC_HIGHPOWER);      
   	DAC9_Ia_Start(DAC9_Ia_HIGHPOWER);
	DAC6_VGND_Start(DAC6_VGND_MEDPOWER);
	DAC6_VGND_WriteStall (31);
    PWM8_Vout_DisableInt();  
    PWM8_Vout_Start();     
    PWM8_Heater_DisableInt();  
    PWM8_Heater_Start();
	PWM8_NB_Out_DisableInt();  
    PWM8_NB_Out_Start(); 
	ADCINC_GetSamples(0);
	SleepTimer_Start();  
   	SleepTimer_SetInterval(SleepTimer_512_HZ);  
   	SleepTimer_EnableInt();   
	M8C_EnableGInt ;  
	LCD_Start();                  // Initialize LCD
	LCD_InitBG(LCD_SOLID_BG);

	while(1) {
		temp_ulong++;
		if ((ADC_IF & 1) == 1) {
			ADC_IF = ADC_IF & 254;
			Ri_Min = IIR_Int(Ri_Min_x1*2,Ri_Min,Ri_Filter_Strength);
			Ri_Delta = Ri_Max - Ri_Min;
			Ri_Mid = (Ri_Max + Ri_Min) / 2;
		}

		if ((ADC_IF & 2) == 2) {
			ADC_IF = ADC_IF & 253;
			Ri_Max = IIR_Int(Ri_Max_x1 * 2, Ri_Max, Ri_Filter_Strength);
			Ri_Delta = Ri_Max - Ri_Min;
			Ri_Mid = (Ri_Max + Ri_Min) / 2;
		}

		if ((ADC_IF & 4) == 4) {
			ADC_IF = ADC_IF & 251;
			ip = IIR_Int(ip_x1 * 2, ip, ip_Filter_Strength);
		}

		Ia_PID_Counter += Sleep_Counter;
		Heater_PID_Counter += Sleep_Counter;
		Heatup_Counter += Sleep_Counter;
		Vout_Lookup_Counter += Sleep_Counter;
		LCD_Counter += Sleep_Counter;
		Sleep_Counter = 0;

		if (Ia_PID_Counter > Ia_PID_Counter_Set) {
			Ia_PID_Counter = 0;
			Ia_PID();
		}

		if (Heater_PID_Counter > Heater_PID_Counter_Set) {
			Heater_PID_Counter = 0;
			Heater_PID();
		}

		if (Vout_Lookup_Counter > Vout_Lookup_Counter_Set) {}
			Vout_Lookup_Counter = 0;
			temp_int = ip - ip_to_Vout_Lookup_Start;
			if (temp_int < 0) {
				temp_int = 0;
			} else if (temp_int > (ip_to_Vout_Lookup_Size - 1)) {
				temp_int = (ip_to_Vout_Lookup_Size - 1);
			}
			PWM8_Vout_WritePulseWidth(ip_to_Vout_Lookup[temp_int]);
			
			#ifdef NB_Out
				temp_byte = 23;//0.45v
				if (ip < 251) { // 251 =0.9797787392968
					temp_byte = 46; //0.9v		
				} else if (ip > 259) { //259 = 1.02295956968912
					temp_byte = 0; //0v
				}
				PWM8_NB_Out_WritePulseWidth(temp_byte);
			#endif
		}

		if (LCD_Counter > LCD_Counter_Set) {
			LCD_Counter = 0;
			
			#ifdef LCD_Lambda_Text
				temp_int = ip - ip_to_Lambda_Lookup_Start;
				
				if (temp_int < 0) {
					temp_int = 0;
				} else if (temp_int > (ip_to_Lambda_Lookup_Size - 1)) {
					temp_int=(ip_to_Lambda_Lookup_Size - 1);
				}

				Lambda_x100 = ip_to_Lambda_Lookup[temp_int];
				temp_int = Lambda_x100;
				LCD_Position(0,0);
				temp_int2 = temp_int / 100;
				Str1[9] = btoa(temp_int2);
				temp_int = temp_int - (100 * temp_int2);
				temp_int2 = temp_int / 10;
				Str1[11] = btoa(temp_int2);
				temp_int = temp_int - (10 * temp_int2);
				Str1[12] = btoa(temp_int);
				LCD_PrString(Str1);
			#endif

			#ifdef LCD_AFR_Text
				temp_int = ip - ip_to_Lambda_Lookup_Start;

				if (temp_int < 0) {
					temp_int = 0;
				} else if (temp_int > (ip_to_Lambda_Lookup_Size - 1)) {
					temp_int = (ip_to_Lambda_Lookup_Size - 1);
				}

				Lambda_x100=ip_to_Lambda_Lookup[temp_int];
				temp_int = (INT) Lambda_x100 * 147;
				LCD_Position(0,0);
				temp_int2 = temp_int / 1000;
				Str1[6] = btoa(temp_int2);
				temp_int = temp_int - (1000 * temp_int2);
				temp_int2 = temp_int / 100;
				Str1[7] = btoa(temp_int2);
				temp_int = temp_int - (100 * temp_int2);
				temp_int2 = temp_int / 10;
				Str1[9] = btoa(temp_int2);
				temp_int = temp_int - (10 * temp_int2);
				Str1[10] = btoa(temp_int);
				LCD_PrString(Str1);
			#endif
			
			#ifdef LCD_Lambda_Graph
				temp_int = ip - ip_to_Lambda_Lookup_Start;
				
				if (temp_int < 0) {
					temp_int = 0;
				} else if (temp_int > (ip_to_Lambda_Lookup_Size-1)) {
					temp_int = (ip_to_Lambda_Lookup_Size - 1);
				}

				Lambda_x100 = ip_to_Graph_Lookup[temp_int];
				LCD_DrawBG(0, 0, 16, Lambda_x100);
			#endif
			
			#ifdef LCD_Temperature_Text
				temp_int = Ri_Delta-Ri_Delta_to_Temperature_C_Start;

				if (temp_int < 0) {
					temp_int = 0;
				} else if (temp_int > (Ri_Delta_to_Temperature_C_Size - 1)) {
					temp_int = (Ri_Delta_to_Temperature_C_Size - 1);
				}

				LSU_Temperature_C = Ri_Delta_to_Temperature_C[temp_int] + Ri_Delta_to_Temperature_C_Offset;
				temp_int = LSU_Temperature_C;
				LCD_Position(1,0);           
				temp_int2 = temp_int / 100;
				Str2[7] = btoa(temp_int2);
				temp_int = temp_int - (100 * temp_int2);
				temp_int2 = temp_int / 10;
				Str2[8] = btoa(temp_int2);
				temp_int = temp_int - (10 * temp_int2);
				Str2[9] = btoa(temp_int);
				LCD_PrString(Str2);
			#endif
			
			#ifdef LCD_Temperature_Graph
				temp_int = Ri_Delta - Ri_Delta_to_Temperature_C_Start;

				if (temp_int < 0) {
					temp_int = 0;
				} else if (temp_int > (Ri_Delta_to_Temperature_C_Size - 1)) {
					temp_int = (Ri_Delta_to_Temperature_C_Size - 1);
				}

				LSU_Temperature_C = Ri_Delta_to_Graph[temp_int];
				LCD_DrawBG(1, 0, 16, LSU_Temperature_C);
			#endif
		}

		if (Heatup_Heater_Output < 255) {   
			if (Heatup_Counter > Heatup_Counter_Set) {
				Heatup_Counter = 0;
				Heatup_Heater_Output++;
			}

			if ((Ri_Min > 50) && (Ri_Max < 475) && (Ri_Delta < Ri_Delta_Target)) {
				Heatup_Heater_Output = 255;
				Ri_Delta_Error_Sum = 0;
			}
		}
	}
Ejemplo n.º 6
0
Archivo: main.c Proyecto: kLabUM/IoT
void main(){

    CyGlobalIntEnable;

    // Start up initial mote processes
    SleepTimer_Start();
    isr_SleepTimer_StartEx(sleepTimerWake_INT);
    FS_Init();  // SD Card
    modem_set_api_feed(FEED_ID, API_KEY);
    modem_start();
    ultrasonic_start();
    solinst_start();
    ADC_SAR_1_Start();
    //modem_power_off();
    
    packet_ready = 0u; 
    take_reading = 1u;
    t_sample = 2u;                                  // Initialize Sample Period to 2 minutes
    trigger_sampler = 0u;                           // Initialize automated sampler to not take a sample    
    bottle_count = 0u;                              // Initialize bottle count to zero

    NeoRTC_Start(rtcCallBackReceived);              // Start and enable the RTC.   
    NeoRTC_Set_Repeating_Minute_Alarm(t_sample);    // Set 1-minute alarm
   
    
    //  Uncomment below to set RTC
    /*
    NeoRtcTimeStruct tm = {
      .second = 00,
      .minute = 54,
      .hour = 18,
      .day = 27,
      .weekday = 2,
      .month = 5,
      .year = 2014,
    } ;
    RTC_WriteTime(tm); //sets time
    */


    for(;;){
    
        RTC_Process_Tasks();
        
        // Loop continuously and take a reading  
        // every "t_sample" minutes.
        // The variable "take_reading" is set to TRUE 
        // by rtcCallBackReceived() defined after the for() loop
        if(take_reading){ 

            // Turn on the modem to update state variables:
            //  sampling frequency; triggering the autosampler
            modem_power_on();   
           
            if (clear_packet(data_packet)) {
                packet_ready = 0u;
            }
                        
            // To communicate with the IoT platform, the node 
            // 1) listens on a given port and 2) parses an incoming string 
            // for relevant commands. Once a packet is ready to send (3), 
            // the node 4) transmits the packet.  This is achieved in as little 
            // as four lines of code by leveraging an existing TCP/IP library.
            //
            // 1) modem_get_packet()
            // 2) packet_get_uint8()
            // 3) sprintf(data_packet, ...
            // 4) modem_send_packet()
            //
            if (modem_get_packet(data_packet,"t_sample,trigger_sampler")) {
                
                // Read in any updated values
                if(packet_get_uint8(data_packet, "t_sample", &tmp)){
                    t_sample = tmp;
                }
                if(packet_get_uint8(data_packet, "trigger_sampler", &tmp)){
                    trigger_sampler = tmp;
                }
            }

            /*
            if (send_attempts > 0) {
                debug_write("New reading taken before previous packet was sent");
            }
            */

            // Trigger the autosampler to collect the sample 
            // if a sample is requested
            if (trigger_sampler){
                
                // Send in acknowledgement that packet containing
                // info re:automated sampler has been received:
                modem_send_packet("trigger_sampler, 0");
                
                trigger_sampler = 0; // Update the value locally
                
                // Trigger the autosampler as long as there are still available samples
                // If bottle_count >= MAX_BOTTLE_COUNT, skip taking a sample and avoid
                // waiting for the code to timeout
                if (bottle_count < MAX_BOTTLE_COUNT) {
                    // Turn off modem to conserve energy
                    modem_power_off();
                    
                    // Start up the autosampler processes 
                    // and power on the autosampler
                    autosampler_start();
                    autosampler_power_on();
                    

                    // Trigger the autosampler and update current bottle_count
                    autosampler_take_sample(&bottle_count);
                    
                    // Power off the autosampler
                    // and shut down the autosampler processes 
                    autosampler_power_off(); 
                    autosampler_stop();    
                }
                else {
                    debug_write("bottle_count >= MAX_BOTTLE_COUNT");
                }
            }
            
            // Construct the data packet that will be transmitted
            // by the sensor node, starting with the bottle the
            // autosampler last collected a sample in
            // (the packet is in .csv format)
            sprintf(data_packet,"%s, %u\r\n",
                "bottle", bottle_count);

            // Get Sensor Readings for:
            //  Depth, Pressure, Temperature, Conductivity
            // and update the data packet                
            if (solinst_get_reading(&solinst_reading)){
                sprintf(data_packet,"%s%s, %f\r\n", data_packet,
                    "depth_press", solinst_reading.depth);            
                    
                sprintf(data_packet,"%s%s, %f\r\n", data_packet,
                    "temp_press", solinst_reading.temp);                      
            }
            
            if (ultrasonic_get_reading(&ultrasonic_reading)){
                sprintf(data_packet,"%s%s, %f\r\n", data_packet,
                    "depth_sonic", ultrasonic_reading.depth);              
            }
            if (ReadBatteryVoltage(&vBattery)){
                sprintf(data_packet,"%s%s, %f\r\n", data_packet,
                    "V_batt", vBattery);              
            }

            // An example of writing data in JSON format
            // This was replaced with .csv format due to 
            // .csv's smaller packet size
            /*
            sprintf(data_packet, "{"
                      "\"method\":\"put\","
                      "\"resource\":\"/feeds/%d\","
                      "\"headers\":{\"X-ApiKey\":\"%s\"},"
                      "\"body\":{\"version\":\"1.0.0\",\"datastreams\":["
                            "{ \"id\" : \"depth_sonic\", \"current_value\" : \"%f\"},"
                            "{ \"id\" : \"depth_press\", \"current_value\" : \"%f\"},"
                            "{ \"id\" : \"temp_press\", \"current_value\" : \"%f\"},"
                            "{ \"id\" : \"trigger_sampler\", \"current_value\" : \"%d\"},"
                            "{ \"id\" : \"V_batt\",  \"current_value\" : \"%f\"}"
                      "]}}",
                      FEED_ID,API_KEY,
                      ultrasonic_reading.depth, solinst_reading.depth, solinst_reading.temp,0u,vBattery);
            */

            // Update flags for sending a packet and taking sensor readings
            packet_ready = 1u;
            take_reading = 0u;
            //send_attempts = 0u;
        
        // Once the flag for packet_ready has been set, 
        // send the packet and save the packet locally
        } else if(packet_ready){
            isr_SleepTimer_Stop;  //  <============= ADDRESS THIS WITH B.K.
            
            // Power on the modem in case it was turned off
            modem_power_on();
            
            // modem_state should now be IDLE or READY
            // and we can send the packet
            modem_send_packet(data_packet);
            
            // Power off the modem and conserve energy
            modem_power_off();
            
            // Backup data to SD Card
            // Use the current time to time-stamp the current reading
            NeoRtcTimeStruct tm_neo = NeoRTC_Read_Time();

            // Overwrite current data_packet with time-stamped data
            sprintf(data_packet, "\r\n%d:%d:%d %d/%d/%d -- [ID %d] u_d=%f s_d=%f s_t=%f bottle=%d v_b=%f", tm_neo.hour, tm_neo.minute,
                             tm_neo.second, tm_neo.day, tm_neo.month, tm_neo.year, moteID,
                             ultrasonic_reading.depth, solinst_reading.depth, solinst_reading.temp,bottle_count,vBattery);
            
            // Write data_packet to a *.txt file on the SD Card
            Write_To_SD_Card("data.txt","a",data_packet,strlen(data_packet));            

            // Clear the current packet in preparation for the next reading
            // when the node awakens from sleep
            if (clear_packet(data_packet)) {
                packet_ready = 0u;
            }
        
        // If either the sensor node should take a reading nor send a packet
        // go to low power mode to increase battery life    
        }else{
            // Set repeating alarm to trigger every "t_sample" minutes
            NeoRTC_Set_Repeating_Minute_Alarm(t_sample);  
            Goto_Low_Power_Mode();
        }
    CyDelay(1u);  // Quick Pause to prevent locking
    }
}
Ejemplo n.º 7
0
int main()
{
    //Enable custom ISR's
    initAllISR();
    
    //Enable Globals
    CyGlobalIntEnable;    
    
    //INITIALIZE 12C, XBEE, Veg
    Veg_Start();
    Veg_StartConvert();
    Xbee_UART_Start();
    I2C_1_Start();   
    CyDelay(250);
    
    //read EEPROM, config, and oscillator trimming value
    readEEPROM();
    writeOscTrimValue();
    writeConfigValue();
    
    //Initialize PSOC state
    state = WAKE;
    
    //Start Sleep Timer 
    SleepTimer_Start();
    
    
    for(;;)
    {
        /* Place your application code here. */
        if (state == WAKE){
            
            //change state
            state = GETDATA;
        } // end WAKE
        
        else if (state == GETDATA){
            
            //MLX READ------------------------------------------
            //Temperature, compensation, and whole frame read
            uint16 ptat = readPTAT();          
            uint16 comp = readCompensation();
            readFrame();    
            //END MLX READ--------------------------------------

            //VEG READ------------------------------------------
            //Get and scale ADC
            int depth;
            for (depth = 0; depth < NUM_LEVELS; depth++) {
                voltage[depth] = Veg_GetResult16(0)*(3.3/4096);
                temperature[depth] = 22.3;
            }
            
            
            
            //END VEG READ--------------------------------------
            
            //change state
            state = TXDATA;
        } // end GETDATA
        
        else if (state == TXDATA){
            
            //TX MLX
            //tx whole frame, PTAT, and compensation
            XBeeTxFrame();
            XBeeTxPTAT();
            XBeeTxCompensation();
            //END TX MLX----------------------------------------
            
            //TX VEG--------------------------------------------
            char level[4];
            int depth;
            //string for calculating Checksum
            char toCksm[56];
            Xbee_UART_PutString("V,");
            
            for (depth = 0; depth < NUM_LEVELS; depth++) {
                sprintf(level, "%d:", depth);
                sprintf(Veg_out, "%1.4f:", voltage[depth]);
                //Add hard coded temperature value
                sprintf(temp_out, "%2.1f,", temperature[depth]);
                //if first level, copy string, otherwise concatenate
                if (depth == 0) {
                    strcpy(toCksm, level);    
                }
                else {
                    strcat(toCksm, level);
                }
                
                strcat(toCksm, Veg_out);
                strcat(toCksm, temp_out);
                
                //Transmit level string
                Xbee_UART_PutString(level);
                Xbee_UART_PutString(Veg_out);
                Xbee_UART_PutString(temp_out);
            }       //end for
            
            //Transmit Checksum for Vegetronix
             Xbee_UART_PutChar(calculateCheckSum(toCksm, strlen(toCksm)));
            
            //END TX VEG----------------------------------------
            
            //Change State
            state = SLEEPMODE;
        } // end TXDATA
        
        else if (state == SLEEPMODE){
            //put her to sleep
            //CyDelay(2000);
            if (wake_count % wakeCycles == 0) {
                //Change State
                state = WAKE;
                continue;
            }
            else {
                sleepPsoc();   
            }
            
            
        } // END SLEEPM0DE
        
        else{ // Error in state machine, to be filled out. Likely a reset will occur here. 
            
            //Change State
            state = WAKE;
        } // End Error Catch
        
    }
}
Ejemplo n.º 8
0
Archivo: main.c Proyecto: kLabUM/IoT
void main()
{
    /* Place your initialization/startup code here (e.g. MyInst_Start()) */
    CyGlobalIntEnable; /* Uncomment this line to enable global interrupts. */
	
	CyDelay(5u);  // Short delay to make sure device is ready for low power entry
	
	sleep_isr_StartEx(Wakeup_ISR); // Start Sleep ISR
	SleepTimer_Start();			   // Start SleepTimer Compnent	
    
    modem_set_api_feed(FEED_ID, API_KEY);
    
    //ADC_SAR_1_Start();
    //modem_start();
    //modem_power_on();    
    //modem_get_serial_number();
    
    // Check/set flow control: Change from 3 (default - bidirectional) to 0 (no flow control) --> Did NOT fix ability to connect using AT#SD
    /*
	modem_set_flow_control(0u); 
    modem_get_profile(); //
    modem_get_sw_version();
	modem_set_error_reports(2u);
	*/
	
	// Init and enable CDMA
	/*
	modem_set_user("");
	modem_set_password("");	
	modem_test_CDMA_data();
	modem_enable_CDMA_data(1u);
	modem_test_CDMA_data();
	*/

    // Initialize variables
    ready			= 1u;
	loops			= 0u;
	lock			= 0u;
	packet_ready	= 0u;
	iter            = 0;
    sign            = 1;
    packet_len      = 0u;
	wakeup_interval_counter = 0u;	
    v_out           = 0.0;

    
    VBAT_READ_EN_Write(0u);
	Pin2_Write(0u);
	Pin17_Write(0u);
	Pin18_Write(0u);
    Pin37_Write(0u);
    Pin38_Write(0u);
    Pin39_Write(0u);
    //Pin34_Write(0u);
    //Pin35_Write(0u);
    
//    WaveDAC8_1_Start(); /* Start WaveDAC8  */


    for(;;)
	{
		if ( ready == 1u ){
			iter = 0;
			if (clear_packet(data_packet)) {
                packet_len = 0u;
                packet_ready = 0u;
            }
			
			blink_LED(3u);
			
            if (ultrasonic_get_reading(&ultrasonic_reading)){
                sprintf(data_packet,"%s%s, %d\r\n", data_packet,
                    "depth_sonic", (uint16) (ultrasonic_reading.depth));              
            }
			
			blink_LED(3u);
			
			// Set up the modem and connect to network					
			LED_Write(!LED_Read());
			if (modem_startup()) {
				//modem_get_serial_number();

				
				// Send a packet
				LED_Write(!LED_Read());	
				sprintf(data_packet,"%s%s, %u\r\n", data_packet,
	        		"tmp", loops);  
				ready = modem_send_packet(data_packet);
				CyDelay(5000u);
				
				if (clear_packet(data_packet)) {
	                packet_len = 0u;
	                packet_ready = 0u;
	            }
				
				// Request a packet
				LED_Write(!LED_Read());	
				if( modem_get_packet(data_packet,"lock,tmp") ){
				
					// Code to test moving the actuator 					
					if(packet_get_uint8(data_packet,"lock",&lock)){
						if(lock == 1u){	
							// Supply power to breakout board
							// This powers the Reed Switch
							Pin2_Write(1u);
							CyDelay(1000u);
							
							// Move the actuator out
							Pin17_Write(1u);
							CyDelay(3000u);
							Pin17_Write(0u);
							
							CyDelay(2000u);
							
							// Move the actuator in
							Pin18_Write(1u);
							CyDelay(3000u);
							Pin18_Write(0u);
							
							// Cut power to breakout board
							CyDelay(1000u);
							Pin2_Write(0u);
						}
					}	
				}								
				
				/* 
				modem_power_off();
				modem_stop();
				*/
			}
			modem_shutdown();
			
			loops += 10;
			ready = 0u;
		}
		/* If not ready, update the counter for the sleep timer */
		else {
			/* Blink the LED to indicate sleeping */
			blink_LED(5u);
			
			/* Prepares system clocks for the Sleep mode */
	        CyPmSaveClocks();

	        do
	        {
	            /*******************************************************************
	            * Switch to the Sleep Mode for the other devices:
	            *  - PM_SLEEP_TIME_NONE: wakeup time is defined by Sleep Timer
	            *  - PM_SLEEP_SRC_CTW :  wakeup on CTW sources is allowed
	            *******************************************************************/
	            CyPmSleep(PM_SLEEP_TIME_NONE, PM_SLEEP_SRC_CTW);


	            /***********************************************************************
	            * After the device is woken up the Sleep Timer's ISR is executed.
	            * Afterwards the CyPmSleep() execution is finished the clock
	            * configuration is restored.
	            ***********************************************************************/
	            if(SLEEPTIMER_INTERVAL_COUNTER == wakeup_interval_counter)
	            {
	                /* Blink with LED */
	                //(0u == LED_P1_2_Read()) ? LED_P1_2_Write(1u) : LED_P1_2_Write(0u);
	                wakeup_interval_counter = 0u;
					ready = 1u;
	            }
	            else
	            {
	                wakeup_interval_counter++;
	            }
	                
	        } while (wakeup_interval_counter != 0u);
	        

	        /* Restore clock configuration */
	        CyPmRestoreClocks();
		}
		
		/*
        // Place your application code here.
        Pin37_Write(!Pin37_Read());
        
        clear_packet(data_packet);
        sprintf(data_packet,"%s, %u\r\n", "tmp", iter);
        
        modem_connect();
        LED_Write(!LED_Read());
        
        // modem_state should now be IDLE or READY
        //modem_send_packet(data_packet);        
        modem_get_google(); //Can't get to work
               
        if (tmp > 10) {
            sign = -1;
        } else if (tmp < -10) {
            sign = 1;
        }
        tmp = tmp + sign;        
        
        
        Pin37_Write(!Pin37_Read());
        LED_Write(!LED_Read());
        CyDelay(100u);  
        
        modem_get_time();
        Pin38_Write(!Pin38_Read());
        LED_Write(!LED_Read());
        CyDelay(100u);      
        
        modem_get_signal_quality();
        Pin39_Write(!Pin39_Read());
        LED_Write(!LED_Read());
        CyDelay(100u);  
        
        modem_check_network();
        //Pin34_Write(!Pin34_Read());
        LED_Write(!LED_Read());
        CyDelay(100u);  
        
        modem_get_serial_number();
        //modem_disconnect();
        //Pin35_Write(!Pin35_Read());
        LED_Write(!LED_Read());
        CyDelay(100u);   
        
        ADC_SAR_1_StartConvert();
        ADC_SAR_1_IsEndConversion(ADC_SAR_1_WAIT_FOR_RESULT);
        v_out = ADC_SAR_1_CountsTo_Volts(ADC_SAR_1_GetResult16());        
        //v_out = Pin30_Read();
        Pin25_Write(v_out);
		*/
    }
	
	/*
	modem_power_off();  
	modem_stop();
	
	blink_LED(3u);		
	CyPmSaveClocks();
	CyPmHibernate();
	*/
}