-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nodemcu-cloud.ino
172 lines (142 loc) · 4.13 KB
/
Nodemcu-cloud.ino
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
//------- Import Library ----------//
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
//------- WI-FI details ----------//
char ssid[] = "Xav!er"; //SSID here
char pass[] = "12345678"; // Passowrd here
//--------------------------------//
//----------- Channel details ----------------//
unsigned long Channel_ID = 1347787; // Channel ID
const int Field_1 = 1; // Don't change
const int Field_2 = 2; // Don't change
const char * myWriteAPIKey = "8UxxxxxxxxxxxxxOX"; //Your Thingspeak write API key
//-------------------------------------------//
int x;
int y;
WiFiClient client;
void setup()
{
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
internet();
}
void loop()
{
internet();
get_value();
upload();
}
void internet()
{
if (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nWIFI CONNECTED.");
}
}
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
//============ variables to hold the parsed data ============//
float value1 = 0.0;
float value2 = 0.0;
boolean newData = false;
//========================================================== //
//============
void get_value() {
if (Serial.available() > 0)
{
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
else
{
get_value();
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the string
value1 = atof(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
value2 = atof(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.print("value1: ");
Serial.println(value1);
Serial.print("value2: ");
Serial.println(value2);
}
void upload()
{
//========== set the fields with the values
ThingSpeak.setField(Field_1, value1);
ThingSpeak.setField(Field_2, value2);
if (Serial.available() > 0)
{
x = ThingSpeak.writeFields(Channel_ID,myWriteAPIKey);
// int statusCode = ThingSpeak.getLastReadStatus();
if (x == 200){
Serial.print("Data Updated.");
}
else{
Serial.print("Data upload failed, retrying....");
upload();
}
}
else{
Serial.print("Serial Pin Connection Error!!, retrying....");
}
// value = "";
delay(15000);
}