Interesting ESP8266 WiFi Module Topic is solved

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Interesting ESP8266 WiFi Module

Post by ThinkPad »

Another ESP8266 (in combination with Arduino) project i finally got working is this one:

http://gizmosnack.blogspot.nl/2014/10/p ... -hack.html
http://gizmosnack.blogspot.nl/2014/11/p ... eless.html
(not my blog)

A cheap power consumption meter that wirelessly sends the actual power consumption of a device to Domoticz!
Data on the SPI-bus between CPU and display is being sniffed by an Arduino. Getting that to work in my own setup was just copy-paste his code.
But i wanted it wireless, with an ESP8266! And this evening i finally got it to work!

It now wirelessly sends the actual power consumption of a device where it is plugged in between to a virtual sensor in Domoticz, very awesome!

See my Arduino code here: http://pastebin.com/G6dArPfv
Still needs a quite amount of love though, far from perfect. Connecting to wifi isn't even working, i did that by hand (luckily the ESP remembers a network).

The Arduino is already being powered from the meter' own supply (78L05). But that powersupply doesn't have enough juice to also power the ESP8266, so i have to come up with something smart.
I am not active on this forum anymore.
BigDog
Posts: 82
Joined: Tuesday 17 September 2013 13:59
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.9269
Location: The Netherlands
Contact:

Re: Interesting ESP8266 WiFi Module

Post by BigDog »

i have been reading this tread but isnt the ESP8266 WiFi Module not in the way of the Wifi network ?
i have read that the 2.4 megahertz freq already is to crowded
would it be better to work with other freq?
just thinking out loud :)

Greetz BigDog / Bob
1X Raspberry4B : Domoticz Version 2023.1 [Linux 5.10.63-v7l+ armv7l]
1X Conbee II : 2.25.3 - 26720700
1X RFXtrx433 USB Firmware:183
1X Mysensors Gateway 1.5 -3
6X ESP8266: Tosmota firmware
Zigbee : 6 Operators, 13 Sensors
gerardvs
Posts: 81
Joined: Sunday 04 January 2015 0:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest-1
Location: /dev/null
Contact:

Re: Interesting ESP8266 WiFi Module

Post by gerardvs »

"The more the merrier" :)

Sure you have a point about a crowded WiFi environment. But usually these devices do not send a lot of data unless you use them for streaming audio of video.
I have several ESP devices which report temperature and contact status once or twice every minute and have no issues in my already crowded WiFi environment.
mvdbro
Posts: 24
Joined: Saturday 31 January 2015 19:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Interesting ESP8266 WiFi Module

Post by mvdbro »

To increment a counter in Domoticz is seems necessary to read/increment/write the value through json.
I'm working on a LUA script to perform these steps. It's just a basic sample script, because it does not actually count something. But that can be done by using gpio.

Sample script:

Code: Select all

ip = "192.168.0.8"
port = 8080
idx = 72
increment = 0.1

function getdata()
      strData="GET /json.htm?type=devices&rid=" .. idx .. " HTTP/1.1\r\nHost: www.local.home\r\n"
      .."Connection: keep-alive\r\nAccept: */*\r\n\r\n"
      conn=net.createConnection(net.TCP, 0)
      conn:on("receive", function(conn, payload)
        pos=string.find(payload,"Data")
        if pos ~= nil then
          strData=string.sub(payload,pos+9,pos+49)
          pos=string.find(strData," ")
          if pos ~= nil then
            strData=string.sub(strData,1,pos-1)
          end
        end
        value=tonumber(strData)
        return value
      end )
      conn:connect(port,ip)
      conn:send(strData)
      return value
end

function senddata(value)
      strData="GET /json.htm?type=command&param=udevice&idx=" .. idx .. "&svalue=" .. tostring(value) .. " HTTP/1.1\r\nHost: www.local.home\r\n"
      .."Connection: keep-alive\r\nAccept: */*\r\n\r\n"
      conn=net.createConnection(net.TCP, 0)
      conn:on("receive", function(conn, payload)
        if string.find(payload,"OK") ~= nil then
          print("Succes")
        end
      end )
      conn:connect(port,ip)
      conn:send(strData)
end

function update()
  if increment > 0 then
    value=getdata()
    if value ~= nil then
      print(value)
      newvalue = (value + increment)*100
      print(newvalue)
      senddata(newvalue)
    else
      print("error getting data")
    end
  end
end

tmr.alarm(0, 30000, 1, function() update() end)
It will read the current value of the device, increment the value and write back to Domoticz.
mvdbro
Posts: 24
Joined: Saturday 31 January 2015 19:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Interesting ESP8266 WiFi Module

Post by mvdbro »

Updated version, previous version had some sync issues. Send is now performed after conn.receive

Code: Select all

ip = "192.168.0.8"
port = 8080
idx = 72
increment = 1.5

function updatedata()
      strData="GET /json.htm?type=devices&rid=" .. idx .. " HTTP/1.1\r\nHost: www.local.home\r\n"
      .."Connection: keep-alive\r\nAccept: */*\r\n\r\n"
      conn=net.createConnection(net.TCP, 0)
      conn:on("receive", function(conn, payload)
        pos=string.find(payload,"Data")
        if pos ~= nil then
          strData=string.sub(payload,pos+9,pos+49)
          pos=string.find(strData," ")
          if pos ~= nil then
            print("Get OK")
            strData=string.sub(strData,1,pos-1)
            value=tonumber(strData)
            print(value)
            if value ~= nil then
              newvalue = (value + increment)*100
              print(newvalue)
              conn:close()
              senddata(newvalue)
            end
          end
        end
        return value
      end )
      conn:connect(port,ip)
      conn:send(strData)
end

function senddata(value)
      strData="GET /json.htm?type=command&param=udevice&idx=" .. idx .. "&svalue=" .. tostring(value) .. " HTTP/1.1\r\nHost: www.local.home\r\n"
      .."Connection: keep-alive\r\nAccept: */*\r\n\r\n"

      if string.find(strData,"Infinity") == nil then
        conn=net.createConnection(net.TCP, 0)
        conn:on("receive", function(conn, payload)
          if string.find(payload,"OK") ~= nil then
            print("Send OK")
          else
            print(payload)
          end
        end )
        conn:connect(port,ip)
        conn:send(strData)
      end
end

function update()
  if increment > 0 then
    updatedata()
  end
end

tmr.alarm(0, 30000, 1, function() update() end)
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Interesting ESP8266 WiFi Module

Post by ThinkPad »

Breaking!
Since a few days ago, native Arduino code (C++) can be executed on the ESP8266. Very interesting!
I already managed to get a basic sketch from someone else working, it reads 2x DS18B20 (or more) and writes the value to a virtual sensor in Domoticz!
You can find that code here: http://gathering.tweakers.net/forum/lis ... 0#43984260

More information about the Arduino ESP8266 stuff here: https://github.com/esp8266/Arduino/
I am not active on this forum anymore.
mvdbro
Posts: 24
Joined: Saturday 31 January 2015 19:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Interesting ESP8266 WiFi Module

Post by mvdbro »

ThinkPad wrote:...native Arduino code (C++) can be executed on the ESP8266. Very interesting!
Indeed very interesting. Will try to port the lua stuff to native ESP Arduino and see if it will work...
mvdl
Posts: 16
Joined: Friday 13 June 2014 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: Interesting ESP8266 WiFi Module

Post by mvdl »

Okay, apparently this is really true... First read about this on wednesday... April 1st... :lol:

Haven't tried it yet, though. I do have some ESP8266 modules, but struggling to get communication with them working. Probably a power supply problem - my USB to TTL not able to power the module. Looking forward to get them working, though.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Interesting ESP8266 WiFi Module

Post by ThinkPad »

Use a USB-Serial converter with FTDI chip like this: http://www.tinytronics.nl/shop/FT232RL- ... rt-Adapter
I have mine from eBay (looks the same), works great. You can also connect the DTR pin to Arduino so it automatically reboots when uploading starts.
Never had any issues with Arduino or ESP8266.

Oh, about power: i mostly use my lab powersupply to provide external 3.3V for the ESP8266.
I am not active on this forum anymore.
mvdbro
Posts: 24
Joined: Saturday 31 January 2015 19:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Interesting ESP8266 WiFi Module

Post by mvdbro »

Succesfully ported the LUA script logic to native C code for the ESP-Arduino IDE. Promising development...
Specifically for experienced Arduino developers that do not want to struggle with LUA...

So now we have NodeMCU/LUA and native C choice. Nice!

Code: Select all

/*
 *  This demo sketch gets data from Domotics using json api, increment the value and update Domoticz counter
 *
 */

#define WIFI_SSID           "........"
#define WIFI_KEY            "........"
#define DOMOTICZ_IDX        72
#define DOMOTICZ_IP         "192.168.0.8"
#define DOMOTICZ_PORT       8080
#define INCREMENT           1          // demo value, since there's no actual device attached yet...

#include <ESP8266WiFi.h>

const char* ssid     = WIFI_SSID;
const char* password = WIFI_KEY;
const char* host = DOMOTICZ_IP;
const int httpPort = DOMOTICZ_PORT;

float value=0;

void setup() {
  Serial.begin(115200);
  delay(10);
  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());
}

void loop() {
  delay(5000);
  if (getdata(&value))
    {
      value=(value + INCREMENT)*100;
      Serial.println(value);
      senddata(value);
    }
}

boolean getdata(float *data)
{
  boolean success=false;
  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return false;
  }
  
  // We now create a URI for the request
  String url = "/json.htm?type=devices&rid=";
  url += DOMOTICZ_IDX;
  
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");

  unsigned long timer=millis()+200;
  while(!client.available() && millis()<timer) {}
  
  // Read all the lines of the reply from server and print them to Serial

  while(client.available()){
    String line = client.readStringUntil('\n');
    if (line.substring(10,14) == "Data")
      {
        String strValue=line.substring(19);
        byte pos=strValue.indexOf(' ');
        strValue=strValue.substring(0,pos);
        strValue.trim();
        value = strValue.toFloat();
        *data=value;
        Serial.println("Succes!");
        success=true;
      }
  }
  Serial.println("closing connection");
  return success;
}

boolean senddata(float value)
{
  boolean success=false;
  
  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return false;
  }
  
  // We now create a URI for the request
  String url = "/json.htm?type=command&param=udevice&idx=";
  url += DOMOTICZ_IDX;
  url += "&svalue=";
  url += value;

  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");

  unsigned long timer=millis()+200;
  while(!client.available() && millis()<timer) {}
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\n');
    if (line.substring(0,15) == "HTTP/1.0 200 OK")
      {
        Serial.println("Succes!");
        success=true;
      }
  }
  Serial.println("closing connection");
  return success;
}

ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Interesting ESP8266 WiFi Module

Post by ThinkPad »

Where do you use your code for? Some kind of counter, but in what kind of physical usage?
I am not active on this forum anymore.
mvdbro
Posts: 24
Joined: Saturday 31 January 2015 19:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Interesting ESP8266 WiFi Module

Post by mvdbro »

ThinkPad wrote:Where do you use your code for? Some kind of counter, but in what kind of physical usage?
My first usage will be for my water usage sensor (optical sensor). But i also have electric and gas meter sensors (also optical sensors). So next step is to connect the optical sensor to one of the GPIO pins of the ESP8266.
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Interesting ESP8266 WiFi Module

Post by ThinkPad »

For the water you could have a look at: http://www.mysensors.org/build/pulse_water (see also the other examples at the site). It is Arduino code, so you can probably adopt large pieces of it :)
I am not active on this forum anymore.
mvdl
Posts: 16
Joined: Friday 13 June 2014 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: Interesting ESP8266 WiFi Module

Post by mvdl »

Hi Thinkpad,
ThinkPad wrote:Use a USB-Serial converter with FTDI chip like this: http://www.tinytronics.nl/shop/FT232RL- ... rt-Adapter
I have mine from eBay (looks the same), works great. You can also connect the DTR pin to Arduino so it automatically reboots when uploading starts.
Never had any issues with Arduino or ESP8266.

Oh, about power: i mostly use my lab powersupply to provide external 3.3V for the ESP8266.
Thanks. Yeah, I have a USB to TTL converter, like this one: http://goo.gl/JapNiW. No DTR unfortunately. The VCC is selectable (3.3/5V) but not sure whether that alters the levels of the Tx/Rx pins, though.
And it is probably unable to feed the ESP8266 sufficiently. Alternative was to hook it up to my 3.3V Arduino Nano (328) and then access it through the Arduino Serial terminal. Again, no luck. So, either a) my ESP8266 (ESP01) module is bust or b) the Nano cannot sufficiently power the module either, or c) I should just stop trying to understand electronics (not in favour of this one ;) ).

Breadboard power supply is being ordered, considering a lab supply.
maluko
Posts: 105
Joined: Sunday 02 February 2014 23:57
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Portugal
Contact:

Re: Interesting ESP8266 WiFi Module

Post by maluko »

hi guys

is any changes to put this idea working?

ESP-01(A) on car (Station mode) and other ESP-01(B) (AP mode) doing ping, if B pings A do Open the gate.

or ESP-01 and a arduino.

thanks
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Interesting ESP8266 WiFi Module

Post by ThinkPad »

Clever idea :)

Should work i think....
I am not active on this forum anymore.
mvdbro
Posts: 24
Joined: Saturday 31 January 2015 19:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Interesting ESP8266 WiFi Module

Post by mvdbro »

mvdl wrote:... or c) I should just stop trying to understand electronics (not in favour of this one ;) ).
or d) purchase one of these and feed with USB power from your PC or use a cheap USB charger:
MS1117.png
MS1117.png (53.07 KiB) Viewed 6177 times
mvdl
Posts: 16
Joined: Friday 13 June 2014 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: Interesting ESP8266 WiFi Module

Post by mvdl »

mvdbro,

Looks promising. Seems to take anything between 4.2V and 10V and converts down to 3.3V. Nice when moving from breadboard to prototyping board, or ...
Have seen these on aliexpress range in price between €0,40 (when ordering at least 10) and €3,46 per piece.
Is the €4,- for 10 too good to be true, or are others trying to make a little extra money at my expense?

Thanks for the tip!
Raspberry Piet
Posts: 158
Joined: Saturday 11 January 2014 16:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: NL
Contact:

Re: Interesting ESP8266 WiFi Module

Post by Raspberry Piet »

mvdl wrote: Is the €4,- for 10 too good to be true?
No, i paid about the same price for ten of them. Works ok.
mvdl
Posts: 16
Joined: Friday 13 June 2014 16:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: Interesting ESP8266 WiFi Module

Post by mvdl »

Raspberry Piet wrote: No, i paid about the same price for ten of them. Works ok.
Ordered...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest