Advertisement
mstranieri

Arduino Domotic

Mar 24th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. #include "Ethernet.h"
  2. #include "SPI.h"
  3. #include "WebServer.h"
  4. #include <MasterUtils.h>
  5.  
  6. MasterUtils utils;
  7.  
  8. /****************************************************/
  9. /*         CONFIGURAZIONE                           */
  10. /****************************************************/
  11.  
  12. /* Parametri di rete */
  13. static byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  14. static byte ip[] = { 10, 0, 1, 100 };
  15. WebServer webserver("", 80);
  16.  
  17. /* Parametri No-IP */
  18. EthernetClient client;
  19. char noIpServer[] = "dynupdate.no-ip.com";
  20. String noIpHostServer = "dynupdate.no-ip.com"; //Uguale a noIpServer
  21. String auth64 = "Y29tdW5pY2FyZWl0YTp6ZXJvY29vbDEzMDM4Mw==";
  22. String noIpHostToUpdate = "arduino.no-ip.org";
  23. String noIpEmailAgent = "marco.stranieri@me.com";
  24.  
  25. /****************************************************/
  26. /*        FINE CONFIGURAZIONE                       */
  27. /****************************************************/
  28.  
  29. template<class T>
  30. inline Print &operator <<(Print &obj, T arg)
  31. {
  32.   obj.print(arg);
  33.   return obj;
  34. }
  35.  
  36. void outputJson(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
  37.   server.httpSuccess("application/json");
  38.  
  39.   server << "{";
  40.   server << "\"temperature\": " << utils.checkTemperature(0,200) << ", ";
  41.   server << "\"humidity\": 47, ";
  42.   server << "\"notification\": " << "1" << ", ";
  43.   server << "\"notification-list\": {";
  44.   server << "\"0\":" << "\"Sensori attivati correttamente\"";
  45.   server << "}";
  46.   server << " }";
  47.  
  48.   utils.logger("Dati jSon rilevati correttamente.");
  49.   delay(1000);
  50. }
  51.  
  52. boolean updateIpAddress() {
  53.   if(client.connect(noIpServer, 80)) {
  54.     client.println("GET /nic/update?hostname=" + noIpHostToUpdate + " HTTP/1.0");
  55.     client.println("Host: " + noIpHostServer);
  56.     client.println("Authorization: Basic " + auth64);
  57.     client.println("User-Agent: arduino_ethernet/1.0 " + noIpEmailAgent);
  58.     client.println();
  59.    
  60.     Serial.println("################## NO IP ################## ");
  61.     Serial.println("GET /nic/update?hostname=" + noIpHostToUpdate + " HTTP/1.0");
  62.     Serial.println("Host: " + noIpHostServer);
  63.     Serial.println("Authorization: Basic " + auth64);
  64.     Serial.println("User-Agent: arduino_ethernet/1.0 " + noIpEmailAgent);
  65.     Serial.println("################## NO IP ################## ");
  66.     Serial.println();
  67.  
  68.     return true;
  69.   } else {
  70.     return false;
  71.   }
  72. }
  73.  
  74. void readNoIpResponse() {
  75.   if (client.available()) {
  76.       char c = client.read();
  77.       Serial.print(c);
  78.   }
  79.  
  80.   if (!client.connected()) {
  81.     Serial.println();
  82.     Serial.println("Disconnessione da No-IP.");
  83.     client.stop();
  84.  
  85.     while(true);
  86.   }
  87. }
  88.  
  89. void setup() {
  90.   if(utils.initialize())
  91.     utils.logger("Sistema avviato con successo!");
  92.  
  93.   Ethernet.begin(mac, ip);
  94.  
  95.   delay(2000); // Attendo due secondi per essere sicuro che la rete sia inizializzata.
  96.  
  97.   if(updateIpAddress()) {
  98.     utils.logger("Aggiornamento a No-IP effettuato.");
  99.   } else {
  100.     utils.logger("Aggiornamento ip statico fallito.");
  101.   }
  102.    
  103.   webserver.begin();
  104.   webserver.setDefaultCommand(&outputJson);
  105. }
  106.  
  107. void loop() {
  108.   readNoIpResponse();
  109.   webserver.processConnection();
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement