1109박지우1102김민성1101김동우1104김유건장광재박다솜
Published

SSHS_CS_1반_2019_CO2 measurement/communication system

CO2 makes life SAD, but our Device makes life GLAD!

IntermediateWork in progress534
SSHS_CS_1반_2019_CO2 measurement/communication system

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×2
MQ135 CO2 sensor
×2
Android device
Android device
×2
RF Module
×2

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

src

C/C++
아두이노 소스 코드
#include <MQ135.h>
#include <VirtualWire.h>

const int SEND_PIN = 12; //  
const int GET_PIN = 11; //  

const int BOARD_NUM = 1; //  

const int MAP_WIDTH = 40; // 
const int MAP_HEIGHT = 20; // 

const int PPM_STANDARD = 1000;

#define ANALOGPIN A0    //  Define Analog PIN on Arduino Board
MQ135 gasSensor = {ANALOGPIN};

struct LocalData{
  int x, y; // x, y
  char place_name[10];
  int co2_ppm;
};

char print_map[MAP_HEIGHT][MAP_WIDTH]; //  map  

void setup() {
  vw_set_tx_pin(SEND_PIN);
  vw_set_rx_pin(GET_PIN);
  vw_setup(2000);
  vw_rx_start();

  Serial.begin(9600);
  //   
  float rzero = gasSensor.getRZero();
  delay(3000);
  Serial.print("MQ135 RZERO Calibration Value : ");
  Serial.println(rzero);

  mapInitialize();
}

void loop() {
  // put your main code here, to run repeatedly:
  
  sendData(getCo2Ppm());

  int board, value;
  if (getData(board, value)){
    //Serial.print("Got Data ");
    //Serial.print(board);
    //Serial.print("_");
    //Serial.println(value);

    //board    
    printUserInterface(getLocalData(board, value));
  }

  
  delay(1000);
}

void sendData(int co2_ppm){
  char send_char[VW_MAX_MESSAGE_LEN];
  String send_str = String(BOARD_NUM)+String("_")+String(co2_ppm);
  strcpy(send_char, send_str.c_str());
  vw_send((uint8_t*)send_char, strlen(send_char));
  vw_wait_tx();
}

bool getData(int& board, int& co2_ppm){
  char get_value[VW_MAX_MESSAGE_LEN];
  byte message_length = VW_MAX_MESSAGE_LEN;
  bool is_valid = vw_get_message((uint8_t*)get_value, &message_length) ;
  if (!is_valid) return false; //     

  String get_str(get_value);

  board = get_str.substring(0,1).toInt();
  co2_ppm = get_str.substring(2).toInt();

  if (board == BOARD_NUM) return true; //      
  
  return true;
}

int getCo2Ppm(){ //CO2 PPM 
  float ppm = gasSensor.getPPM();
  //Serial.println("XXXX");
  //Serial.println(ppm);
  return(int(ppm));
}

LocalData* const getLocalData(int board, int co2_ppm){ //  CO2 PPM  LocalData  
  LocalData* const data_ptr = new LocalData;
  //x, y   
  switch(board){
    case 0:
    data_ptr->x=5;
    data_ptr->y=5;
    strcpy(data_ptr->place_name, "");
    break;

    case 1:
    data_ptr->x=10;
    data_ptr->y=10;
    strcpy(data_ptr->place_name, "");
    break;
  }

  data_ptr->co2_ppm = co2_ppm;
  return data_ptr;
}

void mapInitialize(){ //map  
  for (int _y = 0; _y < MAP_HEIGHT; _y++){
    for (int _x = 0; _x < MAP_WIDTH; _x++){
      print_map[_y][_x] = ' ';
    }
  }
}

void printUserInterface(LocalData* const data){

  int x = data->x;
  int y = data->y;

  // 
  print_map[y][x] = 'O';
  print_map[y-1][x+2]=':';
  if(data->co2_ppm < PPM_STANDARD) print_map[y-1][x+3] = ')';
  else if(data->co2_ppm > PPM_STANDARD
  ) print_map[y-1][x+3] = '(';

  String place_name = data->place_name;
  int pos = 0;
  for (char c : place_name){
    print_map[y][x+2 + (pos)++] = c;
  }

  //CO2  
  String s(data->co2_ppm);
  s += "PPM  ";
  pos = 0;
  for (char c : s){
    print_map[y+1][x+2 + (pos)++] = c;
  }
  
  for(int _x=0; _x < MAP_WIDTH; _x++){
    //   
    Serial.print('_');
  }
  Serial.println("");
  
  for(int _y = 0; _y < MAP_HEIGHT; _y++){
    for(int _x = 0; _x < MAP_WIDTH; _x++){
      Serial.print(print_map[_y][_x]);
    }
    Serial.println("");
  }
  
}

Credits

1109박지우
1 project • 0 followers
1102김민성
1 project • 0 followers
1101김동우
1 project • 0 followers
1104김유건
1 project • 0 followers
장광재
29 projects • 16 followers
박다솜
14 projects • 5 followers

Comments