With ESP8266 send data to fhem

Begonnen von kroonen, 05 März 2015, 18:53:11

Vorheriges Thema - Nächstes Thema

Porky666

Go Robin go.

Und dann alles in dein meintechblog.de.
Großartig diese Seite.

Gruß Stefan

ODROID U3 1GB Ubuntu immer aktuell
FHEM immer das aktuellste Development
Defined modules:

COC; CULv3; HMLAN :HM-CC-SCD,HM-CC-TC,HM-LC-SW4-PCB,HM-LC-Sw1PBU-FM,HM-OU-LED16,HM-SCI-3-FM,HM-SEC-SC,HM-SEC-WIN,HM-WDS10-TH-O; ESA2000; FS20; HUEBridge; Huedevices; IT; JeeLink :PCA301 :panstamp:

fh168

i will test this. i have a dth here, but not today.
Hue, Lacrosse, PCA301, MySensors, V 1.67 CUL 868 V3.4, Lacrosse-WLAN-Gateway, Tasmota RF-

fh168

@stefan, no i will write a short statement my own blog.
But the interesting part of the 8266 is the sleep mode, because at the moment i takes too much current.

Robin
Hue, Lacrosse, PCA301, MySensors, V 1.67 CUL 868 V3.4, Lacrosse-WLAN-Gateway, Tasmota RF-

kroonen

Yes we must now look at deep sleep, but I think it's not easy on an esp-01. I think needed is the esp8266-12 for that.

Goal: dht22 with deep sleep fully battery powered push data to fhem every 1 minutes.

fh168

Hello kroonen,

i used your code in my blog with a few modifications. I hope its okay.

http://blog.moneybag.de/fhem-temperaturmessung-mit-dem-esp-8266-und-ds18b20-ohne-arduino-datenuebertragung-per-wlan/

@Stefan. Du kannst den Beitrag abschreiben und bei dir im blog setzen.

LG
/robin
Hue, Lacrosse, PCA301, MySensors, V 1.67 CUL 868 V3.4, Lacrosse-WLAN-Gateway, Tasmota RF-

kroonen

No problem, mostly I also copied from others.

Let me know if it is up for a week or so, to see how stable it is.

regards Richard

Porky666

Hi,
The Site meintechblog.de is not mine,
This Site belongs to another User from FhemForum.

Thx
Stefan
ODROID U3 1GB Ubuntu immer aktuell
FHEM immer das aktuellste Development
Defined modules:

COC; CULv3; HMLAN :HM-CC-SCD,HM-CC-TC,HM-LC-SW4-PCB,HM-LC-Sw1PBU-FM,HM-OU-LED16,HM-SCI-3-FM,HM-SEC-SC,HM-SEC-WIN,HM-WDS10-TH-O; ESA2000; FS20; HUEBridge; Huedevices; IT; JeeLink :PCA301 :panstamp:

kroonen

Hi,

I'm also busy with the dht22, but the temp and humidity are not stable, any ideas are welcome. I used a dht22 with resistor 4.7k and without, and also tried this schema

http://www.esp8266.com/viewtopic.php?f=19&t=1363

output in log file

2015-03-09_13:16:31 Testtemp T: 21.9 H: 42.2
2015-03-09_13:17:01 Testtemp T: 24.3 H: 44.2
2015-03-09_13:17:31 Testtemp T: 21.3 H: 43.7
2015-03-09_13:18:01 Testtemp T: 21 H: 45
2015-03-09_13:18:31 Testtemp T: 20.8 H: 45
2015-03-09_13:19:01 Testtemp T: 20.6 H: 45.5
2015-03-09_13:19:31 Testtemp T: 20.4 H: 45.8
2015-03-09_13:20:01 Testtemp T: 20.4 H: 46.2
2015-03-09_13:20:31 Testtemp T: 20.3 H: 46.6
2015-03-09_13:21:01 Testtemp T: 20.2 H: 46.8
2015-03-09_13:21:31 Testtemp T: 20.7 H: 47.4
2015-03-09_13:22:01 Testtemp T: 20 H: 47.1
2015-03-09_13:22:31 Testtemp T: 20 H: 47.4
2015-03-09_13:23:01 Testtemp T: 19.9 H: 47.5
2015-03-09_13:23:31 Testtemp T: 20.7 H: 48.1
2015-03-09_13:24:01 Testtemp T: 20 H: 48.1
2015-03-09_13:24:31 Testtemp T: 19.8 H: 47.8
2015-03-09_13:25:01 Testtemp T: 21 H: 49.2
2015-03-09_13:25:31 Testtemp T: 19.8 H: 47.9
2015-03-09_13:26:01 Testtemp T: 20.7 H: 48.5


using this config

laster dht22.lua from node mcu , the init.la like above for ds18b20

https://github.com/nodemcu/nodemcu-firmware/tree/master/lua_modules/dht22

and fhem.lua

