-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
188 lines (167 loc) · 6.54 KB
/
main.cpp
File metadata and controls
188 lines (167 loc) · 6.54 KB
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
#include "mbed.h"
// For the Wi-Fi
//#include "ESP8266Interface.h"
// Easy connet library for the HTTP server
#include "easy-connect.h"
// For the Web Server
//#include "http_server.h"
//#include "http_response_builder.h"
// TCP libraries
//#include "EthernetInterface.h"
#include "TCPServer.h"
#include "TCPSocket.h"
Serial pc(USBTX, USBRX);
DigitalOut led(LED1);
//ESP8266Interface net(D8, D2, true); // WiFi driver
InterruptIn mybutton(USER_BUTTON);
TCPSocket socket; // Socket for sending requests
// LED
DigitalOut led1(LED1);
// Accelerometer
AnalogIn x(A2);
AnalogIn y(A1);
AnalogIn z(A0);
bool openClosed = false; // Window
bool wifiAllowed = false; // Allow communication
// Interrupt function
void pressed() {
pc.printf("\n\r=========Button Interrupt Came=============");
// Allow wifi send emails
wifiAllowed = !wifiAllowed;
}
// test Function
void test()
{
// pc.printf("\r\nAccelerometer X : %f\n\r", x.read()*3.3);
// pc.printf("\r\nAccelerometer Y : %f\n\r", y.read()*3.3);
// pc.printf("\r\nAccelerometer Z : %f\n\r", z.read()*3.3);
}
void send_request(NetworkInterface* network, bool openedOrClosed) {
// int remaining;
// int rcount;
// char *p;
char *buffer = new char[256];
// Open a socket on the network interface, and create a TCP connection to ifconfig.io
TCPSocket socket;
// Send a simple http request
char sbuffer_window_closed[] = "POST /scripts/form-to-email.php HTTP/1.1\r\nHost: form.tw1.ru\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 20\r\n\r\nfield1=window-closed\r\n";
char sbuffer_window_opened[] = "POST /scripts/form-to-email.php HTTP/1.1\r\nHost: form.tw1.ru\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 20\r\n\r\nfield1=window-opened\r\n";
nsapi_size_t size;
if (openedOrClosed) {
size = strlen(sbuffer_window_closed);
} else {
size = strlen(sbuffer_window_opened);
}
nsapi_size_or_error_t result;
result = socket.open(network);
if (result != 0) {
pc.printf("Error! socket.open() returned: %d\n", result);
}
result = socket.connect("form.tw1.ru", 80);
if (result != 0) {
pc.printf("Error! socket.connect() returned: %d\n", result);
}
// Loop until whole request sent
if (openedOrClosed) {
while(size) {
result = socket.send(sbuffer_window_closed+result, size);
if (result < 0) {
pc.printf("Error! socket.send() returned: %d\n", result);
}
size -= result;
pc.printf("sent %d [%.*s]\n", result, strstr(sbuffer_window_closed, "\r\n")-sbuffer_window_closed, sbuffer_window_closed);
}
} else {
result = socket.send(sbuffer_window_opened+result, size);
if (result < 0) {
pc.printf("Error! socket.send() returned: %d\n", result);
}
size -= result;
pc.printf("sent %d [%.*s]\n", result, strstr(sbuffer_window_opened, "\r\n")-sbuffer_window_opened, sbuffer_window_opened);
}
// Receieve an HTTP response and print out the response line
// remaining = 256;
// rcount = 0;
// p = buffer;
// while (remaining > 0 && 0 < (result = socket.recv(p, remaining))) {
// p += result;
// rcount += result;
// remaining -= result;
// }
// if (result < 0) {
// pc.printf("Error! socket.recv() returned: %d\n", result);
// }
// // the HTTP response code
// pc.printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
delete[] buffer;
// Close the socket to return its memory and bring down the network interface
socket.close();
}
// main() runs in its own thread in the OS
int main() {
led1 = true;
// Instanciate interrupts
mybutton.fall(&pressed);
pc.printf("\r\nNUCLEO-mbed Application\r\n");
// ================================================
// Establish a connection
// ================================================
pc.printf("\r\nconnecting to AP\r\n");
NetworkInterface* network = easy_connect(true);
// ================================================
// Display network parameters
// ================================================
const char *ip = network->get_ip_address();
const char *mac = network->get_mac_address();
const char *netmask = network->get_netmask();
const char *gateway = network->get_gateway();
pc.printf("\r\nIP Address is: %s\r\n", (ip) ? ip : "None");
pc.printf("\r\nMAC Address is: %s\r\n", (mac) ? mac : "None");
pc.printf("\r\nNetmask is: %s\r\n", (netmask) ? netmask : "None");
pc.printf("\r\nGateway is: %s\r\n", (gateway) ? gateway : "None");
// ================================================
// Infinite loop
// ================================================
while(true) {
// Check for the interrupt on the button
if(wifiAllowed) {
// Check the sensor data
if (z.read()/y.read() > 0.86) {
if (!openClosed) {
// Call the function which will send the request
send_request(network, false);
pc.printf("\r\nWindow is opened\n\r");
pc.printf("\r\nAccelerometer X : %f\n\r", x.read());
pc.printf("\r\nAccelerometer Y : %f\n\r", y.read());
pc.printf("\r\nAccelerometer Z : %f\n\r", z.read());
pc.printf("\r\nRatio (Z/Y) : %f\n\r", z.read()/y.read());
openClosed = true;
}
} else {
if (openClosed) {
// Call the function which will send the request
send_request(network, true);
pc.printf("\r\nWindow is closed\n\r");
pc.printf("\r\nAccelerometer X : %f\n\r", x.read());
pc.printf("\r\nAccelerometer Y : %f\n\r", y.read());
pc.printf("\r\nAccelerometer Z : %f\n\r", z.read());
pc.printf("\r\nRatio (Z/Y) : %f\n\r", z.read()/y.read());
openClosed = false;
}
}
}
// Blink with different frequency when wifiAllowed
led1 = !led1;
if (wifiAllowed) {
wait(0.2);
} else {
wait(1.0);
}
}
// ================================================
// The end
// ================================================
network->disconnect();
pc.printf("\r\nReached the end of the program");
wait(osWaitForever);
}