![](/media/cache/profiles/mypic_AeCA35r.jpg.50x50_q85.jpg)
Onboard temperature sensor sample for WIZnet IoT Shield
main.cpp@3:c9cd5d11484d, 2019-03-25 (annotated)
- Committer:
- stkim92
- Date:
- Mon Mar 25 10:11:25 2019 +0000
- Revision:
- 3:c9cd5d11484d
- Parent:
- 2:d7c5df056a3b
- Child:
- 4:5ea8c91f607f
WIoT-Shield-SENSOR-TEMP
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hkjung | 0:5cd6077e0a8e | 1 | /* WIZnet IoT Shield Cat.M1 Sample code for Arm MBED |
hkjung | 0:5cd6077e0a8e | 2 | * Copyright (c) 2019 WIZnet Co., Ltd. |
hkjung | 0:5cd6077e0a8e | 3 | * SPDX-License-Identifier: Apache-2.0 |
hkjung | 0:5cd6077e0a8e | 4 | */ |
stkim92 | 3:c9cd5d11484d | 5 | |
stkim92 | 3:c9cd5d11484d | 6 | /* |
stkim92 | 3:c9cd5d11484d | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
stkim92 | 3:c9cd5d11484d | 8 | * you may not use this file except in compliance with the License. |
stkim92 | 3:c9cd5d11484d | 9 | * You may obtain a copy of the License at |
stkim92 | 3:c9cd5d11484d | 10 | * |
stkim92 | 3:c9cd5d11484d | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
stkim92 | 3:c9cd5d11484d | 12 | * |
stkim92 | 3:c9cd5d11484d | 13 | * Unless required by applicable law or agreed to in writing, software |
stkim92 | 3:c9cd5d11484d | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
stkim92 | 3:c9cd5d11484d | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
stkim92 | 3:c9cd5d11484d | 16 | * |
stkim92 | 3:c9cd5d11484d | 17 | * See the License for the specific language governing permissions and |
stkim92 | 3:c9cd5d11484d | 18 | * limitations under the License. |
stkim92 | 3:c9cd5d11484d | 19 | * |
stkim92 | 3:c9cd5d11484d | 20 | */ |
hkjung | 0:5cd6077e0a8e | 21 | |
hkjung | 0:5cd6077e0a8e | 22 | |
hkjung | 0:5cd6077e0a8e | 23 | #include <string> |
hkjung | 0:5cd6077e0a8e | 24 | #include "mbed.h" |
hkjung | 0:5cd6077e0a8e | 25 | |
hkjung | 0:5cd6077e0a8e | 26 | #define RET_OK 1 |
hkjung | 0:5cd6077e0a8e | 27 | #define RET_NOK -1 |
hkjung | 0:5cd6077e0a8e | 28 | #define DEBUG_ENABLE 1 |
hkjung | 0:5cd6077e0a8e | 29 | #define DEBUG_DISABLE 0 |
hkjung | 0:5cd6077e0a8e | 30 | #define ON 1 |
hkjung | 0:5cd6077e0a8e | 31 | #define OFF 0 |
hkjung | 0:5cd6077e0a8e | 32 | |
hkjung | 0:5cd6077e0a8e | 33 | #define MAX_BUF_SIZE 1024 |
hkjung | 0:5cd6077e0a8e | 34 | |
hkjung | 0:5cd6077e0a8e | 35 | #define BG96_APN_PROTOCOL_IPv4 1 |
hkjung | 0:5cd6077e0a8e | 36 | #define BG96_APN_PROTOCOL_IPv6 2 |
hkjung | 0:5cd6077e0a8e | 37 | #define BG96_DEFAULT_TIMEOUT 1000 |
hkjung | 0:5cd6077e0a8e | 38 | #define BG96_CONNECT_TIMEOUT 15000 |
hkjung | 0:5cd6077e0a8e | 39 | #define BG96_SEND_TIMEOUT 500 |
hkjung | 0:5cd6077e0a8e | 40 | #define BG96_RECV_TIMEOUT 500 |
hkjung | 0:5cd6077e0a8e | 41 | |
hkjung | 0:5cd6077e0a8e | 42 | #define BG96_APN_PROTOCOL BG96_APN_PROTOCOL_IPv6 |
hkjung | 0:5cd6077e0a8e | 43 | #define BG96_DEFAULT_BAUD_RATE 115200 |
hkjung | 0:5cd6077e0a8e | 44 | #define BG96_PARSER_DELIMITER "\r\n" |
hkjung | 0:5cd6077e0a8e | 45 | |
hkjung | 0:5cd6077e0a8e | 46 | #define CATM1_APN_SKT "lte-internet.sktelecom.com" |
hkjung | 0:5cd6077e0a8e | 47 | |
hkjung | 0:5cd6077e0a8e | 48 | #define CATM1_DEVICE_NAME_BG96 "BG96" |
hkjung | 0:5cd6077e0a8e | 49 | #define DEVNAME CATM1_DEVICE_NAME_BG96 |
hkjung | 0:5cd6077e0a8e | 50 | |
hkjung | 0:5cd6077e0a8e | 51 | #define devlog(f_, ...) if(CATM1_DEVICE_DEBUG == DEBUG_ENABLE) { pc.printf("\r\n[%s] ", DEVNAME); pc.printf((f_), ##__VA_ARGS__); } |
hkjung | 0:5cd6077e0a8e | 52 | #define myprintf(f_, ...) {pc.printf("\r\n[MAIN] "); pc.printf((f_), ##__VA_ARGS__);} |
hkjung | 0:5cd6077e0a8e | 53 | |
hkjung | 0:5cd6077e0a8e | 54 | /* Pin configuraiton */ |
hkjung | 0:5cd6077e0a8e | 55 | // Cat.M1 |
hkjung | 0:5cd6077e0a8e | 56 | #define MBED_CONF_IOTSHIELD_CATM1_TX D8 |
hkjung | 0:5cd6077e0a8e | 57 | #define MBED_CONF_IOTSHIELD_CATM1_RX D2 |
hkjung | 1:0d7ba45f12b1 | 58 | #define MBED_CONF_IOTSHIELD_CATM1_RESET D7 |
hkjung | 0:5cd6077e0a8e | 59 | #define MBED_CONF_IOTSHIELD_CATM1_PWRKEY D9 |
hkjung | 1:0d7ba45f12b1 | 60 | |
hkjung | 0:5cd6077e0a8e | 61 | // Sensors |
hkjung | 0:5cd6077e0a8e | 62 | #define MBED_CONF_IOTSHIELD_SENSOR_CDS A0 |
hkjung | 0:5cd6077e0a8e | 63 | #define MBED_CONF_IOTSHIELD_SENSOR_TEMP A1 |
hkjung | 0:5cd6077e0a8e | 64 | |
hkjung | 0:5cd6077e0a8e | 65 | /* Debug message settings */ |
hkjung | 2:d7c5df056a3b | 66 | #define BG96_PARSER_DEBUG DEBUG_DISABLE |
stkim92 | 3:c9cd5d11484d | 67 | #define CATM1_DEVICE_DEBUG DEBUG_ENABLE |
hkjung | 2:d7c5df056a3b | 68 | |
hkjung | 0:5cd6077e0a8e | 69 | |
hkjung | 0:5cd6077e0a8e | 70 | Serial pc(USBTX, USBRX); // USB debug |
hkjung | 0:5cd6077e0a8e | 71 | |
stkim92 | 3:c9cd5d11484d | 72 | UARTSerial *_serial; // Cat.M1 module |
hkjung | 0:5cd6077e0a8e | 73 | |
hkjung | 1:0d7ba45f12b1 | 74 | DigitalOut _RESET_BG96(MBED_CONF_IOTSHIELD_CATM1_RESET); |
hkjung | 1:0d7ba45f12b1 | 75 | DigitalOut _PWRKEY_BG96(MBED_CONF_IOTSHIELD_CATM1_PWRKEY); |
stkim92 | 3:c9cd5d11484d | 76 | AnalogIn tempVal(MBED_CONF_IOTSHIELD_SENSOR_TEMP); |
hkjung | 0:5cd6077e0a8e | 77 | |
stkim92 | 3:c9cd5d11484d | 78 | float val = 0; // 온도 측정 값 넣을 변수 |
stkim92 | 3:c9cd5d11484d | 79 | float voltage = 0; // 전압 계산 값 넣을 변수 |
stkim92 | 3:c9cd5d11484d | 80 | float celsius = 0; // 섭씨 계산 값 넣을 변수 |
stkim92 | 3:c9cd5d11484d | 81 | float fahrenheit = 0; // 화씨 계산 값 넣을 변수 |
stkim92 | 3:c9cd5d11484d | 82 | |
hkjung | 2:d7c5df056a3b | 83 | bool flag_gettime = false; |
hkjung | 2:d7c5df056a3b | 84 | |
stkim92 | 3:c9cd5d11484d | 85 | float getTemperature_F(float _celsius); |
stkim92 | 3:c9cd5d11484d | 86 | float getTemperature_C(float _voltage); |
stkim92 | 3:c9cd5d11484d | 87 | |
hkjung | 0:5cd6077e0a8e | 88 | void serialPcInit(void) |
hkjung | 0:5cd6077e0a8e | 89 | { |
hkjung | 0:5cd6077e0a8e | 90 | pc.baud(115200); |
hkjung | 0:5cd6077e0a8e | 91 | pc.format(8, Serial::None, 1); |
hkjung | 0:5cd6077e0a8e | 92 | } |
hkjung | 0:5cd6077e0a8e | 93 | |
hkjung | 0:5cd6077e0a8e | 94 | |
hkjung | 1:0d7ba45f12b1 | 95 | |
hkjung | 1:0d7ba45f12b1 | 96 | // ---------------------------------------------------------------- |
hkjung | 1:0d7ba45f12b1 | 97 | // Main routine |
hkjung | 1:0d7ba45f12b1 | 98 | // ---------------------------------------------------------------- |
hkjung | 1:0d7ba45f12b1 | 99 | |
hkjung | 0:5cd6077e0a8e | 100 | int main() |
hkjung | 0:5cd6077e0a8e | 101 | { |
stkim92 | 3:c9cd5d11484d | 102 | serialPcInit(); |
stkim92 | 3:c9cd5d11484d | 103 | |
hkjung | 0:5cd6077e0a8e | 104 | myprintf("System Init Complete\r\n"); |
stkim92 | 3:c9cd5d11484d | 105 | |
hkjung | 0:5cd6077e0a8e | 106 | myprintf("WIZnet IoT Shield for Arm MBED"); |
hkjung | 0:5cd6077e0a8e | 107 | myprintf("LTE Cat.M1 Version"); |
hkjung | 0:5cd6077e0a8e | 108 | myprintf("================================================="); |
stkim92 | 3:c9cd5d11484d | 109 | myprintf(">> Sample Code: TEMP Test)"); |
hkjung | 0:5cd6077e0a8e | 110 | myprintf("=================================================\r\n"); |
stkim92 | 3:c9cd5d11484d | 111 | |
stkim92 | 3:c9cd5d11484d | 112 | while(1) { |
stkim92 | 3:c9cd5d11484d | 113 | // read the value from the sensor: |
stkim92 | 3:c9cd5d11484d | 114 | val = tempVal.read_u16()/100; |
stkim92 | 3:c9cd5d11484d | 115 | // 실제 전압 계산 |
stkim92 | 3:c9cd5d11484d | 116 | // 0~5V의 값을 구하기 위해 아래와 같이 5를 곱하고 1024를 나눈다. |
stkim92 | 3:c9cd5d11484d | 117 | // (Analog값 * IC에 걸리는 전압 / Analog 10bit 분해능(1024)) |
stkim92 | 3:c9cd5d11484d | 118 | // IC에 걸리는 전압은 측정기로 측정 |
hkjung | 2:d7c5df056a3b | 119 | |
stkim92 | 3:c9cd5d11484d | 120 | voltage = val * 4.95 / 1024; |
stkim92 | 3:c9cd5d11484d | 121 | celsius = getTemperature_C(voltage); |
stkim92 | 3:c9cd5d11484d | 122 | fahrenheit = getTemperature_F(celsius); |
stkim92 | 3:c9cd5d11484d | 123 | |
stkim92 | 3:c9cd5d11484d | 124 | wait_ms(1000); |
stkim92 | 3:c9cd5d11484d | 125 | myprintf("[ Temperature Data ]"); |
stkim92 | 3:c9cd5d11484d | 126 | myprintf("Sensor value : %.2f", val); |
stkim92 | 3:c9cd5d11484d | 127 | myprintf("Volt : %.2f mV", voltage); |
stkim92 | 3:c9cd5d11484d | 128 | |
stkim92 | 3:c9cd5d11484d | 129 | myprintf("Celsius temp : %.2f C", celsius); |
stkim92 | 3:c9cd5d11484d | 130 | myprintf("Fahrenheit temp : %.2f F", fahrenheit); |
stkim92 | 3:c9cd5d11484d | 131 | wait_ms(1000); |
hkjung | 2:d7c5df056a3b | 132 | } |
hkjung | 0:5cd6077e0a8e | 133 | |
hkjung | 1:0d7ba45f12b1 | 134 | |
hkjung | 0:5cd6077e0a8e | 135 | } |
hkjung | 0:5cd6077e0a8e | 136 | |
hkjung | 1:0d7ba45f12b1 | 137 | |
hkjung | 2:d7c5df056a3b | 138 | |
stkim92 | 3:c9cd5d11484d | 139 | |
stkim92 | 3:c9cd5d11484d | 140 | float getTemperature_F(float _celsius) |
hkjung | 0:5cd6077e0a8e | 141 | { |
stkim92 | 3:c9cd5d11484d | 142 | // 섭씨에서 화씨 환산 -> (화씨온도 = 섭씽온*9/5+32) |
stkim92 | 3:c9cd5d11484d | 143 | fahrenheit = celsius * 9.0 / 5.0 + 32.0; |
stkim92 | 3:c9cd5d11484d | 144 | return fahrenheit; |
hkjung | 0:5cd6077e0a8e | 145 | } |
hkjung | 0:5cd6077e0a8e | 146 | |
stkim92 | 3:c9cd5d11484d | 147 | float getTemperature_C(float _voltage) |
hkjung | 0:5cd6077e0a8e | 148 | { |
stkim92 | 3:c9cd5d11484d | 149 | // LM35는 섭씨 1도당 10mV의 전위차를 갖는다. |
stkim92 | 3:c9cd5d11484d | 150 | // 센서핀의 전압이 0.28V라면 280mV이므로 온도는 28도씨. |
stkim92 | 3:c9cd5d11484d | 151 | // 100을 곱해서 섭씨 온도로 나타냄 |
stkim92 | 3:c9cd5d11484d | 152 | celsius = voltage * 100.0; |
stkim92 | 3:c9cd5d11484d | 153 | return celsius; |
hkjung | 0:5cd6077e0a8e | 154 | } |