test

Dependencies:   F7_Ethernet mbed BSP_DISCO_F746NG SDFileSystem RF24

main.cpp

Committer:
leo44
Date:
2021-06-08
Revision:
1:6210d492bda7
Parent:
0:d984976f1f1c
Child:
3:1c6da30ca347

File content as of revision 1:6210d492bda7:

#include "mbed.h"
#include "SDFileSystem.h"
#include "stdio.h"
#include "rtos.h"
#include "EthernetInterface.h"
#include "NTPClient.h"





EthernetInterface eth;
DigitalOut myled(LED1);
SDFileSystem sd("sd");

// trim '\n'
void ntrim(char *str)
{
    int i;
    for (i = 0; str[i] != 0; ++i);

    if (i > 0 && str[i - 1] == '\n')
        str[i - 1] = 0;
}


int main() { 
    
    int temp_array[1]; // Nur zum Test
    temp_array[0]= 123; // Nur zum Test
    temp_array[1]= 124;// Nur zum Test
    
    
    
    
    
    sd.mount(); 
    FILE *fp;
    NTPClient ntp;
    char buff[64];
    
        if(eth.init()!=0)                    //for DHCP Server
    {
        //if(eth.init(IP,MASK,GATEWAY)!=0) { //for Static IP Address
        printf("EthernetInterface Initialize Error \r\n");

        while (1)
        {
           fprintf(fp,"EthernetInterface Initialize Error \r\n");
        }
    }
    if(eth.connect()!=0)
    {
        printf("EthernetInterface Connect Error \r\n");
        while (1)
        {
            fprintf(fp,"EthernetInterface Connect Error \r\n");
        }
    }
    
        
    int x;
     for (x=1; x<6;x++){
     
     
     
     
       fp = fopen("/sd/test.txt", "a");
        
        if (fp == NULL)
        {
            printf("open error!!\r\n");
            while(1);
        }

        
//////////////////////////////////////// Ethernet connection for timestamp////////////////////////////////////////////////////////////////   
  //  printf("IP Address is %s\r\n", eth.getIPAddress());
  //  printf("NetMask is %s\r\n", eth.getNetworkMask());
  //  printf("Gateway Address is %s\r\n", eth.getGateway());
  //  printf("Ethernet Setup OK\r\n");
  //  printf("Getting time, 10s timeout. \r\n");
    
    
    
    //if (ntp.setTime("1.pool.ntp.org") == 0)
    if (ntp.setTime("ntp0.freenet.de") == 0)
    {
        time_t ctTime;
        ctTime = time(0);
        fprintf(fp,"Temperatur:%i; %s" , temp_array[0], ctime(&ctTime));
        
    }
    else
    {
        printf("Error getting time \r\n");
    }    
     fclose (fp); 
   
        
    } // for end
   
    
        fp = fopen("/sd/test.txt", "r");
        if (fp == NULL)
        {
            printf("open error!!\r\n");
            while(1);
        }
        // read text file
        char buf[1024];
        while (fgets(buf, sizeof(buf), fp) != NULL)
        {
            ntrim(buf);
            printf("%s\r\n", buf);
        }

        // file close
        fclose(fp);
        
}