252 lines
6.9 KiB
Arduino
252 lines
6.9 KiB
Arduino
|
#include "Wire.h"
|
||
|
#include <SoftwareSerial.h>
|
||
|
|
||
|
#define XADOW_DEBUG 1
|
||
|
#define LEDAddress 0x04
|
||
|
|
||
|
|
||
|
#define DISP_CHAR_5X7 0x80
|
||
|
#define DISP_STRING 0x81
|
||
|
#define SET_DISP_ORIENTATION 0x82
|
||
|
/*Marco definitions for the display orientation of the LED matrix*/
|
||
|
#define RIGHT_TO_LEFT 0
|
||
|
#define LEFT_TO_RIGHT 1
|
||
|
#define POWER_DOWN 0x83
|
||
|
|
||
|
#define SerialBaud 9600
|
||
|
#define Serial1Baud 38400
|
||
|
|
||
|
static char buffer_int[32];
|
||
|
byte count_char = 0;
|
||
|
|
||
|
#define MICS_0 0x58
|
||
|
#define MICS_1 0x59
|
||
|
|
||
|
#define V_REF 3.00
|
||
|
|
||
|
#define REG_ADDR_RESULT 0x00
|
||
|
#define REG_ADDR_ALERT 0x01
|
||
|
#define REG_ADDR_CONFIG 0x02
|
||
|
#define REG_ADDR_LIMITL 0x03
|
||
|
#define REG_ADDR_LIMITH 0x04
|
||
|
#define REG_ADDR_HYST 0x05
|
||
|
#define REG_ADDR_CONVL 0x06
|
||
|
#define REG_ADDR_CONVH 0x07
|
||
|
|
||
|
float RS_RO_MICS_CO[22] = {
|
||
|
0.72, 0.68, 0.66, 0.64, 0.63, 0.62, 0.61, 0.6, 0.59, 0.58, 0.5, 0.45, 0.41, 0.38, 0.34, 0.31, 0.29, 0.275, 0.26, 0.17, 0.12, 0.088}; //Rs/Ro
|
||
|
float PPM_MICS_CO[22] = {
|
||
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400}; //ppm
|
||
|
float RS_RO_MICS_NO2[10] = {
|
||
|
25, 55, 90, 140, 190, 260, 330, 410, 500, 1000}; //Rs/Ro
|
||
|
float PPM_MICS_NO2[10] = {
|
||
|
0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.5}; //ppm
|
||
|
|
||
|
#define RO_MICS_CO 450 //Kohm segun tablas estadisticas del fabricante
|
||
|
#define RO_MICS_NO2 3 //Kohm segun tablas estadisticas del fabricante
|
||
|
|
||
|
SoftwareSerial Blue(14, 15); // RX, TX
|
||
|
unsigned int getData;
|
||
|
float analogVal=0; // convert
|
||
|
|
||
|
void init_adc()
|
||
|
{
|
||
|
Wire.beginTransmission(0x58); // transmit to device
|
||
|
Wire.write(REG_ADDR_CONFIG); // Configuration Register
|
||
|
Wire.write(0x20);
|
||
|
Wire.endTransmission();
|
||
|
Wire.beginTransmission(0x59); // transmit to device
|
||
|
Wire.write(REG_ADDR_CONFIG); // Configuration Register
|
||
|
Wire.write(0x20);
|
||
|
Wire.endTransmission();
|
||
|
}
|
||
|
|
||
|
float VCC = 3300 * 2; //mV
|
||
|
float resolution = 4095;
|
||
|
float load = 100; //kOhm
|
||
|
|
||
|
float read_adc(int MICS) //unsigned int *data
|
||
|
{
|
||
|
Wire.beginTransmission(MICS); // transmit to device
|
||
|
Wire.write(REG_ADDR_RESULT); // get reuslt
|
||
|
Wire.endTransmission();
|
||
|
|
||
|
Wire.requestFrom(MICS, 2); // request 2byte from device
|
||
|
delay(1);
|
||
|
if(Wire.available()<=2)
|
||
|
{
|
||
|
getData = (Wire.read()&0x0f)<<8;
|
||
|
getData |= Wire.read();
|
||
|
}
|
||
|
float voltage = ((float)getData * VCC)/resolution;
|
||
|
float current = (VCC - voltage)/load;
|
||
|
float resistor = voltage/current;
|
||
|
//return voltage;
|
||
|
return resistor;
|
||
|
}
|
||
|
|
||
|
void setupBlueToothConnection()
|
||
|
{
|
||
|
Blue.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
|
||
|
Blue.print("\r\n+STWMOD=1\r\n");//set the bluetooth work in master mode
|
||
|
Blue.print("\r\n+STNA=Franky\r\n");//set the bluetooth name as "SeeedBTMaster"
|
||
|
Blue.print("\r\n+STPIN=0000\r\n");//Set Master pincode"0000",it must be same as Slave pincode
|
||
|
Blue.print("\r\n+STAUTO=1\r\n");// Auto-connection is forbidden here
|
||
|
delay(2000); // This delay is required.
|
||
|
Blue.flush();
|
||
|
Blue.print("\r\n+INQ=1\r\n");//make the master inquire
|
||
|
delay(2000); // This delay is required.
|
||
|
}
|
||
|
|
||
|
void setup(void)
|
||
|
{
|
||
|
Wire.begin();
|
||
|
Serial.begin(SerialBaud);
|
||
|
init_adc();
|
||
|
setupBlueToothConnection();
|
||
|
delay(1000);
|
||
|
Serial.flush();
|
||
|
Serial1.flush();
|
||
|
Blue.flush();
|
||
|
Serial1.begin(9600);
|
||
|
pinMode(16, OUTPUT);
|
||
|
digitalWrite(16, LOW);
|
||
|
}
|
||
|
|
||
|
void loop(void)
|
||
|
{
|
||
|
if (Serial1.available())
|
||
|
{
|
||
|
byte inByte = Serial1.read();
|
||
|
if (addData(inByte))
|
||
|
{
|
||
|
if (checkText("GPRMC,", buffer_int))
|
||
|
{
|
||
|
Serial.println(buffer_int);
|
||
|
Blue.println(buffer_int);
|
||
|
float CO = read_adc(MICS_0);//adcRead);
|
||
|
Serial.print("CO =");
|
||
|
Serial.print(CO);
|
||
|
Serial.println("kOhm");
|
||
|
Blue.print("CO =");
|
||
|
Blue.print(CO);
|
||
|
Blue.println("kOhm");
|
||
|
int value2 = map(CO, 0, 10, 9, 1);
|
||
|
int value = map(CO, 0, 10, '0', '9');
|
||
|
dispChar(value,150*value2);
|
||
|
if (CO > 3) digitalWrite(16, HIGH); //Cambialo en funcion delvalor de CO que quieres que salte el vibrador
|
||
|
float NO2 = read_adc(MICS_1);//adcRead);
|
||
|
Serial.print("NO2 =");
|
||
|
Serial.print(NO2);
|
||
|
Serial.println("kOhm");
|
||
|
Blue.print("NO2 =");
|
||
|
Blue.print(NO2);
|
||
|
Blue.println("kOhm");
|
||
|
delay(150*value2);
|
||
|
digitalWrite(16, LOW);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
boolean addData(byte inByte)
|
||
|
{
|
||
|
if (inByte == '\r')
|
||
|
{
|
||
|
buffer_int[count_char] = inByte;
|
||
|
buffer_int[count_char + 1] = 0x00;
|
||
|
count_char = 0;
|
||
|
return true;
|
||
|
}
|
||
|
else if((inByte != '\n')&&(inByte != '#')&&(inByte != '$'))
|
||
|
{
|
||
|
buffer_int[count_char] = inByte;
|
||
|
count_char = count_char + 1;
|
||
|
return false;
|
||
|
}
|
||
|
else if ((inByte == '#')||(inByte == '$'))
|
||
|
{
|
||
|
buffer_int[count_char] = inByte;
|
||
|
count_char = count_char + 1;
|
||
|
if (count_char == 3)
|
||
|
{
|
||
|
buffer_int[count_char] = 0x00;
|
||
|
count_char = 0;
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
boolean checkText(char* text, char *text1)
|
||
|
{
|
||
|
byte check = 0;
|
||
|
byte limit = strlen(text);
|
||
|
int i = 0;
|
||
|
for (i = 0; ((i< strlen(text1))&&(check<limit)); i++)
|
||
|
{
|
||
|
if (text[check]==text1[i]) check++;
|
||
|
else check = 0;
|
||
|
}
|
||
|
if (check == limit)
|
||
|
{
|
||
|
limit = strlen(text1);
|
||
|
int j = 0;
|
||
|
for (j = 0; i<=limit; j++)
|
||
|
{
|
||
|
if (text1[i]=='\r') text1[j]=0x00;
|
||
|
else text1[j] = text1[i];
|
||
|
i++;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
else return false;
|
||
|
}
|
||
|
|
||
|
/**********************************************************************/
|
||
|
/*Function: Send command to Xadow LED to display a string by the I2C. */
|
||
|
/*Parameter:-char* data_,Store a string to display on the xadow LED. */
|
||
|
/* -uint8_t len,The length of the data_. */
|
||
|
/* -uint16_t time,The time of the character moving one step. */
|
||
|
/*Return: void */
|
||
|
void dispString(char* data_,uint8_t len,uint16_t time)
|
||
|
{
|
||
|
Wire.beginTransmission(LEDAddress);
|
||
|
Wire.write(DISP_STRING);
|
||
|
Wire.write(len);
|
||
|
Wire.write((uint8_t*)data_,len);
|
||
|
Wire.write(time>>8); //high byte of time
|
||
|
Wire.write(time);//low byte of time
|
||
|
Wire.endTransmission();
|
||
|
}
|
||
|
void dispChar(uint8_t data_,uint16_t time)
|
||
|
{
|
||
|
Wire.beginTransmission(LEDAddress);
|
||
|
Wire.write(DISP_CHAR_5X7);
|
||
|
Wire.write(data_);
|
||
|
Wire.write(time>>8); //high byte of time
|
||
|
Wire.write(time);//low byte of time
|
||
|
Wire.endTransmission();
|
||
|
}
|
||
|
void setDispOrientation(uint8_t orientation)
|
||
|
{
|
||
|
Wire.beginTransmission(LEDAddress);
|
||
|
Wire.write(SET_DISP_ORIENTATION);
|
||
|
Wire.write(orientation);
|
||
|
Wire.endTransmission();
|
||
|
}
|
||
|
void powerDown()
|
||
|
{
|
||
|
Wire.beginTransmission(LEDAddress);
|
||
|
Wire.write(POWER_DOWN);
|
||
|
Wire.endTransmission();
|
||
|
digitalWrite(3,HIGH);
|
||
|
}
|
||
|
void wakeUp()
|
||
|
{
|
||
|
Wire.beginTransmission(LEDAddress);
|
||
|
Wire.endTransmission();
|
||
|
}
|
||
|
|
||
|
|