-
Notifications
You must be signed in to change notification settings - Fork 76
/
A0_to_ThingSpeak.ino
83 lines (62 loc) · 2.26 KB
/
A0_to_ThingSpeak.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
/*
ESP8266 --> ThingSpeak Channel
This sketch sends the value of Analog Input (A0) to a ThingSpeak channel
using the ThingSpeak API (https://www.mathworks.com/help/thingspeak).
Requirements:
* ESP8266 Wi-Fi Device
* Arduino 1.8.8+ IDE
* Additional Boards URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
* Library: esp8266 by ESP8266 Community
* Library: ThingSpeak by MathWorks
ThingSpeak Setup:
* Sign Up for New User Account - https://thingspeak.com/users/sign_up
* Create a new Channel by selecting Channels, My Channels, and then New Channel
* Enable one field
* Enter SECRET_CH_ID in "secrets.h"
* Enter SECRET_WRITE_APIKEY in "secrets.h"
Setup Wi-Fi:
* Enter SECRET_SSID in "secrets.h"
* Enter SECRET_PASS in "secrets.h"
Tutorial: http://nothans.com/measure-wi-fi-signal-levels-with-the-esp8266-and-thingspeak
Created: Feb 3, 2017 by Hans Scharler (http://nothans.com)
*/
#include "ThingSpeak.h"
#include "secrets.h"
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
#include <ESP8266WiFi.h>
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key index number (needed only for WEP)
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(100);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
// Measure Analog Input (A0)
int valueA0 = analogRead(A0);
// Write value to Field 1 of a ThingSpeak Channel
int httpCode = ThingSpeak.writeField(myChannelNumber, 1, valueA0, myWriteAPIKey);
if (httpCode == 200) {
Serial.println("Channel write successful.");
}
else {
Serial.println("Problem writing to channel. HTTP error code " + String(httpCode));
}
// Wait 20 seconds to update the channel again
delay(20000);
}