Can't refresh sensors

8 years 9 months ago #3132 by Adamyno
I'm using ENC28J60, EasyIoT V0.9 Win
When I made a Dallas 18B20 Temerature sensor and arduino, it works.
Only the one thing: I had to name the parameter: Sensor.Temperature, otherwise it doesn't works.

Now, I try to add a DHT22 sensor. Arduino sends data to server, but It seems to the server does not receive the packages.

Serial output is:
"Lan connected
POST Humidity data to URL: /Api/EasyIoT/Control/Module/Virtual/N20S0/ControlLevel/38.00

Connection closed

"

So that is good with Dallas Sensor:





Serial output:
"Connecting.
Lan connected
POST data to URL: /Api/EasyIoT/Control/Module/Virtual/N6S0/ControlLevel/24.50

Connection closed

"

Here are the sources:
#include <UIPEthernet.h>
#include <SPI.h>
#include <Base64.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define EIOT_USERNAME "admin"
#define EIOT_PASSWORD "test"
#define EIOT_IP_ADDRESS "192.168.1.100"
#define EIOT_PORT 8080
#define EIOT_NODE "N6S0"

#define REPORT_INTERVAL 30 // in sec


#define ONE_WIRE_BUS 3 // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);


#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
float oldTemp;

byte mac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
IPAddress ip(192, 168, 1, 170);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

void setup() {

  Serial.begin(115200);

  lanConnect();

  char uname[USER_PWD_LEN];
  String str = String(EIOT_USERNAME) + ":" + String(EIOT_PASSWORD);
  str.toCharArray(uname, USER_PWD_LEN);
  memset(unameenc, 0, sizeof(unameenc));
  base64_encode(unameenc, uname, strlen(uname));
  oldTemp = -1;
}

void loop() {
  float temp;

  do {
    DS18B20.requestTemperatures();
    temp = DS18B20.getTempCByIndex(0);
    Serial.print("Temperature: ");
    Serial.println(temp);
  } while (temp == 85.0 || temp == (-127.0));

  if (temp != oldTemp)
  {
    sendTeperature(temp);
    oldTemp = temp;
  }

  int cnt = REPORT_INTERVAL;

  while (cnt--)
    delay(1000);
}

void lanConnect()
{
  EthernetClient client;
  Serial.print("Connecting");
  Ethernet.begin(mac, ip, myDns, gateway, subnet); {
    delay(1000);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("Lan connected");
}


void sendTeperature(float temp)
{
  EthernetClient client;
  while (!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
    Serial.println("connection failed");
    lanConnect();
  }

  String url = "";
  url += "/Api/EasyIoT/Control/Module/Virtual/" + String(EIOT_NODE) + "/ControlLevel/" + String(temp); // generate EasIoT server node URL

  Serial.print("POST data to URL: ");
  Serial.println(url);

  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_IP_ADDRESS) + "\r\n" +
               "Connection: close\r\n" +
               "Authorization: Basic " + unameenc + " \r\n" +
               "Content-Length: 0\r\n" +
               "\r\n");

  delay(2500);
  while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("Connection closed");
}
#include <UIPEthernet.h>
#include <SPI.h>
#include <DHT.h>
#include <Base64.h>

#define EIOT_USERNAME "admin"
#define EIOT_PASSWORD "test"
#define EIOT_IP_ADDRESS "192.168.1.100"
#define EIOT_PORT 8080
#define EIOT_NODE_1 "N7S0"
#define EIOT_NODE_2 "N20S0"

#define REPORT_INTERVAL 15

#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
float oldTemp;
float oldHumid;

// DHT Sensor Setup
#define DHTPIN 3 // We have connected the DHT to Digital Pin 2
#define DHTTYPE DHT22 // This is the type of DHT Sensor (Change it to DHT11 if you're using that model)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT object

byte mac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
IPAddress ip(192, 168, 1, 170);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

void lanConnect()
{
  EthernetClient client;
  Serial.print("Connecting");
  Ethernet.begin(mac, ip, myDns, gateway, subnet); {
    delay(1000);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("Lan connected");
}

void setup() {

// Open serial communications and wait for port to open:
Serial.begin(115200);
dht.begin();
delay(10);
lanConnect();

char uname[USER_PWD_LEN];
String str = String(EIOT_USERNAME)+":"+String(EIOT_PASSWORD);
str.toCharArray(uname, USER_PWD_LEN);
memset(unameenc,0,sizeof(unameenc));
base64_encode(unameenc, uname, strlen(uname));
oldTemp = -1;
oldHumid = -1;
}

float h, t;

void loop() {



do {
t = dht.readTemperature();
h = dht.readHumidity();
Serial.print("Temperature: ");
Serial.println(t);
Serial.print("Humidity: ");
Serial.println(h);
} while (t == 85.0 || t == (-127.0));

if (t != oldTemp)
{
sendTemp(t);
oldTemp = t;
}

if (h != oldHumid)
{
sendHumidity(h);
oldHumid = h;
}


int cnt = REPORT_INTERVAL;

while(cnt--)
delay(1000);
}
void sendTemp(float t) {
EthernetClient client;

while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");
lanConnect();
}

String url = "";
url += "/Api/EasyIoT/Control/Module/Virtual/"+ String(EIOT_NODE_1) + "/ControlLevel/"+String(t); // generate EasIoT server node URL

Serial.print("POST Temp data to URL: ");
Serial.println(url);

client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + String(EIOT_IP_ADDRESS) + "\r\n" +
"Connection: close\r\n" +
"Authorization: Basic " + unameenc + " \r\n" +
"Content-Length: 0\r\n" +
"\r\n");