tmr.alarm(0,30000, 1, function()
PIN = 4 --  data pin, GPIO2

dht22 = require("dht22")
dht22.read(PIN)
t = dht22.getTemperature()
h = dht22.getHumidity()
humi=dht22.getHumidity()/10
temp=dht22.getTemperature()/10
if h == nil then
  print("Error reading from DHT22")
else
  -- temperature in degrees Celsius  and Farenheit
  -- floating point and integer version:
  print("Temperature: "..((t-(t % 10)) / 10).."."..(t % 10).." deg C")
  -- only integer version:
  print("Temperature: "..(9 * t / 50 + 32).."."..(9 * t / 5 % 10).." deg F")
  -- only float point version:
  print("Temperature: "..(9 * t / 50 + 32).." deg F")

  -- humidity
  -- floating point and integer version
  print("Humidity: "..((h - (h % 10)) / 10).."."..(h % 10).."%")
end

-- release module
dht22 = nil
package.loaded["dht22"]=nil


conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end )
conn:connect(8083,"192.168.180.5")
conn:send('GET /fhem?cmd=setreading%20Testtemp%20state%20T:%20' ..temp.. '%20H:%20'..humi..'\r\n HTTP/1.1\r\nHost: www.local.lan\r\n".."Connection: keep-alive\r\nAccept: */*\r\n\r\n"')
end)

kroonen

Got it stable. The dht22 must be connected to the 5v , than it works

2015-03-09_14:29:04 Testtemp T: 19.6 H: 48.5
2015-03-09_14:29:34 Testtemp T: 19.6 H: 48.4
2015-03-09_14:33:04 Testtemp T: 19.6 H: 48.3
2015-03-09_14:33:34 Testtemp T: 19.6 H: 48.4
2015-03-09_14:34:04 Testtemp T: 19.6 H: 48.3
2015-03-09_14:34:34 Testtemp T: 19.7 H: 48.5
2015-03-09_14:35:04 Testtemp T: 19.6 H: 48.4
2015-03-09_14:35:34 Testtemp T: 19.6 H: 48.3
2015-03-09_14:37:04 Testtemp T: 19.7 H: 48.4
2015-03-09_14:37:34 Testtemp T: 19.6 H: 48.3
2015-03-09_14:38:04 Testtemp T: 19.7 H: 48.4
2015-03-09_14:39:04 Testtemp T: 19.6 H: 48.3
2015-03-09_14:39:34 Testtemp T: 19.7 H: 48.4
2015-03-09_14:40:04 Testtemp T: 19.6 H: 48.3
2015-03-09_14:41:04 Testtemp T: 19.7 H: 48.4
2015-03-09_14:41:34 Testtemp T: 19.6 H: 48.3
2015-03-09_14:42:04 Testtemp T: 19.7 H: 48.4
2015-03-09_14:46:34 Testtemp T: 19.7 H: 48.3
2015-03-09_14:47:04 Testtemp T: 19.7 H: 48.4
2015-03-09_14:47:34 Testtemp T: 19.7 H: 48.3
2015-03-09_14:48:04 Testtemp T: 19.7 H: 48.4
2015-03-09_14:48:34 Testtemp T: 19.7 H: 48.2
2015-03-09_14:49:04 Testtemp T: 19.7 H: 48.3
2015-03-09_14:49:34 Testtemp T: 19.7 H: 48
2015-03-09_14:50:04 Testtemp T: 19.7 H: 48.2

fh168

thank you kroonen, i will give it a try :-)

Robin
Hue, Lacrosse, PCA301, MySensors, V 1.67 CUL 868 V3.4, Lacrosse-WLAN-Gateway, Tasmota RF-

chanky

Nice work guys :) With your help i've got my ESP8266-201 running. I used arduino to program the ESP8266 instead of lua, and interfacing to the fhem i used your solution. Hat off to Kroonen many thanks.

kroonen

@Chunky,

Can you also post the arduino code. It's based on de arduino ide?

regards Richard

chanky

Yes of course:#include <OneWire.h>
#include <ESP8266WiFi.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire  ds(0);  // on pin 2 (a 4.7K resistor is necessary)

const char* ssid     = "XX-XXXXXX";
const char* password = "XXXXXXXXXXXXXXX";
const char* host     = "10.0.0.15";

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

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
int value = 0;

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
 
  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
 
  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();

  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
 
  delay(60000);     // 1 Minute delay
 
  present = ds.reset();
  ds.select(addr);   
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");
  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 8083;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  // We now create a URI for the request
  String url = "/fhem?cmd=setreading%20esp8266temp%20state%20";
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  // This will send the request to the server
  client.print(String("GET ") + url + celsius + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Authorization: Basic Rsdfsfdsno6MDYwMg==\r\n" +
               "Connection: keep-alive\r\n" +
               "Accept: */*\r\n\r\n");
  delay(10);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
}



It's chanky btw ;-)

cs-online

Hello,

is there a way to use Telnet-port 7270 with user and Password ?

Greetings Christian
FHEM auf RPI 4 4GB, HM-WLAN-Gateway, einige HM-Aktoren,2x EBUSD an Heizung+Solar, ESP8266 am Strom-,Gas-,Wasserzähler, in WLAN-Steckdosen und Relaisleisten, Sonoff S20, Shelly1,2 und 2.5,Lacrosse-Gateway und Sensoren,Sduino,Alexa-Fhem,Huawei PV mit Speicher, alles auf einem RPI und da geht noch mehr

JoWiemann

Jörg Wiemann

Slave: RPi B+ mit 512 MB, COC (868 MHz), CUL V3 (433.92MHz SlowRF); FHEMduino, Aktuelles FHEM

Master: CubieTruck; Debian; Aktuelles FHEM