Dongho Kim
/
DHT22_TCP
CoAP example application for mbed OS 5
Fork of coap-example by
Revision 1:113541750e81, committed 2018-01-02
- Comitter:
- Dongho
- Date:
- Tue Jan 02 21:57:20 2018 +0000
- Parent:
- 0:0681e205d0e9
- Commit message:
- ok
Changed in this revision
diff -r 0681e205d0e9 -r 113541750e81 DHT.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHT.lib Tue Jan 02 21:57:20 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/Wimpie/code/DHT/#9b5b3200688f
diff -r 0681e205d0e9 -r 113541750e81 mbed_app.json --- a/mbed_app.json Wed Feb 15 21:18:58 2017 +0100 +++ b/mbed_app.json Tue Jan 02 21:57:20 2018 +0000 @@ -2,7 +2,7 @@ "config": { "network-interface":{ "help": "options are ETHERNET, WIFI_ESP8266, WIFI_ODIN, MESH_LOWPAN_ND, MESH_THREAD", - "value": "ETHERNET" + "value": "WIFI_ESP8266" }, "mesh_radio_type": { "help": "options are ATMEL, MCR20", @@ -10,20 +10,20 @@ }, "esp8266-tx": { "help": "Pin used as TX (connects to ESP8266 RX)", - "value": "D1" + "value": "D8" }, "esp8266-rx": { "help": "Pin used as RX (connects to ESP8266 TX)", - "value": "D0" + "value": "D2" }, "esp8266-debug": { - "value": true + "value": false }, "wifi-ssid": { - "value": "\"SSID\"" + "value": "\"netlab1\"" }, "wifi-password": { - "value": "\"Password\"" + "value": "\"netlab1@414\"" }, "button": { "help": "Pin which you'll use as button (can be overriden per target below)",
diff -r 0681e205d0e9 -r 113541750e81 source/main.cpp --- a/source/main.cpp Wed Feb 15 21:18:58 2017 +0100 +++ b/source/main.cpp Tue Jan 02 21:57:20 2018 +0000 @@ -1,136 +1,141 @@ /* - * PackageLicenseDeclared: Apache-2.0 - * Copyright (c) 2017 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +#include <string> +#include "mbed.h" +#include "easy-connect.h" +#include "TCPSocket.h" +#include "DHT.h" + +#define SERVER_IP "192.168.0.14" +#define SERVER_PORT 50000 + +Serial esp(D8, D2); // computer to mbed boardSerial esp(D1, D0); +Serial pc(USBTX, USBRX); + +DHT sensor(D6, AM2302); + +int main() { + esp.baud(115200); + 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()); + + 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(network); + socket.connect(SERVER_IP, SERVER_PORT); + + int err; + wait(1); // wait 1 second for device stable status + while (1) { + err = sensor.readData(); + if (err == 0) { + //char sbuffer[] = "{\"Temperature\": \"%4.2f C\", \"Humidity\": \"%4.2f\"} \r\n\r\n", sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity(); + char sbuffer[] = "{\"Temperature\": \"22.0 C\", \"Humidity\": \"65 %\"} \r\n\r\n"; + int scount = socket.send(sbuffer, sizeof sbuffer); + pc.printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); + + //Receive a simple http response and print out the response line + char rbuffer[64]; + int rcount = socket.recv(rbuffer, sizeof rbuffer); + pc.printf("sent %d [%.*s]\r\n", scount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); + } else{ + printf("\r\nErr %i \n",err); + } + wait(3); + } + + socket.close(); +} +*/ #include <string> #include "mbed.h" #include "easy-connect.h" -#include "sn_nsdl.h" -#include "sn_coap_protocol.h" -#include "sn_coap_header.h" +#include "TCPSocket.h" +#include "DHT.h" -UDPSocket socket; // Socket to talk CoAP over -Thread recvfromThread; // Thread to receive messages over CoAP +#define SERVER_IP "192.168.0.14" +#define SERVER_PORT 50000 -struct coap_s* coapHandle; -coap_version_e coapVersion = COAP_VERSION_1; +DHT sensor(D6, AM2302); -// CoAP HAL -void* coap_malloc(uint16_t size) { - return malloc(size); -} +Serial pc(USBTX, USBRX); // computer to mbed boardSerial esp(D1, D0); -void coap_free(void* addr) { - free(addr); -} - -// tx_cb and rx_cb are not used in this program -uint8_t coap_tx_cb(uint8_t *a, uint16_t b, sn_nsdl_addr_s *c, void *d) { - printf("coap tx cb\n"); - return 0; -} +void http_demo(NetworkInterface *net) +{ + TCPSocket socket; // for HTTP + + // Open a socket on the network interface, and create a TCP connection + socket.open(net); + socket.connect(SERVER_IP, SERVER_PORT); -int8_t coap_rx_cb(sn_coap_hdr_s *a, sn_nsdl_addr_s *b, void *c) { - printf("coap rx cb\n"); - return 0; -} - -// Main function for the recvfrom thread -void recvfromMain() { - SocketAddress addr; - uint8_t* recv_buffer = (uint8_t*)malloc(1280); // Suggested is to keep packet size under 1280 bytes - - nsapi_size_or_error_t ret; - - while ((ret = socket.recvfrom(&addr, recv_buffer, 1280)) >= 0) { - // to see where the message came from, inspect addr.get_addr() and addr.get_port() - - printf("Received a message of length '%d'\n", ret); - - sn_coap_hdr_s* parsed = sn_coap_parser(coapHandle, ret, recv_buffer, &coapVersion); - - // We know the payload is going to be a string - std::string payload((const char*)parsed->payload_ptr, parsed->payload_len); - - printf("\tmsg_id: %d\n", parsed->msg_id); - printf("\tmsg_code: %d\n", parsed->msg_code); - printf("\tcontent_format: %d\n", parsed->content_format); - printf("\tpayload_len: %d\n", parsed->payload_len); - printf("\tpayload: %s\n", payload.c_str()); - printf("\toptions_list_ptr: %p\n", parsed->options_list_ptr); + int err; + wait(1); // wait 1 second for device stable status + + err = sensor.readData(); + // Send a simple http request + if(err == 0){ + char sbuffer[64]; + sprintf(sbuffer, "{\"Temperature\": \"%4.2fC\", \"Humidity\": \"%4.2f %\"} \r\n\r\n", sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity()); + int scount = socket.send(sbuffer, sizeof sbuffer); + pc.printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); + + // Recieve a simple http response and print out the response line + char rbuffer[64]; + int rcount = socket.recv(rbuffer, sizeof rbuffer); + pc.printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); + + rcount = socket.recv(rbuffer, sizeof rbuffer); + pc.printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); + } else { + char err[] = "Error\r\n"; + int ecount = socket.send(err, sizeof err); + pc.printf("sent %d [%.*s]\r\n", ecount, strstr(err, "\r\n")-err, err); } - - free(recv_buffer); - - printf("UDPSocket::recvfrom failed, error code %d. Shutting down receive thread.\n", ret); + + // Close the socket to return its memory and bring down the network interface + socket.close(); } int main() { + pc.baud(115200); + pc.printf("\r\n Simple HTTP example over ESP8266\r\n\r\n"); + pc.printf("\r\nConnecting...\r\n"); + NetworkInterface *network = easy_connect(true); if (!network) { - printf("Cannot connect to the network, see serial output"); - return 1; + pc.printf("Error: Cannot connect to the network\r\n"); + return -1; } - printf("Connected to the network. Opening a socket...\n"); - - // Open a socket on the network interface - socket.open(network); - - // Initialize the CoAP protocol handle, pointing to local implementations on malloc/free/tx/rx functions - coapHandle = sn_coap_protocol_init(&coap_malloc, &coap_free, &coap_tx_cb, &coap_rx_cb); - - // UDPSocket::recvfrom is blocking, so run it in a separate RTOS thread - recvfromThread.start(&recvfromMain); - - // Path to the resource we want to retrieve - const char* coap_uri_path = "/hello"; - - // See ns_coap_header.h - sn_coap_hdr_s *coap_res_ptr = (sn_coap_hdr_s*)calloc(sizeof(sn_coap_hdr_s), 1); - coap_res_ptr->uri_path_ptr = (uint8_t*)coap_uri_path; // Path - coap_res_ptr->uri_path_len = strlen(coap_uri_path); - coap_res_ptr->msg_code = COAP_MSG_CODE_REQUEST_GET; // CoAP method - coap_res_ptr->content_format = COAP_CT_TEXT_PLAIN; // CoAP content type - coap_res_ptr->payload_len = 0; // Body length - coap_res_ptr->payload_ptr = 0; // Body pointer - coap_res_ptr->options_list_ptr = 0; // Optional: options list - // Message ID is used to track request->response patterns, because we're using UDP (so everything is unconfirmed). - // See the receive code to verify that we get the same message ID back - coap_res_ptr->msg_id = 7; - - // Calculate the CoAP message size, allocate the memory and build the message - uint16_t message_len = sn_coap_builder_calc_needed_packet_data_size(coap_res_ptr); - printf("Calculated message length: %d bytes\n", message_len); - - uint8_t* message_ptr = (uint8_t*)malloc(message_len); - sn_coap_builder(message_ptr, coap_res_ptr); - - // Uncomment to see the raw buffer that will be sent... - // printf("Message is: "); - // for (size_t ix = 0; ix < message_len; ix++) { - // printf("%02x ", message_ptr[ix]); - // } - // printf("\n"); - - int scount = socket.sendto("coap.me", 5683, message_ptr, message_len); - printf("Sent %d bytes to coap://coap.me:5683\n", scount); - - free(coap_res_ptr); - free(message_ptr); - - Thread::wait(osWaitForever); -} + 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()); + + while(1) { + http_demo(network); + wait(3); + } + + network->disconnect(); + pc.printf("\r\nDone\r\n"); +} \ No newline at end of file