Simpe IoT BoardにGrove温度センサを繋げてIFTTTにプッシュするプログラムです。

Dependencies:   mbed

Committer:
jksoft
Date:
Sat Nov 14 02:53:45 2015 +0000
Revision:
1:8a97f4bd9773
Parent:
0:26b07836cf44
IFTTT????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:26b07836cf44 1 /* Copyright (C) 2012 mbed.org, MIT License
jksoft 0:26b07836cf44 2 *
jksoft 0:26b07836cf44 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jksoft 0:26b07836cf44 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jksoft 0:26b07836cf44 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jksoft 0:26b07836cf44 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jksoft 0:26b07836cf44 7 * furnished to do so, subject to the following conditions:
jksoft 0:26b07836cf44 8 *
jksoft 0:26b07836cf44 9 * The above copyright notice and this permission notice shall be included in all copies or
jksoft 0:26b07836cf44 10 * substantial portions of the Software.
jksoft 0:26b07836cf44 11 *
jksoft 0:26b07836cf44 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jksoft 0:26b07836cf44 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jksoft 0:26b07836cf44 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jksoft 0:26b07836cf44 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jksoft 0:26b07836cf44 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jksoft 0:26b07836cf44 17 */
jksoft 0:26b07836cf44 18
jksoft 0:26b07836cf44 19 #include "UDPSocket.h"
jksoft 0:26b07836cf44 20
jksoft 0:26b07836cf44 21 #include <string>
jksoft 0:26b07836cf44 22 #include <algorithm>
jksoft 0:26b07836cf44 23
jksoft 0:26b07836cf44 24 UDPSocket::UDPSocket()
jksoft 0:26b07836cf44 25 {
jksoft 0:26b07836cf44 26 endpoint_configured = false;
jksoft 0:26b07836cf44 27 endpoint_read = false;
jksoft 0:26b07836cf44 28 Endpoint currentEndpoint;
jksoft 0:26b07836cf44 29 }
jksoft 0:26b07836cf44 30
jksoft 0:26b07836cf44 31 int UDPSocket::init(void)
jksoft 0:26b07836cf44 32 {
jksoft 0:26b07836cf44 33 return 0;
jksoft 0:26b07836cf44 34 }
jksoft 0:26b07836cf44 35
jksoft 0:26b07836cf44 36 // Server initialization
jksoft 0:26b07836cf44 37 int UDPSocket::bind(int port)
jksoft 0:26b07836cf44 38 {
jksoft 0:26b07836cf44 39 return 0;
jksoft 0:26b07836cf44 40 }
jksoft 0:26b07836cf44 41
jksoft 0:26b07836cf44 42 // -1 if unsuccessful, else number of bytes written
jksoft 0:26b07836cf44 43 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
jksoft 0:26b07836cf44 44 {
jksoft 0:26b07836cf44 45 Timer tmr;
jksoft 0:26b07836cf44 46 int idx = 0;
jksoft 0:26b07836cf44 47
jksoft 0:26b07836cf44 48
jksoft 0:26b07836cf44 49 confEndpoint(remote);
jksoft 0:26b07836cf44 50
jksoft 0:26b07836cf44 51 // initialize transparent mode if not already done
jksoft 0:26b07836cf44 52 if(!endpoint_configured) {
jksoft 0:26b07836cf44 53 // initialize UDP (default id of -1 means transparent mode)
jksoft 0:26b07836cf44 54 //!wifi->start(ESP_UDP_TYPE, remote._ipAddress, remote._port, remote._id
jksoft 0:26b07836cf44 55 if(!wifi->startUDP(remote._ipAddress, remote._port, 0,length)) {
jksoft 0:26b07836cf44 56 return(-1);
jksoft 0:26b07836cf44 57 }
jksoft 0:26b07836cf44 58 endpoint_configured = true;
jksoft 0:26b07836cf44 59 }
jksoft 0:26b07836cf44 60
jksoft 0:26b07836cf44 61 tmr.start();
jksoft 0:26b07836cf44 62
jksoft 0:26b07836cf44 63 while ((tmr.read_ms() < _timeout) || _blocking) {
jksoft 0:26b07836cf44 64
jksoft 0:26b07836cf44 65 idx += wifi->send(packet, length);
jksoft 0:26b07836cf44 66
jksoft 0:26b07836cf44 67 if (idx == length)
jksoft 0:26b07836cf44 68 return idx;
jksoft 0:26b07836cf44 69 }
jksoft 0:26b07836cf44 70 return (idx == 0) ? -1 : idx;
jksoft 0:26b07836cf44 71 }
jksoft 0:26b07836cf44 72
jksoft 0:26b07836cf44 73 // -1 if unsuccessful, else number of bytes received
jksoft 0:26b07836cf44 74 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
jksoft 0:26b07836cf44 75 {
jksoft 0:26b07836cf44 76 Timer tmr;
jksoft 0:26b07836cf44 77 int idx = 0;
jksoft 0:26b07836cf44 78 int nb_available = 0;
jksoft 0:26b07836cf44 79 int time = -1;
jksoft 0:26b07836cf44 80
jksoft 0:26b07836cf44 81 //make this the non-blocking case and return if <= 0
jksoft 0:26b07836cf44 82 // remember to change the config to blocking
jksoft 0:26b07836cf44 83 // if ( ! _blocking) {
jksoft 0:26b07836cf44 84 // if ( wifi.readable <= 0 ) {
jksoft 0:26b07836cf44 85 // return (wifi.readable);
jksoft 0:26b07836cf44 86 // }
jksoft 0:26b07836cf44 87 // }
jksoft 0:26b07836cf44 88 //---
jksoft 0:26b07836cf44 89 tmr.start();
jksoft 0:26b07836cf44 90 if (_blocking) {
jksoft 0:26b07836cf44 91 while (1) {
jksoft 0:26b07836cf44 92 nb_available = wifi->readable();
jksoft 0:26b07836cf44 93 if (nb_available != 0) {
jksoft 0:26b07836cf44 94 break;
jksoft 0:26b07836cf44 95 }
jksoft 0:26b07836cf44 96 }
jksoft 0:26b07836cf44 97 }
jksoft 0:26b07836cf44 98 //---
jksoft 0:26b07836cf44 99 // blocking case
jksoft 0:26b07836cf44 100 else {
jksoft 0:26b07836cf44 101 tmr.reset();
jksoft 0:26b07836cf44 102
jksoft 0:26b07836cf44 103 while (time < _timeout) {
jksoft 0:26b07836cf44 104 nb_available = wifi->readable();
jksoft 0:26b07836cf44 105 if (nb_available < 0) return nb_available;
jksoft 0:26b07836cf44 106 if (nb_available > 0) break ;
jksoft 0:26b07836cf44 107 time = tmr.read_ms();
jksoft 0:26b07836cf44 108 }
jksoft 0:26b07836cf44 109
jksoft 0:26b07836cf44 110 if (nb_available == 0) return nb_available;
jksoft 0:26b07836cf44 111 }
jksoft 0:26b07836cf44 112
jksoft 0:26b07836cf44 113 // change this to < 20 mS timeout per byte to detect end of packet gap
jksoft 0:26b07836cf44 114 // this may not work due to buffering at the UART interface
jksoft 0:26b07836cf44 115 tmr.reset();
jksoft 0:26b07836cf44 116 // while ( tmr.read_ms() < 20 ) {
jksoft 0:26b07836cf44 117 // if ( wifi.readable() && (idx < length) ) {
jksoft 0:26b07836cf44 118 // buffer[idx++] = wifi->getc();
jksoft 0:26b07836cf44 119 // tmr.reset();
jksoft 0:26b07836cf44 120 // }
jksoft 0:26b07836cf44 121 // if ( idx == length ) {
jksoft 0:26b07836cf44 122 // break;
jksoft 0:26b07836cf44 123 // }
jksoft 0:26b07836cf44 124 // }
jksoft 0:26b07836cf44 125 //---
jksoft 0:26b07836cf44 126 while (time < _timeout) {
jksoft 0:26b07836cf44 127
jksoft 0:26b07836cf44 128 nb_available = wifi->readable();
jksoft 0:26b07836cf44 129 //for (int i = 0; i < min(nb_available, length); i++) {
jksoft 0:26b07836cf44 130 for (int i = 0; i < min(nb_available, (length-idx)); i++) {
jksoft 0:26b07836cf44 131 buffer[idx] = wifi->getc();
jksoft 0:26b07836cf44 132 idx++;
jksoft 0:26b07836cf44 133 }
jksoft 0:26b07836cf44 134 if (idx == length) {
jksoft 0:26b07836cf44 135 break;
jksoft 0:26b07836cf44 136 }
jksoft 0:26b07836cf44 137 time = tmr.read_ms();
jksoft 0:26b07836cf44 138 }
jksoft 0:26b07836cf44 139 //---
jksoft 0:26b07836cf44 140 readEndpoint(remote);
jksoft 0:26b07836cf44 141 return (idx == 0) ? -1 : idx;
jksoft 0:26b07836cf44 142 }
jksoft 0:26b07836cf44 143
jksoft 0:26b07836cf44 144 bool UDPSocket::confEndpoint(Endpoint & ep)
jksoft 0:26b07836cf44 145 {
jksoft 0:26b07836cf44 146 currentEndpoint = ep;
jksoft 0:26b07836cf44 147 return true;
jksoft 0:26b07836cf44 148 }
jksoft 0:26b07836cf44 149
jksoft 0:26b07836cf44 150 bool UDPSocket::readEndpoint(Endpoint & ep)
jksoft 0:26b07836cf44 151 {
jksoft 0:26b07836cf44 152 ep = currentEndpoint;
jksoft 0:26b07836cf44 153 return true;
jksoft 0:26b07836cf44 154 }