-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunications.h
45 lines (39 loc) · 1.25 KB
/
communications.h
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
/*************************************
* setup wifi communications Nodemcu
*************************************/
#include "credentials.h"
bool connect_wifi()
{
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
// Loops for no more than 10 * (.25 + .25) = 10 seconds.
int loop_count = 0;
while((WiFi.status() != WL_CONNECTED) && (loop_count++ < 20)) {
digitalWrite(LED_BUILTIN, LOW); // Flutter the LED on while we try to connect.
Serial.print("*");
delay(250);
digitalWrite(LED_BUILTIN, HIGH); // Flutter the LED on while we try to connect.
delay(250);
}
if (loop_count >= 20) {
Serial.println("\nFailed to connect, will try again later.");
return false;
}
Serial.println("\nConnected.");
delay(1000);
return true;
}
/* Wrapper for an object method */
long get_rssi() {
return WiFi.RSSI();
}
/***********************************
* Send data to Thingspeak.
***********************************/
void thingspeakPostData(){
Serial.println("thingspeaksenddata started");
int result = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.print("thingspeaksenddata completed, result was ");
Serial.println(result);
}