-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.c
145 lines (117 loc) · 3.15 KB
/
http.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "config.h"
#include "http.h"
#include "uart.h"
//#include "bme280.h"
#include "i2c.h"
#include <stdlib.h>
#include <string.h>
#define _FTOA_TOO_LARGE -2
#define _FTOA_TOO_SMALL -1
char *header="wClient:send(\"<HTML><body style='background-color:black;color:yellow;'>";
char *footer="</body></html>\")";
char *endconn="wClient:close();\r\ncollectgarbage();\r\n";
extern long bme280_temperature;
extern long unsigned int bme280_pressure;
extern long unsigned int bme280_humidity;
extern unsigned int lightlevel;
typedef union {
long L;
float F;
} LF_t;
char *ftoa(float f) {
long mantissa, int_part, frac_part;
short exp2;
LF_t x;
char *p;
static char outbuf[15];
//*status = 0;
if (f == 0.0) {
outbuf[0] = '0';
outbuf[1] = '.';
outbuf[2] = '0';
outbuf[3] = 0;
return outbuf;
}
x.F = f;
exp2 = (unsigned char)(x.L >> 23) - 127;
mantissa = (x.L & 0xFFFFFF) | 0x800000;
frac_part = 0;
int_part = 0;
if (exp2 >= 31) {
//*status = _FTOA_TOO_LARGE;
return 0;
}
else if (exp2 < -23) {
//*status = _FTOA_TOO_SMALL;
return 0;
}
else if (exp2 >= 23)
int_part = mantissa << (exp2 - 23);
else if (exp2 >= 0) {
int_part = mantissa >> (23 - exp2);
frac_part = (mantissa << (exp2 + 1)) & 0xFFFFFF;
}
else /* if (exp2 < 0) */
frac_part = (mantissa & 0xFFFFFF) >> -(exp2 + 1);
p = outbuf;
if (x.L < 0)
*p++ = '-';
if (int_part == 0)
*p++ = '0';
else {
ltoa(p, int_part, 10);
while (*p) p++;
}
*p++ = '.';
if (frac_part == 0)
*p++ = '0';
else {
char m, max;
max = sizeof (outbuf) - (p - outbuf) - 1;
if (max > 7) max = 7;
/* print BCD */
for (m = 0; m < max; m++) {
/* frac_part *= 10; */
frac_part = (frac_part << 3) + (frac_part << 1);
*p++ = (frac_part >> 24) + '0';
frac_part &= 0xFFFFFF;
}
/* delete ending zeroes */
for (--p; p[0] == '0' && p[-1] != '.'; --p);
++p;
}
*p = 0;
return outbuf;
}
void handleHTTP(void) {
unsigned char i;
char *body;
float t,h,p,l;
//char *temperature; //*humidity, *pressure;
t=((float)bme280_temperature)/100;
h=((float)bme280_humidity)/1024;
p=((float)bme280_pressure)/256;
l=((float)lightlevel);
//sprintf(&temperature,"%f",t);
//temperature=ftoa(t);
//temperature = ftoa(t);
//body="Temperature: ";
//strcat(body, temperature);
//body=temperature;
cmdUART1(header);
cmdUART1("T: ");
cmdUART1(ftoa(t));
cmdUART1(" °C.<br/>");
cmdUART1("H: ");
cmdUART1(ftoa(h));
cmdUART1(" %.<br/>");
cmdUART1("P: ");
cmdUART1(ftoa(p));
cmdUART1(" Pa.<br/>");
cmdUART1("L: ");
cmdUART1(ftoa(l));
cmdUART1(" [TBD].<br/>");
//cmdUART1(body);
cmdUART1(footer);
cmdUART1(endconn); //"
}