Exemplo n.º 1
0
/* This function sets the name of an RN-42 module
   name should be an up to 20-character value. It MUST BE TERMINATED by a 
   \r character */
uint8_t makeyMateClass::setName(char * name)
{
  if (bluetooth.available())
    bluetooth.flush();	// Get rid of any characters in the buffer, we'll need to check it fresh

  bluetooth.print("SN,");
  for (int i=0; i<20; i++)
  {
    if (name[i] != '\r')
      bluetooth.write(name[i]);
    else
      break;
  }
  bluetooth.write('\r');
  
  delay(BLUETOOTH_RESPONSE_DELAY);
  bluetoothReceive(rxBuffer);

  /* Double check the setting, output results in Serial monitor */
  bluetooth.flush();
  bluetooth.print("GN");
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  Serial.print("Name set to: ");
  while (bluetooth.available())
    Serial.write(bluetooth.read());

  return bluetoothCheckReceive(rxBuffer, "AOK", 3);
}
boolean readRFID() {
	//if (RFID.available()) RFID.flush();
	digitalWrite(RFID_ENABLE, LOW);    // Activate the RFID reader
	
	for (byte i = 0; i < RFID_LENGTH; i++)
		rfidCode[i] = 0;

	if (RFID.available()) {
		int val = RFID.read();
		Serial.print(val);

		if (val == RFID_START_CHAR) {
			RFID.readBytes(rfidCode, RFID_LENGTH);

			Serial.print("RFID Read: ");
			Serial.println(rfidCode);

			digitalWrite(RFID_ENABLE, HIGH);   // deactivate the RFID reader for a moment so it will not flood
			RFID.flush();                      // clear the buffer
			delay(1500);                       // wait for a bit

			return true;
		 }
	}
	return false;
}
Exemplo n.º 3
0
//clear buffer in the serial port - better - try to do this
void HerkulexClass::clearBuffer()
{
  switch (port)
	{
	case SSerial:
                SwSerial.flush();
                delay(1);
                break;
	#if defined (__AVR_ATmega1280__) || defined (__AVR_ATmega128__) || defined (__AVR_ATmega2560__)
	case HSerial1:
				Serial1.flush();
				while (Serial1.available()){
				Serial1.read();
				delayMicroseconds(200);
				}

		break;
	case HSerial2:
	            Serial2.flush();
				while (Serial2.available()){
				Serial2.read();
				delayMicroseconds(200);
				}
		break;
	case HSerial3:
	            Serial3.flush();
				while (Serial3.available()){
					Serial3.read();
					delayMicroseconds(200);
				}

		break;
	#endif
	}
}
Exemplo n.º 4
0
void setup()
{
  gprsSerial.begin(9600);
  Serial.begin(9600);

  Serial.println("Config SIM900...");
  delay(2000);
  Serial.println("Done!...");
  gprsSerial.flush();
  Serial.flush();

  // attach or detach from GPRS service 
  gprsSerial.println("AT+CGATT?");
  delay(100);
  toSerial();


  // bearer settings
  gprsSerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
  delay(2000);
  toSerial();

  // bearer settings
  gprsSerial.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"");
  delay(2000);
  toSerial();

  // bearer settings
  gprsSerial.println("AT+SAPBR=1,1");
  delay(2000);
  toSerial();
}
/* sendPacket() is called by just about every other member function. It calculates 
	some CRC bytes, then sends the message string.
	If a response is requested, it'll return that in the response array. Otherwise
	that and the responseLength variable should be 0.
	
	If you're using a bluetooth module that's not the RN-42, this'd be the place
	to modify.
*/
void SFE_MetaWatch::sendPacket(unsigned char * data, int length, unsigned char * response, int responseLength)
{
	int crc = ComputeCRC(data, length - 2);	// Get the crc values for our string
	data[length-1] = (crc & 0xFF00) >> 8;	// LSB goes first
	data[length-2] = crc & 0xFF;	// the MSB

	// If you want a response, let's flush out the bt buffer first.
	if (responseLength > 0)
		bt.flush();
	
	// Send the data out to the BlueSMiRF
	for (int i=0; i<length; i++)
	{
		bt.write(data[i]);
	}
	
	// If a response was requested, read that into the response array.
	if (responseLength > 0)
	{
		delay(BLUETOOTH_RESPONSE_DELAY);
		int i=0;
		while (bt.available() && (i < responseLength))
		{
			response[i++] = bt.read();
		}
	}
}
Exemplo n.º 6
0
/* This function returns a 1 if the RN-42 is already in HID mode
   The module MUST BE IN COMMAND MODE for this function to work! */
