Can't refresh sensors

3 years 3 months ago #3155 by Adamyno
The problem solved. I'm using Arduino 1.6.7, UIPEthernet by ntruchsess V1.50.0.
Using client.stop(); solves the problem.

Here is the good, working source:
#include <UIPEthernet.h>
#include <SPI.h>
#include <Base64.h>
#include <OneWire.h>
#include <DallasTemperature.h>


#define EIOT_USERNAME F("admin")
#define EIOT_PASSWORD F("test")
#define EIOT_IP_ADDRESS "192.168.1.100"
#define EIOT_PORT 8080
#define EIOT_NODE F("N20S0")

#define REPORT_INTERVAL 60 // in sec


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


#define USER_PWD_LEN 40
char unameenc[USER_PWD_LEN];
float temp;
float oldTemp;
byte s = 5;

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;
  sensors.begin();
  
  
}

void loop() {
 
  
  do {  
    delay(50);
    sensors.requestTemperatures();
    temp = sensors.getTempCByIndex(0);
    Serial.print(F("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(F("Connecting"));
  Ethernet.begin(mac, ip, myDns, gateway, subnet); {
    delay(500);
    Serial.print(".");
  }

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


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

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

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

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

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

  Serial.println();
  client.stop();
  Serial.println(F("Connection closed"));
}

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

Time to create page: 0.182 seconds