Simple code to upload temperature readings to SensorUp SensorThings Playground (http://pg.sensorup.com)from the LM75B temperature sensor in a mbed application board.It works with mbed LPC1768.

Dependencies:   C12832 EthernetNetIf LM75B mbed

Committer:
robinlk
Date:
Thu Dec 10 01:53:20 2015 +0000
Revision:
0:597aea501d68
Simple code to upload temperature readings to SensorUp SensorThings Playground (http://pg.sensorup.com)from the LM75B temperature sensor in an mbed application board.It works with mbed LPC1768.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robinlk 0:597aea501d68 1 /*
robinlk 0:597aea501d68 2 * mbed Tiny HTTP Client
robinlk 0:597aea501d68 3 * Copyright (c) 2011 Hiroshi Suga
robinlk 0:597aea501d68 4 * Released under the MIT License: http://mbed.org/license/mit
robinlk 0:597aea501d68 5 */
robinlk 0:597aea501d68 6
robinlk 0:597aea501d68 7 /** @file
robinlk 0:597aea501d68 8 * @brief Tiny HTTP Client
robinlk 0:597aea501d68 9 */
robinlk 0:597aea501d68 10
robinlk 0:597aea501d68 11 #include "mbed.h"
robinlk 0:597aea501d68 12 #include "EthernetNetIf.h"
robinlk 0:597aea501d68 13 #include "TCPSocket.h"
robinlk 0:597aea501d68 14 #include "DNSRequest.h"
robinlk 0:597aea501d68 15 #include "TinyHTTP.h"
robinlk 0:597aea501d68 16 #include <ctype.h>
robinlk 0:597aea501d68 17
robinlk 0:597aea501d68 18
robinlk 0:597aea501d68 19 TCPSocket *http;
robinlk 0:597aea501d68 20 volatile int tcp_ready, tcp_readable, tcp_writable;
robinlk 0:597aea501d68 21 volatile int dns_status;
robinlk 0:597aea501d68 22
robinlk 0:597aea501d68 23 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
robinlk 0:597aea501d68 24 int base64enc(const char *input, unsigned int length, char *output, int len) {
robinlk 0:597aea501d68 25 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
robinlk 0:597aea501d68 26 unsigned int c, c1, c2, c3;
robinlk 0:597aea501d68 27
robinlk 0:597aea501d68 28 if (len < ((((length-1)/3)+1)<<2)) return -1;
robinlk 0:597aea501d68 29 for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
robinlk 0:597aea501d68 30 c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
robinlk 0:597aea501d68 31 c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
robinlk 0:597aea501d68 32 c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
robinlk 0:597aea501d68 33
robinlk 0:597aea501d68 34 c = ((c1 & 0xFC) >> 2);
robinlk 0:597aea501d68 35 output[j+0] = base64[c];
robinlk 0:597aea501d68 36 c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
robinlk 0:597aea501d68 37 output[j+1] = base64[c];
robinlk 0:597aea501d68 38 c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
robinlk 0:597aea501d68 39 output[j+2] = (length>i+1)?base64[c]:'=';
robinlk 0:597aea501d68 40 c = (c3 & 0x3F);
robinlk 0:597aea501d68 41 output[j+3] = (length>i+2)?base64[c]:'=';
robinlk 0:597aea501d68 42 }
robinlk 0:597aea501d68 43 output[(((length-1)/3)+1)<<2] = '\0';
robinlk 0:597aea501d68 44 return 0;
robinlk 0:597aea501d68 45 }
robinlk 0:597aea501d68 46
robinlk 0:597aea501d68 47 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
robinlk 0:597aea501d68 48 int urlencode(char *str, char *buf, int len) {
robinlk 0:597aea501d68 49 static const char to_hex[] = "0123456789ABCDEF";
robinlk 0:597aea501d68 50 // char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
robinlk 0:597aea501d68 51 char *pstr = str, *pbuf = buf;
robinlk 0:597aea501d68 52
robinlk 0:597aea501d68 53 if (len < (strlen(str) * 3 + 1)) return -1;
robinlk 0:597aea501d68 54 while (*pstr) {
robinlk 0:597aea501d68 55 if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
robinlk 0:597aea501d68 56 *pbuf++ = *pstr;
robinlk 0:597aea501d68 57 } else if (*pstr == ' ') {
robinlk 0:597aea501d68 58 *pbuf++ = '+';
robinlk 0:597aea501d68 59 } else {
robinlk 0:597aea501d68 60 *pbuf++ = '%';
robinlk 0:597aea501d68 61 *pbuf++ = to_hex[(*pstr >> 4) & 0x0f];
robinlk 0:597aea501d68 62 *pbuf++ = to_hex[*pstr & 0x0f];
robinlk 0:597aea501d68 63 }
robinlk 0:597aea501d68 64 pstr++;
robinlk 0:597aea501d68 65 }
robinlk 0:597aea501d68 66 *pbuf = '\0';
robinlk 0:597aea501d68 67 return 0;
robinlk 0:597aea501d68 68 }
robinlk 0:597aea501d68 69
robinlk 0:597aea501d68 70
robinlk 0:597aea501d68 71 void isr_http (TCPSocketEvent e) {
robinlk 0:597aea501d68 72
robinlk 0:597aea501d68 73 #ifdef DEBUG
robinlk 0:597aea501d68 74 printf("tcp(%d)\r\n", e);
robinlk 0:597aea501d68 75 #endif
robinlk 0:597aea501d68 76 switch(e) {
robinlk 0:597aea501d68 77 case TCPSOCKET_CONNECTED:
robinlk 0:597aea501d68 78 tcp_ready = 1;
robinlk 0:597aea501d68 79 break;
robinlk 0:597aea501d68 80
robinlk 0:597aea501d68 81 case TCPSOCKET_READABLE: //Incoming data
robinlk 0:597aea501d68 82 tcp_readable = 1;
robinlk 0:597aea501d68 83 break;
robinlk 0:597aea501d68 84
robinlk 0:597aea501d68 85 case TCPSOCKET_WRITEABLE: //We can send data
robinlk 0:597aea501d68 86 tcp_writable = 1;
robinlk 0:597aea501d68 87 break;
robinlk 0:597aea501d68 88
robinlk 0:597aea501d68 89 case TCPSOCKET_CONTIMEOUT:
robinlk 0:597aea501d68 90 case TCPSOCKET_CONRST:
robinlk 0:597aea501d68 91 case TCPSOCKET_CONABRT:
robinlk 0:597aea501d68 92 case TCPSOCKET_ERROR:
robinlk 0:597aea501d68 93 case TCPSOCKET_DISCONNECTED:
robinlk 0:597aea501d68 94 tcp_ready = 0;
robinlk 0:597aea501d68 95 break;
robinlk 0:597aea501d68 96 }
robinlk 0:597aea501d68 97 }
robinlk 0:597aea501d68 98
robinlk 0:597aea501d68 99 void createauth (char *user, char *pwd, char *buf, int len) {
robinlk 0:597aea501d68 100 char tmp[80];
robinlk 0:597aea501d68 101
robinlk 0:597aea501d68 102 strncpy(buf, "Authorization: Basic ", len);
robinlk 0:597aea501d68 103 snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
robinlk 0:597aea501d68 104 base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf));
robinlk 0:597aea501d68 105 strncat(buf, "\r\n", len - strlen(buf));
robinlk 0:597aea501d68 106 }
robinlk 0:597aea501d68 107
robinlk 0:597aea501d68 108 void isr_dns (DNSReply r) {
robinlk 0:597aea501d68 109
robinlk 0:597aea501d68 110 #ifdef DEBUG
robinlk 0:597aea501d68 111 printf("dns(%d)\r\n", r);
robinlk 0:597aea501d68 112 #endif
robinlk 0:597aea501d68 113 if (DNS_FOUND) {
robinlk 0:597aea501d68 114 dns_status = 1;
robinlk 0:597aea501d68 115 } else {
robinlk 0:597aea501d68 116 dns_status = -1;
robinlk 0:597aea501d68 117 }
robinlk 0:597aea501d68 118 }
robinlk 0:597aea501d68 119
robinlk 0:597aea501d68 120 int httpRequest (int method, Host *host, char *uri, char *head, char *body) {
robinlk 0:597aea501d68 121 TCPSocketErr err;
robinlk 0:597aea501d68 122 Timer timeout;
robinlk 0:597aea501d68 123 char buf[1500];
robinlk 0:597aea501d68 124 int i, ret = -1;
robinlk 0:597aea501d68 125
robinlk 0:597aea501d68 126 http = new TCPSocket;
robinlk 0:597aea501d68 127 tcp_ready = 0;
robinlk 0:597aea501d68 128 tcp_readable = 0;
robinlk 0:597aea501d68 129 tcp_writable = 0;
robinlk 0:597aea501d68 130
robinlk 0:597aea501d68 131 http->setOnEvent(isr_http);
robinlk 0:597aea501d68 132
robinlk 0:597aea501d68 133 // connect
robinlk 0:597aea501d68 134 if (host->getIp().isNull()) {
robinlk 0:597aea501d68 135 // resolv
robinlk 0:597aea501d68 136 DNSRequest dns;
robinlk 0:597aea501d68 137 dns_status = 0;
robinlk 0:597aea501d68 138 dns.setOnReply(isr_dns);
robinlk 0:597aea501d68 139 if (dns.resolve(host) != DNS_OK) goto exit;
robinlk 0:597aea501d68 140 timeout.reset();
robinlk 0:597aea501d68 141 timeout.start();
robinlk 0:597aea501d68 142 while (timeout.read_ms() < HTTP_TIMEOUT) {
robinlk 0:597aea501d68 143 if (dns_status) break;
robinlk 0:597aea501d68 144 Net::poll();
robinlk 0:597aea501d68 145 }
robinlk 0:597aea501d68 146 timeout.stop();
robinlk 0:597aea501d68 147 if (dns_status <= 0) goto exit;
robinlk 0:597aea501d68 148 #ifdef DEBUG
robinlk 0:597aea501d68 149 printf("%s [%d.%d.%d.%d]\r\n", host->getName(), (unsigned char)host->getIp()[0], (unsigned char)host->getIp()[1], (unsigned char)host->getIp()[2], (unsigned char)host->getIp()[3]);
robinlk 0:597aea501d68 150 #endif
robinlk 0:597aea501d68 151 }
robinlk 0:597aea501d68 152 if (! host->getPort()) {
robinlk 0:597aea501d68 153 host->setPort(HTTP_PORT);
robinlk 0:597aea501d68 154 }
robinlk 0:597aea501d68 155 err = http->connect(*host);
robinlk 0:597aea501d68 156 if (err != TCPSOCKET_OK) goto exit;
robinlk 0:597aea501d68 157
robinlk 0:597aea501d68 158 // wait connect
robinlk 0:597aea501d68 159 timeout.reset();
robinlk 0:597aea501d68 160 timeout.start();
robinlk 0:597aea501d68 161 while (timeout.read_ms() < HTTP_TIMEOUT) {
robinlk 0:597aea501d68 162 if (tcp_ready) break;
robinlk 0:597aea501d68 163 Net::poll();
robinlk 0:597aea501d68 164 }
robinlk 0:597aea501d68 165 timeout.stop();
robinlk 0:597aea501d68 166 if (! tcp_ready) goto exit;
robinlk 0:597aea501d68 167
robinlk 0:597aea501d68 168 // send request
robinlk 0:597aea501d68 169 if (method == METHOD_POST) {
robinlk 0:597aea501d68 170 http->send("POST ", 5);
robinlk 0:597aea501d68 171 } else {
robinlk 0:597aea501d68 172 http->send("GET ", 4);
robinlk 0:597aea501d68 173 }
robinlk 0:597aea501d68 174 http->send(uri, strlen(uri));
robinlk 0:597aea501d68 175 http->send(" HTTP/1.1\r\nHost: ", 17);
robinlk 0:597aea501d68 176 http->send(host->getName(), strlen(host->getName()));
robinlk 0:597aea501d68 177 http->send("\r\n", 2);
robinlk 0:597aea501d68 178 http->send("Connection: close\r\n", 19);
robinlk 0:597aea501d68 179 if (head) {
robinlk 0:597aea501d68 180 http->send(head, strlen(head));
robinlk 0:597aea501d68 181 }
robinlk 0:597aea501d68 182 if (method == METHOD_POST) {
robinlk 0:597aea501d68 183 sprintf(buf, "Content-Length: %d\r\n", strlen(body));
robinlk 0:597aea501d68 184 http->send(buf, strlen(buf));
robinlk 0:597aea501d68 185 }
robinlk 0:597aea501d68 186 http->send("\r\n", 2);
robinlk 0:597aea501d68 187
robinlk 0:597aea501d68 188 // post method
robinlk 0:597aea501d68 189 if (method == METHOD_POST && body) {
robinlk 0:597aea501d68 190 http->send(body, strlen(body));
robinlk 0:597aea501d68 191 }
robinlk 0:597aea501d68 192
robinlk 0:597aea501d68 193 // wait responce
robinlk 0:597aea501d68 194 timeout.reset();
robinlk 0:597aea501d68 195 timeout.start();
robinlk 0:597aea501d68 196 while (timeout.read_ms() < HTTP_TIMEOUT) {
robinlk 0:597aea501d68 197 if (tcp_readable) break;
robinlk 0:597aea501d68 198 Net::poll();
robinlk 0:597aea501d68 199 }
robinlk 0:597aea501d68 200 timeout.stop();
robinlk 0:597aea501d68 201 if (! tcp_readable) goto exit;
robinlk 0:597aea501d68 202
robinlk 0:597aea501d68 203 // recv responce
robinlk 0:597aea501d68 204 i = http->recv(buf, sizeof(buf) - 1);
robinlk 0:597aea501d68 205 buf[i] = 0;
robinlk 0:597aea501d68 206 if (i < sizeof(buf) - 1) tcp_readable = 0;
robinlk 0:597aea501d68 207 if (strncmp(buf, "HTTP/", 5) == 0) {
robinlk 0:597aea501d68 208 ret = atoi(&buf[9]);
robinlk 0:597aea501d68 209 }
robinlk 0:597aea501d68 210 #ifdef DEBUG
robinlk 0:597aea501d68 211 printf(buf);
robinlk 0:597aea501d68 212 #endif
robinlk 0:597aea501d68 213
robinlk 0:597aea501d68 214 // recv dummy
robinlk 0:597aea501d68 215 timeout.reset();
robinlk 0:597aea501d68 216 timeout.start();
robinlk 0:597aea501d68 217 while (timeout.read_ms() < HTTP_TIMEOUT) {
robinlk 0:597aea501d68 218 if (tcp_readable) {
robinlk 0:597aea501d68 219 i = http->recv(buf, sizeof(buf) - 1);
robinlk 0:597aea501d68 220 buf[i] = 0;
robinlk 0:597aea501d68 221 if (i < sizeof(buf) - 1) tcp_readable = 0;
robinlk 0:597aea501d68 222 #ifdef DEBUG
robinlk 0:597aea501d68 223 printf(buf);
robinlk 0:597aea501d68 224 #endif
robinlk 0:597aea501d68 225 timeout.reset();
robinlk 0:597aea501d68 226 } else
robinlk 0:597aea501d68 227 if (! tcp_ready) {
robinlk 0:597aea501d68 228 break;
robinlk 0:597aea501d68 229 }
robinlk 0:597aea501d68 230 Net::poll();
robinlk 0:597aea501d68 231 }
robinlk 0:597aea501d68 232 timeout.stop();
robinlk 0:597aea501d68 233
robinlk 0:597aea501d68 234 exit:
robinlk 0:597aea501d68 235 http->resetOnEvent();
robinlk 0:597aea501d68 236 http->close();
robinlk 0:597aea501d68 237 delete http;
robinlk 0:597aea501d68 238
robinlk 0:597aea501d68 239 return ret;
robinlk 0:597aea501d68 240 }