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

Committer:
reedas
Date:
Wed Jul 01 16:21:41 2020 +0100
Revision:
2:a92d7ea0f3e3
Parent:
1:352a27b3a1ec
Child:
3:9d9e29b54a1a
testing

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