Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Ethernet.h"
- #include "SPI.h"
- #include "WebServer.h"
- #include <MasterUtils.h>
- MasterUtils utils;
- /****************************************************/
- /* CONFIGURAZIONE */
- /****************************************************/
- /* Parametri di rete */
- static byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- static byte ip[] = { 10, 0, 1, 100 };
- WebServer webserver("", 80);
- /* Parametri No-IP */
- EthernetClient client;
- char noIpServer[] = "dynupdate.no-ip.com";
- String noIpHostServer = "dynupdate.no-ip.com"; //Uguale a noIpServer
- String auth64 = "Y29tdW5pY2FyZWl0YTp6ZXJvY29vbDEzMDM4Mw==";
- String noIpHostToUpdate = "arduino.no-ip.org";
- String noIpEmailAgent = "marco.stranieri@me.com";
- /****************************************************/
- /* FINE CONFIGURAZIONE */
- /****************************************************/
- template<class T>
- inline Print &operator <<(Print &obj, T arg)
- {
- obj.print(arg);
- return obj;
- }
- void outputJson(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
- server.httpSuccess("application/json");
- server << "{";
- server << "\"temperature\": " << utils.checkTemperature(0,200) << ", ";
- server << "\"humidity\": 47, ";
- server << "\"notification\": " << "1" << ", ";
- server << "\"notification-list\": {";
- server << "\"0\":" << "\"Sensori attivati correttamente\"";
- server << "}";
- server << " }";
- utils.logger("Dati jSon rilevati correttamente.");
- delay(1000);
- }
- boolean updateIpAddress() {
- if(client.connect(noIpServer, 80)) {
- client.println("GET /nic/update?hostname=" + noIpHostToUpdate + " HTTP/1.0");
- client.println("Host: " + noIpHostServer);
- client.println("Authorization: Basic " + auth64);
- client.println("User-Agent: arduino_ethernet/1.0 " + noIpEmailAgent);
- client.println();
- Serial.println("################## NO IP ################## ");
- Serial.println("GET /nic/update?hostname=" + noIpHostToUpdate + " HTTP/1.0");
- Serial.println("Host: " + noIpHostServer);
- Serial.println("Authorization: Basic " + auth64);
- Serial.println("User-Agent: arduino_ethernet/1.0 " + noIpEmailAgent);
- Serial.println("################## NO IP ################## ");
- Serial.println();
- return true;
- } else {
- return false;
- }
- }
- void readNoIpResponse() {
- if (client.available()) {
- char c = client.read();
- Serial.print(c);
- }
- if (!client.connected()) {
- Serial.println();
- Serial.println("Disconnessione da No-IP.");
- client.stop();
- while(true);
- }
- }
- void setup() {
- if(utils.initialize())
- utils.logger("Sistema avviato con successo!");
- Ethernet.begin(mac, ip);
- delay(2000); // Attendo due secondi per essere sicuro che la rete sia inizializzata.
- if(updateIpAddress()) {
- utils.logger("Aggiornamento a No-IP effettuato.");
- } else {
- utils.logger("Aggiornamento ip statico fallito.");
- }
- webserver.begin();
- webserver.setDefaultCommand(&outputJson);
- }
- void loop() {
- readNoIpResponse();
- webserver.processConnection();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement