read water level from aduino water sensor, and forward it ti artik 710 board. and It controls humidifier by 2 channel relay module
Dependencies: MbedJSONValue WebSocketClient
Revision 0:a3a9a2aa25eb, committed 2018-01-09
- Comitter:
- Dongho
- Date:
- Tue Jan 09 07:00:32 2018 +0000
- Commit message:
- iot project;
Changed in this revision
diff -r 000000000000 -r a3a9a2aa25eb MbedJSONValue.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedJSONValue.lib Tue Jan 09 07:00:32 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/samux/code/MbedJSONValue/#10a99cdf7846
diff -r 000000000000 -r a3a9a2aa25eb README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Tue Jan 09 07:00:32 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 a3a9a2aa25eb WebSocketClient.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebSocketClient.lib Tue Jan 09 07:00:32 2018 +0000 @@ -0,0 +1,1 @@ +http://os.mbed.com/teams/mbed_example/code/WebSocketClient/#efa2c147bee1
diff -r 000000000000 -r a3a9a2aa25eb easy-connect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/easy-connect.lib Tue Jan 09 07:00:32 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/easy-connect/#a913964341394430cd3997c6f2950f93ba1d75c8
diff -r 000000000000 -r a3a9a2aa25eb mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue Jan 09 07:00:32 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#269f58d75b752a4e67a6a2d8c5c698635ffd6752
diff -r 000000000000 -r a3a9a2aa25eb mbed_app.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Tue Jan 09 07:00:32 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": "\"netlab1\"" + }, + "wifi-password": { + "value": "\"netlab1@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 a3a9a2aa25eb source/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/main.cpp Tue Jan 09 07:00:32 2018 +0000 @@ -0,0 +1,189 @@ + +#include <string> +#include <vector> +#include "mbed.h" +#include "easy-connect.h" +#include "Websocket.h" +#include "MbedJSONValue.h" + + /* connect this pin to both the CH_PD (aka EN) & RST pins on the ESP8266 just in case */ +#define _threshold 2000.0 +#define SERVER_IP "192.168.0.9" +#define SERVER_PORT 8080 + +Serial pc(USBTX, USBRX); // computer to mbed boardSerial esp(D1, D0); +AnalogIn analog_value(A0); +DigitalOut relay(D4); + + + +Thread recvThread; +int power = 0; + +char* str_to_char(std::string str){ + std::vector<char> writable(str.begin(), str.end()); + writable.push_back('\0'); + char* ptr = &writable[0]; + + return ptr; +} + +std::string send_data(float level, std::string name){ + MbedJSONValue data_json; + + data_json["type"] = "value"; + if(!strcmp(name.c_str(), "waterlevel")){ + data_json["name"] = name; + } else if(!strcmp(name.c_str(), "humidity")) { + data_json["name"] = name; + } + data_json["value"] = level; + + return data_json.serialize(); + } + + std::string send_ack(int id, std::string type, std::string name, std::string value, std::string tgt_type, int tgt_idx){ + MbedJSONValue ack_json; + + ack_json["paired_action_id"] = id; + ack_json["type"] = type; + ack_json["name"] = name; + ack_json["value"] = value; + ack_json["target"]["type"] = tgt_type; + ack_json["target"]["index"] = tgt_idx; + + return ack_json.serialize(); +} + +void recv_tcp(TCPSocket* tcp) { + char recvBuffer[200] = {0, }; + while (1) { + if (tcp->recv(recvBuffer, sizeof(char) * 200)) { + + char value[100] = {0, }; + int idx = 0; + int tgt_idx = 0; + sscanf(recvBuffer, "{\"id\":%d,\"type\":\"action\",\"name\":\"power_control\",\"value\":\"%s\",\"target\":{\"type\":\"humidifier\",\"index\":%d}}", &idx, value, &tgt_idx); + + if(strstr(value, "on") == value) { + if(power == 1){ + pc.printf("power is already on\r\n"); + } + else{ + relay.write(0); + power = 1; + pc.printf("power on\r\n"); + + std::string send_msg = send_ack(idx, "status", "power_state", "on", "humidifier", tgt_idx); + pc.printf("send ack: %s\r\n", send_msg.c_str()); + + int scount = tcp->send(str_to_char(send_msg), send_msg.size()); + pc.printf("length of message : %d\r\n", scount); + } + + } else if(strstr(value, "off") == value) { + if(power == 0){ + pc.printf("power is already off\r\n"); + } + else{ + relay.write(1); + power = 0; + pc.printf("power off\r\n"); + + std::string send_msg = send_ack(idx, "status", "power_state", "off", "humidifier", tgt_idx); + pc.printf("send ack: %s\r\n", send_msg.c_str()); + + int scount = tcp->send(str_to_char(send_msg), send_msg.size()); + pc.printf("length of message : %d\r\n", scount); + } + } + else { + pc.printf("%s", value); + } + } + } +} + +int main() { + + pc.baud(115200); + relay.write(1); + power = 0; + + pc.printf("\r\n TCP connection\r\n"); + pc.printf("Resetting ESP8266 Hardware...\r\n"); + + wait_ms(500); + + NetworkInterface *network = easy_connect(true); + + + while (1) { + TCPSocket socket; // for HTTP + + // Open a socket on the network interface, and create a TCP connection + socket.open(network); + bool status = socket.connect(SERVER_IP, SERVER_PORT); + + if (status == true) { + recvThread.start(&socket, recv_tcp); + + while(1) { + float meas_lev = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) + meas_lev = meas_lev * 3300; // Change the value to be in the 0 to 3300 range + + if(meas_lev <= _threshold){ + pc.printf("alert!: water level is low\r\n"); + } + + pc.printf("Current water level: %.2f\r\n", meas_lev); + std::string send_msg = send_data(meas_lev, "waterlevel"); + + pc.printf("send data: %s\r\n", send_msg.c_str()); + int scount = socket.send(str_to_char(send_msg), send_msg.size()); + pc.printf("length of message : %d\r\n", scount); + + if(scount == -3012){ + recvThread.terminate(); + break; + } + + Thread::wait(3000); + } + } + socket.close(); + } +} + + + +/* +#include "mbed.h" + #include "MbedJSONValue.h" + #include <string> + + Serial pc(USBTX, USBRX); + + int main() { + MbedJSONValue recv_json; + pc.baud(115200); + + char value[64] = {0, }; + + const char * json = "{\"id\":0,\"type\":\"action\",\"name\":\"power_control\",\"value\":\"off\",\"target\":{\"type\":\"humidifier\",\"index\":0}}"; + // parse(recv_json, json); + + pc.printf("recv %s\r\n", json); + + sscanf(json, "{\"id\":0,\"type\":\"action\",\"name\":\"power_control\",\"value\":\"%s\",\"target\":{\"type\":\"humidifier\",\"index\":0}}", value); + + if(strstr(value, "on") == value) { + pc.printf("on found"); + } else if(strstr(value, "off") == value) { + pc.printf("off found"); + } + + return 0; + } + +*/ \ No newline at end of file