Hola amigos, les paso mi proyecto que una mezcla de los encontró por la web y con ayuda de otro amigo lo desarrollamos de tal manera que pueda publicar online via Twitter, aquí el código:
#include <TimeLib.h>
#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>
#include <EthernetUdp.h>
#include <Time.h>
String UV = "0";
const long oneSecond = 1000; // a second is a thousand milliseconds
const long oneMinute = oneSecond * 60;
const long halfHour = oneMinute * 30;
const long oneHour = oneMinute * 60;
const long oneDay = oneHour * 24;
const long tenMinute = oneMinute * 10;
// The includion of EthernetDNS is not needed in Arduino IDE 1.0 or later.
// Please uncomment below in Arduino IDE 0022 or earlier.
//#include <EthernetDNS.h>
// Ethernet Shield Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// NTP Servers:
IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
// IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov
//const int timeZone = 1; // Central European Time
const int timeZone = -5; // Eastern Standard Time (USA)
//const int timeZone = -4; // Eastern Daylight Time (USA)
//const int timeZone = -8; // Pacific Standard Time (USA)
//const int timeZone = -7; // Pacific Daylight Time (USA)
EthernetUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets
// If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later).
byte ip[] = { 192, 168, 1, 22 };
//byte ip[] = { };
// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("twittertoken");
// First message to post
char msg[140]; // mensaje de inicio personalizado
char buff[140];
time_t date = 0; // when the digital clock was displayed
void setup() {
// put your setup code here, to run once:
if (!Ethernet.begin(mac)) // Try to connect using DHCP
Ethernet.begin(mac, ip); // Try to connect using a Static IP address.
delay(100);
Serial.begin(9600);
Serial.print(F("Connected to IP address: ")); // Print out the IP address.
for (int i = 0; i < 4; i++)
{
Serial.print(Ethernet.localIP()[i], DEC);
Serial.print(".");
}
Serial.println();
Serial.println();
Serial.println("connecting ...");
Udp.begin(localPort);
Serial.println("waiting for sync");
setSyncProvider(getNtpTime);
if (timeStatus() != timeNotSet) {
if (now() != date) { //update the display only if time has changed
date = now();
}
}
// Personaliza tu mensaje de inicio...
sprintf(msg, "%d/%d/%d - %d horas con %d minutos, @WSILIMALIM7 periodo de pruebas UV Meter.....", day(date), month(date), year(date), hour(date), minute(date));
if (twitter.post(msg)) {
// Specify &Serial to output received response to Serial.
// If no output is required, you can just omit the argument, e.g.
// int status = twitter.wait();
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("connection failed.");
}
}
void loop() {
// put your main code here, to run repeatedly:
if (timeStatus() != timeNotSet) {
if (now() != date) { //update the display only if time has changed
date = now();
}
}
UV = readSensor();
//Serial.println(UV); //enviar índice UV al monitor serial
int uv_int = UV.toInt();
sprintf(buff, "%d/%d/%d - %dhrs y %dmin, Indice UV en el Distrito de Miraflores: %d #ArduinoEthernet", day(date), month(date), year(date), hour(date), minute(date), uv_int);
tweet(buff);
delay(tenMinute); //oneDay...
}
String readSensor()
{
String UVIndex = "0";
int sensorValue = 0;
sensorValue = analogRead(A4); //connect UV sensor to Analog 0
int voltage = (sensorValue * (5.0 / 1023.0))*1000; //Voltage in miliVolts
if(voltage<50)
{
UVIndex = "0";
}else if (voltage>50 && voltage<=227)
{
UVIndex = "0";
}else if (voltage>227 && voltage<=318)
{
UVIndex = "1";
}
else if (voltage>318 && voltage<=408)
{
UVIndex = "2";
}else if (voltage>408 && voltage<=503)
{
UVIndex = "3";
}
else if (voltage>503 && voltage<=606)
{
UVIndex = "4";
}else if (voltage>606 && voltage<=696)
{
UVIndex = "5";
}else if (voltage>696 && voltage<=795)
{
UVIndex = "6";
}else if (voltage>795 && voltage<=881)
{
UVIndex = "7";
}
else if (voltage>881 && voltage<=976)
{
UVIndex = "8";
}
else if (voltage>976 && voltage<=1079)
{
UVIndex = "9";
}
else if (voltage>1079 && voltage<=1170)
{
UVIndex = "10";
}else if (voltage>1170)
{
UVIndex = "11";
}
return UVIndex;
}
void tweet(char msg[]) {
Serial.println("connecting ...");
if (twitter.post(msg)) {
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
} else {
Serial.print("failed : code ");
Serial.println(status);
}
} else {
Serial.println("connection failed.");
}
}
/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime()
{
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
Hasta hace un tiempo atrás todo funcionaba de maravilla, pero ahora tengo el siguiente error Dropbox - Captura de pantalla 2018-04-02 12.16.05.png - Simplify your life, me sale como fecha 0/0/1970 y horas y minutos en cero (0).
Cuando entro al Monitor Serie, tengo el siguiente error y no se como resolverlo:
Connected to IP address: 192.168.1.22.
connecting ...
waiting for sync
Transmit NTP Request
No NTP Response :-(
HTTP/1.0 403 Forbidden
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
X-Cloud-Trace-Context: 8dfc24537334dff17591413e21a889c3
Date: Mon, 02 Apr 2018 17:03:44 GMT
Server: Google Frontend
Content-Length: 73
Error 403 - {"errors":[{"code":187,"message":"Status is a duplicate."}]}
failed : code 403
connecting ...
HTTP/1.0 403 Forbidden
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
X-Cloud-Trace-Context: a21e8625771fa581e247f283b625ae15;o=1
Date: Mon, 02 Apr 2018 17:03:45 GMT
Server: Google Frontend
Content-Length: 73
Error 403 - {"errors":[{"code":187,"message":"Status is a duplicate."}]}
failed : code 403
Por favor si alguien me pudiera ayudar, se los voy a agradecer bastante, mil gracias
PD: el proyecto original del UV meter es: https://www.instructables.com/id/DIY-UV-Meter-With-Arduino-and-a-Nokia-5110-Display/