· 6 years ago · Apr 02, 2020, 12:24 PM
1#include <SoftwareSerial.h>
2#include "RTClib.h" // For Real Time Clock
3#include "ThingSpeak.h"
4#include "WiFiEsp.h"
5#include "secrets.h"
6#include "time.h"
7#include <TimeLib.h>
8#include <math.h>
9#include "WiFiEspUdp.h"
10#include <Timezone.h>
11
12// Real time clock
13RTC_DS3231 rtc;
14
15char ssid[] = SECRET_SSID; // network SSID (name)
16char pass[] = SECRET_PASS; // network password
17int keyIndex = 0; // network key Index number (needed only for WEP)
18WiFiEspClient client;
19
20unsigned long myChannelNumber = SECRET_CH_ID; // Thingspeak channel ID
21const char * myWriteAPIKey = SECRET_WRITE_APIKEY; // Thingspeak channel API key
22
23int ThingSpeakResponse = 0;
24
25// Timing-related variables
26time_t UTCtime;
27time_t nowUTC;
28time_t nowLocal;
29
30// Timezone rules for United Kingdom (London, Belfast)
31TimeChangeRule BST = {"BST", Last, Sun, Mar, 1, 60}; // British Summer Time
32TimeChangeRule GMT = {"GMT", Last, Sun, Oct, 2, 0}; // Standard Time
33Timezone UK(BST, GMT);
34
35void setup(){
36 // Initialize serial for output
37 Serial.begin(9600);
38
39 // Initialize DS3231 Real Time Clock
40 if (! rtc.begin()) {
41 Serial.println(F("Couldn't find RTC"));
42 while (1); // can't go further
43 }
44
45 // Initialize serial for ESP module
46 Serial1.begin(115200);
47 // Initialize ESP module
48 WiFi.init(&Serial1);
49
50 // Check for the presence of the shield
51 if (WiFi.status() == WL_NO_SHIELD) {
52 Serial.println(F("WiFi shield not present"));
53 while (true);
54 }
55 // Initialize ThingSpeak
56 ThingSpeak.begin(client);
57
58 // Connect or reconnect to WiFi
59 if(WiFi.status() != WL_CONNECTED){
60 Serial.print(F("Attempting to connect to SSID: "));
61 Serial.println(SECRET_SSID);
62 while(WiFi.status() != WL_CONNECTED){
63 WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
64 delay(5000);
65 }
66 }
67
68 // Set RTC time
69 rtc.adjust(DateTime(2020, 4, 2, 12, 0, 0)); // Set real time clock manually for testing purposes
70
71 nowUTC = rtc.now().unixtime(); // Get current unix timestamp from rtc
72 Serial.print(F("nowUTC: "));
73 Serial.println(nowUTC); // Correctly is 1585828800
74 Serial.print(F("Human readable: "));
75 Serial.println(datetimestring(nowUTC)); // Correctly is 2020/04/02 12:00:00
76 nowLocal = UK.toLocal(nowUTC); // Convert time according to day light saving time rule
77 Serial.print(F("nowLocal: "));
78 Serial.println(nowLocal); // Should be 1585832400
79 Serial.print(F("Human readable: "));
80 Serial.println(datetimestring(nowLocal)); // Should be 2020/04/02 13:00:00
81}
82
83void loop(){
84 // Set ThingSpeak fields with the values
85 ThingSpeak.setField(1, 1);
86
87 // Set ThingSpeak channel status
88 ThingSpeak.setStatus("Testing");
89
90 // Write to the ThingSpeak channel
91 //ThingSpeakResponse = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
92}
93
94// Function to concatenate date and time information
95// Returns String with date and time
96String datetimestring(DateTime datetime) {
97 String fulldatetime;
98 fulldatetime = String(datetime.year(), DEC) + "/";
99 if (datetime.month() < 10) {
100 fulldatetime += "0";
101 }
102 fulldatetime += String(datetime.month(), DEC) + "/";
103 if (datetime.day() < 10) {
104 fulldatetime += "0";
105 }
106 fulldatetime += String(datetime.day(), DEC) + " ";
107 if (datetime.hour() < 10) {
108 fulldatetime += "0";
109 }
110 fulldatetime += String(datetime.hour(), DEC) + ":";
111 if (datetime.minute() < 10) {
112 fulldatetime += "0";
113 }
114 fulldatetime += String(datetime.minute(), DEC) + ":";
115 if (datetime.second() < 10) {
116 fulldatetime += "0";
117 }
118 fulldatetime += String(datetime.second(), DEC);
119 return fulldatetime;
120}