-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
262 lines (221 loc) · 6.99 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*-----------------------------------------------------------------------------
/
/
/----------------------------------------------------------------------------*/
#include "main.h"
/*---------------------------------------------------------------------------*/
void init_adc();
void init_uart();
void client_mode();
void server_mode();
void dbg(uint8_t ch);
void init_hardware();
void fail_if_not_ok();
void initClock_32Mhz();
uint16_t read_adc(uint8_t ch);
void sendch_module(uint8_t ch);
/*---------------------------------------------------------------------------*/
int main()
{
init_hardware();
set_rgb(255,0,0);
/* Wait for the powerup */
wait_for_message("com]\r\n",10000);
/* Send data to PC */
xfprintf(dbg,"Hello World!\r\n");
/* Enter AP mode for configuration */
if(digitalRead(D,3) == LOW)
{
server_mode();
}
else
{
client_mode();
}
return 0;
}
/*---------------------------------------------------------------------------*/
void client_mode()
{
uint8_t i = 1;
int16_t integer;
int16_t decimal;
int32_t temp = 0;
uint16_t tcp_len;
/* Read the settings */
eeprom_read_block(SSID_name,SSID_name_addr,32);
eeprom_read_block(SSID_pass,SSID_pass_addr,32);
eeprom_read_block(PublicKey,PublicKey_addr,32);
eeprom_read_block(PrivateKey,PrivateKey_addr,32);
xprintf("AT+CWMODE=1\r\n");
_delay_ms(100);
xprintf("AT+RST\r\n");
fail_if_not_ok();
/* Wait for the powerup */
wait_for_message("com]\r\n",10000);
/* SSID details ... */
xprintf("AT+CWJAP=\"");
xprintf("%s\",\"",SSID_name);
xprintf("%s\"\r\n",SSID_pass);
/* Check the result of join */
fail_if_not_ok();
/* Wait until system get IP */
wait_until_gotIP();
/* ... */
xprintf("AT+CIPMUX=1\r\n");
fail_if_not_ok();
while(filterStable == 0)
{
/*-------------------------------------------------------------------*/
/* In place average */
/*-------------------------------------------------------------------*/
while(i++)
{
temp += read_adc(1);
}
temp >>= 8;
temp -= 500; /* temperature sensor have some offset for 0 celcius */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Moving average */
/*-------------------------------------------------------------------*/
totalSum = totalSum + temp - movingBuffer[movingIndex];
movingBuffer[movingIndex] = temp;
movingIndex++;
if(movingIndex == 8)
{
movingIndex = 0;
filterStable = 1;
}
/*-------------------------------------------------------------------*/
}
while(1)
{
set_rgb(255,255,255);
/*-------------------------------------------------------------------*/
/* In place average */
/*-------------------------------------------------------------------*/
while(i++)
{
temp += read_adc(1);
}
temp >>= 8;
temp -= 500; /* temperature sensor have some offset for 0 celcius */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Moving average */
/*-------------------------------------------------------------------*/
totalSum = totalSum + temp - movingBuffer[movingIndex];
movingBuffer[movingIndex] = temp;
movingIndex++;
if(movingIndex == 8)
{
movingIndex = 0;
}
temp = totalSum >> 3;
/*-------------------------------------------------------------------*/
integer = temp / 10;
decimal = temp - (10 * integer);
/* Open a socket to "data.sparkfun.com" address at port 80 */
xprintf("AT+CIPSTART=0,\"TCP\",\"data.sparkfun.com\",80\r\n");
fail_if_not_ok();
wait_for_message("Linked\r\n",10000);
xsprintf(smallBuffer,"/input/%s?private_key=%s&temp=%d.%d",PublicKey,PrivateKey,integer,decimal);
tcp_len = create_GetRequest("data.sparkfun.com", smallBuffer, txBuffer, 0);
send_TCPData(txBuffer,tcp_len);
/* Wait for some time for if we get some response */
_delay_ms(500);
/* close the socket */
xprintf("AT+CIPCLOSE=0\r\n");
fail_if_not_ok();
wait_for_message("Unlink\r\n",10000);
/* Wait some time for next data */
set_rgb(0,255,0);
_delay_ms(40000);
}
}
/*---------------------------------------------------------------------------*/
void server_mode()
{
uint16_t tcp_len;
xprintf("AT+CWMODE=2\r\n");
_delay_ms(100);
xprintf("AT+RST\r\n");
check_ok();
/* Wait for the powerup */
wait_for_message("com]\r\n",10000);
/* SSID details ... */
xprintf("AT+CWSAP=\"myssid\",\"123456789\",1,0\r\n");
/* Check the result of join */
fail_if_not_ok();
/* Wait until system get IP */
wait_until_gotIP();
/* ... */
xprintf("AT+CIPMUX=1\r\n");
fail_if_not_ok();
/* Enable server at port 80 */
xprintf("AT+CIPSERVER=1,80\r\n");
fail_if_not_ok();
set_rgb(0,0,255);
while(1)
{
waitTCPMessage_blocking();
/*-----------------------------------------------------------------
/ Respond to HTTP GET messages - Basic web server
/------------------------------------------------------------------
/ Example of a starting part of an HTTP GET message is:
/ GET /index.html HTTP/1.1
/----------------------------------------------------------------*/
if(strncmp("GET /",tcpData,5) == 0)
{
memset(txBuffer,0x00,sizeof(txBuffer));
switch(tcpData[5])
{
/* Default web page */
case ' ':
{
tcp_len = fill_tcp_data(txBuffer,0,HTTP_RESPONSE_HEADER);
tcp_len = fill_tcp_data(txBuffer,tcp_len,"OK!\r\n");
break;
}
/* SSID name */
case 's':
{
tcp_len = parseTCP_setAndResponse("SSID name", SSID_name_addr, SSID_name, sizeof(SSID_name), 0, txBuffer);
break;
}
/* SSID password */
case 'p':
{
tcp_len = parseTCP_setAndResponse("SSID pass", SSID_pass_addr, SSID_pass, sizeof(SSID_pass), 0, txBuffer);
break;
}
/* Set data.sparkfun.com Public Key */
case 'x':
{
tcp_len = parseTCP_setAndResponse("PublicKey", PublicKey_addr, PublicKey, sizeof(PublicKey), 0, txBuffer);
break;
}
/* Set data.sparkfun.com Private Key */
case 'y':
{
tcp_len = parseTCP_setAndResponse("PrivateKey", PrivateKey_addr, PrivateKey, sizeof(PrivateKey), 0, txBuffer);
break;
}
/* Unknown command */
default:
{
tcp_len = fill_tcp_data(txBuffer,0,HTTP_RESPONSE_HEADER);
tcp_len = fill_tcp_data(txBuffer,tcp_len,"Unknown command!\r\n");
break;
}
}
/* send the response */
send_TCPData(txBuffer,tcp_len);
/* close the socket */
xprintf("AT+CIPCLOSE=0\r\n");
fail_if_not_ok();
}
}
}
/*---------------------------------------------------------------------------*/