I have created my own dynamic dns service. My code works when I send data to WS inside setup() function, but not when I create a its own function. I need to run it at startup and periodically to keep my dynamic ip updated. I have tried Interrupts, SimpleTimer and millis control. Any help is welcomed.
#include <SPI.h>
#include <Ethernet.h>
#include <SimpleTimer.h>
#include "DHT.h"
#define DHTPIN A7 // pino que estamos conectado
#define DHTTYPE DHT11 // DHT 11
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,1,177); //Define o endereco IP
IPAddress gateway(192,168,1,1); //Define o gateway
IPAddress subnet(255, 255, 255, 0); //Define a máscara de rede
char webServer[] = "www.myserver.com"; // webservice for current ethernet ip
// port you want to use
EthernetServer server(4162);
// web service to receive data
EthernetClient wsClient;
// the sensor temperature and humidity object
DHT dht(DHTPIN, DHTTYPE);
// the timer object
SimpleTimer timer;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
delay(10);
dht.begin();
timer.setInterval(10000, noIPService);
}
void loop() {
// start timer
timer.run();
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 60"); // refresh the page automatically every 60 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
float h = dht.readHumidity();
float t = dht.readTemperature();
client.print("Temperatura: ");
client.print(t);
client.print("C");
client.println("
");
client.print("Humidade: ");
client.print(h);
client.print("%");
client.println("
");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(10);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
void noIPService(){
// get current ip
if (wsClient.connect(webServer, 80)){
Serial.println("Connected to web service");
delay(1500);
// Make a HTTP request:
wsClient.println("GET /myip.asp HTTP/1.1");
wsClient.println("Host: www.myserver.com");
wsClient.println("Connection: close");
wsClient.println();
} else {
Serial.println("No web service available");
}
if (wsClient.available())
{
char c = wsClient.read();
Serial.println(c);
}
}