Arduino Ethernet Shield + OpenWRT LEDE

Hello

Im trying to setup temp sensor to report to a webserver on internet via POST request. I want to put it in a remote location so I have setup Openwrt router with a 3G modem so it has access to internet

So far :

  • I got arduino to work with sensor and shield. It works flawlessly with DHCP when its connected to my home internet over DSL
  • I have setup the Openwrt+3G and internet works flawlessly on my laptop connected to it.

Problem is when Arduino is connected to Openwrt the http request doesnt go outside of network. Ethernet gets connected and recieves IP adress. The sketch doesnt throw any errors. I’m lost as I have tried doing the same request with curl from routers console and it works…

I have found the way to debug from arduino level very limited. How can I check what dns it recieved from router ? Is there someway to recover what response it gets.

I have checked the server http server logs and there is no call from router visible inside logs so i rule out errors on server side.
I have studied some dns logs but they are not conclusive , at the bottom sketch from arduino.
Any more I could test or debug whats wrong ?

dns logs the .208 ip is arduino . Localhost is curl from router.

Wed Jan 31 22:14:59 2018 daemon.info dnsmasq[1646]: 130 192.168.1.208/1026 query[A] www.one.xxx.xxx.pl from 192.168.1.208
Wed Jan 31 22:14:59 2018 daemon.info dnsmasq[1646]: 130 192.168.1.208/1026 cached www.one.xxx.xxx.pl is <CNAME>
Wed Jan 31 22:14:59 2018 daemon.info dnsmasq[1646]: 130 192.168.1.208/1026 cached one.xxx.xxx.pl is 212.xx.xxx.114
Wed Jan 31 22:15:46 2018 daemon.info dnsmasq[1646]: 131 127.0.0.1/50655 query[A] www.one.xxx.xxx.pl from 127.0.0.1
Wed Jan 31 22:15:46 2018 daemon.info dnsmasq[1646]: 131 127.0.0.1/50655 cached www.one.xxx.xxx.pl is <CNAME>
Wed Jan 31 22:15:46 2018 daemon.info dnsmasq[1646]: 131 127.0.0.1/50655 cached one.xxx.xxx.pl is 212.xx.xxx.114
Wed Jan 31 22:15:46 2018 daemon.info dnsmasq[1646]: 132 127.0.0.1/50655 query[AAAA] www.one.xxx.xxx.pl from 127.0.0.1
Wed Jan 31 22:15:46 2018 daemon.info dnsmasq[1646]: 132 127.0.0.1/50655 cached www.one.xxx.xxx.pl is <CNAME>
Wed Jan 31 22:15:46 2018 daemon.info dnsmasq[1646]: 132 127.0.0.1/50655 forwarded www.one.xxx.xxx.pl to 8.8.4.4
Wed Jan 31 22:15:46 2018 daemon.info dnsmasq[1646]: 132 127.0.0.1/50655 forwarded www.one.xxx.xxx.pl to 8.8.8.8
Wed Jan 31 22:15:46 2018 daemon.info dnsmasq[1646]: 132 127.0.0.1/50655 forwarded www.one.xxx.xxx.pl to 194.204.159.1
Wed Jan 31 22:15:46 2018 daemon.info dnsmasq[1646]: 132 127.0.0.1/50655 forwarded www.one.xxx.xxx.pl to 194.204.152.34
Wed Jan 31 22:15:46 2018 daemon.info dnsmasq[1646]: 132 127.0.0.1/50655 reply www.one.xxx.xxx.pl is <CNAME>
Wed Jan 31 22:15:46 2018 daemon.info dnsmasq[1646]: 132 127.0.0.1/50655 reply one.xxx.xxx.pl is NODATA-IPv6
#include <DHT.h>
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress dnServer(192, 168, 0, 1);
IPAddress ip(192, 168, 1, 200);
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(192, 168, 1, 1);

EthernetClient client;

#define DHTPIN 2 // SENSOR PIN
#define DHTTYPE DHT22 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);

long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 50000; // READING INTERVAL

float t = 0;    // TEMPERATURE VAR
float h = 0;    // HUMIDITY VAR
String data;

void setup() {
    Serial.begin(115200);

    Ethernet.begin(mac, ip, dnServer, gateway, subnet);

    dht.begin();
    delay(10000); // GIVE THE SENSOR SOME TIME TO START

    h = (float) dht.readHumidity();
    t = (float) dht.readTemperature();

    data = "";
}

void loop(){

    currentMillis = millis();
    if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
        previousMillis = currentMillis;
        h = (float) dht.readHumidity();
        t = (float) dht.readTemperature();
    Serial.println(t);
    }

    data = "temp1=" + String(t) + "&hum1=" + String(h);

    if (client.connect("www.xxxx.pl",80)) { // REPLACE WITH YOUR SERVER ADDRESS
        client.println("POST /add.php HTTP/1.1");
        client.println("Host: www.xxxxxx.pl"); // SERVER ADDRESS HERE TOO
        client.println("Content-Type: application/x-www-form-urlencoded");
        client.print("Content-Length: ");
        client.println(data.length());
        client.println();
        client.print(data);
    }

    if (client.connected()) {
        client.stop();    // DISCONNECT FROM THE SERVER
    }

    delay(60000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}

Maybe you should read the response from the server. This has no timeout, so a connection fail will cause a lockup, but you can work on that later.

// replace this
    if (client.connected()) {
        client.stop();    // DISCONNECT FROM THE SERVER


// with this
   while(client.connected()) {
      while(client.available()) {
         char ch = client.read();
         Serial.write(ch);
      }
   }

   client.stop();

Thank you for your reply. When I changed what you suggest it started to work by itself :S

One thing that is not right is that the loop is set every 60000ms but the data at server appears every 150seconds so something is still dodgy but it works.

The response is

HTTP/1.1 302
Date: Thu, 01 Feb 2018 16:28:39 GMT
Content-Type: text/html
Content-Length: 165
Connection: keep-alive
Location: index.php
Server: IdeaWebServer/v0.80
Status: 302

<HTML>
<HEAD>

 <TITLE>302 Moved</TITLE>

</HEAD>
<BODY BGCOLOR=#FFFFFF>

 <H1>302 Moved</H1>
 The document has moved <A HREF="index.php">here</A>.

</BODY>
</HTML>

Sorry I reply to myself. Thanks very much for your reply it helped a lot. I think the real issue was Connection : keep-alive.

I added

Connection :close

to my header in arduino and now it appears to work properly.

I don't think maintaining open connection makes much sense here, i think this approach is idiot proof now if something internet/server/power goes down for a while.

Any other suggestions you would make so its fool proof when I leave it in my lonely home for winter :)