uint8_t makeyMateClass::getHIDMode(void)
{
  bluetooth.flush();
  bluetooth.print("G~");  // '~' is the RN-42's HID/SPP set command
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  bluetoothReceive(rxBuffer);  // receive all response chars into rxBuffer

  return bluetoothCheckReceive(rxBuffer, "6", 1);	
}
Exemplo n.º 7
0
void setupBlueToothConnection()
{
  blueToothSerial.begin(38400); 
  blueToothSerial.print("\r\n+STWMOD=1\r\n");
  blueToothSerial.print("\r\n+STNA=SeeedBTMaster\r\n");
  blueToothSerial.print("\r\n+STAUTO=0\r\n");
  delay(2000); 
  blueToothSerial.flush();
  blueToothSerial.print("\r\n+INQ=1\r\n");
  Serial.println("inquiring");
  delay(2000); 
  
  char recvChar;
 /* while(1){                                       //this part is for auto detecting and connecting to a slave but something is wrong with the parsing of the address, use this to print out the address and then put it in manulely 								below
    if(blueToothSerial.available()){
     recvChar = blueToothSerial.read();
      recvBuf += recvChar;
      nameIndex = recvBuf.indexOf(slaveName);
  
      if ( nameIndex != -1 ){
        
 	addrIndex = (recvBuf.indexOf(retSymb,(nameIndex - retSymb.length()- 18) ) + retSymb.length());	 		
 	slaveAddr = recvBuf.substring(addrIndex, nameIndex);			
 	break;
      }
   }
 }*/
  
  connectCmd += slaveAddr;
  connectCmd += "\r\n";
  int connectOK = 0;
  Serial.print("Connecting to slave:");
  Serial.print(slaveAddr);
  Serial.println(slaveName);

  do{
    blueToothSerial.print("/*put slave address here*/");
    recvBuf = "";
    while(1){
      if(blueToothSerial.available()){
        recvChar = blueToothSerial.read();
 	recvBuf += recvChar;
 	if(recvBuf.indexOf("CONNECT:OK") != -1){
          connectOK = 1;
 	  Serial.println("Connected!");
 	  blueToothSerial.print("Connected!");
 	  break;
 	}else if(recvBuf.indexOf("CONNECT:FAIL") != -1){
 	  Serial.println("Connect again!");
 	  break;
 	}
      }
    }
  }while(0 == connectOK);
}
Exemplo n.º 8
0
/* This function will attempt a connection to the stored remote address
   The first time you connect the the RN-42 HID, the master device will
   need to initiate the connection. The first time a connection is made
   the bluetooth address of the master device will be stored on the RN-42.
   If no remote address is stored, a connection will not be made. */
uint8_t makeyMateClass::connect()
{
  freshStart();  // Get the module disconnected, and out of command mode
  
  while (!enterCommandMode())
  {  // Enter command mode
    delay(BLUETOOTH_RESPONSE_DELAY);
  }
  delay(BLUETOOTH_RESPONSE_DELAY);
  bluetooth.flush();
  
  /* get the remote address and print it in the serial monitor */
  bluetooth.print("GR");  // Get the remote address
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  if (bluetooth.peek() == 'N')  // Might say "No remote address stored */
  {  // (bluetooth address is hex values only, so won'te start with 'N'.
    Serial.println("Can't connect. No paired device!");
    bluetooth.flush();
    bluetooth.print("---");  // exit command mode
    bluetooth.write('\r');
    return 0;  // No connect is attempted
  }
  else if (bluetooth.available() == 0)  
  { // If we can't communicate with the module at all, print error
    Serial.println("ERROR!");
    return 0;  // return error
  }
  /* otherwise print the address we're trying to connect to */
  Serial.print("Attempting to connect to: ");
  while (bluetooth.available())
    Serial.write(bluetooth.read());
    
  /* Attempt to connect */
  bluetooth.print("C");  // The connect command
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  while (bluetooth.available())
    Serial.write(bluetooth.read());  // Should print "TRYING"
  
  return 1;
}
Exemplo n.º 9
0
/* This function will set the RN-42 into HID mode, from SPP mode.
   Requires a reboot to take effect! */
