-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharduino.ino
140 lines (123 loc) · 3.37 KB
/
arduino.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
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#include "SPI.h"
#include "Wire.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#include <ESP8266HTTPClient.h>
int Test = 1;
const char *ssid = "YOUR-SSID";
const char *password = "YOUR-PASSWORD";
#define OLED_RESET 0 // GPIO0
Adafruit_SSD1306 display(OLED_RESET);
WiFiUDP ntpUDP;
// Change this depending on your location
NTPClient timeClient(ntpUDP, "2.uk.pool.ntp.org", 3600, 60000);
String Day = "Sun";
String Dose = "0mg";
void calculateDay() {
// Subroutine to convert the Day
Serial.print("Today is day: ");
Serial.println(timeClient.getDay());
if (timeClient.getDay() == 0) {
Day = "Wed";
}
else if (timeClient.getDay() == 1) {
Day = "Thu";
}
else if (timeClient.getDay() == 2) {
Day = "Fri";
}
else if (timeClient.getDay() == 3) {
Day = "Sat";
}
else if (timeClient.getDay() == 4) {
Day = "Sun";
}
else if (timeClient.getDay() == 5) {
Day = "Mon";
}
else if (timeClient.getDay() == 6) {
Day = "Tue";
} else {
Serial.println("Error calculating day!");
display.println("Error!");
}
}
void fetchDose() {
// Fetch the amount of warferin from a webpage.
HTTPClient http;
Serial.print("[HTTP] begin...\n");
// configure the URL where we can pull the dosage from. Might
// Also, if you don't want anyone to see it, it's a good idea to
// create a long, random webpage name.
http.begin("YOUR URL TO THE DOSE SCRIPT"); //HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
Dose = http.getString();
Serial.println("Dosage updated!");
Serial.println(Dose);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
}
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 1000 );
Serial.print ( "." );
}
timeClient.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// init done
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(13,0);
calculateDay();
fetchDose();
display.setTextSize(1);
display.setCursor(7,40);
display.println(timeClient.getFormattedTime());
display.display();
//Serial.println(dose);
delay(1000);
}
void loop() {
timeClient.update();
if (timeClient.getFormattedTime() == "00:00:00") {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(13,0);
calculateDay();
fetchDose();
display.setTextSize(1);
display.setCursor(7,40);
display.println(timeClient.getFormattedTime());
display.display();
} else {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(14,0);
display.println(Day);
display.setCursor(14,20);
display.println(Dose);
display.setTextSize(1);
display.setCursor(7,40);
display.println(timeClient.getFormattedTime());
display.display();
}
delay(1000);
}