-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathANode.cpp
More file actions
182 lines (151 loc) · 4.44 KB
/
ANode.cpp
File metadata and controls
182 lines (151 loc) · 4.44 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
#include "Arduino.h"
#include "ANode.h"
#define DEBUG true
// empty constructor
Event::Event() { }
Event::Event(String timestamp, String source, String name, String data)
{
// constructor
_timestamp = timestamp;
_source = source;
_name = name;
_data = data;
}
String Event::getTimestamp()
{
return _timestamp;
}
String Event::getSource()
{
return _source;
}
String Event::getName()
{
return _name;
}
String Event::getData()
{
return _data;
}
// empty constructor
ANode::ANode() { }
ANode::ANode(HardwareSerial &espSerial, HardwareSerial &debugSerial, String sendChannel, String gatewayIp, String gatewayPort,
String ssid, String pass)
{
// constructor
// espSerial is the serial port the esp8266 is connected to.
_espSerial = &espSerial;
_debugSerial = &debugSerial;
_sendChannel = sendChannel;
_gatewayIp = gatewayIp;
_gatewayPort = gatewayPort;
_ssid = ssid;
_pass = pass;
startServer();
}
// private methods
void ANode::startServer() {
_debugSerial->println("STARTING...");
// rst
sendCommand("AT+RST", 2000, DEBUG);
// Connect to wireless network
sendCommand("AT+CWJAP=\"" + _ssid + "\",\"" + _pass + "\"", 2000, DEBUG);
delay(10000);
sendCommand("AT+CWMODE=1", 1000, DEBUG);
// Shows IP Address
sendCommand("AT+CIFSR", 1000, DEBUG);
// Configure to multiple connections
sendCommand("AT+CIPMUX=1", 1000, DEBUG);
// Start web server at port 80
sendCommand("AT+CIPSERVER=1,80", 1000, DEBUG);
// if successful, return true
return true;
}
void ANode::listenToEvent() {
if (_espSerial->available())
{
if (_espSerial->find((char*)"+IPD,"))
{
delay(300);
int connectionId = _espSerial->read() - 48;
int connection = _espSerial->read();
_debugSerial->println(connection);
// String webpage = "<head><meta http-equiv=""refresh"" content=""3"">";
// webpage += "</head><h1><u>ESP8266 - Web Server</u></h1><h2>Porta";
// webpage += "Digital 8: </h2>";
// webpage += "<h2>Porta Digital 9: ";
// webpage += "</h2>";
// String cipSend = "AT+CIPSEND=";
// cipSend += connectionId;
// cipSend += ",";
// cipSend += webpage.length();
// sendCommand(cipSend, 1000, DEBUG);
// sendCommand(webpage, 1000, DEBUG);
String closeCommand = "AT+CIPCLOSE=";
closeCommand += connectionId; // append connection id
sendCommand(closeCommand, 3000, DEBUG);
}
}
return false;
}
void ANode::sendCommand(String command, const int timeout, boolean debug)
{
// Send AT commands to the module
String response = "";
_espSerial->println(command);
long int time = millis();
while ((time + timeout) > millis())
{
while (_espSerial->available())
{
// The esp has data so display its output to the serial window
char c = _espSerial->read(); // read the next character.
response += c;
}
}
if (debug)
{
_debugSerial->print(response);
}
}
// public commands
bool ANode::sendEvent(Event &event, bool debug)
{
_debugSerial->println("before tcp connection"); //xxx
// start a TCP connection.
_espSerial->println("AT+CIPSTART=" + _sendChannel + ",\"TCP\",\"" + _gatewayIp + "\"," + _gatewayPort);
_debugSerial->println("after tcp connection"); //xxx
if(_espSerial->find((char*)"OK") && debug && _debugSerial) { _debugSerial->println("TCP connection ready"); }
delay(300);
// Send packet
// arduino event protocol (aep) format:
// 1970-01-01T00:00:00Z\r\n
// source\r\n
// event_name\r\n
// data_length(int32)\r\n
// data
String packet = event.getTimestamp() + "\r\n" +
event.getSource() + "\r\n" +
event.getName() + "\r\n" +
event.getData().length() + "\r\n" +
event.getData();
String sendCmd = "AT+CIPSEND=2,"; // determine the number of caracters to be sent.
_espSerial->print(sendCmd);
_espSerial->println(packet.length());
delay(500);
if(_espSerial->find((char*)">")) {
if (debug && _debugSerial) { _debugSerial->println("Sending event..."); }
_espSerial->print(packet);
if(_espSerial->find((char*)"SEND OK")) {
if (debug && _debugSerial) { _debugSerial->println("Event sent"); }
while (_espSerial->available()) {
String tmpResp = _espSerial->readString();
if (debug && _debugSerial) { _debugSerial->println(tmpResp); }
}
// close connection
_espSerial->println("AT+CIPCLOSE");
return true;
}
}
return false;
}