uint8_t makeyMateClass::setHIDMode(void)
{
  if (bluetooth.available())
    bluetooth.flush();	// Get rid of any characters in the buffer, we'll need to check it fresh

    bluetooth.print("S~,6");  // Bluetooth HID Mode
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  bluetoothReceive(rxBuffer);

  /* Double check the setting, output results in Serial monitor */
  bluetooth.flush();
  bluetooth.print("G~");
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  Serial.print("Profile set to: ");
  while (bluetooth.available())
    Serial.write(bluetooth.read());

  return bluetoothCheckReceive(rxBuffer, "AOK", 3);
}
Exemplo n.º 10
0
void ICACHE_FLASH_ATTR FlashHM() {  //server request to flash avr file to HM...file exists on spiffs	
	if (!MyWebServer.isAuthorized()) return;

	String fname = "";	
	if (server.hasArg("fname")) { fname=server.arg("fname"); }

	if (fname == "") return server.send(200, "text/html", "Flashing File NOT FOUND");;
	DebugPrintln("FLashing :" + server.arg("fname"));
	MyWebServer.OTAisflashing = true;	
	//delay(200);
#ifdef SoftSerial
	qCon.enableRx(true);
#endif
	qCon.flush();	
//	delay(10);
	qCon.begin(115200);  //HM speed for flashing with optiboot	
	Esp8266AVRFlash.FlashAVR(&qCon, "/"+fname);  //flashAVR HM
	qCon.flush();  	
	server.send(200, "text/html", "Flashing AVR....please wait...will auto-reboot...do NOT touch system!!!");
	delay(2000);
	ESP.restart(); //restart ESP after reboot.....
}
Exemplo n.º 11
0
/* This function can send one of the 5 special configuration commands:
   0: Disable all special commands
   4: Disable reading values of GPIO3 and 6 on power-up.
   16: Configure firmware to optimize for low-latency transfers.
   128: Allow for fast reconnect.
   256: Set 2-stop bit mode on UART.
   
   Most of these are not recommended, but the low-latency is useful. */
uint8_t makeyMateClass::setSpecialConfig(uint8_t num)
{
  if (bluetooth.available())
    bluetooth.flush();	// Get rid of any characters in the buffer, we'll need to check it fresh

    bluetooth.print("SQ,");  // SQ sets special config
  bluetooth.print(num);  // Should print ASCII decimal vaule
  bluetooth.write('\r');

  delay(BLUETOOTH_RESPONSE_DELAY);  // Response will go to software serial buffer
  bluetoothReceive(rxBuffer);

  /* Double check the setting, output results in Serial monitor */
  bluetooth.flush();
  bluetooth.print("GQ");
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  Serial.print("Special Config set to: ");
  while (bluetooth.available())
    Serial.write(bluetooth.read());

  return bluetoothCheckReceive(rxBuffer, "AOK", 3);  
}
Exemplo n.º 12
0
/* This function enables low power SNIFF mode. Send a 4-byte string as the 
   sleepConfig variable
   "0000" = disabled
   e.g.: "0050" = Wake up every 50ms
   "8xxx" = Enables deep sleep mode */
