Advertisement
mstranieri

Arduino

Mar 24th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. #include "SD.h"
  2. #include "Ethernet.h"
  3. #include "SPI.h"
  4. #include "WebServer.h"
  5.  
  6. File MioFile;
  7.  
  8. template<class T>
  9. inline Print &operator <<(Print &obj, T arg)
  10. {
  11.   obj.print(arg);
  12.   return obj;
  13. }
  14.  
  15. static byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  16. static byte ip[] = { 10, 0, 1, 100 };
  17. #define PREFIX ""
  18. WebServer webserver(PREFIX, 80);
  19.  
  20. void outputJson(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
  21.  
  22.   server.httpSuccess("application/json");
  23.  
  24.   if (type == WebServer::HEAD)
  25.     return;
  26.  
  27.   server << "{";
  28.   server << "\"temperature\": " << checkTemperature(0) << ", ";
  29.   server << " }";
  30. }
  31.  
  32. void initSD() {
  33.   if (SD.begin(4) == true) {
  34.     if (SD.exists("conf/conf.txt")) {
  35.       MioFile = SD.open("conf/conf.txt", FILE_READ);
  36.       if (MioFile == true) {
  37.         while (MioFile.available())
  38.         {
  39.           Serial.write(MioFile.read());
  40.         }
  41.  
  42.         MioFile.close();
  43.       }
  44.     }
  45.     else {
  46.       SD.mkdir("conf");
  47.       MioFile = SD.open("conf/conf.txt", FILE_WRITE);
  48.  
  49.       if (MioFile != false)
  50.         MioFile.println("empty.");
  51.     }
  52.   }
  53.   else {
  54.     Serial.println("Errore nella lettura della SD Card.");
  55.   }
  56. }
  57.  
  58. float checkTemperature(int tempPin) {
  59.   float temp;
  60.   temp = analogRead(tempPin);
  61.   temp = temp * 0.48828125;
  62.   delay(200);
  63.   return temp;
  64. }
  65.  
  66. void setup() {
  67.   Serial.begin(9600);
  68.   //updateIpAddress();
  69.   initSD();
  70.  
  71.   Ethernet.begin(mac, ip);
  72.   webserver.begin();
  73.   webserver.setDefaultCommand(&outputJson);
  74. }
  75.  
  76. void loop() {
  77.   //float temp = checkTemperature(0);
  78.   webserver.processConnection();
  79.   //checkIpAddress();
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement