thermometer, hygrometer and barometer. Using VFD for display.

Dependencies:   AM2321 LPS331_I2C mbed-rtos mbed EthernetInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "EthernetInterface.h"
00004 #include "LPS331_I2C.h"
00005 #include "AM2321.h"
00006 #include <string>
00007 
00008 //for debug
00009 DigitalOut led1(LED1, 0);
00010 DigitalOut led2(LED2, 0);
00011 DigitalOut led3(LED3, 0);
00012 DigitalOut led4(LED4, 0);
00013 Serial pc(USBTX, USBRX);
00014 
00015 // data
00016 float pressure = 0;
00017 float temperature = 0;
00018 float humidity = 0;
00019 
00020 // for ethernet
00021 EthernetInterface eth;
00022 DigitalIn lnk(P1_25);
00023 DigitalIn spd(P1_26);
00024 DigitalOut speed(p29);
00025 DigitalOut link(p30);
00026 
00027 void flip(void const *args) {
00028     speed = !spd;
00029     link = !lnk;
00030 }
00031 
00032 // for fluentd in_http
00033 void send_fluentd(const string host, const int port)
00034 {
00035     TCPSocketConnection sock;
00036     sock.connect(host.c_str(), port);
00037 
00038     char post_data[128];
00039     sprintf(post_data, "json={\"humid\":%d,\"temp\":%d,\"press\":%d}", (int)humidity, (int)temperature, (int)pressure);
00040     pc.printf("POST Data:\r\n%s\r\n%d", post_data, strlen(post_data));
00041     char http_cmd[512];
00042     sprintf(http_cmd, "POST /mbed.climate HTTP/1.1\r\nHost: %s:%d\r\nAccept: */*\r\nContent-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n%s\r\n\r\n", host.c_str(), port, strlen(post_data), post_data);
00043     pc.printf("Request:\r\n%s\r\n", http_cmd);
00044     sock.send_all(http_cmd, sizeof(http_cmd)-1);
00045 
00046     char buffer[1024];
00047     int ret;
00048     while (true) {
00049         ret = sock.receive(buffer, sizeof(buffer)-1);
00050         if (ret <= 0)
00051             break;
00052         buffer[ret] = '\0';
00053         pc.printf("Received %d chars from server:\r\n%s\r\n", ret, buffer);
00054     }
00055 
00056     sock.close();
00057 }
00058 
00059 
00060 // for LPS331
00061 LPS331_I2C lps331(p9, p10, LPS331_I2C_SA0_HIGH);
00062 
00063 void setup_lps331()
00064 {   
00065     if(lps331.isAvailable()) {
00066         pc.printf("LPS331 is available!\r\n");
00067     } else {
00068         pc.printf("LPS331 is unavailable!\r\n");
00069     }
00070     
00071     lps331.setResolution(LPS331_I2C_PRESSURE_AVG_512, LPS331_I2C_TEMP_AVG_128);
00072     lps331.setDataRate(LPS331_I2C_DATARATE_7HZ);
00073     lps331.setActive(true);
00074 }
00075 
00076 void update_pressure()
00077 {
00078     led2 = !led2;
00079     pressure = lps331.getPressure();
00080     pc.printf("press:%f\r\n", pressure);
00081 }
00082 
00083 // for AM2321
00084 AM2321 am2321(p9, p10);
00085 
00086 void update_temperature_and_humidity()
00087 {
00088     if(am2321.poll())
00089     {
00090         led3 = !led3;
00091         temperature = am2321.getTemperature();
00092         humidity = am2321.getHumidity();
00093         pc.printf("temp:%.1f,humid:%.1f\r\n", temperature, humidity);
00094     }
00095 }
00096 
00097 // for display mode
00098 int mode = 1; // 1: temp, 2: humid, 3: press
00099 void next_mode(void const *args)
00100 {
00101     mode++;
00102     if(mode > 3)
00103     {
00104         mode = 1;
00105     }
00106 }
00107 
00108 // for VFD
00109 const int ANODE_PINS_NUM = 9;
00110 DigitalOut anode_pins[ANODE_PINS_NUM] = {
00111     DigitalOut(p28),
00112     DigitalOut(p27),
00113     DigitalOut(p26),
00114     DigitalOut(p25),
00115     DigitalOut(p24),
00116     DigitalOut(p23),
00117     DigitalOut(p22),
00118     DigitalOut(p16),
00119     DigitalOut(p21)
00120 };
00121 
00122 const int CATHODE_PINS_NUM = 4;
00123 DigitalOut cathode_pins[CATHODE_PINS_NUM] = {
00124     DigitalOut(p17),
00125     DigitalOut(p18),
00126     DigitalOut(p19),
00127     DigitalOut(p20)
00128 };
00129 
00130 const int DOT_PIN_POS = 8;
00131 const int VFD_LIGHT_MAP[][ANODE_PINS_NUM] = {
00132     {1, 1, 1, 1, 1, 1, 0, 0, 0}, // 0
00133     {0, 1, 1, 0, 0, 0, 0, 0, 0}, // 1
00134     {1, 1, 0, 1, 1, 0, 1, 0, 0}, // 2
00135     {1, 1, 1, 1, 0, 0, 1, 0, 0}, // 3
00136     {0, 1, 1, 0, 0, 1, 1, 1, 0}, // 4
00137     {1, 0, 1, 1, 0, 1, 1, 0, 0}, // 5
00138     {1, 0, 1, 1, 1, 1, 1, 0, 0}, // 6
00139     {1, 1, 1, 0, 0, 0, 0, 0, 0}, // 7
00140     {1, 1, 1, 1, 1, 1, 1, 0, 0}, // 8
00141     {1, 1, 1, 1, 0, 1, 1, 0, 0}, // 9
00142     {1, 1, 1, 0, 1, 1, 1, 0, 0}, // A
00143     {0, 0, 1, 1, 1, 1, 1, 0, 0}, // B
00144     {1, 0, 0, 1, 1, 1, 0, 0, 0}, // C
00145     {0, 1, 1, 1, 1, 0, 1, 0, 0}, // D
00146     {1, 0, 0, 1, 1, 1, 1, 0, 0}, // E
00147     {1, 0, 0, 0, 1, 1, 1, 0, 0}, // F
00148     {1, 1, 0, 0, 1, 1, 1, 0, 0}, // P
00149     {0, 0, 1, 0, 1, 1, 1, 0, 0}  // h
00150 };
00151 
00152 void vfd_display_number(int pos, int number, int add_dot)
00153 {
00154     cathode_pins[pos] = 1;
00155     for(int i = 0; i < DOT_PIN_POS; i++) {
00156         anode_pins[i] = VFD_LIGHT_MAP[number][i];
00157     }
00158     anode_pins[DOT_PIN_POS] = add_dot;
00159     Thread::wait(2);
00160     for(int i = 0; i < ANODE_PINS_NUM; i++) {
00161         anode_pins[i] = 0;
00162     }
00163     cathode_pins[pos] = 0;
00164 }
00165 
00166 void vfd_display_numbers(void const *args)
00167 {
00168     int ones_place, tens_place, hundreds_place, thousands_place, use_dot;
00169     int display_number;
00170     led1 = !led1;
00171     
00172     switch(mode)
00173     {
00174         case 1:
00175             display_number = (int)(temperature * 10);
00176             ones_place = 12;
00177             tens_place = display_number % 10;
00178             thousands_place = (int)(display_number / 100);
00179             hundreds_place = (int)((display_number - thousands_place * 100 - tens_place) / 10);
00180             use_dot = 1;
00181             break;
00182         case 2:
00183             display_number = (int)(humidity * 10);
00184             ones_place = 16;
00185             tens_place = display_number % 10;
00186             thousands_place = (int)(display_number / 100);
00187             hundreds_place = (int)((display_number - thousands_place * 100 - tens_place) / 10);
00188             use_dot = 1;
00189             break;
00190         default:
00191             display_number = (int)pressure;
00192             if(display_number < 1000)
00193             {
00194                 ones_place = 17;
00195                 tens_place = display_number % 10;
00196                 thousands_place = (int)(display_number / 100);
00197                 hundreds_place = (int)((display_number - thousands_place * 100 - tens_place) / 10);
00198             }
00199             else
00200             {
00201                 ones_place = display_number % 10;
00202                 thousands_place = (int)(display_number / 1000);
00203                 hundreds_place = (int)((display_number - thousands_place * 1000) / 100);
00204                 tens_place = (int)((display_number - thousands_place * 1000 - hundreds_place * 100) / 10);
00205             }
00206             use_dot = 0;
00207     }
00208 
00209     vfd_display_number(3, thousands_place, 0);
00210     vfd_display_number(2, hundreds_place, use_dot);
00211     vfd_display_number(1, tens_place, 0);
00212     vfd_display_number(0, ones_place, 0);
00213 }
00214 
00215 int main()
00216 {
00217     // start ethernet
00218     RtosTimer flipper(flip, osTimerPeriodic, NULL);
00219     flipper.start(50);
00220     eth.init();
00221     eth.connect();
00222 
00223     // start auto mode change
00224     RtosTimer mode_changer(next_mode, osTimerPeriodic, NULL);
00225     mode_changer.start(7000);
00226 
00227     // start vfd
00228     RtosTimer vfd_timer(vfd_display_numbers, osTimerPeriodic, NULL);
00229     vfd_timer.start(16); //60fps
00230     
00231     // start LPS331
00232     setup_lps331();
00233     Thread::wait(2000);
00234 
00235     while(1) {
00236         for(int i=0;i < 30;i++){
00237             update_pressure();
00238             update_temperature_and_humidity();
00239             Thread::wait(1000);
00240         }
00241         send_fluentd("192.168.10.100", 8888);
00242     }
00243 }