Components and supplies
Arduino Oplà IoT Kit
Apps and platforms
Arduino IoT Cloud
Project description
Code
opla_alarm.ino
arduino
1#include "ScreenController.hpp" 2 3#include "arduino_secrets.h" 4#include "thingProperties.h" 5 6MKRIoTCarrier carrier; 7RTCZero rtclock; 8const int GMT = +1; 9ScreenController screen(&carrier, &rtclock, GMT); 10 11// Check the connection every 15 seconds 12const long interval = 15 * 1000; 13unsigned long previous_millis = 0; 14bool wifi_first_connection = true; 15 16const long ntp_interval = 60000 * 5; //5 minutes 17 18unsigned long ntp_previous_millis = 0; 19 20const long date_interval = 1000; //every second 21unsigned long date_previous_millis = 0; 22 23bool is_ntp_time_sync = false; 24 25void setup() { 26 /* Initialize serial and wait up to 5 seconds for port to open */ 27 Serial.begin(115200); 28 for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime > 5000);) { 29 continue; 30 } 31 32 /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ 33 initProperties(); 34 35 /* Initialize Arduino IoT Cloud library */ 36 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 37 38 //setDebugMessageLevel(DBG_INFO); 39 setDebugMessageLevel(DBG_DEBUG); 40 ArduinoCloud.printDebugInfo(); 41 // ArduinoCloud.updateInternalTimezoneInfo(); 42 43 rtclock.begin(true); //resetTime = true 44 //Serial.println(TOUCH.getSensorsSensitivity()); 45 CARRIER_CASE = true; 46 carrier.begin(); 47 48 TOUCH.setSensorsSensitivity(100); 49 Serial.print("Button sensitivity:"); 50 Serial.println(TOUCH.getSensorsSensitivity()); 51} 52 53unsigned long prev_sound = 0; 54volatile bool play_alarm = false; 55 56void loop() { 57 ArduinoCloud.update(); 58 const unsigned long current_millis = millis(); 59 60 if ((current_millis - date_previous_millis) >= date_interval) { 61 date_previous_millis = current_millis; 62 screen.render(); 63 } 64 65 if (play_alarm) { 66 if ((current_millis - prev_sound > 1000)) { 67 prev_sound = current_millis; 68 tone(carrier.Buzzer.getPin(), 500, 500);// async 69 } 70 } 71 //Serial.println(TOUCH.getSensorsSensitivity()); 72 carrier.Buttons.update(); 73 if (carrier.Buttons.onTouchUp(TOUCH0) or 74 carrier.Buttons.onTouchUp(TOUCH1) or 75 carrier.Buttons.onTouchUp(TOUCH2) or 76 carrier.Buttons.onTouchUp(TOUCH3) or 77 carrier.Buttons.onTouchUp(TOUCH4) 78 ) { 79 Serial.println("Touched Down a button"); 80 play_alarm = false; 81 } 82 83 // Checking the network status every X seconds 84 if (current_millis - previous_millis >= interval or wifi_first_connection) { 85 previous_millis = current_millis; 86 if (ArduinoCloud.connected() != 0) { 87 // Wifi Ok 88 screen.is_wifi_connected = true; 89 90 // I'm sure that the WIFI is active I sync the time with NTP Protocol 91 if (current_millis - ntp_previous_millis >= ntp_interval or not is_ntp_time_sync) { 92 uint32_t epoch = 0; 93 uint8_t attempts = 0; 94 95 while (epoch == 0) { 96 epoch = ArduinoCloud.getInternalTime(); 97 // Limit 98 if (++attempts > 4) { 99 break; 100 } 101 } 102 if (epoch == 0) { 103 Serial.println("NTP unreachable!"); 104 } else { 105 screen.is_time_set = true; 106 if (is_ntp_time_sync) { 107 // Checking if the internal RTC is still sync 108 uint32_t rtc_epoch = rtclock.getEpoch(); 109 if (rtc_epoch != epoch) { 110 Serial.print("Internal epoch NOT sync anymore: rtc_epoch != epoch "); 111 Serial.print(rtc_epoch); 112 Serial.print(" != "); 113 Serial.println(epoch); 114 is_ntp_time_sync = false; 115 } else { 116 Serial.print("Internal epoch still sync at "); 117 printRTCInternalDate(); 118 } 119 } 120 // First epoch set or out of sync.. 121 if (not is_ntp_time_sync) { 122 rtclock.setEpoch(epoch); 123 is_ntp_time_sync = true; //TODO check if the time is still on sync! 124 Serial.print("Epoch set: "); 125 Serial.println(epoch); 126 Serial.print("Internal RTC time: "); 127 printRTCInternalDate(); 128 } 129 ntp_previous_millis = current_millis; // Force NTP check every n minutes 130 } 131 } 132 } else { 133 screen.is_wifi_connected = false; 134 } 135 } 136} 137 138void setAlarm() { 139 rtclock.disableAlarm(); 140 rtclock.detachInterrupt(); 141 if (switchAlarm) { 142 uint alarm_time_second = alarmTime; 143 uint raw_minutes = alarm_time_second / 60; 144 uint8_t clock_hour = raw_minutes / 60 - GMT; 145 uint8_t clock_minute = raw_minutes % 60; 146 if (clock_hour >= 24) { 147 clock_hour -= 24; 148 } else if (clock_hour < 0) { 149 clock_hour += 24; 150 } 151 152 rtclock.setAlarmTime(clock_hour, clock_minute, 0); 153 rtclock.enableAlarm(rtclock.MATCH_HHMMSS); 154 155 rtclock.attachInterrupt(alarmMatch); 156 Serial.println("Alarm set"); 157 return; 158 } 159 Serial.println("Alarm UNset"); 160} 161 162void alarmMatch() { 163 Serial.println("Interrupt triggered"); 164 play_alarm = true; 165} 166 167void onAlarmTimeChange() { 168 setAlarm(); 169 Serial.print("Alarm changed: "); 170 Serial.println(alarmTime); 171// uint raw_minutes = alarmTime / 60; 172// uint clock_hour = raw_minutes / 60; 173// uint clock_minute = raw_minutes % 60; 174// Serial.print(clock_hour); 175// Serial.print(":"); 176// Serial.println(clock_minute); 177 screen.printAlarmTime(alarmTime); 178} 179 180void onSwitchAlarmChange() { 181 setAlarm(); 182 Serial.print("Switching alarm status to: "); 183 Serial.print(switchAlarm); 184 screen.printAlarm(switchAlarm); 185} 186 187void printRTCInternalDate() { 188 Serial.print(rtclock.getDay()); 189 Serial.print("/"); 190 Serial.print(rtclock.getMonth()); 191 Serial.print("/"); 192 Serial.print(rtclock.getYear()); 193 Serial.print(" "); 194 Serial.print(rtclock.getHours() + GMT); 195 Serial.print(":"); 196 Serial.print(rtclock.getMinutes()); 197 Serial.print(":"); 198 Serial.print(rtclock.getSeconds()); 199 Serial.println(" "); 200} 201
images.h
c_cpp
1//https://www.mischianti.org/images-to-byte-array-online-converter-cpp-arduino/ 2const unsigned char wifi [] PROGMEM = { 3 // 'wifi, 15x15px 4 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xe0, 0x38, 0x38, 0xe0, 0x0e, 0x4f, 0xe4, 0x3c, 0x78, 5 0x10, 0x10, 0x07, 0xc0, 0x04, 0x40, 0x00, 0x00, 0x03, 0x80, 0x03, 0x80, 0x01, 0x00 6}; 7 8const unsigned char nowifi [] PROGMEM = { 9 // 'nowifi, 15x15px 10 0x03, 0x80, 0x03, 0x80, 0x03, 0x80, 0x0f, 0xe0, 0x3b, 0xb8, 0xe1, 0x0e, 0x4d, 0x64, 0x19, 0x30, 11 0x11, 0x10, 0x07, 0xc0, 0x05, 0x40, 0x00, 0x00, 0x03, 0x80, 0x03, 0x80, 0x01, 0x00 12}; 13 14 15const unsigned char clean [] PROGMEM = { 16 // 'clean, 15x15px 17 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 18 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 19}; 20 21const unsigned char alarmon [] PROGMEM = { 22 // 'alarmon, 15x15px 23 0x00, 0x00, 0x30, 0x18, 0x69, 0x2c, 0x59, 0x34, 0xb6, 0xda, 0xa8, 0x2a, 0xa8, 0x2a, 0xa8, 0x2a, 24 0x08, 0x20, 0x08, 0x20, 0x10, 0x10, 0x30, 0x18, 0x1f, 0xf0, 0x03, 0x80, 0x00, 0x00 25}; 26
ScreenController.hpp
c_cpp
1#ifndef SCREEN_CONTROLLER 2#define SCREEN_CONTROLLER 3 4#include <RTCZero.h> 5#include <Arduino_MKRIoTCarrier.h> 6 7#include "images.h" 8 9class ScreenController { 10 public: 11 int GMT; //change this to adapt it to your time zone 12 bool is_wifi_connected; 13 bool is_time_set; 14 15 ScreenController(MKRIoTCarrier* carrier, RTCZero* rtc, int gmt): 16 GMT(gmt), 17 m_carrier(carrier), 18 m_rtc(rtc), 19 is_first_time_render(true), 20 current_color(ST77XX_WHITE), 21 screen_status_second(0), 22 is_wifi_connected(false), // allows to do the first render 23 screen_status_is_wifi_connected(true), 24 is_time_set(false) { 25 } 26 27 bool isFirstRender() const { 28 return is_first_time_render; 29 } 30 31 32 void off() const { 33 m_carrier->display.enableDisplay(false); 34 } 35 36 void on() const { 37 m_carrier->display.enableDisplay(true); 38 } 39 void cleanScreen() const { 40 m_carrier->display.fillScreen(ST77XX_BLACK); //black background 41 } 42 43 void render() { 44 on(); 45 if (is_time_set) { 46 if (isFirstRender()) { 47 initRender(); 48 } 49 uint16_t second = m_rtc->getSeconds(); 50 51 //printint everything until 0 52 if (second < (screen_status_second + 1)) { 53 for (uint16_t this_second = (screen_status_second + 1); this_second <= 59; this_second++) { 54 printBorderSeconds(this_second); 55 } 56 printBorderSeconds(0); 57 printTime(); 58 screen_status_second = 0; 59 } 60 for (uint16_t this_second = (screen_status_second + 1); this_second <= second; this_second++) { 61 if (printBorderSeconds(this_second)) { 62 printTime(); 63 } 64 } 65 screen_status_second = second; 66 } 67 68 drawWifiIcon(); 69 70 if (screen_status_is_wifi_connected and not is_time_set) { 71 printDateUnavailable(); 72 } else if (screen_status_is_wifi_connected and is_time_set) { 73 cleanMessage(); 74 } 75 } 76 77 void cleanMessage() { 78 printMessage(" "); 79 } 80 81 void printDateUnavailable() { 82 printMessage("Fetching date.."); 83 } 84 85 void drawWifiIcon() { 86 if (screen_status_is_wifi_connected == is_wifi_connected) { 87 // the screen already reflects the status no need to update. 88 return; 89 } 90 screen_status_is_wifi_connected = is_wifi_connected; 91 92 cleanMessage(); 93 m_carrier->display.drawBitmap(110, 200, clean, 15, 15, ST77XX_BLACK); 94 if (is_wifi_connected) { 95 m_carrier->display.drawBitmap(110, 200, wifi, 15, 15, ST77XX_WHITE); 96 return; 97 } 98 printConnectingWiFI(); 99 m_carrier->display.drawBitmap(110, 200, nowifi, 15, 15, ST77XX_RED); 100 } 101 102 103 void printAlarm(bool alarm_is_enabled) { 104 if (alarm_is_enabled) { 105 m_carrier->display.drawBitmap(110, 30, alarmon, 15, 15, ST77XX_RED); 106 return; 107 } 108 m_carrier->display.drawBitmap(110, 30, clean, 15, 15, ST77XX_BLACK); 109 } 110 111 void printAlarmTime(uint alarm_time_second) { 112 uint raw_minutes = alarm_time_second / 60; 113 uint clock_hour = raw_minutes / 60; 114 uint clock_minute = raw_minutes % 60; 115 m_carrier->display.setTextColor(ST77XX_RED, ST77XX_BLACK); 116 m_carrier->display.setCursor(87, 55); //sets position for printing (x and y) 117 m_carrier->display.setTextSize(2); 118 if (clock_hour < 10) { 119 m_carrier->display.print(" "); 120 } 121 m_carrier->display.print(clock_hour); 122 m_carrier->display.print(":"); 123 if (clock_minute < 10) { 124 m_carrier->display.print("0"); 125 } 126 m_carrier->display.print(clock_minute); 127 } 128 129 private: 130 bool is_first_time_render; 131 bool screen_status_is_wifi_connected; 132 uint16_t current_color; 133 uint16_t screen_status_second; 134 //uint32_t alarm_time_second; 135 136 MKRIoTCarrier* m_carrier; 137 RTCZero* m_rtc; 138 139 void printMessage(String message) { 140 m_carrier->display.setTextColor(ST77XX_WHITE, ST77XX_BLACK); //white text 141 m_carrier->display.setCursor(75, 155); //sets position for printing (x and y) 142 m_carrier->display.setTextSize(1); 143 m_carrier->display.print(message); 144 } 145 146 147 void printConnectingWiFI() { 148 printMessage(" Connecting.."); 149 } 150 151 void initRender() { 152 printTime(); 153 is_first_time_render = false; 154 } 155 156 bool printBorderSeconds(uint16_t second) { 157 const float pi = 3.14; 158 const float pi_mezzi = pi / 2; 159 //Serial.print("printing: "); 160 //Serial.println(second); 161 const float step = 2 * pi / 60; 162 const float start = (second - 1) * step - pi_mezzi; 163 const float stop = second * step - pi_mezzi; 164 165 //for (float rad = start; rad < stop; rad = rad + 0.1) { 166 //for (float rad = start; rad < stop; rad = rad + 0.05) { 167 for (float rad = start; rad < stop; rad = rad + 0.01) { 168 for (uint16_t radius = 110; radius <= 117; radius++) { 169 const uint16_t x = 120 + radius * cos(rad); 170 const uint16_t y = 120 + radius * sin(rad); 171 //Serial.print(x); 172 //Serial.print(" "); 173 //Serial.println(y); 174 m_carrier->display.drawPixel(x, y, current_color); 175 } 176 } 177 if (second == 0) { 178 if (current_color == ST77XX_WHITE) { 179 current_color = ST77XX_BLACK; 180 } else { 181 current_color = ST77XX_WHITE; 182 } 183 return true; 184 } 185 return false; 186 } 187 188 void printTime() { 189 m_carrier->display.setTextColor(ST77XX_WHITE, ST77XX_BLACK); //white text black Background 190 m_carrier->display.setTextSize(6); //medium sized text 191 192 m_carrier->display.setCursor(30, 100); //sets position for printing (x and y) 193 uint16_t hour = m_rtc->getHours() + GMT; 194 if (hour > 23) { 195 // Deal with impossible hours intoduced by GTM conversion 196 m_carrier->display.print(hour - 24); 197 } else { 198 if (hour < 10) { 199 m_carrier->display.print(" "); 200 } 201 m_carrier->display.print(hour); 202 } 203 m_carrier->display.print(":"); 204 if (m_rtc->getMinutes() < 10) { 205 m_carrier->display.print("0"); 206 } 207 m_carrier->display.print(m_rtc->getMinutes()); 208 } 209}; 210#endif 211
arduino_secrets.h
c_cpp
1#include <Arduino_ConnectionHandler.h> 2 3#define THING_ID "CHANGEME" 4#define BOARD_ID "CHANGEME" 5 6/* MKR1000, MKR WiFi 1010 */ 7#if defined(BOARD_HAS_WIFI) 8 #define SECRET_SSID "CHANGEME" 9 #define SECRET_PASS "CHANGEME" 10#endif 11
ScreenController.hpp
c_cpp
1#ifndef SCREEN_CONTROLLER 2#define SCREEN_CONTROLLER 3 4#include <RTCZero.h> 5#include <Arduino_MKRIoTCarrier.h> 6 7#include "images.h" 8 9class ScreenController { 10 public: 11 int GMT; //change this to adapt it to your time zone 12 bool is_wifi_connected; 13 bool is_time_set; 14 15 ScreenController(MKRIoTCarrier* carrier, RTCZero* rtc, int gmt): 16 GMT(gmt), 17 m_carrier(carrier), 18 m_rtc(rtc), 19 is_first_time_render(true), 20 current_color(ST77XX_WHITE), 21 screen_status_second(0), 22 is_wifi_connected(false), // allows to do the first render 23 screen_status_is_wifi_connected(true), 24 is_time_set(false) { 25 } 26 27 bool isFirstRender() const { 28 return is_first_time_render; 29 } 30 31 32 void off() const { 33 m_carrier->display.enableDisplay(false); 34 } 35 36 void on() const { 37 m_carrier->display.enableDisplay(true); 38 } 39 void cleanScreen() const { 40 m_carrier->display.fillScreen(ST77XX_BLACK); //black background 41 } 42 43 void render() { 44 on(); 45 if (is_time_set) { 46 if (isFirstRender()) { 47 initRender(); 48 } 49 uint16_t second = m_rtc->getSeconds(); 50 51 //printint everything until 0 52 if (second < (screen_status_second + 1)) { 53 for (uint16_t this_second = (screen_status_second + 1); this_second <= 59; this_second++) { 54 printBorderSeconds(this_second); 55 } 56 printBorderSeconds(0); 57 printTime(); 58 screen_status_second = 0; 59 } 60 for (uint16_t this_second = (screen_status_second + 1); this_second <= second; this_second++) { 61 if (printBorderSeconds(this_second)) { 62 printTime(); 63 } 64 } 65 screen_status_second = second; 66 } 67 68 drawWifiIcon(); 69 70 if (screen_status_is_wifi_connected and not is_time_set) { 71 printDateUnavailable(); 72 } else if (screen_status_is_wifi_connected and is_time_set) { 73 cleanMessage(); 74 } 75 } 76 77 void cleanMessage() { 78 printMessage(" "); 79 } 80 81 void printDateUnavailable() { 82 printMessage("Fetching date.."); 83 } 84 85 void drawWifiIcon() { 86 if (screen_status_is_wifi_connected == is_wifi_connected) { 87 // the screen already reflects the status no need to update. 88 return; 89 } 90 screen_status_is_wifi_connected = is_wifi_connected; 91 92 cleanMessage(); 93 m_carrier->display.drawBitmap(110, 200, clean, 15, 15, ST77XX_BLACK); 94 if (is_wifi_connected) { 95 m_carrier->display.drawBitmap(110, 200, wifi, 15, 15, ST77XX_WHITE); 96 return; 97 } 98 printConnectingWiFI(); 99 m_carrier->display.drawBitmap(110, 200, nowifi, 15, 15, ST77XX_RED); 100 } 101 102 103 void printAlarm(bool alarm_is_enabled) { 104 if (alarm_is_enabled) { 105 m_carrier->display.drawBitmap(110, 30, alarmon, 15, 15, ST77XX_RED); 106 return; 107 } 108 m_carrier->display.drawBitmap(110, 30, clean, 15, 15, ST77XX_BLACK); 109 } 110 111 void printAlarmTime(uint alarm_time_second) { 112 uint raw_minutes = alarm_time_second / 60; 113 uint clock_hour = raw_minutes / 60; 114 uint clock_minute = raw_minutes % 60; 115 m_carrier->display.setTextColor(ST77XX_RED, ST77XX_BLACK); 116 m_carrier->display.setCursor(87, 55); //sets position for printing (x and y) 117 m_carrier->display.setTextSize(2); 118 if (clock_hour < 10) { 119 m_carrier->display.print(" "); 120 } 121 m_carrier->display.print(clock_hour); 122 m_carrier->display.print(":"); 123 if (clock_minute < 10) { 124 m_carrier->display.print("0"); 125 } 126 m_carrier->display.print(clock_minute); 127 } 128 129 private: 130 bool is_first_time_render; 131 bool screen_status_is_wifi_connected; 132 uint16_t current_color; 133 uint16_t screen_status_second; 134 //uint32_t alarm_time_second; 135 136 MKRIoTCarrier* m_carrier; 137 RTCZero* m_rtc; 138 139 void printMessage(String message) { 140 m_carrier->display.setTextColor(ST77XX_WHITE, ST77XX_BLACK); //white text 141 m_carrier->display.setCursor(75, 155); //sets position for printing (x and y) 142 m_carrier->display.setTextSize(1); 143 m_carrier->display.print(message); 144 } 145 146 147 void printConnectingWiFI() { 148 printMessage(" Connecting.."); 149 } 150 151 void initRender() { 152 printTime(); 153 is_first_time_render = false; 154 } 155 156 bool printBorderSeconds(uint16_t second) { 157 const float pi = 3.14; 158 const float pi_mezzi = pi / 2; 159 //Serial.print("printing: "); 160 //Serial.println(second); 161 const float step = 2 * pi / 60; 162 const float start = (second - 1) * step - pi_mezzi; 163 const float stop = second * step - pi_mezzi; 164 165 //for (float rad = start; rad < stop; rad = rad + 0.1) { 166 //for (float rad = start; rad < stop; rad = rad + 0.05) { 167 for (float rad = start; rad < stop; rad = rad + 0.01) { 168 for (uint16_t radius = 110; radius <= 117; radius++) { 169 const uint16_t x = 120 + radius * cos(rad); 170 const uint16_t y = 120 + radius * sin(rad); 171 //Serial.print(x); 172 //Serial.print(" "); 173 //Serial.println(y); 174 m_carrier->display.drawPixel(x, y, current_color); 175 } 176 } 177 if (second == 0) { 178 if (current_color == ST77XX_WHITE) { 179 current_color = ST77XX_BLACK; 180 } else { 181 current_color = ST77XX_WHITE; 182 } 183 return true; 184 } 185 return false; 186 } 187 188 void printTime() { 189 m_carrier->display.setTextColor(ST77XX_WHITE, ST77XX_BLACK); //white text black Background 190 m_carrier->display.setTextSize(6); //medium sized text 191 192 m_carrier->display.setCursor(30, 100); //sets position for printing (x and y) 193 uint16_t hour = m_rtc->getHours() + GMT; 194 if (hour > 23) { 195 // Deal with impossible hours intoduced by GTM conversion 196 m_carrier->display.print(hour - 24); 197 } else { 198 if (hour < 10) { 199 m_carrier->display.print(" "); 200 } 201 m_carrier->display.print(hour); 202 } 203 m_carrier->display.print(":"); 204 if (m_rtc->getMinutes() < 10) { 205 m_carrier->display.print("0"); 206 } 207 m_carrier->display.print(m_rtc->getMinutes()); 208 } 209}; 210#endif 211
opla_alarm.ino
arduino
1#include "ScreenController.hpp" 2 3#include "arduino_secrets.h" 4#include 5 "thingProperties.h" 6 7MKRIoTCarrier carrier; 8RTCZero rtclock; 9const 10 int GMT = +1; 11ScreenController screen(&carrier, &rtclock, GMT); 12 13// Check 14 the connection every 15 seconds 15const long interval = 15 * 1000; 16unsigned 17 long previous_millis = 0; 18bool wifi_first_connection = true; 19 20const long 21 ntp_interval = 60000 * 5; //5 minutes 22 23unsigned long ntp_previous_millis = 24 0; 25 26const long date_interval = 1000; //every second 27unsigned long date_previous_millis 28 = 0; 29 30bool is_ntp_time_sync = false; 31 32void setup() { 33 /* Initialize 34 serial and wait up to 5 seconds for port to open */ 35 Serial.begin(115200); 36 37 for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime 38 > 5000);) { 39 continue; 40 } 41 42 /* This function takes care of connecting 43 your sketch variables to the ArduinoIoTCloud object */ 44 initProperties(); 45 46 47 /* Initialize Arduino IoT Cloud library */ 48 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 49 50 51 //setDebugMessageLevel(DBG_INFO); 52 setDebugMessageLevel(DBG_DEBUG); 53 ArduinoCloud.printDebugInfo(); 54 55 // ArduinoCloud.updateInternalTimezoneInfo(); 56 57 rtclock.begin(true); //resetTime 58 = true 59 //Serial.println(TOUCH.getSensorsSensitivity()); 60 CARRIER_CASE = 61 true; 62 carrier.begin(); 63 64 TOUCH.setSensorsSensitivity(100); 65 Serial.print("Button 66 sensitivity:"); 67 Serial.println(TOUCH.getSensorsSensitivity()); 68} 69 70unsigned 71 long prev_sound = 0; 72volatile bool play_alarm = false; 73 74void loop() { 75 76 ArduinoCloud.update(); 77 const unsigned long current_millis = millis(); 78 79 80 if ((current_millis - date_previous_millis) >= date_interval) { 81 date_previous_millis 82 = current_millis; 83 screen.render(); 84 } 85 86 if (play_alarm) 87 { 88 if ((current_millis - prev_sound > 1000)) { 89 prev_sound = current_millis; 90 91 tone(carrier.Buzzer.getPin(), 500, 500);// async 92 } 93 } 94 //Serial.println(TOUCH.getSensorsSensitivity()); 95 96 carrier.Buttons.update(); 97 if (carrier.Buttons.onTouchUp(TOUCH0) or 98 carrier.Buttons.onTouchUp(TOUCH1) 99 or 100 carrier.Buttons.onTouchUp(TOUCH2) or 101 carrier.Buttons.onTouchUp(TOUCH3) 102 or 103 carrier.Buttons.onTouchUp(TOUCH4) 104 ) { 105 Serial.println("Touched 106 Down a button"); 107 play_alarm = false; 108 } 109 110 // Checking the network 111 status every X seconds 112 if (current_millis - previous_millis >= interval or 113 wifi_first_connection) { 114 previous_millis = current_millis; 115 if (ArduinoCloud.connected() 116 != 0) { 117 // Wifi Ok 118 screen.is_wifi_connected = true; 119 120 // 121 I'm sure that the WIFI is active I sync the time with NTP Protocol 122 if (current_millis 123 - ntp_previous_millis >= ntp_interval or not is_ntp_time_sync) { 124 uint32_t 125 epoch = 0; 126 uint8_t attempts = 0; 127 128 while 129 (epoch == 0) { 130 epoch = ArduinoCloud.getInternalTime(); 131 // 132 Limit 133 if (++attempts > 4) { 134 break; 135 } 136 137 } 138 if (epoch == 0) { 139 Serial.println("NTP unreachable!"); 140 141 } else { 142 screen.is_time_set = true; 143 if (is_ntp_time_sync) 144 { 145 // Checking if the internal RTC is still sync 146 uint32_t 147 rtc_epoch = rtclock.getEpoch(); 148 if (rtc_epoch != epoch) { 149 Serial.print("Internal 150 epoch NOT sync anymore: rtc_epoch != epoch "); 151 Serial.print(rtc_epoch); 152 153 Serial.print(" != "); 154 Serial.println(epoch); 155 156 is_ntp_time_sync = false; 157 } else { 158 Serial.print("Internal 159 epoch still sync at "); 160 printRTCInternalDate(); 161 } 162 163 } 164 // First epoch set or out of sync.. 165 if (not 166 is_ntp_time_sync) { 167 rtclock.setEpoch(epoch); 168 is_ntp_time_sync 169 = true; //TODO check if the time is still on sync! 170 Serial.print("Epoch 171 set: "); 172 Serial.println(epoch); 173 Serial.print("Internal 174 RTC time: "); 175 printRTCInternalDate(); 176 } 177 ntp_previous_millis 178 = current_millis; // Force NTP check every n minutes 179 } 180 } 181 182 } else { 183 screen.is_wifi_connected = false; 184 } 185 } 186} 187 188void 189 setAlarm() { 190 rtclock.disableAlarm(); 191 rtclock.detachInterrupt(); 192 193 if (switchAlarm) { 194 uint alarm_time_second = alarmTime; 195 uint raw_minutes 196 = alarm_time_second / 60; 197 uint8_t clock_hour = raw_minutes / 60 - GMT; 198 199 uint8_t clock_minute = raw_minutes % 60; 200 if (clock_hour >= 24) { 201 202 clock_hour -= 24; 203 } else if (clock_hour < 0) { 204 clock_hour 205 += 24; 206 } 207 208 rtclock.setAlarmTime(clock_hour, clock_minute, 0); 209 210 rtclock.enableAlarm(rtclock.MATCH_HHMMSS); 211 212 rtclock.attachInterrupt(alarmMatch); 213 214 Serial.println("Alarm set"); 215 return; 216 } 217 Serial.println("Alarm 218 UNset"); 219} 220 221void alarmMatch() { 222 Serial.println("Interrupt triggered"); 223 224 play_alarm = true; 225} 226 227void onAlarmTimeChange() { 228 setAlarm(); 229 230 Serial.print("Alarm changed: "); 231 Serial.println(alarmTime); 232// uint 233 raw_minutes = alarmTime / 60; 234// uint clock_hour = raw_minutes / 60; 235// 236 uint clock_minute = raw_minutes % 60; 237// Serial.print(clock_hour); 238// 239 Serial.print(":"); 240// Serial.println(clock_minute); 241 screen.printAlarmTime(alarmTime); 242} 243 244void 245 onSwitchAlarmChange() { 246 setAlarm(); 247 Serial.print("Switching alarm status 248 to: "); 249 Serial.print(switchAlarm); 250 screen.printAlarm(switchAlarm); 251} 252 253void 254 printRTCInternalDate() { 255 Serial.print(rtclock.getDay()); 256 Serial.print("/"); 257 258 Serial.print(rtclock.getMonth()); 259 Serial.print("/"); 260 Serial.print(rtclock.getYear()); 261 262 Serial.print(" "); 263 Serial.print(rtclock.getHours() + GMT); 264 Serial.print(":"); 265 266 Serial.print(rtclock.getMinutes()); 267 Serial.print(":"); 268 Serial.print(rtclock.getSeconds()); 269 270 Serial.println(" "); 271} 272
thingProperties.h
c_cpp
1#include <ArduinoIoTCloud.h> 2#include <Arduino_ConnectionHandler.h> 3 4#include "arduino_secrets.h" 5 6void onSwitchAlarmChange(); 7void onAlarmTimeChange(); 8 9bool switchAlarm; 10CloudTime alarmTime; 11 12void initProperties() { 13 14 ArduinoCloud.setThingId(THING_ID); 15#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) 16 ArduinoCloud.addProperty(switchAlarm, READWRITE, ON_CHANGE, onSwitchAlarmChange); 17 ArduinoCloud.addProperty(alarmTime, READWRITE, ON_CHANGE, onAlarmTimeChange); 18#endif 19} 20 21#if defined(BOARD_HAS_WIFI) 22 WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_PASS); 23#endif 24
arduino_secrets.h
c_cpp
1#include <Arduino_ConnectionHandler.h> 2 3#define THING_ID "CHANGEME" 4#define BOARD_ID "CHANGEME" 5 6/* MKR1000, MKR WiFi 1010 */ 7#if defined(BOARD_HAS_WIFI) 8 #define SECRET_SSID "CHANGEME" 9 #define SECRET_PASS "CHANGEME" 10#endif 11
images.h
c_cpp
1//https://www.mischianti.org/images-to-byte-array-online-converter-cpp-arduino/ 2const unsigned char wifi [] PROGMEM = { 3 // 'wifi, 15x15px 4 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xe0, 0x38, 0x38, 0xe0, 0x0e, 0x4f, 0xe4, 0x3c, 0x78, 5 0x10, 0x10, 0x07, 0xc0, 0x04, 0x40, 0x00, 0x00, 0x03, 0x80, 0x03, 0x80, 0x01, 0x00 6}; 7 8const unsigned char nowifi [] PROGMEM = { 9 // 'nowifi, 15x15px 10 0x03, 0x80, 0x03, 0x80, 0x03, 0x80, 0x0f, 0xe0, 0x3b, 0xb8, 0xe1, 0x0e, 0x4d, 0x64, 0x19, 0x30, 11 0x11, 0x10, 0x07, 0xc0, 0x05, 0x40, 0x00, 0x00, 0x03, 0x80, 0x03, 0x80, 0x01, 0x00 12}; 13 14 15const unsigned char clean [] PROGMEM = { 16 // 'clean, 15x15px 17 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 18 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 19}; 20 21const unsigned char alarmon [] PROGMEM = { 22 // 'alarmon, 15x15px 23 0x00, 0x00, 0x30, 0x18, 0x69, 0x2c, 0x59, 0x34, 0xb6, 0xda, 0xa8, 0x2a, 0xa8, 0x2a, 0xa8, 0x2a, 24 0x08, 0x20, 0x08, 0x20, 0x10, 0x10, 0x30, 0x18, 0x1f, 0xf0, 0x03, 0x80, 0x00, 0x00 25}; 26
Comments
Only logged in users can leave comments
mboretto
0 Followers
•0 Projects
Table of contents
Intro
5
0