forked from julisa99/Lovebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.ino
133 lines (112 loc) · 2.83 KB
/
message.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
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <EEPROM.h>
#include <Servo.h>
#include "SSD1306Wire.h"
#include "credentials.h"
const char* ssid = _ssid;
const char* password = _password;
const String url = _url;
SSD1306Wire oled(0x3C, D2, D1);
Servo myservo;
int pos = 90;
int increment = -1;
int lightValue;
String line;
String modus;
char idSaved;
bool wasRead;
void drawMessage(const String& message) {
oled.clear();
// Unterscheide zwischen Text und Bild
if(modus[0] == 't'){
oled.drawStringMaxWidth(0, 0, 128, message);
}
else {
for(int i = 0; i <= message.length(); i++){
int x = i % 129;
int y = i / 129;
if(message[i] == '1'){
oled.setPixel(x, y);
}
}
}
oled.display();
}
void wifiConnect() {
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
// Warte auf Verbindung
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
}
void getGistMessage() {
const int httpsPort = 443;
const char* host = "gist.githubusercontent.com";
const char fingerprint[] = "CC AA 48 48 66 46 0E 91 53 2C 9C 7C 23 2A B1 74 4D 29 9D 33";
WiFiClientSecure client;
client.setFingerprint(fingerprint);
if (!client.connect(host, httpsPort)) {
return; // Verbindung fehlgeschlagen
}
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String temp = client.readStringUntil('\n');
if (temp == "\r") {
break;
}
}
String id = client.readStringUntil('\n');
if(id[0] != idSaved){ // Neue Nachricht
wasRead = 0;
idSaved = id[0];
EEPROM.write(142, idSaved);
EEPROM.write(144, wasRead);
EEPROM.commit();
modus = client.readStringUntil('\n');
line = client.readStringUntil(0);
drawMessage(line);
}
}
void spinServo(){
myservo.write(pos);
delay(50); // Warte 50ms um den Servo zu drehen
if(pos == 75 || pos == 105){ // Drehbereich zwischen 75°-105°
increment *= -1;
}
pos += increment;
}
void setup() {
myservo.attach(16); // Servo an D0
oled.init();
oled.flipScreenVertically();
oled.setColor(WHITE);
oled.setTextAlignment(TEXT_ALIGN_LEFT);
oled.setFont(ArialMT_Plain_10);
oled.clear();
oled.drawString(30, 30, "<3 LOVEBOX <3");
oled.display();
wifiConnect();
EEPROM.begin(512);
idSaved = EEPROM.get(142, idSaved);
wasRead = EEPROM.get(144, wasRead);
}
void loop() {
if(wasRead){
getGistMessage();
}
while(!wasRead){
spinServo(); // Drehe Herz
lightValue = analogRead(0); // Lese Helligkeitswert
if(lightValue > 300) {
wasRead = 1;
EEPROM.write(144, wasRead);
EEPROM.commit();
}
}
}