-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharduino-esp8266-feather-oled.ino
106 lines (84 loc) · 1.99 KB
/
arduino-esp8266-feather-oled.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
/*
Connect to WiFi and query a URL for a payload to be printed on an LCD screen.
This code is somewhat specific for an Adafruit HUZZAH with an OLED FeatherWing
attached.
Author: stigok ([email protected])
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#define OLED_RESET 3
Adafruit_SSD1306 display(OLED_RESET);
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
const char* url = "http://192.168.0.7:4000/6013"; // Stig
WiFiClient client;
HTTPClient http;
void setup() {
// Serial init
Serial.begin(115200);
// Display init
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
display.setTextSize(1);
display.setTextColor(WHITE);
clear();
log("WiFi connecting:\n");
log(String(ssid) + '\n');
WiFi.mode(WIFI_STA); // client mode (not AP)
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
log(".");
delay(500);
}
clear();
log("Connected!\n");
log("IP:" + WiFi.localIP().toString() + '\n');
delay(3000);
// keep-alive
http.setReuse(true);
}
void clear() {
display.clearDisplay();
display.setCursor(0, 0);
Serial.println("--- display cleared ---");
}
void log(String s) {
display.print(s);
display.display();
Serial.print(s);
}
void error(String s) {
clear();
log(String("err: ") + s + '\n');
delay(3000);
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
error("lost wifi connection.\nrestarting...");
ESP.restart();
return;
}
if (!http.begin(client, url)) {
error("http unable to connect\n");
return;
}
int code = http.GET();
if (code <= 0) {
error(String("HTTP error (") + http.errorToString(code).c_str() + ")\n");
return;
}
String payload = http.getString();
clear();
log(payload);
http.end();
Serial.println("done. sleeping...");
delay(5000);
}