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
source/main.cpp@0:a3a9a2aa25eb, 2018-01-09 (annotated)
- Committer:
- Dongho
- Date:
- Tue Jan 09 07:00:32 2018 +0000
- Revision:
- 0:a3a9a2aa25eb
iot project;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dongho | 0:a3a9a2aa25eb | 1 | |
Dongho | 0:a3a9a2aa25eb | 2 | #include <string> |
Dongho | 0:a3a9a2aa25eb | 3 | #include <vector> |
Dongho | 0:a3a9a2aa25eb | 4 | #include "mbed.h" |
Dongho | 0:a3a9a2aa25eb | 5 | #include "easy-connect.h" |
Dongho | 0:a3a9a2aa25eb | 6 | #include "Websocket.h" |
Dongho | 0:a3a9a2aa25eb | 7 | #include "MbedJSONValue.h" |
Dongho | 0:a3a9a2aa25eb | 8 | |
Dongho | 0:a3a9a2aa25eb | 9 | /* connect this pin to both the CH_PD (aka EN) & RST pins on the ESP8266 just in case */ |
Dongho | 0:a3a9a2aa25eb | 10 | #define _threshold 2000.0 |
Dongho | 0:a3a9a2aa25eb | 11 | #define SERVER_IP "192.168.0.9" |
Dongho | 0:a3a9a2aa25eb | 12 | #define SERVER_PORT 8080 |
Dongho | 0:a3a9a2aa25eb | 13 | |
Dongho | 0:a3a9a2aa25eb | 14 | Serial pc(USBTX, USBRX); // computer to mbed boardSerial esp(D1, D0); |
Dongho | 0:a3a9a2aa25eb | 15 | AnalogIn analog_value(A0); |
Dongho | 0:a3a9a2aa25eb | 16 | DigitalOut relay(D4); |
Dongho | 0:a3a9a2aa25eb | 17 | |
Dongho | 0:a3a9a2aa25eb | 18 | |
Dongho | 0:a3a9a2aa25eb | 19 | |
Dongho | 0:a3a9a2aa25eb | 20 | Thread recvThread; |
Dongho | 0:a3a9a2aa25eb | 21 | int power = 0; |
Dongho | 0:a3a9a2aa25eb | 22 | |
Dongho | 0:a3a9a2aa25eb | 23 | char* str_to_char(std::string str){ |
Dongho | 0:a3a9a2aa25eb | 24 | std::vector<char> writable(str.begin(), str.end()); |
Dongho | 0:a3a9a2aa25eb | 25 | writable.push_back('\0'); |
Dongho | 0:a3a9a2aa25eb | 26 | char* ptr = &writable[0]; |
Dongho | 0:a3a9a2aa25eb | 27 | |
Dongho | 0:a3a9a2aa25eb | 28 | return ptr; |
Dongho | 0:a3a9a2aa25eb | 29 | } |
Dongho | 0:a3a9a2aa25eb | 30 | |
Dongho | 0:a3a9a2aa25eb | 31 | std::string send_data(float level, std::string name){ |
Dongho | 0:a3a9a2aa25eb | 32 | MbedJSONValue data_json; |
Dongho | 0:a3a9a2aa25eb | 33 | |
Dongho | 0:a3a9a2aa25eb | 34 | data_json["type"] = "value"; |
Dongho | 0:a3a9a2aa25eb | 35 | if(!strcmp(name.c_str(), "waterlevel")){ |
Dongho | 0:a3a9a2aa25eb | 36 | data_json["name"] = name; |
Dongho | 0:a3a9a2aa25eb | 37 | } else if(!strcmp(name.c_str(), "humidity")) { |
Dongho | 0:a3a9a2aa25eb | 38 | data_json["name"] = name; |
Dongho | 0:a3a9a2aa25eb | 39 | } |
Dongho | 0:a3a9a2aa25eb | 40 | data_json["value"] = level; |
Dongho | 0:a3a9a2aa25eb | 41 | |
Dongho | 0:a3a9a2aa25eb | 42 | return data_json.serialize(); |
Dongho | 0:a3a9a2aa25eb | 43 | } |
Dongho | 0:a3a9a2aa25eb | 44 | |
Dongho | 0:a3a9a2aa25eb | 45 | std::string send_ack(int id, std::string type, std::string name, std::string value, std::string tgt_type, int tgt_idx){ |
Dongho | 0:a3a9a2aa25eb | 46 | MbedJSONValue ack_json; |
Dongho | 0:a3a9a2aa25eb | 47 | |
Dongho | 0:a3a9a2aa25eb | 48 | ack_json["paired_action_id"] = id; |
Dongho | 0:a3a9a2aa25eb | 49 | ack_json["type"] = type; |
Dongho | 0:a3a9a2aa25eb | 50 | ack_json["name"] = name; |
Dongho | 0:a3a9a2aa25eb | 51 | ack_json["value"] = value; |
Dongho | 0:a3a9a2aa25eb | 52 | ack_json["target"]["type"] = tgt_type; |
Dongho | 0:a3a9a2aa25eb | 53 | ack_json["target"]["index"] = tgt_idx; |
Dongho | 0:a3a9a2aa25eb | 54 | |
Dongho | 0:a3a9a2aa25eb | 55 | return ack_json.serialize(); |
Dongho | 0:a3a9a2aa25eb | 56 | } |
Dongho | 0:a3a9a2aa25eb | 57 | |
Dongho | 0:a3a9a2aa25eb | 58 | void recv_tcp(TCPSocket* tcp) { |
Dongho | 0:a3a9a2aa25eb | 59 | char recvBuffer[200] = {0, }; |
Dongho | 0:a3a9a2aa25eb | 60 | while (1) { |
Dongho | 0:a3a9a2aa25eb | 61 | if (tcp->recv(recvBuffer, sizeof(char) * 200)) { |
Dongho | 0:a3a9a2aa25eb | 62 | |
Dongho | 0:a3a9a2aa25eb | 63 | char value[100] = {0, }; |
Dongho | 0:a3a9a2aa25eb | 64 | int idx = 0; |
Dongho | 0:a3a9a2aa25eb | 65 | int tgt_idx = 0; |
Dongho | 0:a3a9a2aa25eb | 66 | sscanf(recvBuffer, "{\"id\":%d,\"type\":\"action\",\"name\":\"power_control\",\"value\":\"%s\",\"target\":{\"type\":\"humidifier\",\"index\":%d}}", &idx, value, &tgt_idx); |
Dongho | 0:a3a9a2aa25eb | 67 | |
Dongho | 0:a3a9a2aa25eb | 68 | if(strstr(value, "on") == value) { |
Dongho | 0:a3a9a2aa25eb | 69 | if(power == 1){ |
Dongho | 0:a3a9a2aa25eb | 70 | pc.printf("power is already on\r\n"); |
Dongho | 0:a3a9a2aa25eb | 71 | } |
Dongho | 0:a3a9a2aa25eb | 72 | else{ |
Dongho | 0:a3a9a2aa25eb | 73 | relay.write(0); |
Dongho | 0:a3a9a2aa25eb | 74 | power = 1; |
Dongho | 0:a3a9a2aa25eb | 75 | pc.printf("power on\r\n"); |
Dongho | 0:a3a9a2aa25eb | 76 | |
Dongho | 0:a3a9a2aa25eb | 77 | std::string send_msg = send_ack(idx, "status", "power_state", "on", "humidifier", tgt_idx); |
Dongho | 0:a3a9a2aa25eb | 78 | pc.printf("send ack: %s\r\n", send_msg.c_str()); |
Dongho | 0:a3a9a2aa25eb | 79 | |
Dongho | 0:a3a9a2aa25eb | 80 | int scount = tcp->send(str_to_char(send_msg), send_msg.size()); |
Dongho | 0:a3a9a2aa25eb | 81 | pc.printf("length of message : %d\r\n", scount); |
Dongho | 0:a3a9a2aa25eb | 82 | } |
Dongho | 0:a3a9a2aa25eb | 83 | |
Dongho | 0:a3a9a2aa25eb | 84 | } else if(strstr(value, "off") == value) { |
Dongho | 0:a3a9a2aa25eb | 85 | if(power == 0){ |
Dongho | 0:a3a9a2aa25eb | 86 | pc.printf("power is already off\r\n"); |
Dongho | 0:a3a9a2aa25eb | 87 | } |
Dongho | 0:a3a9a2aa25eb | 88 | else{ |
Dongho | 0:a3a9a2aa25eb | 89 | relay.write(1); |
Dongho | 0:a3a9a2aa25eb | 90 | power = 0; |
Dongho | 0:a3a9a2aa25eb | 91 | pc.printf("power off\r\n"); |
Dongho | 0:a3a9a2aa25eb | 92 | |
Dongho | 0:a3a9a2aa25eb | 93 | std::string send_msg = send_ack(idx, "status", "power_state", "off", "humidifier", tgt_idx); |
Dongho | 0:a3a9a2aa25eb | 94 | pc.printf("send ack: %s\r\n", send_msg.c_str()); |
Dongho | 0:a3a9a2aa25eb | 95 | |
Dongho | 0:a3a9a2aa25eb | 96 | int scount = tcp->send(str_to_char(send_msg), send_msg.size()); |
Dongho | 0:a3a9a2aa25eb | 97 | pc.printf("length of message : %d\r\n", scount); |
Dongho | 0:a3a9a2aa25eb | 98 | } |
Dongho | 0:a3a9a2aa25eb | 99 | } |
Dongho | 0:a3a9a2aa25eb | 100 | else { |
Dongho | 0:a3a9a2aa25eb | 101 | pc.printf("%s", value); |
Dongho | 0:a3a9a2aa25eb | 102 | } |
Dongho | 0:a3a9a2aa25eb | 103 | } |
Dongho | 0:a3a9a2aa25eb | 104 | } |
Dongho | 0:a3a9a2aa25eb | 105 | } |
Dongho | 0:a3a9a2aa25eb | 106 | |
Dongho | 0:a3a9a2aa25eb | 107 | int main() { |
Dongho | 0:a3a9a2aa25eb | 108 | |
Dongho | 0:a3a9a2aa25eb | 109 | pc.baud(115200); |
Dongho | 0:a3a9a2aa25eb | 110 | relay.write(1); |
Dongho | 0:a3a9a2aa25eb | 111 | power = 0; |
Dongho | 0:a3a9a2aa25eb | 112 | |
Dongho | 0:a3a9a2aa25eb | 113 | pc.printf("\r\n TCP connection\r\n"); |
Dongho | 0:a3a9a2aa25eb | 114 | pc.printf("Resetting ESP8266 Hardware...\r\n"); |
Dongho | 0:a3a9a2aa25eb | 115 | |
Dongho | 0:a3a9a2aa25eb | 116 | wait_ms(500); |
Dongho | 0:a3a9a2aa25eb | 117 | |
Dongho | 0:a3a9a2aa25eb | 118 | NetworkInterface *network = easy_connect(true); |
Dongho | 0:a3a9a2aa25eb | 119 | |
Dongho | 0:a3a9a2aa25eb | 120 | |
Dongho | 0:a3a9a2aa25eb | 121 | while (1) { |
Dongho | 0:a3a9a2aa25eb | 122 | TCPSocket socket; // for HTTP |
Dongho | 0:a3a9a2aa25eb | 123 | |
Dongho | 0:a3a9a2aa25eb | 124 | // Open a socket on the network interface, and create a TCP connection |
Dongho | 0:a3a9a2aa25eb | 125 | socket.open(network); |
Dongho | 0:a3a9a2aa25eb | 126 | bool status = socket.connect(SERVER_IP, SERVER_PORT); |
Dongho | 0:a3a9a2aa25eb | 127 | |
Dongho | 0:a3a9a2aa25eb | 128 | if (status == true) { |
Dongho | 0:a3a9a2aa25eb | 129 | recvThread.start(&socket, recv_tcp); |
Dongho | 0:a3a9a2aa25eb | 130 | |
Dongho | 0:a3a9a2aa25eb | 131 | while(1) { |
Dongho | 0:a3a9a2aa25eb | 132 | float meas_lev = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
Dongho | 0:a3a9a2aa25eb | 133 | meas_lev = meas_lev * 3300; // Change the value to be in the 0 to 3300 range |
Dongho | 0:a3a9a2aa25eb | 134 | |
Dongho | 0:a3a9a2aa25eb | 135 | if(meas_lev <= _threshold){ |
Dongho | 0:a3a9a2aa25eb | 136 | pc.printf("alert!: water level is low\r\n"); |
Dongho | 0:a3a9a2aa25eb | 137 | } |
Dongho | 0:a3a9a2aa25eb | 138 | |
Dongho | 0:a3a9a2aa25eb | 139 | pc.printf("Current water level: %.2f\r\n", meas_lev); |
Dongho | 0:a3a9a2aa25eb | 140 | std::string send_msg = send_data(meas_lev, "waterlevel"); |
Dongho | 0:a3a9a2aa25eb | 141 | |
Dongho | 0:a3a9a2aa25eb | 142 | pc.printf("send data: %s\r\n", send_msg.c_str()); |
Dongho | 0:a3a9a2aa25eb | 143 | int scount = socket.send(str_to_char(send_msg), send_msg.size()); |
Dongho | 0:a3a9a2aa25eb | 144 | pc.printf("length of message : %d\r\n", scount); |
Dongho | 0:a3a9a2aa25eb | 145 | |
Dongho | 0:a3a9a2aa25eb | 146 | if(scount == -3012){ |
Dongho | 0:a3a9a2aa25eb | 147 | recvThread.terminate(); |
Dongho | 0:a3a9a2aa25eb | 148 | break; |
Dongho | 0:a3a9a2aa25eb | 149 | } |
Dongho | 0:a3a9a2aa25eb | 150 | |
Dongho | 0:a3a9a2aa25eb | 151 | Thread::wait(3000); |
Dongho | 0:a3a9a2aa25eb | 152 | } |
Dongho | 0:a3a9a2aa25eb | 153 | } |
Dongho | 0:a3a9a2aa25eb | 154 | socket.close(); |
Dongho | 0:a3a9a2aa25eb | 155 | } |
Dongho | 0:a3a9a2aa25eb | 156 | } |
Dongho | 0:a3a9a2aa25eb | 157 | |
Dongho | 0:a3a9a2aa25eb | 158 | |
Dongho | 0:a3a9a2aa25eb | 159 | |
Dongho | 0:a3a9a2aa25eb | 160 | /* |
Dongho | 0:a3a9a2aa25eb | 161 | #include "mbed.h" |
Dongho | 0:a3a9a2aa25eb | 162 | #include "MbedJSONValue.h" |
Dongho | 0:a3a9a2aa25eb | 163 | #include <string> |
Dongho | 0:a3a9a2aa25eb | 164 | |
Dongho | 0:a3a9a2aa25eb | 165 | Serial pc(USBTX, USBRX); |
Dongho | 0:a3a9a2aa25eb | 166 | |
Dongho | 0:a3a9a2aa25eb | 167 | int main() { |
Dongho | 0:a3a9a2aa25eb | 168 | MbedJSONValue recv_json; |
Dongho | 0:a3a9a2aa25eb | 169 | pc.baud(115200); |
Dongho | 0:a3a9a2aa25eb | 170 | |
Dongho | 0:a3a9a2aa25eb | 171 | char value[64] = {0, }; |
Dongho | 0:a3a9a2aa25eb | 172 | |
Dongho | 0:a3a9a2aa25eb | 173 | const char * json = "{\"id\":0,\"type\":\"action\",\"name\":\"power_control\",\"value\":\"off\",\"target\":{\"type\":\"humidifier\",\"index\":0}}"; |
Dongho | 0:a3a9a2aa25eb | 174 | // parse(recv_json, json); |
Dongho | 0:a3a9a2aa25eb | 175 | |
Dongho | 0:a3a9a2aa25eb | 176 | pc.printf("recv %s\r\n", json); |
Dongho | 0:a3a9a2aa25eb | 177 | |
Dongho | 0:a3a9a2aa25eb | 178 | sscanf(json, "{\"id\":0,\"type\":\"action\",\"name\":\"power_control\",\"value\":\"%s\",\"target\":{\"type\":\"humidifier\",\"index\":0}}", value); |
Dongho | 0:a3a9a2aa25eb | 179 | |
Dongho | 0:a3a9a2aa25eb | 180 | if(strstr(value, "on") == value) { |
Dongho | 0:a3a9a2aa25eb | 181 | pc.printf("on found"); |
Dongho | 0:a3a9a2aa25eb | 182 | } else if(strstr(value, "off") == value) { |
Dongho | 0:a3a9a2aa25eb | 183 | pc.printf("off found"); |
Dongho | 0:a3a9a2aa25eb | 184 | } |
Dongho | 0:a3a9a2aa25eb | 185 | |
Dongho | 0:a3a9a2aa25eb | 186 | return 0; |
Dongho | 0:a3a9a2aa25eb | 187 | } |
Dongho | 0:a3a9a2aa25eb | 188 | |
Dongho | 0:a3a9a2aa25eb | 189 | */ |