NRF: receive, ntp, Data to Sd-card working

Dependencies:   F7_Ethernet mbed BSP_DISCO_F746NG Test_Mainboard SDFileSystem RF24

Committer:
leo44
Date:
Tue Jun 08 10:06:19 2021 +0000
Revision:
0:d984976f1f1c
Child:
1:6210d492bda7
proto

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leo44 0:d984976f1f1c 1 #include "mbed.h"
leo44 0:d984976f1f1c 2 #include "SDFileSystem.h"
leo44 0:d984976f1f1c 3 #include "stdio.h"
leo44 0:d984976f1f1c 4 #include "rtos.h"
leo44 0:d984976f1f1c 5 #include "EthernetInterface.h"
leo44 0:d984976f1f1c 6 #include "NTPClient.h"
leo44 0:d984976f1f1c 7
leo44 0:d984976f1f1c 8
leo44 0:d984976f1f1c 9
leo44 0:d984976f1f1c 10
leo44 0:d984976f1f1c 11
leo44 0:d984976f1f1c 12 EthernetInterface eth;
leo44 0:d984976f1f1c 13 DigitalOut myled(LED1);
leo44 0:d984976f1f1c 14 SDFileSystem sd("sd");
leo44 0:d984976f1f1c 15
leo44 0:d984976f1f1c 16 // trim '\n'
leo44 0:d984976f1f1c 17 void ntrim(char *str)
leo44 0:d984976f1f1c 18 {
leo44 0:d984976f1f1c 19 int i;
leo44 0:d984976f1f1c 20 for (i = 0; str[i] != 0; ++i);
leo44 0:d984976f1f1c 21
leo44 0:d984976f1f1c 22 if (i > 0 && str[i - 1] == '\n')
leo44 0:d984976f1f1c 23 str[i - 1] = 0;
leo44 0:d984976f1f1c 24 }
leo44 0:d984976f1f1c 25
leo44 0:d984976f1f1c 26
leo44 0:d984976f1f1c 27 int main() {
leo44 0:d984976f1f1c 28
leo44 0:d984976f1f1c 29 int temp_array[1]; // Nur zum Test
leo44 0:d984976f1f1c 30 temp_array[0]= 123;
leo44 0:d984976f1f1c 31 temp_array[1]= 124;
leo44 0:d984976f1f1c 32
leo44 0:d984976f1f1c 33
leo44 0:d984976f1f1c 34
leo44 0:d984976f1f1c 35
leo44 0:d984976f1f1c 36
leo44 0:d984976f1f1c 37 sd.mount();
leo44 0:d984976f1f1c 38 FILE *fp;
leo44 0:d984976f1f1c 39 NTPClient ntp;
leo44 0:d984976f1f1c 40 char buff[64];
leo44 0:d984976f1f1c 41
leo44 0:d984976f1f1c 42 if(eth.init()!=0) //for DHCP Server
leo44 0:d984976f1f1c 43 {
leo44 0:d984976f1f1c 44 //if(eth.init(IP,MASK,GATEWAY)!=0) { //for Static IP Address
leo44 0:d984976f1f1c 45 printf("EthernetInterface Initialize Error \r\n");
leo44 0:d984976f1f1c 46
leo44 0:d984976f1f1c 47 while (1)
leo44 0:d984976f1f1c 48 {
leo44 0:d984976f1f1c 49 }
leo44 0:d984976f1f1c 50 }
leo44 0:d984976f1f1c 51 if(eth.connect()!=0)
leo44 0:d984976f1f1c 52 {
leo44 0:d984976f1f1c 53 printf("EthernetInterface Connect Error \r\n");
leo44 0:d984976f1f1c 54 while (1)
leo44 0:d984976f1f1c 55 {
leo44 0:d984976f1f1c 56 }
leo44 0:d984976f1f1c 57 }
leo44 0:d984976f1f1c 58
leo44 0:d984976f1f1c 59
leo44 0:d984976f1f1c 60 int x;
leo44 0:d984976f1f1c 61 for (x=1; x<6;x++){
leo44 0:d984976f1f1c 62
leo44 0:d984976f1f1c 63
leo44 0:d984976f1f1c 64 fp = fopen("/sd/test.txt", "a");
leo44 0:d984976f1f1c 65
leo44 0:d984976f1f1c 66 if (fp == NULL)
leo44 0:d984976f1f1c 67 {
leo44 0:d984976f1f1c 68 printf("open error!!\r\n");
leo44 0:d984976f1f1c 69 while(1);
leo44 0:d984976f1f1c 70 }
leo44 0:d984976f1f1c 71
leo44 0:d984976f1f1c 72
leo44 0:d984976f1f1c 73 //////////////////////////////////////// Ethernet connection for timestamp////////////////////////////////////////////////////////////////
leo44 0:d984976f1f1c 74 printf("Durchlauf\n");
leo44 0:d984976f1f1c 75
leo44 0:d984976f1f1c 76 // printf("IP Address is %s\r\n", eth.getIPAddress());
leo44 0:d984976f1f1c 77 // printf("NetMask is %s\r\n", eth.getNetworkMask());
leo44 0:d984976f1f1c 78 // printf("Gateway Address is %s\r\n", eth.getGateway());
leo44 0:d984976f1f1c 79 // printf("Ethernet Setup OK\r\n");
leo44 0:d984976f1f1c 80 // printf("Getting time, 10s timeout. \r\n");
leo44 0:d984976f1f1c 81
leo44 0:d984976f1f1c 82
leo44 0:d984976f1f1c 83
leo44 0:d984976f1f1c 84 wait(1);
leo44 0:d984976f1f1c 85 if (ntp.setTime("0.uk.pool.ntp.org") == 0)
leo44 0:d984976f1f1c 86 {
leo44 0:d984976f1f1c 87 time_t ctTime;
leo44 0:d984976f1f1c 88 ctTime = time(NULL);
leo44 0:d984976f1f1c 89 fprintf(fp,"Time:%s \r", ctime(&ctTime));
leo44 0:d984976f1f1c 90 fprintf(fp,"Temp:%i; ", temp_array[0]);
leo44 0:d984976f1f1c 91 printf("Hole Uhrzeit\n");
leo44 0:d984976f1f1c 92 }
leo44 0:d984976f1f1c 93 else
leo44 0:d984976f1f1c 94 {
leo44 0:d984976f1f1c 95 printf("Error getting time \r\n");
leo44 0:d984976f1f1c 96 }
leo44 0:d984976f1f1c 97 fclose (fp);
leo44 0:d984976f1f1c 98
leo44 0:d984976f1f1c 99
leo44 0:d984976f1f1c 100 } // for end
leo44 0:d984976f1f1c 101
leo44 0:d984976f1f1c 102
leo44 0:d984976f1f1c 103 fp = fopen("/sd/test.txt", "r");
leo44 0:d984976f1f1c 104 if (fp == NULL)
leo44 0:d984976f1f1c 105 {
leo44 0:d984976f1f1c 106 printf("open error!!\r\n");
leo44 0:d984976f1f1c 107 while(1);
leo44 0:d984976f1f1c 108 }
leo44 0:d984976f1f1c 109 // read text file
leo44 0:d984976f1f1c 110 char buf[1024];
leo44 0:d984976f1f1c 111 while (fgets(buf, sizeof(buf), fp) != NULL)
leo44 0:d984976f1f1c 112 {
leo44 0:d984976f1f1c 113 ntrim(buf);
leo44 0:d984976f1f1c 114 printf("%s\r\n", buf);
leo44 0:d984976f1f1c 115 }
leo44 0:d984976f1f1c 116
leo44 0:d984976f1f1c 117 // file close
leo44 0:d984976f1f1c 118 fclose(fp);
leo44 0:d984976f1f1c 119
leo44 0:d984976f1f1c 120
leo44 0:d984976f1f1c 121
leo44 0:d984976f1f1c 122 }