JamEon Jeong
/
_HW3_DHT_TCP
HW3_DHT22_TCP
Revision 0:e548610ed175, committed 2018-01-02
- Comitter:
- Proverbs
- Date:
- Tue Jan 02 11:43:43 2018 +0000
- Commit message:
- HW2_DHT_TCP
Changed in this revision
diff -r 000000000000 -r e548610ed175 DHT.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHT.lib Tue Jan 02 11:43:43 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/Proverbs/code/DHT/#d041691f5c29
diff -r 000000000000 -r e548610ed175 MMA7455.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7455.cpp Tue Jan 02 11:43:43 2018 +0000 @@ -0,0 +1,295 @@ +/****************************************************************************** + * Includes + *****************************************************************************/ + +#include "mbed.h" +#include "mbed_debug.h" + +#include "MMA7455.h" + +/****************************************************************************** + * Defines and typedefs + *****************************************************************************/ + +#define MMA7455_I2C_ADDR (0x1D << 1) + +#define MMA7455_ADDR_XOUTL 0x00 +#define MMA7455_ADDR_XOUTH 0x01 +#define MMA7455_ADDR_YOUTL 0x02 +#define MMA7455_ADDR_YOUTH 0x03 +#define MMA7455_ADDR_ZOUTL 0x04 +#define MMA7455_ADDR_ZOUTH 0x05 +#define MMA7455_ADDR_XOUT8 0x06 +#define MMA7455_ADDR_YOUT8 0x07 +#define MMA7455_ADDR_ZOUT8 0x08 +#define MMA7455_ADDR_STATUS 0x09 +#define MMA7455_ADDR_DETSRC 0x0A +#define MMA7455_ADDR_TOUT 0x0B +#define MMA7455_ADDR_I2CAD 0x0D +#define MMA7455_ADDR_USRINF 0x0E +#define MMA7455_ADDR_WHOAMI 0x0F +#define MMA7455_ADDR_XOFFL 0x10 +#define MMA7455_ADDR_XOFFH 0x11 +#define MMA7455_ADDR_YOFFL 0x12 +#define MMA7455_ADDR_YOFFH 0x13 +#define MMA7455_ADDR_ZOFFL 0x14 +#define MMA7455_ADDR_ZOFFH 0x15 +#define MMA7455_ADDR_MCTL 0x16 +#define MMA7455_ADDR_INTRST 0x17 +#define MMA7455_ADDR_CTL1 0x18 +#define MMA7455_ADDR_CTL2 0x19 +#define MMA7455_ADDR_LDTH 0x1A +#define MMA7455_ADDR_PDTH 0x1B +#define MMA7455_ADDR_PW 0x1C +#define MMA7455_ADDR_LT 0x1D +#define MMA7455_ADDR_TW 0x1E + +#define MMA7455_MCTL_MODE(m) ((m) << 0) +#define MMA7455_MCTL_GLVL(g) ((g) << 2) + +#define MMA7455_STATUS_DRDY (1 << 0) +#define MMA7455_STATUS_DOVR (1 << 1) +#define MMA7455_STATUS_PERR (1 << 2) + + +MMA7455::MMA7455(PinName sda, PinName scl) : _i2c(sda, scl) +{ + _mode = ModeStandby; + _range = Range_8g; + + _xOff = 0; + _yOff = 0; + _zOff = 0; +} + +bool MMA7455::setMode(Mode mode) { + bool result = false; + int mCtrl = 0; + + do { + mCtrl = getModeControl(); + if (mCtrl < 0) break; + + mCtrl &= ~(0x03 << 0); + mCtrl |= MMA7455_MCTL_MODE(mode); + + if (setModeControl((uint8_t)mCtrl) < 0) { + break; + } + + _mode = mode; + result = true; + } while(0); + + + + return result; +} + +bool MMA7455::setRange(Range range) { + bool result = false; + int mCtrl = 0; + + do { + mCtrl = getModeControl(); + if (mCtrl < 0) break; + + mCtrl &= ~(0x03 << 2); + mCtrl |= MMA7455_MCTL_GLVL(range); + + if (setModeControl((uint8_t)mCtrl) < 0) { + break; + } + + _range = range; + result = true; + } while(0); + + + + return result; + +} + +bool MMA7455::read(int32_t& x, int32_t& y, int32_t& z) { + bool result = false; + + + // nothing to read in standby mode + if (_mode == ModeStandby) return false; + + // wait for ready flag + int status = 0; + do { + status = getStatus(); + } while (status >= 0 && (status & MMA7455_STATUS_DRDY) == 0); + + + do { + if (status < 0) break; + + + char buf[6]; + buf[0] = MMA7455_ADDR_XOUTL; + if (_i2c.write(MMA7455_I2C_ADDR, buf, 1) != 0) break; + if (_i2c.read(MMA7455_I2C_ADDR, buf, 6) != 0) break; + + // check if second bit is set in high byte -> negative value + // expand negative value to full byte + if (buf[1] & 0x02) buf[1] |= 0xFC; + if (buf[3] & 0x02) buf[3] |= 0xFC; + if (buf[5] & 0x02) buf[5] |= 0xFC; + + x = (int16_t)((buf[1] << 8) | buf[0]) + _xOff; + y = (int16_t)((buf[3] << 8) | buf[2]) + _yOff; + z = (int16_t)((buf[5] << 8) | buf[4]) + _zOff; + + + result = true; + + } while(0); + + + return result; +} + +bool MMA7455::calibrate() { + bool result = false; + bool failed = false; + + int32_t x = 0; + int32_t y = 0; + int32_t z = 0; + + int32_t xr = 0; + int32_t yr = 0; + int32_t zr = 0; + + int xOff = 0; + int yOff = 0; + int zOff = 16; + if (_range == Range_2g) { + zOff = 64; + } + if (_range == Range_4g) { + zOff = 32; + } + + do { + + // get an average of 6 values + for (int i = 0; i < 6; i++) { + if (!read(xr, yr, zr)) { + failed = true; + break; + } + x += xr; + y += yr; + z += zr; + + wait_ms(100); + } + + if (failed) break; + x /= 6; + y /= 6; + z /= 6; + + xOff -= x; + yOff -= y; + zOff -= z; + + /* + * For some reason we have not got correct/reliable calibration + * by using the offset drift registers. Instead we are + * calculating the offsets and store them in member variables. + * + * These member variables are then used in the read() method + */ + + _xOff = xOff; + _yOff = yOff; + _zOff = zOff; + + + result = true; + + } while (0); + + + + return result; +} + +bool MMA7455::setCalibrationOffsets(int32_t xOff, int32_t yOff, int32_t zOff) { + _xOff = xOff; + _yOff = yOff; + _zOff = zOff; + + return true; +} + +bool MMA7455::getCalibrationOffsets(int32_t& xOff, int32_t& yOff, int32_t& zOff) { + xOff = _xOff; + yOff = _yOff; + zOff = _zOff; + + return true; +} + +int MMA7455::getStatus() { + int result = -1; + char data[1]; + + do { + data[0] = MMA7455_ADDR_STATUS; + if (_i2c.write(MMA7455_I2C_ADDR, data, 1) != 0) break; + + if (_i2c.read(MMA7455_I2C_ADDR, data, 1) != 0) break; + + result = data[0]; + + } while (0); + + + + return result; +} + +int MMA7455::getModeControl() { + + int result = -1; + char data[1]; + + do { + data[0] = MMA7455_ADDR_MCTL; + if (_i2c.write(MMA7455_I2C_ADDR, data, 1) != 0) break; + + if (_i2c.read(MMA7455_I2C_ADDR, data, 1) != 0) break; + + result = data[0]; + + } while (0); + + + + return result; +} + +int MMA7455::setModeControl(uint8_t mctl) { + int result = -1; + char data[2]; + + do { + data[0] = MMA7455_ADDR_MCTL; + data[1] = (char)mctl; + if (_i2c.write(MMA7455_I2C_ADDR, data, 2) != 0) break; + + result = 0; + + } while (0); + + + + return result; +}
diff -r 000000000000 -r e548610ed175 MMA7455.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7455.h Tue Jan 02 11:43:43 2018 +0000 @@ -0,0 +1,93 @@ +#ifndef MMA7455_H +#define MMA7455_H + + +/** + * Freescale Accelerometer MMA7455. + */ +class MMA7455 { +public: + + enum Mode { + ModeStandby = 0, + ModeMeasurement = 1, + }; + + /** Acceleration range */ + enum Range { + Range_8g = 0, + Range_2g = 1, + Range_4g = 2 + }; + + /** + * Create an interface to the MMA7455 accelerometer + * + * @param sda I2C data line pin + * @param scl I2C clock line pin + */ + MMA7455(PinName sda, PinName scl); + + bool setMode(Mode mode); + bool setRange(Range range); + + bool read(int32_t& x, int32_t& y, int32_t& z); + + /** + * Calibrate for 0g, that is, calculate offset to achieve + * 0g values when accelerometer is placed on flat surface. + * + * Please make sure the accelerometer is placed on a flat surface before + * calling this function. + * + * @return true if request was successful; otherwise false + */ + bool calibrate(); + + /** + * Get calculated offset values. Offsets will be calculated by the + * calibrate() method. + * + * Use these values and put them in persistent storage to avoid + * having to calibrate the accelerometer after a reset/power cycle. + * + * @param xOff x offset is written to this argument + * @param yOff y offset is written to this argument + * @param zOff z offset is written to this argument + * + * @return true if request was successful; otherwise false + */ + bool getCalibrationOffsets(int32_t& xOff, int32_t& yOff, int32_t& zOff); + + /** + * Set calibration offset values. These values should normally + * at one point in time have been retrieved by calling the + * getCalibrationOffsets method. + * + * + * @param xOff x offset + * @param yOff y offset + * @param zOff z offset + * + * @return true if request was successful; otherwise false + */ + bool setCalibrationOffsets(int32_t xOff, int32_t yOff, int32_t zOff); + + + +private: + + I2C _i2c; + Mode _mode; + Range _range; + int32_t _xOff; + int32_t _yOff; + int32_t _zOff; + + int getStatus(); + int getModeControl(); + int setModeControl(uint8_t mctl); + +}; + +#endif
diff -r 000000000000 -r e548610ed175 README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Tue Jan 02 11:43:43 2018 +0000 @@ -0,0 +1,10 @@ +# mbed-os-example-coap + +This application demonstrates how to connect to a CoAP server from mbed OS 5. It connects to coap://coap.me, sends a message to `/hello` and receives the response. + +## To build + +1. Open ``mbed_app.json`` and change the `network-interface` option to your connectivity method ([more info](https://github.com/ARMmbed/easy-connect)). +2. Build the project in the online compiler or using mbed CLI. +3. Flash the project to your development board. +4. Attach a serial monitor to your board to see the debug messages.
diff -r 000000000000 -r e548610ed175 SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Tue Jan 02 11:43:43 2018 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/mbed_official/code/SDFileSystem/#8db0d3b02cec
diff -r 000000000000 -r e548610ed175 easy-connect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/easy-connect.lib Tue Jan 02 11:43:43 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/easy-connect/#a913964341394430cd3997c6f2950f93ba1d75c8
diff -r 000000000000 -r e548610ed175 mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue Jan 02 11:43:43 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#269f58d75b752a4e67a6a2d8c5c698635ffd6752
diff -r 000000000000 -r e548610ed175 mbed_app.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Tue Jan 02 11:43:43 2018 +0000 @@ -0,0 +1,78 @@ +{ + "config": { + "network-interface":{ + "help": "options are ETHERNET, WIFI_ESP8266, WIFI_ODIN, MESH_LOWPAN_ND, MESH_THREAD", + "value": "WIFI_ESP8266" + }, + "mesh_radio_type": { + "help": "options are ATMEL, MCR20", + "value": "ATMEL" + }, + "esp8266-tx": { + "help": "Pin used as TX (connects to ESP8266 RX)", + "value": "D8" + }, + "esp8266-rx": { + "help": "Pin used as RX (connects to ESP8266 TX)", + "value": "D2" + }, + "esp8266-debug": { + "value": false + }, + "wifi-ssid": { + "value": "\"netlab2\"" + }, + "wifi-password": { + "value": "\"netlab2@414\"" + }, + "button": { + "help": "Pin which you'll use as button (can be overriden per target below)", + "value": "BUTTON1" + }, + "builtin_led_on": { + "help": "Value to write to built-in LEDs to turn them on", + "value": 1, + "macro_name": "BUILTIN_LED_ON" + }, + "builtin_led_off": { + "help": "Value to write to built-in LEDs to turn them off", + "value": 0, + "macro_name": "BUILTIN_LED_OFF" + } + }, + "target_overrides": { + "*": { + "target.features_add": ["NANOSTACK", "LOWPAN_ROUTER", "COMMON_PAL"], + "platform.stdio-baud-rate": 115200, + "platform.stdio-convert-newlines": true, + "mbed-mesh-api.6lowpan-nd-channel-page": 0, + "mbed-mesh-api.6lowpan-nd-channel": 12, + "mbed-trace.enable": 0 + }, + "K64F": { + "button": "SW2", + "builtin_led_on": 0, + "builtin_led_off": 1 + }, + "HEXIWEAR": { + "button": "PTA12", + "esp8266-tx": "PTD3", + "esp8266-rx": "PTD2" + }, + "NUCLEO_F401RE": { + "button": "USER_BUTTON", + "esp8266-tx": "D8", + "esp8266-rx": "D2" + }, + "NUCLEO_F411RE": { + "button": "USER_BUTTON", + "esp8266-tx": "D8", + "esp8266-rx": "D2" + }, + "NUMAKER_PFM_NUC472": { + "button": "SW1", + "builtin_led_on": 0, + "builtin_led_off": 1 + } + } +}
diff -r 000000000000 -r e548610ed175 source/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/main.cpp Tue Jan 02 11:43:43 2018 +0000 @@ -0,0 +1,138 @@ +#include <string> +#include "mbed.h" +#include "easy-connect.h" +#include "TCPSocket.h" +#include "DHT.h" + +#define SERVER_IP "192.168.0.9" +#define SERVER_PORT 50000 + +Serial pc(USBTX, USBRX); // computer to mbed boardSerial esp(D1, D0); +DHT sensor(D7,DHT22); + +void task_DHT(); + +void http_demo(NetworkInterface *net) +{ + int error = 0; + int h, c, f; + float dp = 0.0f; + + TCPSocket socket; // for TCP + + pc.printf("Sending TCP request to %s:%d ...\r\n", SERVER_IP, SERVER_PORT); + + // Open a socket on the network interface, and create a TCP connection + socket.open(net); + socket.connect(SERVER_IP, SERVER_PORT); + + int scount; + + while(1){ + + char buf[64]; + int len = socket.recv(buf, 64); + buf[len] = '\0'; + + char buffer[64] = "GET /DHT22"; + int result = strcmp(buf, buffer); + printf("received: %s %d\r\n",buf,result); + + if (strcmp(buf, buffer)){ + printf("*****The wrong request come.*****\r\n"); + char sbuffer4[] = "GET /Unknown request\r\n"; + //char sbuffer4[] = "{\"method\":\"GET\",\"content\":\"Unknown request\"}\r\n"; + scount = socket.send(sbuffer4, sizeof sbuffer4); + pc.printf("send error message: %d [%.*s]\r\n", scount, strstr(sbuffer4, "\r\n")-sbuffer4, sbuffer4); + + }else{ // strcmp(buf, buffer)값이 0일 때, 즉, 전송된 값이 "GET /DHT22\r\n" 일 때 + + printf("*****The correct request come.*****\r\n"); + char sbuffer1[] = "GET /Ok\r\n"; + //char sbuffer1[] = "{\"method\":\"GET\",\"message\":\"Ok\"}\r\n"; + scount = socket.send(sbuffer1, sizeof sbuffer1); + pc.printf("send Ok message: %d [%.*s]\r\n", scount, strstr(sbuffer1, "\r\n")-sbuffer1, sbuffer1); + + error = sensor.readData(); + if (0 == error) { + c = sensor.ReadTemperature(CELCIUS); + f = sensor.ReadTemperature(FARENHEIT); + h = sensor.ReadHumidity(); + dp = sensor.CalcdewPoint(c, h); + + char sbuffer2[33]; + sprintf(sbuffer2, "{\"type\":\"temperature\",\"value\":%d}\r\n", c); + scount = socket.send(sbuffer2, sizeof sbuffer2); + pc.printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer2, "\r\n")-sbuffer2, sbuffer2); + + printf("Temperature in Celcius: %d, Farenheit %d\r\n", c, f); + + char sbuffer3[32]; + sprintf(sbuffer3, "{\"type\":\"humidity\",\"value\":%d}\r\n", h); + scount = socket.send(sbuffer3, sizeof sbuffer3); + pc.printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer3, "\r\n")-sbuffer3, sbuffer3); + + printf("Humidity is %d, Dewpoint: %4.2f\r\n\r\n", h, dp); + + } else { // checksum error가 발생하는 경우 터미널에 error 발생을 표시 + printf("Checksum Error: %d\r\n", error); + } + } + } + // Close the socket to return its memory and bring down the network interface + socket.close(); +} +/* +void task_DHT(){ + + int error = 0; + int h, c, f; + float dp = 0.0f; + char buf[256]; + + wait(3); + error = sensor.readData(); + if (0 == error) { + c = sensor.ReadTemperature(CELCIUS); + f = sensor.ReadTemperature(FARENHEIT); + h = sensor.ReadHumidity(); + dp = sensor.CalcdewPoint(c, h); + sprintf(buf, "Temperature in Celcius: %d, Farenheit %d\r\n", c, f); + //printf("Temperature in Celcius: %d, Farenheit %d\r\n", c, f); + sprintf(buf, "Humidity is %d, Dewpoint: %4.2f\r\n\n", h, dp); + //printf("Humidity is %d, Dewpoint: %4.2f\r\n\n", h, dp); + + } else { + printf("Error: %d\r\n", error); + } +} +*/ +int main() { + + pc.baud(115200); + pc.printf("\r\n Simple TCP example over ESP8266\r\n\r\n"); + + pc.printf("\r\nConnecting...\r\n"); + + NetworkInterface *network = easy_connect(true); + + if (!network) { + pc.printf("Error: Cannot connect to the network\r\n"); + return -1; + } + + pc.printf("Success\r\n\r\n"); + pc.printf("MAC: %s\r\n", network->get_mac_address()); + pc.printf("IP: %s\r\n", network->get_ip_address()); + pc.printf("Netmask: %s\r\n", network->get_netmask()); + pc.printf("Gateway: %s\r\n", network->get_gateway()); + pc.printf("RSSI: %d\r\n\r\n", wifi.get_rssi()); + + http_demo(network); + //network->disconnect(); + //pc.printf("\r\nDone\r\n"); + + while(1) { + //task_DHT(); + } +}