NTP demo for cypress Pioneer and Proto Dev kits also reads available analogue sensors

Committer:
reedas
Date:
Fri Jul 03 16:35:46 2020 +0100
Revision:
7:a92448410a8e
Parent:
6:7fb6949e4f7e
Child:
8:4d13f66d6b5f
made pretty

Who changed what in which revision?

UserRevisionLine numberNew contents of line
reedas 7:a92448410a8e 1 #include "GUI.h"
reedas 0:01c8e1277c91 2 #include "mbed.h"
reedas 0:01c8e1277c91 3 #include "mbed_events.h"
reedas 0:01c8e1277c91 4 #include "ntp-client/NTPClient.h"
reedas 0:01c8e1277c91 5
reedas 5:6b62d5cb48e9 6 #undef CY8CKIT_TFT // Light or Temperature?
reedas 0:01c8e1277c91 7 Thread netTimeThreadHandle;
reedas 0:01c8e1277c91 8 WiFiInterface *wifi;
reedas 0:01c8e1277c91 9 #define MBED_CONF_APP_WIFI_SSID "brackenhillc"
reedas 0:01c8e1277c91 10 #define MBED_CONF_APP_WIFI_PASSWORD "1broches"
reedas 0:01c8e1277c91 11
reedas 0:01c8e1277c91 12 /* Reference resistor in series with the thermistor is of 10 KOhm */
reedas 0:01c8e1277c91 13 #define R_REFERENCE (float)(10000)
reedas 0:01c8e1277c91 14
reedas 0:01c8e1277c91 15 /* Beta constant of this thermistor is 3380 Kelvin. See the thermistor
reedas 0:01c8e1277c91 16 (NCP18XH103F03RB) data sheet for more details. */
reedas 0:01c8e1277c91 17 #define B_CONSTANT (float)(3380)
reedas 0:01c8e1277c91 18
reedas 0:01c8e1277c91 19 /* Resistance of the thermistor is 10K at 25 degrees C (from data sheet)
reedas 0:01c8e1277c91 20 Therefore R0 = 10000 Ohm, and T0 = 298.15 Kelvin, which gives
reedas 0:01c8e1277c91 21 R_INFINITY = R0 e^(-B_CONSTANT / T0) = 0.1192855 */
reedas 0:01c8e1277c91 22 #define R_INFINITY (float)(0.1192855)
reedas 0:01c8e1277c91 23
reedas 0:01c8e1277c91 24 /* Zero Kelvin in degree C */
reedas 0:01c8e1277c91 25 #define ABSOLUTE_ZERO (float)(-273.15)
reedas 0:01c8e1277c91 26
reedas 0:01c8e1277c91 27 EventQueue *displayQueue;
reedas 0:01c8e1277c91 28 Semaphore WiFiSemaphore;
reedas 0:01c8e1277c91 29 #define SENSOR_BOARD
reedas 0:01c8e1277c91 30 #ifdef CY8CKIT_TFT
reedas 7:a92448410a8e 31 DigitalOut PwrEnable(P6_2);
reedas 0:01c8e1277c91 32 #ifdef SENSOR_BOARD
reedas 7:a92448410a8e 33 AnalogIn ALS(P10_7); // Sensor Board
reedas 0:01c8e1277c91 34 #else
reedas 7:a92448410a8e 35 AnalogIn ALS(P10_0); // TFT Board
reedas 0:01c8e1277c91 36 #endif
reedas 0:01c8e1277c91 37 static float lightlevel;
reedas 0:01c8e1277c91 38 #else
reedas 7:a92448410a8e 39 #define GUI_ \/\/
reedas 7:a92448410a8e 40 DigitalOut PwrEnable(P10_0);
reedas 7:a92448410a8e 41 DigitalOut ThermGnd(P10_3);
reedas 7:a92448410a8e 42 AnalogIn Thermistor(P10_1);
reedas 0:01c8e1277c91 43 static float TempAverage;
reedas 0:01c8e1277c91 44 #endif
reedas 0:01c8e1277c91 45 /******************************************************************************************
reedas 7:a92448410a8e 46 *
reedas 7:a92448410a8e 47 * Display Functions
reedas 7:a92448410a8e 48 *
reedas 7:a92448410a8e 49 ********************************************************************************************/
reedas 0:01c8e1277c91 50 #define DISP_LEFTMARGIN 10
reedas 0:01c8e1277c91 51 #define DISP_TOPMARGIN 4
reedas 0:01c8e1277c91 52 #define DISP_LINESPACE 2
reedas 0:01c8e1277c91 53 // updateDisplayWiFiStatus
reedas 0:01c8e1277c91 54 // Used to display the wifi status
reedas 7:a92448410a8e 55 void updateDisplayWiFiStatus(char *status) {
reedas 7:a92448410a8e 56 GUI_SetFont(GUI_FONT_16_1);
reedas 7:a92448410a8e 57 GUI_DispStringAt(status, DISP_LEFTMARGIN, DISP_TOPMARGIN);
reedas 7:a92448410a8e 58 printf("%s\r\n", status);
reedas 7:a92448410a8e 59 free(status);
reedas 0:01c8e1277c91 60 }
reedas 0:01c8e1277c91 61 // updateDisplayWiFiConnectAttempts
reedas 0:01c8e1277c91 62 // This function displays the number of attempted connections
reedas 7:a92448410a8e 63 void updateDisplayWiFiConnectAttempts(int count) {
reedas 7:a92448410a8e 64 char buffer[128];
reedas 7:a92448410a8e 65 snprintf(buffer, sizeof(buffer), "WiFi Connect Attempts = %d", count);
reedas 7:a92448410a8e 66 GUI_SetFont(GUI_FONT_16_1);
reedas 7:a92448410a8e 67 GUI_DispStringAt(buffer, DISP_LEFTMARGIN,
reedas 7:a92448410a8e 68 DISP_TOPMARGIN + (GUI_GetFontSizeY() + DISP_LINESPACE));
reedas 0:01c8e1277c91 69 }
reedas 0:01c8e1277c91 70 // updateDisplayNTPFailed
reedas 0:01c8e1277c91 71 // updates the display with the number of time the NTP Server has been called
reedas 0:01c8e1277c91 72 // and got a failed response
reedas 7:a92448410a8e 73 void updateDisplayNTPFailed(void) {
reedas 7:a92448410a8e 74 static int count = 0;
reedas 7:a92448410a8e 75 char buffer[128];
reedas 7:a92448410a8e 76 count = count + 1;
reedas 7:a92448410a8e 77 snprintf(buffer, sizeof(buffer), "NTP Update Failure = %d\n", count);
reedas 7:a92448410a8e 78 GUI_SetFont(GUI_FONT_16_1);
reedas 7:a92448410a8e 79 GUI_DispStringHCenterAt(buffer, LCD_GetXSize() / 2,
reedas 7:a92448410a8e 80 LCD_GetYSize() -
reedas 7:a92448410a8e 81 2 * GUI_GetFontSizeY()); // near the bottom
reedas 0:01c8e1277c91 82 }
reedas 0:01c8e1277c91 83 // updateDisplayNTPCount
reedas 0:01c8e1277c91 84 // updates the display with the number of time the NTP Server has been called
reedas 7:a92448410a8e 85 void updateDisplayNTPCount(void) {
reedas 7:a92448410a8e 86 static int count = 0;
reedas 7:a92448410a8e 87 char buffer[128];
reedas 7:a92448410a8e 88 count = count + 1;
reedas 7:a92448410a8e 89 snprintf(buffer, sizeof(buffer), "NTP Updates = %d\n", count);
reedas 7:a92448410a8e 90 GUI_SetFont(GUI_FONT_16_1);
reedas 7:a92448410a8e 91 GUI_DispStringHCenterAt(buffer, LCD_GetXSize() / 2,
reedas 7:a92448410a8e 92 LCD_GetYSize() -
reedas 7:a92448410a8e 93 GUI_GetFontSizeY()); // near the bottom
reedas 0:01c8e1277c91 94 }
reedas 0:01c8e1277c91 95 // updateDisplayTime
reedas 0:01c8e1277c91 96 // This function updates the time on the screen
reedas 7:a92448410a8e 97 void updateDisplayTime() {
reedas 7:a92448410a8e 98 time_t rawtime;
reedas 7:a92448410a8e 99 struct tm *timeinfo;
reedas 7:a92448410a8e 100 char buffer[128];
reedas 7:a92448410a8e 101 time(&rawtime);
reedas 7:a92448410a8e 102 // rawtime = rawtime - (4*60*60); // UTC - 4hours ... serious hack which only
reedas 7:a92448410a8e 103 // works in summer
reedas 7:a92448410a8e 104 rawtime = rawtime + 3600; // GMT + 1 for BST
reedas 7:a92448410a8e 105 timeinfo = localtime(&rawtime);
reedas 7:a92448410a8e 106 strftime(buffer, sizeof(buffer), " %T ", timeinfo);
reedas 7:a92448410a8e 107 GUI_SetFont(GUI_FONT_32B_1);
reedas 7:a92448410a8e 108 GUI_DispStringHCenterAt(buffer, LCD_GetXSize() / 2,
reedas 7:a92448410a8e 109 LCD_GetYSize() / 2 - GUI_GetFontSizeY() / 2 - 20);
reedas 7:a92448410a8e 110 printf("%s%c[K\r\n", buffer, 0x1b);
reedas 7:a92448410a8e 111 strftime(buffer, sizeof(buffer), " %A ", timeinfo);
reedas 7:a92448410a8e 112 GUI_SetFont(GUI_FONT_32B_1);
reedas 7:a92448410a8e 113 GUI_DispStringHCenterAt(buffer, LCD_GetXSize() / 2,
reedas 7:a92448410a8e 114 LCD_GetYSize() / 2 - GUI_GetFontSizeY() / 2 + 20);
reedas 7:a92448410a8e 115 printf("%s%c[K\r\n", buffer, 0x1b);
reedas 7:a92448410a8e 116 strftime(buffer, sizeof(buffer), " %d/%m/%y ", timeinfo);
reedas 7:a92448410a8e 117 GUI_SetFont(GUI_FONT_32B_1);
reedas 7:a92448410a8e 118 GUI_DispStringHCenterAt(buffer, LCD_GetXSize() / 2,
reedas 7:a92448410a8e 119 LCD_GetYSize() / 2 - GUI_GetFontSizeY() / 2 + 60);
reedas 7:a92448410a8e 120 printf("%s%c[K\r\n", buffer, 0x1b);
reedas 0:01c8e1277c91 121 #ifdef CY8CKIT_TFT
reedas 7:a92448410a8e 122 sprintf(buffer, " Light = %d%c ", int(lightlevel * 100), '%');
reedas 7:a92448410a8e 123 GUI_SetFont(GUI_FONT_32B_1);
reedas 7:a92448410a8e 124 GUI_DispStringHCenterAt(buffer, LCD_GetXSize() / 2,
reedas 7:a92448410a8e 125 LCD_GetYSize() / 2 - GUI_GetFontSizeY() / 2 - 60);
reedas 7:a92448410a8e 126 printf("%s%c[K", buffer, 0x1b);
reedas 0:01c8e1277c91 127 #else
reedas 7:a92448410a8e 128 float temperature =
reedas 7:a92448410a8e 129 (B_CONSTANT /
reedas 7:a92448410a8e 130 (logf((R_REFERENCE / (1 / TempAverage - 1)) / R_INFINITY))) +
reedas 7:a92448410a8e 131 ABSOLUTE_ZERO;
reedas 0:01c8e1277c91 132
reedas 7:a92448410a8e 133 sprintf(buffer, " Temperature = %d.%1d C\r\n", int(temperature),
reedas 7:a92448410a8e 134 int((temperature - (int)temperature) * 10));
reedas 7:a92448410a8e 135 GUI_SetFont(GUI_FONT_32B_1);
reedas 7:a92448410a8e 136 GUI_DispStringHCenterAt(buffer, LCD_GetXSize() / 2,
reedas 7:a92448410a8e 137 LCD_GetYSize() / 2 - GUI_GetFontSizeY() / 2 - 60);
reedas 7:a92448410a8e 138 printf("%s%c[K", buffer, 0x1b);
reedas 0:01c8e1277c91 139 #endif
reedas 7:a92448410a8e 140 printf("%c[4A", 0x1b); // Cursor up 4
reedas 0:01c8e1277c91 141 }
reedas 7:a92448410a8e 142 void readSensors() {
reedas 7:a92448410a8e 143 PwrEnable = 1;
reedas 0:01c8e1277c91 144 #ifdef CY8CKIT_TFT
reedas 7:a92448410a8e 145 static int lcount = 0;
reedas 7:a92448410a8e 146 static float Light[8];
reedas 7:a92448410a8e 147 Light[lcount] = ALS;
reedas 7:a92448410a8e 148 lcount = (lcount + 1) & 0x7;
reedas 7:a92448410a8e 149 if (lcount == 0) {
reedas 0:01c8e1277c91 150 lightlevel = 0;
reedas 7:a92448410a8e 151 for (int i = 0; i < 8; i++)
reedas 7:a92448410a8e 152 lightlevel += Light[i];
reedas 7:a92448410a8e 153 lightlevel = lightlevel / 8;
reedas 0:01c8e1277c91 154 #ifdef SENSOR_BOARD
reedas 7:a92448410a8e 155 lightlevel = 1 - lightlevel;
reedas 7:a92448410a8e 156 #endif
reedas 0:01c8e1277c91 157 #else
reedas 7:a92448410a8e 158 ThermGnd = 0;
reedas 7:a92448410a8e 159 static int lcount = 0;
reedas 7:a92448410a8e 160 static float Therm[8];
reedas 7:a92448410a8e 161 float thermValue;
reedas 7:a92448410a8e 162 float rThermistor;
reedas 7:a92448410a8e 163 thermValue = Thermistor;
reedas 7:a92448410a8e 164 Therm[lcount] = thermValue;
reedas 7:a92448410a8e 165 lcount = (lcount + 1) & 0x7;
reedas 7:a92448410a8e 166 if (lcount == 0) {
reedas 7:a92448410a8e 167 TempAverage = 0;
reedas 7:a92448410a8e 168 for (int i = 0; i < 8; i++)
reedas 7:a92448410a8e 169 TempAverage += Therm[i];
reedas 7:a92448410a8e 170 TempAverage = TempAverage / 8;
reedas 7:a92448410a8e 171 /* rThermistor = (R_REFERENCE/(1 / TempAverage -1 ));
reedas 7:a92448410a8e 172 * temperature =
reedas 7:a92448410a8e 173 * (B_CONSTANT / (logf(rThermistor / R_INFINITY))) + ABSOLUTE_ZERO;
reedas 7:a92448410a8e 174 *
reedas 7:a92448410a8e 175 * temperature = (B_CONSTANT / (logf((R_REFERENCE / ( 1 / TempAverage
reedas 7:a92448410a8e 176 * - 1 )) / R_INFINITY))) + ABSOLUTE_ZERO;
reedas 7:a92448410a8e 177 *
reedas 7:a92448410a8e 178 * printf("Temp is %d\r\n",(int)(temperature*10));
reedas 7:a92448410a8e 179 * for (int i = 0; i < 8; i++)
reedas 7:a92448410a8e 180 * printf("%d ", (int)(Therm[i]*1000));
reedas 7:a92448410a8e 181 * printf("therm=%d\r\n", (int)(thermValue*1000));
reedas 7:a92448410a8e 182 */
reedas 0:01c8e1277c91 183
reedas 0:01c8e1277c91 184 #endif
reedas 0:01c8e1277c91 185 PwrEnable = 0;
reedas 7:a92448410a8e 186 }
reedas 0:01c8e1277c91 187 }
reedas 0:01c8e1277c91 188 /******************************************************************************************
reedas 7:a92448410a8e 189 * NTPTimeThread
reedas 7:a92448410a8e 190 * This thread calls the NTP Timeserver to get the UTC time
reedas 7:a92448410a8e 191 * It then updates the time in the RTC
reedas 7:a92448410a8e 192 * And it updates the display by adding an event to the display queue
reedas 7:a92448410a8e 193 ********************************************************************************************/
reedas 7:a92448410a8e 194 void NTPTimeThread() {
reedas 7:a92448410a8e 195 static time_t old_timestamp = 0, timestamp;
reedas 7:a92448410a8e 196 NTPClient ntpclient(wifi);
reedas 7:a92448410a8e 197 while (1) {
reedas 7:a92448410a8e 198 if (wifi->get_connection_status() == NSAPI_STATUS_GLOBAL_UP) {
reedas 7:a92448410a8e 199 timestamp = ntpclient.get_timestamp();
reedas 7:a92448410a8e 200 if (timestamp < 1593627000) {
reedas 7:a92448410a8e 201 displayQueue->call(updateDisplayNTPFailed);
reedas 7:a92448410a8e 202 printf("faulty timestamp %d\r\n", timestamp);
reedas 7:a92448410a8e 203 } else {
reedas 7:a92448410a8e 204 set_time(timestamp);
reedas 7:a92448410a8e 205 old_timestamp = timestamp;
reedas 7:a92448410a8e 206 displayQueue->call(updateDisplayNTPCount);
reedas 7:a92448410a8e 207 }
reedas 0:01c8e1277c91 208 }
reedas 7:a92448410a8e 209 ThisThread::sleep_for(300s); // Goto the NTP server every 5 minutes
reedas 7:a92448410a8e 210 }
reedas 0:01c8e1277c91 211 }
reedas 0:01c8e1277c91 212 /******************************************************************************************
reedas 7:a92448410a8e 213 *
reedas 7:a92448410a8e 214 * Main & WiFi Thread
reedas 7:a92448410a8e 215 *
reedas 7:a92448410a8e 216 ********************************************************************************************/
reedas 0:01c8e1277c91 217 // wifiStatusCallback
reedas 0:01c8e1277c91 218 // Changes the display when the wifi status is changed
reedas 7:a92448410a8e 219 void wifiStatusCallback(nsapi_event_t status, intptr_t param) {
reedas 7:a92448410a8e 220 const int buffSize = 40;
reedas 7:a92448410a8e 221 char *statusText;
reedas 7:a92448410a8e 222 SocketAddress a;
reedas 7:a92448410a8e 223 statusText = (char *)malloc(buffSize);
reedas 7:a92448410a8e 224 switch (param) {
reedas 7:a92448410a8e 225 case NSAPI_STATUS_LOCAL_UP:
reedas 7:a92448410a8e 226 wifi->get_ip_address(&a);
reedas 7:a92448410a8e 227 snprintf(statusText, buffSize, "WiFi IP = %s", a.get_ip_address());
reedas 7:a92448410a8e 228 break;
reedas 7:a92448410a8e 229 case NSAPI_STATUS_GLOBAL_UP:
reedas 7:a92448410a8e 230 wifi->get_ip_address(&a);
reedas 7:a92448410a8e 231 snprintf(statusText, buffSize, "WiFi IP = %s", a.get_ip_address());
reedas 7:a92448410a8e 232 break;
reedas 7:a92448410a8e 233 case NSAPI_STATUS_DISCONNECTED:
reedas 7:a92448410a8e 234 WiFiSemaphore.release();
reedas 7:a92448410a8e 235 snprintf(statusText, buffSize, "WiFi Disconnected");
reedas 7:a92448410a8e 236 break;
reedas 7:a92448410a8e 237 case NSAPI_STATUS_CONNECTING:
reedas 7:a92448410a8e 238 snprintf(statusText, buffSize, "WiFi Connecting");
reedas 7:a92448410a8e 239 break;
reedas 7:a92448410a8e 240 default:
reedas 7:a92448410a8e 241 snprintf(statusText, buffSize, "Not Supported");
reedas 7:a92448410a8e 242 break;
reedas 7:a92448410a8e 243 }
reedas 7:a92448410a8e 244 displayQueue->call(updateDisplayWiFiStatus, statusText);
reedas 0:01c8e1277c91 245 }
reedas 7:a92448410a8e 246 int main() {
reedas 7:a92448410a8e 247 int wifiConnectionAttempts;
reedas 7:a92448410a8e 248 int ret;
reedas 7:a92448410a8e 249 GUI_Init();
reedas 7:a92448410a8e 250 printf("%c[2J", 0x1b); // Clear Screen
reedas 7:a92448410a8e 251 printf("%c[?25l", 0x1b); // Cursor Off
reedas 7:a92448410a8e 252 displayQueue = mbed_event_queue();
reedas 7:a92448410a8e 253 displayQueue->call_every(1s, &updateDisplayTime);
reedas 7:a92448410a8e 254 displayQueue->call_every(100ms, &readSensors);
reedas 7:a92448410a8e 255 wifi = WiFiInterface::get_default_instance();
reedas 7:a92448410a8e 256 wifi->attach(&wifiStatusCallback);
reedas 0:01c8e1277c91 257
reedas 7:a92448410a8e 258 while (1) {
reedas 7:a92448410a8e 259 wifiConnectionAttempts = 1;
reedas 7:a92448410a8e 260 do {
reedas 7:a92448410a8e 261 ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD,
reedas 7:a92448410a8e 262 NSAPI_SECURITY_WPA_WPA2);
reedas 7:a92448410a8e 263 displayQueue->call(updateDisplayWiFiConnectAttempts,
reedas 7:a92448410a8e 264 wifiConnectionAttempts);
reedas 7:a92448410a8e 265 if (ret != 0) {
reedas 7:a92448410a8e 266 wifiConnectionAttempts += 1;
reedas 7:a92448410a8e 267 ThisThread::sleep_for(
reedas 7:a92448410a8e 268 2s); // If for some reason it doesnt work wait 2s and try again
reedas 7:a92448410a8e 269 }
reedas 7:a92448410a8e 270 } while (ret != 0);
reedas 7:a92448410a8e 271 // If the NTPThread is not running... then start it up
reedas 7:a92448410a8e 272 if (netTimeThreadHandle.get_state() == Thread::Deleted)
reedas 7:a92448410a8e 273 netTimeThreadHandle.start(NTPTimeThread);
reedas 7:a92448410a8e 274 WiFiSemaphore.acquire();
reedas 7:a92448410a8e 275 }
reedas 0:01c8e1277c91 276 }