delay(2500);
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("Connection closed");
}
void sendHumidity(float h) {
EthernetClient client;

while(!client.connect(EIOT_IP_ADDRESS, EIOT_PORT)) {
Serial.println("connection failed");
lanConnect();
}

String url = "";
url += "/Api/EasyIoT/Control/Module/Virtual/"+ String(EIOT_NODE_2) + "/ControlLevel/"+String(h); // generate EasIoT server node URL

Serial.print("POST Humidity data to URL: ");
Serial.println(url);

client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + String(EIOT_IP_ADDRESS) + "\r\n" +
"Connection: close\r\n" +
"Authorization: Basic " + unameenc + " \r\n" +
"Content-Length: 0\r\n" +
"\r\n");

delay(2500);
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("Connection closed");
}

What could be the problem with DHT? :unsure:

Please Log in or Create an account to join the conversation.

8 years 9 months ago #3136 by NightOne
Replied by NightOne on topic Can't refresh sensors
What do you see when you connect to the arduino serial..... Do this to make sure that the DHT is reading Values....

You should see:

Temperature: (then some value)
Humidity: (then some value).

If DHT is not working these will be blank.

You can "force" you code to make sure its reading the DHT sensor by adding the following:

In your void loop

do {
delay(2000);
t = dht.readTemperature();
h = dht.readHumidity();
Serial.print("Temperature: ");
Serial.println(t);
Serial.print("Humidity: ");
Serial.println(h);
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
} while (t == 85.0 || t == (-127.0));

The 2000 delay at the beginning is more for the cheaper DHT11 sensor as the sensor needs time to stabalise before the read....but leaving it in for the DHT22 wont hurt.

Please Log in or Create an account to join the conversation.

8 years 9 months ago #3138 by Adamyno
Replied by Adamyno on topic Can't refresh sensors
Yes, DHT works. I will see with Wireshark, what happening on the network. I think, my ethernet module is not reliable becouse when I leave it with Dallas temperature sensor, after some minutes or hours it doesnt works. Tomorrow I will have a Raspberry Pi, maybe it will be more reliable the server also. ( that doesn't run any other programs like my win ). So.. lets see what happens :)

Please Log in or Create an account to join the conversation.

8 years 9 months ago #3139 by Adamyno
Replied by Adamyno on topic Can't refresh sensors
I found something. The source code has many strings, what uses more RAM. I use F("String") method to transmit static strings to Flash. Before I had 72% used of RAM, after now it is only 63%, so the remaining is free for variables. It is true for Dallas Temp sensor.

With DHT22 I used the same procedure, and the used RAM is 62%. Now it works, but after some minutes the ethernet POST messaging stops. I don't know why. I think that may be due to memory fragmentation. I will try with an Arduino Mega,
I am curious what will happen.

Please Log in or Create an account to join the conversation.

8 years 9 months ago #3140 by Adamyno
Replied by Adamyno on topic Can't refresh sensors
I know what was the problem.
Now I use Uno and Ethernet Shield. I have more more free RAM and it works correctly. The problem is the UIPEthernet library or the ENC28J60 module. In the future I will detect that. Problem solved, but I don't know what is the problem with ENC28J60. There are another librarie(s), I will try.

Please Log in or Create an account to join the conversation.

8 years 9 months ago - 8 years 9 months ago #3141 by NightOne
Replied by NightOne on topic Can't refresh sensors
I see a lot of people on the internet have the same problem with ENC28J60 freezing or stop working over time....
What you can try is to get it to disconnect from the network while it is not updating.... something like ethernet,stop()
I have this on my ESP8266 but that is more to save power.....
When you want to post temp and humidity just call lanconnect again and then drop the connection afterwards with ethernet.stop()

You can also try this library:

github.com/muanis/arduino-projects/tree/master/libraries
The following user(s) said Thank You: Adamyno

Please Log in or Create an account to join the conversation.

Time to create page: 0.260 seconds

Forum latest

  • No posts to display.