HT-TEAM / Mbed 2 deprecated Main_ntp_sd_nrf

Dependencies:   F7_Ethernet mbed BSP_DISCO_F746NG Test_Mainboard SDFileSystem RF24

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "stdio.h"
00004 #include "rtos.h"
00005 #include "EthernetInterface.h"
00006 #include "NTPClient.h"
00007 
00008 
00009 #include "RF24.h"
00010 const uint64_t  ADDRESS = 0xF0F0F0F0F0F0F001L;
00011 
00012 RF24            radio(A2, A3, A4, D4, D2);    // SPI5 mosi, miso, sck, csn, ce (irq unused) (PINS DISCO)
00013 
00014 struct MyData 
00015 {
00016     uint8_t  board;
00017     uint8_t counter;
00018     uint8_t temperature;
00019 
00020 };
00021 
00022 MyData data;
00023 
00024 
00025 
00026 EthernetInterface eth;
00027 DigitalOut myled(LED1);
00028 SDFileSystem sd("sd");
00029 
00030 // trim '\n'
00031 void ntrim(char *str)
00032 {
00033     int i;
00034     for (i = 0; str[i] != 0; ++i);
00035 
00036     if (i > 0 && str[i - 1] == '\n')
00037         str[i - 1] = 0;
00038 }
00039 
00040 
00041 int main(void) { 
00042     
00043 
00044     if (!radio.begin()) {
00045         printf("Failed to initialize nRF24L01. Check whether connected.\r\n");
00046         return -1;  // Exit the program
00047     }
00048     radio.setPALevel(RF24_PA_LOW);
00049     radio.setRetries(5, 15);
00050     radio.setPayloadSize(sizeof(MyData));
00051     radio.setAutoAck(true);
00052     radio.openReadingPipe(0, ADDRESS);  // use pipe 0 of this slave to receive messsages and send back auto acknowledge
00053     radio.startListening();
00054     
00055     
00056     
00057     sd.mount(); 
00058     FILE *fp;
00059     NTPClient ntp;
00060  //   char buff[64];
00061     
00062         if(eth.init()!=0)                    //for DHCP Server
00063     {
00064         //if(eth.init(IP,MASK,GATEWAY)!=0) { //for Static IP Address
00065         printf("EthernetInterface Initialize Error \r\n");
00066 
00067         while (1)
00068         {
00069            fprintf(fp,"EthernetInterface Initialize Error \r\n");
00070         }
00071     }
00072     if(eth.connect()!=0)
00073     {
00074         printf("EthernetInterface Connect Error \r\n");
00075         while (1)
00076         {
00077             fprintf(fp,"EthernetInterface Connect Error \r\n");
00078         }
00079     }
00080            
00081     
00082     fp = fopen("/sd/test.txt", "a");
00083 
00084     fprintf(fp,"board: , counter, temperature, time, \r\n");
00085     
00086     fclose (fp); 
00087 
00088        
00089                 
00090          while (1) {
00091         
00092  
00093          
00094             if (radio.available()) {
00095             radio.read(&data, sizeof(MyData)); // read message and send acknowledge back to the master
00096             //led = payload;
00097              
00098              printf("------------------------\r\n");
00099              printf("Received from Board: %d \r\n", data.board);
00100 
00101              printf( "counter: %d \r\n", data.counter);
00102              printf( "temperature: %d \r\n", data.temperature);
00103              printf( "board: %d \r\n", data.board);
00104              printf("------------------------\r\n\n");
00105 
00106             
00107         
00108     
00109      
00110      
00111        fp = fopen("/sd/test.txt", "a");
00112      
00113        
00114         if (fp == NULL)
00115         {
00116             printf("open error!!\r\n");
00117             while(1);
00118         }
00119 
00120         
00121 //////////////////////////////////////// Ethernet connection for timestamp////////////////////////////////////////////////////////////////   
00122   //  printf("IP Address is %s\r\n", eth.getIPAddress());
00123   //  printf("NetMask is %s\r\n", eth.getNetworkMask());
00124   //  printf("Gateway Address is %s\r\n", eth.getGateway());
00125   //  printf("Ethernet Setup OK\r\n");
00126   //  printf("Getting time, 10s timeout. \r\n");
00127     
00128     
00129     
00130     //if (ntp.setTime("1.pool.ntp.org") == 0)
00131     if (ntp.setTime("ntp0.freenet.de") == 0)
00132     {
00133         time_t ctTime;
00134         ctTime = time(0);
00135         fprintf(fp,"%i, %i, %i, " , data.board, data.counter, data.temperature);
00136         fprintf(fp,"%s" , ctime(&ctTime));
00137         
00138     }
00139     else
00140     {
00141         printf("Error getting time \r\n");
00142     }    
00143      fclose (fp); 
00144    
00145         
00146     } 
00147     
00148    
00149     /*
00150         fp = fopen("/sd/test.txt", "r");
00151         if (fp == NULL)
00152         {
00153             printf("open error!!\r\n");
00154             while(1);
00155         }
00156         // read text file
00157         char buf[1024];
00158         while (fgets(buf, sizeof(buf), fp) != NULL)
00159         {
00160             ntrim(buf);
00161             printf("%s\r\n", buf);
00162         }
00163 
00164 
00165         // file close
00166         fclose(fp);
00167         
00168         */
00169         
00170         }
00171         
00172 }
00173