Components and supplies
Jumper wires (generic)
Breadboard (generic)
Arduino UNO
Alphanumeric LCD, 16 x 2
Arduino Ethernet Shield 2
Apps and platforms
Arduino IDE
Project description
Code
PHP Code
php
The server-side code
1<?php 2function check_email($username, $password) 3{ 4 //url to connect to 5 $url = "https://mail.google.com/mail/feed/atom"; 6 7 // sendRequest 8 $curl = curl_init(); 9 curl_setopt($curl, CURLOPT_URL, $url); 10 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 11 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 12 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 13 curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password); 14 curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 15 curl_setopt($curl, CURLOPT_ENCODING, ""); 16 $curlData = curl_exec($curl); 17 curl_close($curl); 18 19 //returning retrieved feeds 20 return $curlData; 21 22} 23$feeds = check_email("YOUR@EMAIL.ADDRESS", "YOURPASSWORD"); 24 25$xmlobjc = new SimpleXMLElement($feeds); 26 27echo "mail" . $xmlobjc->fullcount[0]; 28?> 29
ArduinoGMailClock
arduino
1/* 2 3 Arduino Gmail Clock 4 5 scritto un pomeriggio del 2019 da FRancesco 6 7 This code is in the public domain. 8 9 */ 10 11#include <SPI.h> 12#include <Ethernet.h> 13#include <EthernetUdp.h> 14#include <LiquidCrystal.h> 15#include <TextFinder.h> 16 17 18byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 19unsigned int localPort = 8888; // porta locale 20 21char timeServer[] = "time.nist.gov"; // server dell'orario 22 23 24const int NTP_PACKET_SIZE = 48; 25 26byte packetBuffer[ NTP_PACKET_SIZE]; 27 28EthernetUDP Udp; 29 30char server[] = "192.168.0.100"; 31IPAddress ip(192, 168, 0, 177); 32IPAddress myDns(192, 168, 0, 1); 33 34EthernetClient client; 35 36TextFinder finder( client ); //inizializza il parsing 37 38unsigned long beginMicros, endMicros; 39unsigned long byteCount = 0; 40bool printWebData = true; 41 42const int rs = 7, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2; 43LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 44 45// variabili di utility 46int refresh = 0; 47String tempo = ""; 48 49void setup() 50{ 51 lcd.begin(16, 2); 52 lcd.print("Caricamento..."); 53 lcd.setCursor(1,1); 54 lcd.print("Caricamento..."); 55 56 57 Serial.begin(9600); 58 while (!Serial) { 59 ; 60 } 61 62 63 // inizializza 64 if (Ethernet.begin(mac) == 0) { 65 Serial.println("Failed to configure Ethernet using DHCP"); 66 for (;;) 67 ; 68 } 69 Udp.begin(localPort); 70} 71 72void loop() 73{ 74 // controllo tempo 75 sendNTPpacket(timeServer); // manda un pacchetto al server e aspetta una risposta dopo 1 secondo 76 77 delay(1000); 78 79 // handling dell'errore di connessione alla rete locale 80 if (Ethernet.begin(mac) == 0) { 81 lcd.clear(); 82 lcd.print("Errore DHCP"); 83 Serial.println("Failed to configure Ethernet using DHCP"); 84 85 if (Ethernet.hardwareStatus() == EthernetNoHardware) { 86 Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); 87 while (true) { 88 delay(1); 89 } 90 } 91 if (Ethernet.linkStatus() == LinkOFF) { 92 Serial.println("Ethernet cable is not connected."); 93 lcd.clear(); 94 lcd.print("Cavo non connesso"); 95 } 96 97 Ethernet.begin(mac, ip, myDns); 98 } else { 99 // la connessione alla LAN avvenuta con successo 100 Serial.print(" DHCP assigned IP "); 101 Serial.println(Ethernet.localIP()); 102 } 103 104 // se ha ricevuto un pacchetto in risposta 105 if ( Udp.parsePacket() ) { 106 Serial.println("pacchetto ricevuto"); 107 lcd.clear(); 108 lcd.setCursor(0,1); 109 lcd.print("Elaborazione..."); 110 111 Udp.read(packetBuffer, NTP_PACKET_SIZE); 112 113 unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); 114 unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); 115 116 unsigned long secsSince1900 = highWord << 16 | lowWord; 117 Serial.print("Seconds since Jan 1 1900 = " ); 118 Serial.println(secsSince1900); 119 120 121 Serial.print("Unix time = "); 122 123 const unsigned long seventyYears = 2208988800UL; 124 125 unsigned long epoch = secsSince1900 - seventyYears; 126 // print Unix time: 127 Serial.println(epoch); 128 129 Serial.print("The UTC time is "); 130 Serial.print((epoch % 86400L) / 3600); 131 tempo = String((epoch % 86400L) / 3600); 132 133 Serial.print(':'); 134 tempo = tempo + ":"; 135 if ( ((epoch % 3600) / 60) < 10 ) { 136 137 Serial.print('0'); 138 tempo = tempo + String(0); 139 } 140 Serial.print((epoch % 3600) / 60); 141 tempo = tempo + String((epoch % 3600) / 60); 142 143 Serial.print(':'); 144 if ( (epoch % 60) < 10 ) { 145 146 Serial.print('0'); 147 //tempo = tempo + ":" + String(0); 148 } 149 Serial.println(epoch % 60); 150 //tempo = tempo + ":" + String(epoch % 60); 151 lcd.clear(); //pulisce lo schermo 152 lcd.setCursor(0,0); //imposta il cursore 153 lcd.print(" " + tempo); // stampa il tempo al centro del display (impostabile anche nel cursore) 154 } 155 tempo = ""; // azzerra la stringa del tempo 156 157 // se le 10 richieste di tempo sono state eseguite procede al controllo delle mail 158 if (refresh == 10) { 159 delay(1000); 160 Serial.print("connecting to "); 161 162 Serial.print(server); 163 Serial.println("..."); 164 165 if (client.connect(server, 80)) { 166 Serial.print("connected to "); 167 Serial.println(client.remoteIP()); 168 // Make a HTTP request: 169 client.println("GET /test.php HTTP/1.1"); 170 client.println("Host: www.google.com"); 171 client.println("Connection: close"); 172 client.println(); 173 char c = client.read(); //responso 174 finder.find("mail"); //trova il valore immediatamente successivo alla parola "mail" 175 long value1 = finder.getValue(); 176 Serial.println(String(value1) + " email non lette"); 177 lcd.setCursor(0,1); 178 lcd.print(String(value1) + " email non lette"); //stampa la stringa 179 } else { 180 // if you didn't get a connection to the server: 181 Serial.println("connection failed"); 182 } 183 beginMicros = micros(); 184 refresh = 0; 185 } 186 187 // wait ten seconds before asking for the time again 188 delay(10000); // richiede il tempo ogni 10 secondi per 10 volte quindi le mail si aggiorneranno dopo 100 secondi 189 refresh++; 190} 191 192//funzione presa da WebClient per mandare i pacchetti udp 193 194unsigned long sendNTPpacket(char* address) 195{ 196 // set all bytes in the buffer to 0 197 memset(packetBuffer, 0, NTP_PACKET_SIZE); 198 // Initialize values needed to form NTP request 199 // (see URL above for details on the packets) 200 packetBuffer[0] = 0b11100011; // LI, Version, Mode 201 packetBuffer[1] = 0; // Stratum, or type of clock 202 packetBuffer[2] = 6; // Polling Interval 203 packetBuffer[3] = 0xEC; // Peer Clock Precision 204 // 8 bytes of zero for Root Delay & Root Dispersion 205 packetBuffer[12] = 49; 206 packetBuffer[13] = 0x4E; 207 packetBuffer[14] = 49; 208 packetBuffer[15] = 52; 209 210 // all NTP fields have been given values, now 211 // you can send a packet requesting a timestamp: 212 Udp.beginPacket(address, 123); //NTP requests are to port 123 213 Udp.write(packetBuffer, NTP_PACKET_SIZE); 214 Udp.endPacket(); 215} 216
PHP Code
php
The server-side code
1<?php 2function check_email($username, $password) 3{ 4 //url 5 to connect to 6 $url = "https://mail.google.com/mail/feed/atom"; 7 8 9 // sendRequest 10 $curl = curl_init(); 11 curl_setopt($curl, CURLOPT_URL, 12 $url); 13 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 14 curl_setopt($curl, 15 CURLOPT_RETURNTRANSFER, 1); 16 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 17 18 curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password); 19 curl_setopt($curl, 20 CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 21 curl_setopt($curl, CURLOPT_ENCODING, ""); 22 23 $curlData = curl_exec($curl); 24 curl_close($curl); 25 26 //returning 27 retrieved feeds 28 return $curlData; 29 30} 31$feeds = check_email("YOUR@EMAIL.ADDRESS", 32 "YOURPASSWORD"); 33 34$xmlobjc = new SimpleXMLElement($feeds); 35 36echo 37 "mail" . $xmlobjc->fullcount[0]; 38?> 39
ArduinoGMailClock
arduino
1/* 2 3 Arduino Gmail Clock 4 5 scritto un pomeriggio del 2019 6 da FRancesco 7 8 This code is in the public domain. 9 10 */ 11 12#include 13 <SPI.h> 14#include <Ethernet.h> 15#include <EthernetUdp.h> 16#include <LiquidCrystal.h> 17#include 18 <TextFinder.h> 19 20 21byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 22unsigned 23 int localPort = 8888; // porta locale 24 25char timeServer[] = "time.nist.gov"; 26 // server dell'orario 27 28 29const int NTP_PACKET_SIZE = 48; 30 31byte 32 packetBuffer[ NTP_PACKET_SIZE]; 33 34EthernetUDP Udp; 35 36char server[] = 37 "192.168.0.100"; 38IPAddress ip(192, 168, 0, 177); 39IPAddress myDns(192, 168, 40 0, 1); 41 42EthernetClient client; 43 44TextFinder finder( client ); //inizializza 45 il parsing 46 47unsigned long beginMicros, endMicros; 48unsigned long byteCount 49 = 0; 50bool printWebData = true; 51 52const int rs = 7, en = 6, d4 = 5, d5 = 53 4, d6 = 3, d7 = 2; 54LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 55 56// variabili 57 di utility 58int refresh = 0; 59String tempo = ""; 60 61void setup() 62{ 63 64 lcd.begin(16, 2); 65 lcd.print("Caricamento..."); 66 lcd.setCursor(1,1); 67 68 lcd.print("Caricamento..."); 69 70 71 Serial.begin(9600); 72 while (!Serial) 73 { 74 ; 75 } 76 77 78 // inizializza 79 if (Ethernet.begin(mac) == 80 0) { 81 Serial.println("Failed to configure Ethernet using DHCP"); 82 for 83 (;;) 84 ; 85 } 86 Udp.begin(localPort); 87} 88 89void loop() 90{ 91 92 // controllo tempo 93 sendNTPpacket(timeServer); // manda un pacchetto al server 94 e aspetta una risposta dopo 1 secondo 95 96 delay(1000); 97 98 // handling 99 dell'errore di connessione alla rete locale 100 if (Ethernet.begin(mac) == 0) { 101 102 lcd.clear(); 103 lcd.print("Errore DHCP"); 104 Serial.println("Failed 105 to configure Ethernet using DHCP"); 106 107 if (Ethernet.hardwareStatus() == 108 EthernetNoHardware) { 109 Serial.println("Ethernet shield was not found. Sorry, 110 can't run without hardware. :("); 111 while (true) { 112 delay(1); 113 114 } 115 } 116 if (Ethernet.linkStatus() == LinkOFF) { 117 Serial.println("Ethernet 118 cable is not connected."); 119 lcd.clear(); 120 lcd.print("Cavo non 121 connesso"); 122 } 123 124 Ethernet.begin(mac, ip, myDns); 125 } else { 126 127 // la connessione alla LAN avvenuta con successo 128 Serial.print(" DHCP 129 assigned IP "); 130 Serial.println(Ethernet.localIP()); 131 } 132 133 // se 134 ha ricevuto un pacchetto in risposta 135 if ( Udp.parsePacket() ) { 136 Serial.println("pacchetto 137 ricevuto"); 138 lcd.clear(); 139 lcd.setCursor(0,1); 140 lcd.print("Elaborazione..."); 141 142 143 Udp.read(packetBuffer, NTP_PACKET_SIZE); 144 145 unsigned long highWord 146 = word(packetBuffer[40], packetBuffer[41]); 147 unsigned long lowWord = word(packetBuffer[42], 148 packetBuffer[43]); 149 150 unsigned long secsSince1900 = highWord << 16 | lowWord; 151 152 Serial.print("Seconds since Jan 1 1900 = " ); 153 Serial.println(secsSince1900); 154 155 156 157 Serial.print("Unix time = "); 158 159 const unsigned long seventyYears 160 = 2208988800UL; 161 162 unsigned long epoch = secsSince1900 - seventyYears; 163 164 // print Unix time: 165 Serial.println(epoch); 166 167 Serial.print("The 168 UTC time is "); 169 Serial.print((epoch % 86400L) / 3600); 170 tempo = 171 String((epoch % 86400L) / 3600); 172 173 Serial.print(':'); 174 tempo = tempo 175 + ":"; 176 if ( ((epoch % 3600) / 60) < 10 ) { 177 178 Serial.print('0'); 179 180 tempo = tempo + String(0); 181 } 182 Serial.print((epoch % 3600) / 183 60); 184 tempo = tempo + String((epoch % 3600) / 60); 185 186 187 Serial.print(':'); 188 if ( (epoch % 60) < 10 ) { 189 190 Serial.print('0'); 191 192 //tempo = tempo + ":" + String(0); 193 } 194 Serial.println(epoch 195 % 60); 196 //tempo = tempo + ":" + String(epoch % 60); 197 lcd.clear(); 198 //pulisce lo schermo 199 lcd.setCursor(0,0); //imposta il cursore 200 lcd.print(" 201 " + tempo); // stampa il tempo al centro del display (impostabile anche nel 202 cursore) 203 } 204 tempo = ""; // azzerra la stringa del tempo 205 206 // se 207 le 10 richieste di tempo sono state eseguite procede al controllo delle mail 208 209 if (refresh == 10) { 210 delay(1000); 211 Serial.print("connecting to "); 212 213 214 Serial.print(server); 215 Serial.println("..."); 216 217 if (client.connect(server, 218 80)) { 219 Serial.print("connected to "); 220 Serial.println(client.remoteIP()); 221 222 // Make a HTTP request: 223 client.println("GET /test.php HTTP/1.1"); 224 225 client.println("Host: www.google.com"); 226 client.println("Connection: 227 close"); 228 client.println(); 229 char c = client.read(); //responso 230 231 finder.find("mail"); //trova il valore immediatamente successivo alla parola 232 "mail" 233 long value1 = finder.getValue(); 234 Serial.println(String(value1) 235 + " email non lette"); 236 lcd.setCursor(0,1); 237 lcd.print(String(value1) 238 + " email non lette"); //stampa la stringa 239 } else { 240 // if you didn't 241 get a connection to the server: 242 Serial.println("connection failed"); 243 244 } 245 beginMicros = micros(); 246 refresh = 0; 247 } 248 249 // wait ten 250 seconds before asking for the time again 251 delay(10000); // richiede il tempo 252 ogni 10 secondi per 10 volte quindi le mail si aggiorneranno dopo 100 secondi 253 254 refresh++; 255} 256 257//funzione presa da WebClient per mandare i pacchetti 258 udp 259 260unsigned long sendNTPpacket(char* address) 261{ 262 // set all bytes 263 in the buffer to 0 264 memset(packetBuffer, 0, NTP_PACKET_SIZE); 265 // Initialize 266 values needed to form NTP request 267 // (see URL above for details on the packets) 268 269 packetBuffer[0] = 0b11100011; // LI, Version, Mode 270 packetBuffer[1] = 0; 271 // Stratum, or type of clock 272 packetBuffer[2] = 6; // Polling Interval 273 274 packetBuffer[3] = 0xEC; // Peer Clock Precision 275 // 8 bytes of zero for Root 276 Delay & Root Dispersion 277 packetBuffer[12] = 49; 278 packetBuffer[13] = 0x4E; 279 280 packetBuffer[14] = 49; 281 packetBuffer[15] = 52; 282 283 // all NTP fields 284 have been given values, now 285 // you can send a packet requesting a timestamp: 286 287 Udp.beginPacket(address, 123); //NTP requests are to port 123 288 Udp.write(packetBuffer, 289 NTP_PACKET_SIZE); 290 Udp.endPacket(); 291} 292
Downloadable files
Schematic
Schematic
Comments
Only logged in users can leave comments
fmattiussi
0 Followers
•0 Projects
Table of contents
Intro
1
0