forked from hansvana/CMDWifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMDWifi.cpp
148 lines (127 loc) · 3.97 KB
/
CMDWifi.cpp
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
/*
CMDWifi.cpp - Wrapper for the WiFiNINA library
Based on the Repeating Wifi Web Client example
Created 23 April 2012, modified 31 May 2012
by Tom Igoe
Modified 13 Jan 2014
by Federico Vanzati
CMDWifi wrapper created 4 April 2017
by Hans van Arken
Modified 12 Febuary 2023
by Michael Tjia
Released into the public domain.
*/
#include "Arduino.h"
#include <SPI.h>
#include <WiFiNINA.h>
#include "CMDWifi.h"
CMDWifi::CMDWifi() {
status = WL_IDLE_STATUS;
}
void CMDWifi::connect(char * ssid, char * pass, char * server, int port = 80)
{
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if (ssid == 0 || server == 0) {
Serial.println("SSID or server not set");
while (true);
}
// check for the presence of the module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("WiFi module not present");
while (true);
}
hosturl = server;
hostport = port;
// attempt to connect to WiFi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
if (strlen(pass) == 0) {
// Connect to open network.
status = WiFi.begin(ssid);
} else {
// Connect to WPA/WPA2 network.
status = WiFi.begin(ssid, pass);
}
// wait 5 seconds for connection:
delay(5000);
}
// you're connected now, so print out the status:
printWiFiStatus();
}
int CMDWifi::sendGet(char* sendKey, int value, char* getKey) { sprintf(urlPars, "/iot/sendget/%s/%i/%s", sendKey, value, getKey); return read();};
int CMDWifi::send(char* sendKey, int value) { sprintf(urlPars, "/iot/send/%s/%i", sendKey, value); return read();};
int CMDWifi::get(char* getKey) { sprintf(urlPars, "/iot/get/%s", getKey); return read();};
int CMDWifi::read() {
// if three seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest(urlPars);
}
char buffer[50] = "";
int recording = 0;
int i = 0;
// if there is a message waiting from the server
while (client.available()) {
char c = client.read();
if ((recording == 0 && c == 13) || // dumps all characters until
(recording == 1 && c == 10) || // a sequence of 13 10 13 10 (CR LF CR LF)
(recording == 2 && c == 13) || // is detected, then records
(recording == 3 && c == 10)) // to buffer
recording++;
else if (recording != 4)
recording = 0;
else
buffer[i++] = c;
}
if (buffer[0] != '\0') { // if buffer is not empty
buffer[i++] = '\0'; // append \0 to terminate string
return atoi(buffer); // return buffer as an integer
}
else
return -1;
}
void CMDWifi::httpRequest(char* urlPars) {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(hosturl, hostport)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.print("GET ");
client.print(urlPars);
client.println(" HTTP/1.1");
client.print("Host: ");
client.print(hosturl);
client.print(":");
client.println(hostport);
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
void CMDWifi::printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}