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

Committer:
reedas
Date:
Fri Jul 03 15:21:43 2020 +0100
Revision:
5:6b62d5cb48e9
Parent:
4:6f3c2a46cec2
Child:
6:7fb6949e4f7e
testing proto

Who changed what in which revision?

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