uint8_t makeyMateClass::setSleepMode(char * sleepConfig)
{
  if (bluetooth.available())
    bluetooth.flush();	// Get rid of any characters in the buffer, we'll need to check it fresh

    bluetooth.print("SW,");  // SW sets the sniff mode
  bluetooth.print(sleepConfig);  // Should print ASCII vaule
  bluetooth.write('\r');

  delay(BLUETOOTH_RESPONSE_DELAY);  // Response will go to software serial buffer
  bluetoothReceive(rxBuffer);

  /* Double check the setting, output results in Serial monitor */
  bluetooth.flush();
  bluetooth.print("GW");
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  Serial.print("Deep Sleep Mode set to: ");
  while (bluetooth.available())
    Serial.write(bluetooth.read());

  return bluetoothCheckReceive(rxBuffer, "AOK", 3);
}
Exemplo n.º 13
0
/* This function enables or disables authentication (pincode pairing)
   Two options are available for Authmode:
   0: Disabled
   1: Enabled */
uint8_t makeyMateClass::setAuthentication(uint8_t authMode)
{
  if (bluetooth.available())
    bluetooth.flush();	// Get rid of any characters in the buffer, we'll need to check it fresh

    bluetooth.print("SA,");  // SA sets the authentication
  bluetooth.print(authMode);  // Should print ASCII vaule
  bluetooth.write('\r');

  delay(BLUETOOTH_RESPONSE_DELAY);  // Response will go to software serial buffer
  bluetoothReceive(rxBuffer);

  /* Double check the setting, output results in Serial monitor */
  bluetooth.flush();
  bluetooth.print("GA");
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  Serial.print("Authentication Mode set to: ");
  while (bluetooth.available())
    Serial.write(bluetooth.read());

  return bluetoothCheckReceive(rxBuffer, "AOK", 3);
}
Exemplo n.º 14
0
/* This function issues the reboot command, and adds a lengthy delay to 
   give the RN-42 time to restart. */
uint8_t makeyMateClass::reboot(void)
{
  if (bluetooth.available())
    bluetooth.flush();	// Get rid of any characters in the buffer, we'll need to check it fresh

  bluetooth.print("R,1");  // reboot command
  bluetooth.write('\r');
  delay(BLUETOOTH_RESPONSE_DELAY);
  bluetoothReceive(rxBuffer);

  delay(BLUETOOTH_RESET_DELAY);

  return bluetoothCheckReceive(rxBuffer, "Reboot!", 7);
}
Exemplo n.º 15
0
/* freshStart() attempts to get the module into a known state from 
 any of these 3 possible states:
 1) Connected - Sending 0 will disconnect the module, then we'll put 
 it into command mode
 2) Disconnected - In this case, we'll just get it into command mode
 3) Disconnected, already in Command Mode - Sending 0 makes the module
 unresponsive until an undefined \r\n's are sent. I can't find any 
 reason for this in the RN-42 User manual 
 
 Once this function has completed, the RN-42 should be disconnected, 
 in non-command mode */
void makeyMateClass::freshStart(void)
{
  int timeout = 1000;  // timeout, in the rare case the module is unresponsive
  bluetooth.write((uint8_t) 0);	// Disconnects, if connected
  delay(BLUETOOTH_RESPONSE_DELAY);
  
  bluetooth.flush();  // delete buffer contents
  bluetooth.print("$$$");  // Command mode string
  do // This gets the module out of state 3
  {  // continuously send \r until there is a response, usually '?'
    bluetooth.write('\r');  
    Serial.print("-");  // Debug info for how many \r's required to get a respsonse
  } while ((!bluetooth.available()) && (timeout-- > 0));
  
  while (bluetooth.available())
    Serial.write(bluetooth.read());
  delay(BLUETOOTH_RESPONSE_DELAY);
  bluetooth.flush();  // delay and flush the receive buffer
  
  bluetooth.print("---");  // exit command mode
  bluetooth.write('\r');
  delay(BLUETOOTH_RESET_DELAY);  // longer delay
  bluetooth.flush();  // Flush the receive buffer
}
Exemplo n.º 16
0
/* enterCommandMode() will get the module into command mode if it's either
   in slow-STAT-blink mode, or already in command mode. Will not disconnect
   if the module is already connected.
   returns a 1 if command mode was successful, 0 otherwise */
uint8_t makeyMateClass::enterCommandMode(void)
{	 	
  bluetooth.flush();  // Get rid of any characters in the buffer, we'll need to check it fresh
  bluetooth.print("$$$");  // Command mode string
  bluetooth.write('\r');  // Will give us the ?, if we're already in command mode
  delay(BLUETOOTH_RESPONSE_DELAY);
  bluetoothReceive(rxBuffer);  // receive all response chars into rxBuffer

  if (rxBuffer[0] == '?')  // RN-42 will respond with ? if we're already in cmd
  {
    return 1;
  }
  else
  {
    return bluetoothCheckReceive(rxBuffer, "CMD", 3);
  }	
}
Exemplo n.º 17
0
void loop()
{
	Button *b;
	bool oneWasTriggered = false;
	for(int i = 0; i < STICK_DIRECTION_COUNT; i++) {
		b = stick->getJoystickDirection(i);
		pinMode(b->getPinNumber(), INPUT);
		if(digitalRead(b->getPinNumber()) == HIGH) {
			serial.print(b->getCharacter());
			oneWasTriggered = true;
		}
	}
	if(oneWasTriggered){
		serial.println(' ');
	}
	serial.flush();
	delay(20);
}
Exemplo n.º 18
0
void setup() {
  p=(int*)malloc(11*sizeof(int));
  Serial.begin(9600);
  pinMode(6, INPUT);
  pinMode(7, OUTPUT);
  pinMode(11, 'output');
  pinMode(12, 'output');
  pinMode(10, 'output');
  pinMode(b1, 'output');
  pinMode(b0, 'output');
  digitalWrite(11,LOW);
  digitalWrite(10,LOW);
  digitalWrite(12,LOW);
  pinMode(b1, 'output');
  holding=0;
  pinMode(b0, 'output');
setupBlueToothConnection();
  
    delay(1000);
  Serial.flush();
  blueToothSerial.flush();
}
Exemplo n.º 19
0
void init_rfid()
{
    Serial.println(getstr(STR_INIT_RFID_READER));

    rfidport.begin(115200);

    // SoftwareSerial 최초 사용시 몇 바이트는 깨져서 들어감.
    // 이후 RFID 리더와 통신을 원활히 할 수 있도록 더미 데이터 전송
    for(int i = 0 ; i < 8 ; i++)
        rfidport.write((uint8_t)0x00);

    rfidport.flush();

    FirmwareInfo fwinfo;

    // 가끔 응답 없는 경우가 있어 응답할때까지 5번정도 시도
    bool connected = false;
    for(int i = 0 ; i < 5 ; i++)
    {
        if(is3400.fwinfo(fwinfo))
        {
            connected = true;
            break;
        }
    }

    if(connected)
    {
        Serial.print(getstr(STR_TITLE_RFID_READER));
        Serial.println(fwinfo.fwinfo);
    }
    else
    {
        Serial.println(getstr(STR_RFID_READER_NOT_RESPONDING));
    }

    Serial.println();
}
Exemplo n.º 20
0
uint8_t 	EV3_Setup(uint8_t port, uint8_t type)  // Sets up the sensor to data mode
{
	if(port == PORT_1){  // PORT_1
    DDRC |= 0x04;   // Set the Rx pin as output
    DDRC &= 0xFE;

    uint8_t mode;   // Calculate the mode as a number 
    if(type < TYPE_SENSOR_EV3_COLOR_M0)
      mode = type - TYPE_SENSOR_EV3_US_M0;
    else if(type < TYPE_SENSOR_EV3_GYRO_M0)
      mode = type - TYPE_SENSOR_EV3_COLOR_M0;
    else if(type < TYPE_SENSOR_EV3_INFRARED_M0)
      mode = type - TYPE_SENSOR_EV3_GYRO_M0;
    else if(type < TYPE_SENSOR_EV3_INFRARED_M5+1)
      mode = type - TYPE_SENSOR_EV3_INFRARED_M0;
    else mode = 0;


    data16[0] = dat16[type-43];
    sets[0] = setsd[type-43];

    sensor1.begin(2400);  // Start SoftwareSerial at base Baud rate
    sensor1.write(BYTE_ACK);  // Write ACK 
    setupDone=false;    
    while(!setupDone){
      if(sensor1.available()){   // Check if data is available 
        if(sensor1.read() == BYTE_ACK){   // if an ACK Byte was read
          delay(1);
          sensor1.write(BYTE_ACK);      // Write 2 ACKs (because, sometimes SoftwareSerial might not write the data properly)
          delay(1);
          sensor1.write(BYTE_ACK);
          sensor1.end();
          sensor1.flush();
          sensor1.begin(57600);     // Try to read at higher baud rate
          delay(10);
          if((sensor1.read() & CMD_MASK) == CMD_DATA){  // If the sensor had entered Data Mode
            sensor1.write(BYTE_NACK);     // Write NACK at higher baud rate
              setupDone=true;   // quit setup
          }
          else {        // if corrupt data or no data was received, go back to base baud rate
            sensor1.end();      
            sensor1.begin(2400);
          }
        }
      }
    }

    sensor1.write(BYTE_NACK);   // Keep the sensor in Data mode
    if(mode != 0){              // If a mode other than 0 is required, write the mode datas
      sensor1.write(CMD_SELECT);
      sensor1.write(mode);
      sensor1.write(check(0x00,CMD_SELECT,mode));
    }
    sensor1.write(BYTE_NACK);
  }
  else{                    // PORT_2
    DDRC |= 0x08; 
    DDRC &= 0xFD;

    uint8_t mode;
    if(type < TYPE_SENSOR_EV3_COLOR_M0)
      mode = type - TYPE_SENSOR_EV3_US_M0;
    else if(type < TYPE_SENSOR_EV3_GYRO_M0)
      mode = type - TYPE_SENSOR_EV3_COLOR_M0;
    else if(type < TYPE_SENSOR_EV3_INFRARED_M0)
      mode = type - TYPE_SENSOR_EV3_GYRO_M0;
    else if(type < TYPE_SENSOR_EV3_INFRARED_M5+1)
      mode = type - TYPE_SENSOR_EV3_INFRARED_M0;
    else mode = 0;

    data16[1] = dat16[type-43];
    sets[1] = setsd[type-43];

    sensor2.begin(2400);
    sensor2.write(BYTE_ACK);
    setupDone=false;
    while(!setupDone){
      if(sensor2.available()){
        if(sensor2.read() == BYTE_ACK){
          delay(1);
          sensor2.write(BYTE_ACK);
          delay(1);
          sensor2.write(BYTE_ACK);
          sensor2.end();
          sensor2.flush();
          sensor2.begin(57600);
          delay(10);
          if((sensor2.read() & CMD_MASK) == CMD_DATA){
            sensor2.write(BYTE_NACK);
              setupDone=true;
          }
          else {
            sensor2.end();
            sensor2.begin(2400);
          }
        }
      }
    }

    sensor2.write(BYTE_NACK);
    if(mode != 0){
      sensor2.write(CMD_SELECT);
      sensor2.write(mode);
      sensor2.write(check(0x00,CMD_SELECT,mode));
    }
    sensor2.write(BYTE_NACK);
  }
  return 0;
}
Exemplo n.º 21
0
void EV3_Reset(){   // Called to reset the SoftwareSerial and use the pin for some other function
  sensor1.flush();
  sensor1.end();
  sensor2.flush();
  sensor2.end();
}
Exemplo n.º 22
0
long	EV3_Update(uint8_t port)
{
	if( port == PORT_1 ){

    sensor1.write(BYTE_NACK);   // To keep the sensor in data mode
    if(sensor1.available())
	{
      se = sensor1.read();      // Read the first CMD byte
      if ( (se & CMD_MASK) == CMD_DATA ){ // if it is data
        ret = 0;
        byte chk = 0xff^se;       // Checksum 
        for(byte n=0 ; n < sets[0] ; n++ ){   // read all sets
          l[n] = sensor1.read();
          m[n] = (data16[0])? sensor1.read() : 0;
          chk = chk^l[n]^m[n];
          ret = (ret<<(data16[0]?16:8)) ;
          ret |= (m[n]<<8) | l[n] ;
        }
        if (sensor1.read() == chk){ // if the checksum is matched, return data
			return ret;
        } else {
			sensor1.flush();    // else flush
        return -2;
        }
      }
    }
    sensor1.write(BYTE_NACK);
/*    sensor1.end();
    sensor1.begin(2400);
    sensor1.write(0x04);
    sensor1.end();
    sensor1.begin(57600);
*/    return -4;
	} else if( port == PORT_2 ){

    sensor2.write(BYTE_NACK);
    if(sensor2.available()){
      se = sensor2.read();
      if ( (se & CMD_MASK) == CMD_DATA ){
        ret = 0;
        byte chk = 0xff^se;
        for(byte n=0 ; n < sets[1] ; n++ ){
          l[n] = sensor2.read();
          m[n] = (data16[1])? sensor2.read() : 0;
          chk = chk^l[n]^m[n];
          ret = (ret<<(data16[1]?16:8));
          ret |= (m[n]<<8) | l[n] ;
        }
        if (sensor2.read() == chk){
          return ret;
        } else {
        sensor2.flush();
        return -2;
        }
      }
    }

    sensor2.write(BYTE_NACK);
/*    sensor2.end();
    sensor2.begin(2400);
    sensor2.write(0x04);
    sensor2.end();
    sensor2.begin(57600);
*/    return -4;
	}
  return -6;
}
Exemplo n.º 23
0
void loop()
{
   // initialize http service
   if(count%10 == 0){
     count= 0 ;
     gprsSerial.flush();
      Serial.flush();
      gprsSerial.println("AT+CIPSHUT");
      delay(100);
      toSerial();
      gprsSerial.println("AT+CGATT?");
      delay(100);
      toSerial();
    
      // attach or detach from GPRS service 
      gprsSerial.println("AT+CGATT=1");
      delay(100);
      toSerial();
    
    
      // bearer settings
      gprsSerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
      delay(2000);
      toSerial();
    
      // bearer settings
      gprsSerial.println("AT+SAPBR=3,1,\"APN\",\"airtelgprs.com\"");
      delay(2000);
      toSerial();
    
      // bearer settings
      gprsSerial.println("AT+SAPBR=1,1");
      delay(2000);
      toSerial();
   }
   count++;
   Receive_GPS_Data();
   gprsSerial.println("AT+HTTPINIT");
   delay(2000); 
   toSerial();

   // set http param value
   gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://shuttletracker.hostei.com/?lat="+save_lati+"&long="+save_lg+"&speed="+save_spd+"\"");
   delay(2000);
   toSerial();

   // set http action type 0 = GET, 1 = POST, 2 = HEAD
   gprsSerial.println("AT+HTTPACTION=0");
   delay(6000);
   toSerial();

   // read server response
   gprsSerial.println("AT+HTTPREAD"); 
   delay(1000);
   toSerial();

   gprsSerial.println("");
   gprsSerial.println("AT+HTTPTERM");
   toSerial();
   delay(300);

   gprsSerial.println("");
   delay(30000);
   finish=0;pos_cnt = 0;
}
Exemplo n.º 24
0
// * Receiving the lenght of bytes from Serial port
void HerkulexClass::readData(int size)
{
	int i = 0;
    int beginsave=0;
    int Time_Counter=0;
    switch (port)
	{
	case SSerial:

        while((SwSerial.available() < size) & (Time_Counter < TIME_OUT)){
        		Time_Counter++;
        		delayMicroseconds(1000);  //wait 1 millisecond for 10 times
		}
        	
		while (SwSerial.available() > 0){
			byte inchar = (byte)SwSerial.read();
			if ( (inchar == 0xFF) & ((byte)SwSerial.peek() == 0xFF) ){
					beginsave=1; 
					i=0; 				 // if found new header, begin again
			}
			if (beginsave==1 && i<size) {
				   dataEx[i] = inchar;
				   i++;
			}
		}
		SwSerial.flush();
		break;
	
	#if defined (__AVR_ATmega1280__) || defined (__AVR_ATmega128__) || defined (__AVR_ATmega2560__)
	case HSerial1:
		while((Serial1.available() < size) & (Time_Counter < TIME_OUT)){
				Time_Counter++;
				delayMicroseconds(1000);
		}
		while (Serial1.available() > 0){
			byte inchar = (byte)Serial1.read();
			//printHexByte(inchar);
			if ( (inchar == 0xFF) & ((byte)Serial1.peek() == 0xFF) ){
				beginsave=1;
				i=0;
			}
            if (beginsave==1 && i<size) {
                       dataEx[i] = inchar;
                       i++;
			}
		}
		break;
	
	case HSerial2:
	    while((Serial2.available() < size) & (Time_Counter < TIME_OUT)){
        		Time_Counter++;
        		delayMicroseconds(1000);
		}
        	
		while (Serial2.available() > 0){
			byte inchar = (byte)Serial2.read();
			if ( (inchar == 0xFF) & ((byte)Serial2.peek() == 0xFF) ){
					beginsave=1;
					i=0; 					
			}
			if (beginsave==1 && i<size) {
				   dataEx[i] = inchar;
				   i++;
			}
		}
		break;

	case HSerial3:
		while((Serial3.available() < size) & (Time_Counter < TIME_OUT)){
			Time_Counter++;
			delayMicroseconds(1000);
		}
		
		while (Serial3.available() > 0){
			byte inchar = (byte)Serial3.read();
			if ( (inchar == 0xFF) & ((byte)Serial3.peek() == 0xFF) ){
					beginsave=1;
					i=0; 
			}
			if (beginsave==1 && i<size) {
				   dataEx[i] = inchar;
				   i++;
			}
		}
		break;
	#elif (__AVR_ATmega32U4__)
	case HSerial1:
		while((Serial1.available() < size) & (Time_Counter < TIME_OUT)){
			Time_Counter++;
			delayMicroseconds(1000);
		}
		while (Serial1.available() > 0){
			byte inchar = (byte)Serial1.read();
			//printHexByte(inchar);
			if ( (inchar == 0xFF) & ((byte)Serial1.peek() == 0xFF) ){
				beginsave=1;
				i=0;
			}
            if (beginsave==1 && i<size) {
                       dataEx[i] = inchar;
                       i++;
			}
		}
		break;
	#endif
	}
}
Exemplo n.º 25
0
void clearWifi() {
    ESPserial.flush();
}
// TODO: This is extremely kludgy right now. 
//		The delay()'s should be made more robust, faster by periodically checking for a response,
//		while still checking for a timeout.
//		Checking for just the first char of a response is a questionabl solution.
//		Needs more testing.  So many unknown states the BlueSMiRF could be in. 
//		We need some flow chart action here.
int SFE_MetaWatch::connect()
{
	char c;
	int timeout = CONNECT_TIMEOUT;
	
	bt.print('\r');	// Clear any previous commands
	delay(BLUETOOTH_RESPONSE_DELAY);
	bt.flush();
	// Entering command mode. Should either print "CMD" or "?" if already there
	while ((c != 'C') && (c != '?') && (timeout > 0))
	{
		bt.print("$$$");	// Enter command mode
		delay(BLUETOOTH_RESPONSE_DELAY);
		c = bt.read();	// Read first character of response
		bt.flush();
		timeout--;
	}
	
	if (timeout == 0)
		return -2;
	else
		timeout = CONNECT_TIMEOUT;
	
	// After sending connect command, should print "TRYING", may also print "ERR-connected"
	while ((c != 'T') && (c != 'E') && (timeout > 0)) 
	{
		bt.print("C,");
		bt.print(watchAddress);
		bt.print('\r');
		delay(BLUETOOTH_RESPONSE_DELAY);
		c = bt.read();
		bt.flush();
		timeout--;
	}
	// If there was an error, try to exit command mode
	if ((c == 'E') || timeout == 0)
	{
		bt.print("---");	// Exit command mode
		bt.print('\r');
		delay(BLUETOOTH_RESPONSE_DELAY);
		bt.flush();
	}
	
	if (timeout == 0)
		return -1;	// Return -1 if connect command error
	else if (c == 'E')
		return 2;	// Return 2 if we think we're already connected
	else
		return 1;	// Return 1 if all went according to plan
}