/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Contacts:
Andrea Esposito
andrea.espostio@studenti.uniparthenope.it
*/
//librerie per la ethernet shield
#include
#include
//librerie per la macro del rest forzato
#include
#include
#define Reset_AVR() wdt_enable(WDTO_30MS); while(1) {}
//librerie per l'lcd
#include "Wire.h"
#include "LiquidCrystal.h"
//buffer di memorizzazione della risposta delle get
#define bufflen 305
char buffer[bufflen];
//MAC address
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client; //oggetto client (ethernet shield)
IPAddress server(192,167,9,33); //ipaddress del server sebeto
//oggetto liquiudcrystal 0 per i2c
LiquidCrystal lcd(0);
void setup() {
lcd.begin(16, 2); //inizializzazione dell'lcd
lcd.print("Starting...");//printa messaggio diagnostico
Serial.begin(9600);//inizilizzazione della porta seriale (viene usata solo a fini diagnostici)
if (Ethernet.begin(mac) == 0) {//inizializzo l'ethernet shield
Serial.println("Failed to configure Ethernet using DHCP");
//se viene inizializzata male viene resettata
Serial.println("Resetting AVR...");
Reset_AVR();//soft reset
}
delay(1000);//delay per l'inizializzazione ethernet
}
void loop()
{
myConnect(); //connessione al server e relativa get
parse(); //parsing della risposta
disconnect(); //disconnessione
stampa(); //stampa su lcd
}
void myConnect()
{
short count=0;
//se NON connesso tenta 3 volte la connessione
//se dopo i 3 tentativi NON riesce ad instaurare una connessione: resetta tutto
while (!client.connected()){
Serial.println();
Serial.println();
Serial.println("*************************");
if(count!=0)
{
Serial.print("Try: "); //stampa il tentativo numero...
Serial.println(count);
}
Serial.println("Reconnect...");
client.stop(); //stoppa connessione
Serial.println("connection stopped");
client.connect(server,80); //riconnette
delay(1000);
if (client.connected()) //se connessa bene
Serial.println("CONNECTED!!!");
else //altrimenti male :)
Serial.println("...connection lost.");
if (count>3) //se ci son stati più di tre tentativi di connessione resetta l'hw
{
// digitalWrite(9, HIGH); // set the LED on
// delay(1000); // wait for a second
// digitalWrite(9, LOW); // set the LED off
Serial.println("Resetting AVR...");
Reset_AVR(); //reset
}
Serial.println("*************************");
count++;
}
//se siamo riusciti a connetterci...
if (client.connected()) {
Serial.println();
Serial.println("*--------------------------------*");
Serial.println("GET command..."); //comango get!
client.println("GET /~0108000935/weather/api.php?rquest=detail_station&station_id=3 HTTP/1.1");
//Importante: nello standar 1.1 è necessario inserire anche la riga host
client.println("Host: students.uniparthenope.it");
client.println(); //sempre una linea blank dopo la get
Serial.println("Get terminated...");
}
else {
// if you didn't get a connection to the server:
client.stop();
Serial.println("connection failed");
Serial.println("*--------------------------------*");
}
}
void parse()
{
Serial.println("Parsing");
while (client.connected()){
if ((client.available()) && client.find("{")) //appena troviamo la prima parentesi aperta iniziamo la lettura e memorizzazione
{
Serial.println("Meteo trovato!");
client.readBytes(buffer, bufflen); //memorizza la risposta nel buffer
Serial.println("Avviso: ");
Serial.println(buffer); //lo stampiamo sulla seriale
}
}
}
void myPrint() //print diagnostica di tutta la risposta get
{
char c;
Serial.println("Simple print...");
while (client.connected()){
if (client.available())
{
c = client.read();
Serial.print(c);
}
}
}
void disconnect() //disconnessione
{
if (client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}
void stampa()
{
Serial.println("stampa su lcd..");
int i=0,j=0;
lcd.clear();
char* id[]={
"CT","MT","TW","AP","HI","CWS","WD","DMW","MR","RH","LD","NT" }; //array di parametri
//lcd.autoscroll();
lcd.setBacklight(HIGH);
lcd.setCursor(0, 0);
lcd.print("ID stazione:");//stampa statica della stazione
lcd.setCursor(0, 1);
lcd.print("3");//stazione numero tre (napoli?)
delay(2000);
lcd.clear();
/* FUNZIONANTE - scorrimento orizzontale della risposta*/
// da migliorare :)
// lcd.setCursor(0, 0);
// lcd.print("Output: ");
// lcd.setCursor(0, 1);
// while (buffer[i]!='}')
// {
// lcd.print(buffer[i]); //stampo l'iesimo elemento del buffer
// delay(200); //ogni 200ms
// i++;
// if ((i%16)==0) { //il buffer dell'lcd è di 40 caratteri, in questo caso ogni 16 caratteri pulisco il buffer
// lcd.clear();
// lcd.setCursor(0, 0); //e riposizione tutte le scritte ed i cursori
// lcd.print("Output: ");
// lcd.setCursor(0,1);
// }
// }
/* Da migliorare */
while (buffer[i]!='}') //finche non arrivo alla fine del carattere di chiusura della risposta...
{
if (buffer[i]==':') //appena trovo un parametro preceduto dal carattere :
{
lcd.clear(); //pulisco l'lcd
lcd.setCursor(0,0);
lcd.print(id[j]); //stamp il nome del parametro
lcd.setCursor(0,1);
j++; //vado al parametro successivo
}
lcd.print(buffer[i+3]); //stampa del contenuto del buffer
delay(100);
i++;
}
i++;
}