My Cart

How to Send Sensor Data through the Serial Port Profile Using Bluetooth Connectivity Kit

Difficulty Level: Intermediate -

1. Introduction

This tutorial allows the user to measure temperature and amount of light and send it through Bluetooth to an SPP (Serial Port Profile) application.

Ingredients:

    - 1 x Bluetooth Connectivity Kit:
    • 1x Platform
    • 1x Bluetooth PRO
    • 1x XBee Shield
    • 1x LDR
    • 1x 220Ω resistor
    • 1x Temperature sensor (MCP97004)
    • 1x Breadboard
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now
NOTE: Depending on the platform chosen to develop the project, the ingredients and the schematics shown in this tutorial can vary.

This project can be developed with Arduino or Intel Galileo. It is also compatible with Raspberry Pi using the Raspberry Pi to Arduino shields connection bridge.

For further information about the Bluetooth Pro Shield, consult the main tutorial.

Step 1: Connection

Connect the XBee shield to Arduino and then connect Bluetooth PRO module or you can connect it directly to Raspberry Pi connection bridge. Connect the LDR with the resistor and the temperature sensor in the breadboard as you can see in the next diagram.

Connect two wires, red and black, to the two long rows on the side of the breadboard to provide access to the 5 volt supply and ground.

Place the temperature sensor with the flat part looking down like the diagram. Connect the right leg of the temperature sensor to GND and the left to 5 volt. Connect the central leg to the second pin at bottom right of the shield (Analog 4) in Arduino and to the first pin at bottom left (Analog 0) in Raspberry Pi.

Connect the left leg of the light sensor sensor to GND and the right to the first pin at bottom right of the shield (Analog 5) in Arduino and to the second pin at bottom left (Analog 1) in Raspberry Pi and also to a 220 Ohm resistor. Finally connect the other leg of the resistor to 5 volt. This structure is called "voltage divider".

Step 2: The Code

Arduino:

Code:
/*  
 *  Bluetooth module PRO
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Luis Miguel MartĂ­
 */
 
#define LDRPIN 5
#define NTCPIN 4

char error[] = "SYNTAX ERROR";
char dis[100];
char tempmess[40];
char dev;
char mac[] = "**:**:**:**:**:**";	//You have to use your Bluetooth MAC
char temper[6];
uint32_t period;
float tdegrees;
boolean sending = false;
int LDR_val = 0; 

void setup() {
	
  Serial.begin(115200);
  delay(1000);
  Serial.print("Setting up...");
  while (sendATcommand("AT", "OK", error, 1000) == 2);
  while (sendATcommand("SET BT AUTH * 0000", "", error, 1000) == 2);
  while (sendATcommand("SET CONTROL CONFIG 0000 0000 0100", "", error, 1000) == 2);
  delay(1000);
  while (!pair(mac));
  delay(2000);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  waitApp(mac);
  sending = true;
  period = millis();
}

void loop() {
  
  LDR_val = analogRead(LDRPIN); // read input LDR value

  if ((millis() - period > 5000) && sending) {
    period = millis();
    get_temp();
    for(byte i=0; i<sizeof(tempmess);i++){
      tempmess[i] = 0;
    }
    for(byte i=0; i<sizeof(temper);i++){
      temper[i] = 0;
    }
    float2String(tdegrees, temper,2);
    //print all through serial
    Serial.print("Temperature: ");
    Serial.print(temper);
    Serial.println(" Celsius degrees");
    
    Serial.print("LDR = ");
    Serial.println(LDR_val);
    delay(1000);

  }
  if(sending) checkDisconnect();
  while(!sending){
    sending = waitApp(mac);
  }
}

int8_t sendATcommand(char* ATcommand, char* expected_answer, char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  if (ATcommand != NULL)  Serial.println(ATcommand);  // Send the AT command
  x = 0;
  previous = millis();
  do {
    if (Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        Serial.println(expected_answer);
        while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
        answer = 1;
        Serial.println("Expected response");
      } else if (strstr(response, error_answer) != NULL) {
        answer = 2;
        while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  return answer;
}

byte pair(char* device) {
  // call to you phone (bluetooth MAC)
  char aux_string[35];
  sprintf(aux_string, "CALL %s 1101 RFCOMM\r\n", device);
  while (sendATcommand(aux_string, "CALL 0", error, 1000) == 2);
  //while (sendATcommand("CALL 8c:3a:e3:ec:77:60 1101 RFCOMM", "CALL 0", error, 1000) == 2);
  //  delay(500);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  byte ans = 0;
  while (ans == 0) {
    ans = getResponse("NO CARRIER 0 ERROR", 1000);
  }
  ans = 0;
  sprintf(aux_string, "PAIR %s\r\n", device);
  char aux_string2[35];
  sprintf(aux_string2, "PAIR %s OK\r\n", device);
  while (ans == 2 || ans == 0) {
    ans = sendATcommand(aux_string, aux_string2 , error, 1000);
  }
  if (ans == 1) return 1;
  else return 0;
}

byte waitApp(char* device) {
  byte ans = 0;
  char aux_string[40];
  sprintf(aux_string, "RING 0 %s 1 RFCOMM", device);
  while (ans == 0) {
    ans = getResponse(aux_string, 1000);
  }
  if (ans == 1) return 1;
  else return 0;
}

int8_t getResponse(char* expected_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  // Clean the input buffer
  x = 0;
  previous = millis();
  do {
    if (Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        Serial.println(expected_answer);
        answer = 1;
        Serial.println("Expected response");
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  return answer;
}

void get_temp() {
  tdegrees = analogRead(NTCPIN) * 5 / 1024.0;
  tdegrees = tdegrees - 0.5;
  tdegrees = tdegrees / 0.01;
}

void float2String (float fl, char str[], int N)
{

  boolean neg = false;

  if ( fl < 0 ) {
    neg = true;
    fl *= -1;
  }

  float numeroFloat = fl;
  int parteEntera[10];
  int cifra;
  long numero = (long)numeroFloat;
  int sie = 0;

  while (1) {
    sie = sie + 1;
    cifra = numero % 10;
    numero = numero / 10;
    parteEntera[sie - 1] = cifra;
    if (numero == 0) {
      break;
    }
  }
  int indice = 0;
  if ( neg ) {
    indice++;
    str[0] = '-';
  }
  for (int i = sie - 1; i >= 0; i--)
  {
    str[indice] = parteEntera[i] + '0';
    indice++;
  }
  str[indice] = '.';
  indice++;
  numeroFloat = (numeroFloat - (int)numeroFloat);
  for (int i = 1; i <= N ; i++)
  {
    numeroFloat = numeroFloat * 10;
    cifra = (long)numeroFloat;
    numeroFloat = numeroFloat - cifra;
    str[indice] = char(cifra) + 48;
    indice++;
  }
  str[indice] = '\0';
}

void checkDisconnect(){
  byte z = 0;
  for(byte i=0; i<sizeof(dis);i++){
      dis[i] = 0;
    }
  if (sending){
  while(Serial.available()>0){
    
    dis[z] = Serial.read();
    z++;
    delay(10);
    
  }
  byte i = 0;
  while(dis[i] != 'N'){
    if (dis[i+1]=='O'){
      if (dis[i+2]==' '){
        if (dis[i+3]=='C'){
          if (dis[i+4]=='A'){
            if (dis[i+5]=='R'){
              if (dis[i+6]=='R'){
                if (dis[i+7]=='I'){
                  if (dis[i+8]=='E'){
                    if (dis[i+9]=='R'){
                      Serial.print("Disconnected");
                      delay(1000);
                      while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
                      sending = false;
                    } else i++;
                  } else i++;
                } else i++;
              } else i++;
            } else i++;
          } else i++;
        } else i++;
      } else i++;
    } else i++;
      
      
  }
  }
}
        

Raspberry Pi:

Code:
/*  
 *  Bluetooth module PRO
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis MartĂ­n
 */
 
#include "arduPi.h"

#define LDRPIN 1
#define NTCPIN 0

char error[] = "SYNTAX ERROR";
char dis[100];
char tempmess[40];
char dev;
char mac[] = "**:**:**:**:**:**";	//You have to use your Bluetooth MAC
char temper[6];
uint32_t period;
float tdegrees;
boolean sending = false;
float LDR_val; 
char ldr[6];
int8_t sendATcommand(const char* ATcommand, const char* expected_answer, const char* error_answer, unsigned int timeout);
byte pair(char* device);
byte waitApp(char* device);
int8_t getResponse(char* expected_answer, unsigned int timeout);
void get_temp();
void float2String (float fl, char str[], int N);
void checkDisconnect();

void setup() {
	
  pinMode(LDRPIN, INPUT);
  pinMode(NTCPIN, INPUT);
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(1000);
  
  printf("Setting up...\n");
  while (sendATcommand("AT", "OK", error, 1000) == 2);
  while (sendATcommand("SET BT AUTH * 0000", "", error, 1000) == 2);
  while (sendATcommand("SET CONTROL CONFIG 0000 0000 0100", "", error, 1000) == 2);
  delay(1000);
  while (!pair(mac));
  delay(2000);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  waitApp(mac);
  sending = true;
  delay(1000);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  period = millis();
}

void loop() {
	
 

  if ((millis() - period > 5000) && sending) {
    period = millis();
    get_temp();
    LDR_val = analogRead(1); // read input LDR value
    for(byte i=0; i<sizeof(tempmess);i++){
      tempmess[i] = 0;
    }
    for(byte i=0; i<sizeof(temper);i++){
      temper[i] = 0;
    }
    float2String(tdegrees, temper,2);
    float2String(LDR_val, ldr,2);
    //print all through serial
    Serial.print("Temperature: ");
    Serial.print(temper);
    Serial.println(" Celsius degrees");
    printf("Temperature: %s Celsius degrees\n",temper);
    //sprintf(tempmess, "Temperature: %s Celsius degrees", temper);
    
    Serial.print("LDR = ");
	Serial.println(ldr);
	printf("LDR = %s\n",ldr);
	delay(1000);

  }
  if(sending) checkDisconnect();
  while(!sending){
    sending = waitApp(mac);
  }
  delay(200);
}

int8_t sendATcommand(const char* ATcommand, const char* expected_answer, const char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  if (ATcommand != NULL)  Serial.println(ATcommand);  // Send the AT command
  x = 0;
  previous = millis();
  do {
    if (Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        Serial.println(expected_answer);
        while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
        answer = 1;
        printf("Expected response\n");
      } else if (strstr(response, error_answer) != NULL) {
        answer = 2;
        while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  return answer;
}

byte pair(char* device) {
  // call to you phone (bluetooth MAC)
  char aux_string[35];
  sprintf(aux_string, "CALL %s 1101 RFCOMM\r\n", device);
  printf("CALL %s 1101 RFCOMM\r\n", device);
  while (sendATcommand(aux_string, "CALL 0", error, 1000) == 2);
  //while (sendATcommand("CALL 8c:3a:e3:ec:77:60 1101 RFCOMM", "CALL 0", error, 1000) == 2);
  //  delay(500);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  byte ans = 0;
  while (ans == 0) {
    ans = getResponse("NO CARRIER 0 ERROR", 1000);
  }
  ans = 0;
  sprintf(aux_string, "PAIR %s\r\n", device);
  char aux_string2[35];
  sprintf(aux_string2, "PAIR %s OK\r\n", device);
  while (ans == 2 || ans == 0) {
    ans = sendATcommand(aux_string, aux_string2 , error, 1000);
  }
  if (ans == 1) return 1;
  else return 0;
}

byte waitApp(char* device) {
  byte ans = 0;
  char aux_string[40];
  sprintf(aux_string, "RING 0 %s 1 RFCOMM", device);
  while (ans == 0) {
    ans = getResponse(aux_string, 1000);
  }
  if (ans == 1) return 1;
  else return 0;
}

int8_t getResponse(char* expected_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  // Clean the input buffer
  x = 0;
  previous = millis();
  do {
    if (Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        Serial.println(expected_answer);
        answer = 1;
        printf("Expected response\n");
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  return answer;
}

void get_temp() {
  tdegrees = analogRead(NTCPIN) * 5 / 1024.0;
  tdegrees = tdegrees - 0.5;
  tdegrees = tdegrees / 0.01;
}

void float2String (float fl, char str[], int N)
{

  boolean neg = false;

  if ( fl < 0 ) {
    neg = true;
    fl *= -1;
  }

  float numeroFloat = fl;
  int parteEntera[10];
  int cifra;
  long numero = (long)numeroFloat;
  int sie = 0;

  while (1) {
    sie = sie + 1;
    cifra = numero % 10;
    numero = numero / 10;
    parteEntera[sie - 1] = cifra;
    if (numero == 0) {
      break;
    }
  }
  int indice = 0;
  if ( neg ) {
    indice++;
    str[0] = '-';
  }
  for (int i = sie - 1; i >= 0; i--)
  {
    str[indice] = parteEntera[i] + '0';
    indice++;
  }
  str[indice] = '.';
  indice++;
  numeroFloat = (numeroFloat - (int)numeroFloat);
  for (int i = 1; i <= N ; i++)
  {
    numeroFloat = numeroFloat * 10;
    cifra = (long)numeroFloat;
    numeroFloat = numeroFloat - cifra;
    str[indice] = char(cifra) + 48;
    indice++;
  }
  str[indice] = '\0';
}

void checkDisconnect(){
  byte z = 0;
  for(byte i=0; i<sizeof(dis);i++){
      dis[i] = 0;
    }
  if (sending){
  if (Serial.available()>0){
  while(Serial.available()>0){
    
    dis[z] = Serial.read();
    z++;
    delay(10);
    
  }
  byte i = 0;
  while(dis[i] != 'N'){
    if (dis[i+1]=='O'){
      if (dis[i+2]==' '){
        if (dis[i+3]=='C'){
          if (dis[i+4]=='A'){
            if (dis[i+5]=='R'){
              if (dis[i+6]=='R'){
                if (dis[i+7]=='I'){
                  if (dis[i+8]=='E'){
                    if (dis[i+9]=='R'){
                      printf("Disconnected\n");
                      delay(1000);
                      while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
                      sending = false;
                    } else i++;
                  } else i++;
                } else i++;
              } else i++;
            } else i++;
          } else i++;
        } else i++;
      } else i++;
    } else i++;
}
  }
  }
}

int main (){
	setup();
	while(1){
		loop();
	}
	return (0);
}

        

Intel Galileo:

Code:
/*  
 *  Bluetooth module PRO
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis MartĂ­n
 */
 
#define LDRPIN 5
#define NTCPIN 4

char error[] = "SYNTAX ERROR";
char dis[100];
char tempmess[40];
char dev;
char mac[] = "**:**:**:**:**:**";
char temper[6];
uint32_t period;
float tdegrees;
boolean sending = false;
int LDR_val = 0; 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);
  delay(1000);
  Serial.print("Setting up...");
  while (sendATcommand("AT", "OK", error, 1000) == 2);
  while (sendATcommand("SET BT AUTH * 0000", "", error, 1000) == 2);
  while (sendATcommand("SET CONTROL CONFIG 0000 0000 0100", "", error, 1000) == 2);
  delay(1000);
  while (!pair(mac));
  delay(2000);
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  waitApp(mac);
  delay(1000);
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  sending = true;
  period = millis();
}

void loop() {
  // put your main code here, to run repeatedly:
  
  LDR_val = analogRead(LDRPIN); // read input LDR value


  if ((millis() - period > 5000) && sending) {
    period = millis();
    get_temp();
    
    for(byte i=0; i<sizeof(tempmess);i++){
      tempmess[i] = 0;
    }
    for(byte i=0; i<sizeof(temper);i++){
      temper[i] = 0;
    }
    float2String(tdegrees, temper,2);
    //print all through serial
    Serial.print("Temperature: ");
    Serial.print(temper);
    Serial.println(" Celsius degrees");
    //sprintf(tempmess, "Temperature: %s Celsius degrees", temper);
    
    Serial.print("LDR = ");
    Serial.println(LDR_val);
    Serial1.print("Temperature: ");
    Serial1.print(temper);
    Serial1.println(" Celsius degrees");
    //sprintf(tempmess, "Temperature: %s Celsius degrees", temper);
    
    Serial1.print("LDR = ");
    Serial1.println(LDR_val);
    delay(1000);

  }
  
  if(sending) checkDisconnect();
  while(!sending){
    sending = waitApp(mac);
  }
}

int8_t sendATcommand(char* ATcommand, char* expected_answer, char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  if (ATcommand != NULL)  Serial1.println(ATcommand);  // Send the AT command
  x = 0;
  previous = millis();
  do {
    if (Serial1.available() != 0) {
      response[x] = Serial1.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        Serial1.println(expected_answer);
        while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
        answer = 1;
        Serial.println("Expected response");
      } else if (strstr(response, error_answer) != NULL) {
        answer = 2;
        while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  return answer;
}

byte pair(char* device) {
  // call to you phone (bluetooth MAC)
  char aux_string[35];
  sprintf(aux_string, "CALL %s 1101 RFCOMM\r\n", device);
  while (sendATcommand(aux_string, "CALL 0", error, 1000) == 2);
  //while (sendATcommand("CALL 8c:3a:e3:ec:77:60 1101 RFCOMM", "CALL 0", error, 1000) == 2);
  //  delay(500);
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  byte ans = 0;
  while (ans == 0) {
    ans = getResponse("NO CARRIER 0 ERROR", 1000);
  }
  ans = 0;
  sprintf(aux_string, "PAIR %s\r\n", device);
  char aux_string2[35];
  sprintf(aux_string2, "PAIR %s OK\r\n", device);
  while (ans == 2 || ans == 0) {
    ans = sendATcommand(aux_string, aux_string2 , error, 1000);
  }
  if (ans == 1) return 1;
  else return 0;
}

byte waitApp(char* device) {
  byte ans = 0;
  char aux_string[40];
  sprintf(aux_string, "RING 0 %s 1 RFCOMM", device);
  while (ans == 0) {
    ans = getResponse(aux_string, 1000);
  }
  if (ans == 1)return 1;
  else return 0;
}

int8_t getResponse(char* expected_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  // Clean the input buffer
  x = 0;
  previous = millis();
  do {
    if (Serial1.available() != 0) {
      response[x] = Serial1.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        Serial1.println(expected_answer);
        answer = 1;
        Serial.println("Expected response");
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  return answer;
}

void get_temp() {
  tdegrees = analogRead(NTCPIN) * 5 / 1024.0;
  tdegrees = tdegrees - 0.5;
  tdegrees = tdegrees / 0.01;
}

void float2String (float fl, char str[], int N)
{

  boolean neg = false;

  if ( fl < 0 ) {
    neg = true;
    fl *= -1;
  }

  float numeroFloat = fl;
  int parteEntera[10];
  int cifra;
  long numero = (long)numeroFloat;
  int sie = 0;

  while (1) {
    sie = sie + 1;
    cifra = numero % 10;
    numero = numero / 10;
    parteEntera[sie - 1] = cifra;
    if (numero == 0) {
      break;
    }
  }
  int indice = 0;
  if ( neg ) {
    indice++;
    str[0] = '-';
  }
  for (int i = sie - 1; i >= 0; i--)
  {
    str[indice] = parteEntera[i] + '0';
    indice++;
  }
  str[indice] = '.';
  indice++;
  numeroFloat = (numeroFloat - (int)numeroFloat);
  for (int i = 1; i <= N ; i++)
  {
    numeroFloat = numeroFloat * 10;
    cifra = (long)numeroFloat;
    numeroFloat = numeroFloat - cifra;
    str[indice] = char(cifra) + 48;
    indice++;
  }
  str[indice] = '\0';
}

void checkDisconnect(){
  byte z = 0;
  for(byte i=0; i<sizeof(dis);i++){
      dis[i] = 0;
    }
  if (sending){
    
      if (Serial1.available()>0){
  while(Serial1.available()>0){
    
    dis[z] = Serial1.read();
    z++;
    delay(10);
    
  }
  byte i = 0;
  while(dis[i] != 'N'){
    if (dis[i+1]=='O'){
      if (dis[i+2]==' '){
        if (dis[i+3]=='C'){
          if (dis[i+4]=='A'){
            if (dis[i+5]=='R'){
              if (dis[i+6]=='R'){
                if (dis[i+7]=='I'){
                  if (dis[i+8]=='E'){
                    if (dis[i+9]=='R'){
                      Serial.print("Disconnected");
                      delay(1000);
                      while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
                      sending = false;
                    } else i++;
                  } else i++;
                } else i++;
              } else i++;
            } else i++;
          } else i++;
        } else i++;
      } else i++;
    } else i++;
      
      
  }
  }
  }
}
        

Links and Documentation

Related Tutorials

Datasheets and Manuals



If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.

Know more at:

Get the Starter Kits at: