ESP8266 + bme280 battery life: 120 days and counting

The sensor

I have an outside battery powered temperature/humidity/pressure sensor, here is the list of what it is based on:

  • ESP8266 (esp12f), according to espressif
    • from 120 to 170mAh for Tx
    • 10µAh in deep sleep mode
  • bme280
    • 3.6µAh at 1Hz when measuring the 3 parameters
  • MCP1700-3302E LDO
    • 1.6µAh quiescent current
  • Panasonic NCR18650B
    • 3400 mAh

It looks like this:


Active time

When writing the code for this device, I added some debug to time how long it needed to go from deep sleep, connect to wifi, read values, send them to MQTT and go back to deep sleep mode. That was a tiny bit over 4s, and without print, a tiny bit below 4s. So for the final version, I built it without any print, which means even less than what was in my test, as I still need to initialize the serial, and do at least one print to show the time. I guess we can use 4s as a basis here, and assume that it actually is lower than that.

Deep sleep is set to last 10 minutes every time, so:

  • 4 seconds active
  • 596 seconds deep sleep
  • OR 24 seconds active and 3576 seconds of deep sleep in an hour

Theorical estimations

Once again, it is lilkely below that, but let's assume the esp module is using 170mAh when active:

(170 / 3600) * 24 = 1.1333333333333333

And 10µAh for 3576 seconds:

((10 / 1000) / 3600) * 3576 = 0.009933333333333334

The bme280 will only be powered when the esp module is asking for it, so let's round it to the 24 seconds an hour, which is probably a bit more than real world usage as it will likely only be active when doing the reading

((3.6 / 1000) / 3600) * 24 = 2.4e-05

And finally, 0.0016mAh for the LDO:

1.1333333333333333 + 0.009933333333333334 + 2.4e-05 + 0.0016 = 1.1448906666666667

So in a day:

1.1448906666666667 * 24 = 27.477376

Which sums up to 27.477376mA a day.

The Panasonic batteries are apparently measured pretty often around 3280mAh, so

3280 / 27.477376 = 119.37093265383129

We can round that down to 119 days.

If considering 120mAh this figure goes up to 168.4 days.

Also note the actual code goes:

  • wake up
  • connect to wifi
  • connect to MQTT broker
  • read sensor values
  • send values to MQTT broker
  • go back to sleep

I would venture that this probaly mean we are not in Tx mode all the time, so the actual esp module consumption during these ~4 seconds is likely shorter, therefore we can hope for even longer battery life.

Dream mode on: If Tx is at about 120mAh and is actually half the awake time, and the rest is Rx, at about 56mAh, that would bring us to 228 days!

Trying to be more serious, it seems likely that Tx is not using the higher current, and that it only is using part of the awake time, so I would be pretty happy with 6 months battery life.

Real life

At first I wanted to write this once I had depleted an 18650 cell completely.

Knowing I estimated it at first with about the same as today, so 119 days. That being theorical, I was actually expecting 90 days, maybe a 100. Espcially as there was quite some time below freezing point since it was put up, and that this has bad impact on batteries as far as I know.

It now has been 4 months, since november 25th, or 120days and it still is running, Vcc measurement doesn't seem to get any lower, but the only measures I hava is from the ESP.getVcc(), which is what the ESP gets, and not what the battery actually deliver.

The readings jittters between 3.14 and 3.33v knowing the MCP1700-3302E has a dropout voltage of 0.178v, we can assume that at worst, the battery still delivers 3.318v. I got an alarm set when this goes below 3.1v, so if the real battery gets below 3.278v, so the battery is safe.

I'll likely do a new post when the battery is finally depleted.

25 mars 2017 10:53:22 -- tags [ esp28266 , bme280 , 18650 , battery , temperature , sensor ]


Anxiety, small summary of what I found until now


I'm not a neither a psychologist nor a psychiatrist, I have no medical background and I can have totally misunderstood some of the things I went through, bear with me and do not hesitate to reach out to me if you found something that doesn't make sense.


I've spent about a month now reading, watching and reseaching about anxiety to try to find ways to help my wife sort her anxiety issues. The idea is to find progressive ways, that will help her get better and that will last and help avoiding relapsing.

I simply want to share what I found out up to now for the people it could be useful for.


So here are some of the things I went through with some insight of what I found out through each, and what I feel is important to keep in mind from these.

That is indeed a bit long, if you want, you can simply go through the videos and not bothering reading my summary/analysis, but the ones I'll share here are worth watching in my opinion.

It all started with the first video you'll find below, a TED Talk having interesting pieces of information about how the brain works and how our feelings can affect what we think.

What reality are you creating for yourself? | Isaac Lidsk

Youtube Link

I highly recommand watching it over reading my summary below, it may seems unrelated at first, but believe me there are pieces of information that are worth the watch.

What you see impact how you feel / the way you feel can literally change what you see

What you see is a complex mental construction of your own making, but we experience it passively as a direct representation of the world around you

Sight is just one way we shape our reality, we create our own realities in many other way

That's when he starts talking about fear and it gets interesting for anxiety, the most interesting point to keep in mind to try to make sense of how anxiety leads you in its trap is that:

Your fears distord your reality, under the warped logic of fear, anything is better that the uncertain. Fear fills the void at all costs, passing of what you dread for what you know, offering up the worst in place of the ambiguous, substituting assumption for reason. Psychologists have a great term for it: awfulizing. Fear replaces the unknown with the awful.

With some edit to ease the transcription here:

Fear shrinks and distords you view, drown your capacity for critical thought with a flood of disruptive emotions

Fear lulls you into inaction, enticing you to passively watch its prophecies fulfill themselves.

In my opinion he doesn't provide solution for anxiety, note that it wasn't his point in the first place, still, his advices are good, but too generic and not easy to simply follow for an anxious person:

Hold yourself accountable for every moment, every thought, every details. See beyond your fears, recognize your assumptions, harness your internal strenght, silence your internal critic, correct your misconceptions about luck and success.

He then emphasize on this, which basically is what cognitive theray is meant for: identifying the flaws and distorted perception, realize the mistakes and learn to spot them before getting deep into it.

How to hack your biology and be in the zone every single day

Youtube Link

As you may have guessed from te title this isn't directly related to anxiety in the first place either, but there are a lot of explanation about how you brain can be impacted by your stress and the chemicals that comes into play.

He is using cardiac coherency to show when you're at peace or not, and explains the simplest way to regulate it: rythmic and smooth breathing.

He also exlains with simple 2 axis diagram the stress/relax and good/bad states, and that no matter if you're pumped/stressed, if you are on the good side (so when you heart rate variation is coherent), you'll be able to make the best out of this state.

I highly encourage watching the whole video, but to make some quick notes to remember:

  • cortisol, also known as the stress hormone, get you chaotic variance
  • DHEA give a more stable and coherent hear rate
  • rythmic breathing helps getting a coherent heart rate and reducing cortisol

Ain't much, but that's pretty important!

Your body language shapes who you are - Amy Cuddy

Youtube Link

  • Body language can impact the way we think and the way we judge ourselves
  • Power poses for 2 minutes do have a tested impact on biological
  • reduced cortisol: less stress
  • higher testosterone: more risk tolerant, more confidence

Rethinking anxiety - Learning to face fear _ Dawn Huebner

Youtube Link

This mostly talks about Cognitive behavioral therapy and ways to use it. She explains why it is important to face fears to finally understand that the fear response we had for this thing was a false alarm. For this it is useful to find ways to get your attention on something else, like meditation, rythmed breating, mindfulness...

How to make stress your friend - Kelly McGonigal

Youtube Link

I'll simply sum up what I think is the most important, especially for illness anxiety (i.e: awfulizing on healh: my throat hurt, it must be a throat cancer)

  • Stress when not getting worried about being stress is a helpful respond and is, proven by medical study, harmless to your health.
  • Stress is a fear reaction that will switch your body in a state to be more efficient, so if not worried about it, it can help you perform better
  • Stress makes you release oxytocin, that will encourage you to reach out for help, and also to help others, reaching out, exchanging, helping will actually effectively help you out.

Feeling good - David Burns

Youtube Link

This video is on Cognitive Therapy. Basically:

  • anxiety send negative signals to ourselves
  • anxious thoughts are false, warped by our mind, and always to the worst
  • cognitive therapy is meant to identify and change the way we think, to change the way we feel
  • think about what we think when we get anxious, and analyze them to spot the false and deformed things we think
  • feelings are the output of our thinking
  • it is not the outside world and events that are causing our emotions, but only how we perceive them and what we will think at that point

Introduction to unique and powerful features of schema therapy

Youtube Link

  • Schema therapy goal isn't to change the behavior, but to identify some unmet needs for the person and help them reaching these needs, so the associated schema will disappear by themselves.
  • Identify barriers and vulnerability then the associated need
  • Schema therapy won't (like most other therapy) say these needs are bad, not putting an additionnal strain on the patient, but help him/her reach it and grow from it.
  • with use of limited reparenting, therapist is supposed to support and validate patient behavior to help him go over the limitations and vulnerabilities he created for himself

The game that can give you 10 extra years of life - Jane McGonigal

Youtube Link

The most import thing to get out of it is that she suggest playing SuperBetter for small exercises that will help boost your resiliance to various things.

A new plan for anxious feelings: escape the custard! - Neil Hughes

Youtube Link

A very good explanation with images to represent anxiety and how it feels, by someone who really lived through it. Explanations are well done, and the way he illustrate the anxiety traps as custard traps, help understanding things.

  • Brain can get anything a habit and feel normal
  • Habits will fuel the anxiety, unintentionally, that's just an habit
  • Self-observation to detect disruptive thoughts when they happen and allow to choose your reaction is important
  • Anxiety is self-reinforcing, it tires you, you feel bad, and it gets worse
  • When an anxious thought pops up, you need to do something else
  • Brain have path of thought, the more you use the bad ones, the stronger they will be, and the more often they'll happen -> you need new habits to overcome these path and create new ones

How to become more confident -- lay down on the street for 30sec - Till H. Groß

Youtube Link

That is a funny one, it is a more accessible and funnier way to work on confidence, it is just a funny way to apprehend Cognitive behavioral therapy

A good watch, better seen than explained.

You are your greatest ally - Jessica Amos

Youtube Link

A pretty short video, where she presents a small exercise that can be pretty useful if it works on you, can also be used to calm down, to divert attention on something else in case anxiety is starting to rise but is enough under control to do something like that.

Anxiety: Hibernate, Adapt, or Migrate

Youtube Link

Not sure this will bring much help, beside seeing someone with real anxiety issues who actually got out of it and can live now.

Most important things, I'll take from there: - The fear response our body have isn't adapted to our lifestyle anymore - If you avoid things that get you anxious, you're afraid of, the threat level they are to you will raise, that's why Cognitive behavioral therapy is interesting

She also quotes a book

mastery, autonomy and purpose are the 3 nutrients that feed our intrinsic motivation and they promote our greater physical and mental well being

Small summary

So to sum it up here is a small overview of what I think is important.


This is known as the stress hormone, and it will prevent you from logical reflexion and analysis, one of the goal I think, is to find ways to reduce it.

  • Magnesium can help, there are pretty cheap food supplement, make sure if you go this route to take one with vitamin B6 too, as it helps the assimilation of magnesium
  • Power poses for 2 minutes helps reducing it, I guess this is short lived, so better do it trying something you're not at ease, or when you know you'll be start thinking a lot and entering an anxious loop
  • Cardiac Coherency can help regulate it and make yourself more able to analyze and therefore use logic to refrain anxious thoughts, more on it on its own section below.
  • If possible, reduce caffeine and tobacco, but these are addictive, and can create new stress when trying to reduce or stop, if you don't feel like it, don't to it, it won't make you any good if you're more stressed
  • Relaxation and Meditation and Mindfulness can be of help, but may as well create an issue, when trying to get distance on your thoughts they may as well let you more time to think and enter an anxious loops. My guess is that it will depend from your condition, but I'm not sure that is a good idea for most people with chronic anxiety.


It kinda is at the opposite spectrum from cortisol, your body generates it, some food may help your body producing it, but I haven't found anything that seems really serious about it for now.

There also are food supplements, but apparently they are still not allowed everywhere, can make athletes positive to drug test, and effet on longer term are not well know.

I haven't done much research on this yet.

Cardiac Coherency

Should help get to a safer level of stress and allow more logical thinking, therefore allow you to be able to detect the anxious thought and analyze them as wrong and deformed or not worth the anxiety spike and handle it a bit better.

I'm not saying this will cure everything, but that's an additional help, I'm currently searching for various small things to help the whole process.

Training 3 times a day, 5 minutes over a period of about 30 days should normally get your body to have learned it and be more at ease to do it on its own, and to use it in case of a spike of anxiety before you're too far down the spiral of fear.

There are apps, I only have android phones so I only list those I tested on my phone:

  • Kardia Cohérence & Relaxation:
  • free
  • simple
  • no heart rate monitoring, it is a simple trainer
  • it works fine, I like it
  • HeartRate+ Cohérence PRO
  • not free
  • actually measures the heart rate either via camera + flash, or bluetooth heart rate monitor, via camera isn't very accurate sometimes
  • do not work will all bluetooth monitors, mine sends an average rate, and cannot be used for that
  • actually gives you graphs
  • too bad I didn't get a monitor that works fine and camera isn't very accurate for me, it probably could have been a good option
  • free for 1:30 min, so you can test out if it works for you in one way or another
  • Feel
  • app is free, but it requires a sensor that they sell on their website
  • complete with graph, history, various level of guidance depending of your level
  • that's what I ask my wife to use right now

I would suggest to do it before sleeping to try to take advantage of the Memory Consolidation during sleep.


That may sound pretty stupid, but if you can find a sport that will make you workout enough to release endorphin, it helps feeling better, and thinking clearer generally.

Again, this won't be a miracle cure that solves everything, it is an additional stuff that can help you out a little.

  • Keep you busy for a while, as long as it makes you concentrate on the effort you're doing (I fear things like jogging or rope jumping may actually leave too much brain time to think... everyone is different, try it out!)
  • Releases endorphin, feel better
  • Better confidence in your body
  • More fatigue, chances are you'll fell asleep more easily and sleep better, because of the physical fatigue instead of the nervous one

Cognitive Therapy

Cognitive Therapy is meant to analyse the false assumption, the wrong idea you tell yourself, detect them, analyze them and prove them wrong.

In my wife situation that would be a good plan, if she could actually beleive the analysis we do together. Let's take an example:

  • She has a sore throat, she starts thinking she has a throat cancer.
  • People without anxiety issue will simply tell her: go to the doc, he'll tell you that's nothing.
  • People with anxiety issue will know that going to the doc is the scariest thing ever, becaus in an anxious mind, he will confirm that this is a throat cancer and that you're on the verge of death.

The fact is, if we take this idea and try to analyze it together, I can't prove that it isn't a throat cancer, and she'll therefore know there is a possibility it can be that, and won't accept the fact that this possibility, this thought is absolutely wrong and that there is nothing to fear.

So the principle is good, but the application isn't easy, also, apparently the relapsing rate is pretty high, especially for anxiety.

Cognitive Behavioral Therapy

Cognitive Behavioral Therapy, as explained a bit earlier is mainly aimed at facing the fears, because avoidance will let them grow and become more and more of a danger in you mind. So facing it more and more enable the idea of a thing to be frightful to become less scary over time and repetition.

In my wife situation, the fear of sickness cannot be treated by this, but her fear of social interaction, of going out on her own, or to take a car drive (as a passenger, she doesn't drive) can be impacted by this.

As the Cognitive Therapy, the risk of relapsing is pretty high, in my experience, if any situation that became bearable to her, triggers a panic attack again because she wasn't that well this day for example, will go back to its previous state, or even worse, as she'll avoid it more and more and therefore strengethen it.

Schema Therapy

Schema Therapy is apparently using part of various other fields in therapy, amongst them are CT and CBT of course, but it has a different apporach that tries to be more positive and more self healing. I haven't finished my work on this, I bought books that I am still reading right now so it may be a bit early to talk about it too much.

Interesting part is that the schemas are the way we react to events, there are adaptive and maladaptive schemas, and early or late schemas, this theory focuses on the one called: early maladaptive schemas which are schemas learned earlier in life, and that are not using the right way to deal with the situation.

I won't enter into the details of the various parts to work on for a schema therapy, but what is interesting is that by finding the needs and they source in an early maladaptive schema, this therapy aims to help the patient to fullfil its needs, and therefore heal himself from the root cause of the issue, helping rewiring this schemas to a more functional one for each situation.

Jeffrey E. Young, the one at the source of this therapy, wrote various books, I currently own two:

  • Reinventing your life: which is targetted at people who want to work on themselves on their own
  • Schema Therapy: A Practitioner's Guide: which is more targetted at therapist, with more explanation of the principles and the steps and parts of the therapy. I'm currently reading it, and it has a lot of useful informatino, even at the beginning simply to understand how schemas works, where they come from and how our brain uses them.

One drawback is that from what I'm currently reading it looks like you'll most lilkely need a therapist that knows this very well to go through a full healing. I'll may change my mind once I read reinventing your life, but I wanted to know as much as possible on the principles before starting it, that's why I went directly with the bigger, longer and more detailled book first.


So in order to get various small stuff going, I would suggest making a plan, a progressive plan for you to incorporate the small stuff, especially if you're not yet heavily handicapped by your anxiety: DO NOT LET IT GET YOU ANY FURTHER.

Again I'm no doctor, therapist or anything related, these are just suggestion to help out if that can bring any help to anybody, this doesn't have to be done in that order, you can take the progression as slow as you want. And in the worst cases, theres is nothing better than having a good therapist that cares about helping you out and with whom you feel ok.

So my suggestions: - Magnesium + vitamin B6 cure - Play superbetter everyday - Sport - Cardiac Coherency training, with one before bed for memory consolidation - Cardiac Coherency when anxiety rises - Power poses for 2 minutes before stressful thing on when anxiety start - Power poses for 2 minutes before bed, I would say before cardiac coherency - Reach out to others, talk about it, be open about it, and do NOT take personally or badly things they say, that shows they don't understand, better try to explain better to them, and if they act like it is your fault, or still don't get it, find nicer people, or people with more empathy to talk about it. - Taking notes of disruptive thoughts, analyzing them on the spot if possible, later if you can't do it at the time they are coming. - Facing some of the fears, confort zone challenges, whatever way feels the best to you, but that has to be done regularly, not once every now and then or the benefit of the "there finally was no danger doing that" will disappear and won't make any benefit to you - Reading Reinventing your life to find other ways to help yourself

Hope this can be of any help to someone, again, not a doc, I'm just a software engineer trying to find ways to help my wife, and thought the month worth of reaserch I did was worth sharing...

5 février 2017 14:13:29 -- tags [ Anxiety , therapy , schema therapy ]


Brother printer and scanner under archlinux

We received our new Brother DCP-L2520DW printer, a black and white laser printer and scanner with wifi, for 140€, that was a decent deal, my only concern was linux support.

Of course I checked before and saw the drivers provided on Brother's website, a friend also told me he had another model and that it worked really easily, and that is putting it ligthly given what I saw today.

On archlinux, there is an AUR package for this printer, given I use packer, is simply did:

packer -S packer -S brother-dcpl2520dw

Which suggested the brscan4 package, so...

packer -Ss brscan4
sudo brsaneconfig4 -a name="Brother" model="dcp-l2520dw" ip=$IP

I then connected to http://localhost:631 to configure my cups:

  • Adding Printers and Classes
  • Add Printer
  • LPD/LPR Host on Printer
  • lpd://$IP/queue
  • enter info
  • select driver for the correct model
  • valid

And that's it!

Printer works, xsane sees the scanner, and everything works, the longer was actually to figure out to puse lpd://$IP/queue as protocol and configuration, other than that it worked so easily that I was pretty surprised.

25 août 2016 19:03:43 -- tags [ printer , brother , cups , xsane ]


ESP8266 + DHT22 + Homie -> Home-Assistant

So now that I had a Home-Assistant (HA) setup working, an esp8266 booting and that I can write software for, it was time to start making the two talk to each other and make something that can become unseful based on all this.

The choice went to the simplest element I had at hand, a DHT22 temperature and humidity sensor.

There were 5 steps to do this:

  • wire the dht22 on a test setup
  • find how to use it in a Homie firmware, and flash it
  • configure the device
  • configure HA for MQTT, which led me to rewrite the whole configuration
  • test it

So let's get to this step by step

Wiring the DHT22

It can be pretty simple, without pullup resistors and capacitor, I found this on instructable


I simply put this to another GPIO on my esp12f, but that's pretty much it. The result on the breadboard looks like this:

dht22 breadboard

That's about it, for testing of course, for a final sensors, I'll have to make it cleaner than that...

Homie firmware

I started looking up for code on how to use homie with the dht22, there are libs to make use of it, running:

platformio lib search dht

Showed me the DHT sensor library that is esspressif compatible, I simply installed it via:

platformio lib install 19

After that I looked online at examples to use it, and actually found this code which does exactly what I expected. At first I started writting each lines trying to include only the needed one. But I think this is really close to the bare minimum to get something fully working, so I simply renamed it to raton-dht and switch the version to 0.0.1.

Jumping a little off-topic, the version number has an interest, because Homie support OTA, using the pretty cool homie-ota, which includes a listing of firmware and an inventory of devices, which is pretty awesome for handling yor homie devices and update them if need be. I haven't tried it yet myself, but I definitely will once I get real devices up and running in the house. This really looks like a must-have to me. Once again, thanks jpmens!

Back on tracks, not having the OTA setup yes, I simply switched my GPIO0 to low and ran:

platformio run -t upload

And the next boot the dht sensors firmware was sitting in configuration mode.

Homie configuration

In the previous post, I didn't explain how to configure the homie devices, I actually did not discover it at this step, I played with this on the previous useless firmware to understand how it works.

First it is worth noting that the homie-esp8266 documentation has been moved from github to, there on the http json api page, you can find the 3 options that you can use to configure your device in configuration mode:

  • the Web UI
  • the android app which apparently is a a webview directly to the webui (at least that's my guess)
  • a curl example, but you'll need to know what the config.json looks like like described in the dedicated page

In any case, you'll need to be connected to the AP the homie device will create when in configuration mode, for me, working on a workstation, and not having my laptop at hand (and feeling too lazy to grab it, plug it as it isn't charged right now, configure another wireless device via wpa_supplicant), that only left me with my android phone... But there is a catch: android checks that it can reach outside to actually let you use your wifi connection.

I found here that on a rooted device you can run:

settings put global captive_portal_detection_enabled 0

This did the trick for me, so I ran that, connected to the Homie-XXXXX AP, and fired the android app, which found the device, and allow me to configure it, the configuration is pretty straightforward and short, once this is done, you can finally reboot the homie device in normal mode, where it connects to your AP/router and start trying to talk to the mqtt broker. Now on to the HA setup.

Home-Assistant MQTT configuration

As said in the previous post, I was planning on using the embedded MQTT broker in HA. According to the MQTT component page, to enable MQTT support in HA, we have to add


To the configuration file, other parameters are for specifying the broker, to use the embedded one, without further configuration, that's it. Restarting HA with that enables the MQTT broker.

Next we need to configure the sensors, but when I started to look to add this I realized how messy my HA configuration had become, with so few things it looked already pretty cluttered with useless things, comments that are only usefull when beginning to use it. So i decided to go for a huge cleanup and rewrite.

First step, version control, I planned on splitting it in multiple files, and this won't be any easy to handle and maintain if not in git, so I created a ha-cfg github repo

I went from this to that

It took some fiddling to understand the !include which is not totally obvious to use, but I'm pretty happy with the final result.

Now in my sensors.yaml I simply added

# temperature test
- platform: mqtt
  state_topic: devices/01397be0/temperature/degrees
  name: "Test temperature"
  qos: 0
  unit_of_measurement: "°C"

# humidity test
- platform: mqtt
  state_topic: devices/01397be0/humidity/relative
  name: "Test humidity"
  qos: 0
  unit_of_measurement: "%"

And I was all set.


After that and restarting HA with the new configuration, I booted the esp again and could see:

** Booting into normal mode **
{} Stored configuration:
   Hardware device ID: 01397be0
   Device ID: 01397be0
   Boot mode: normal
   Name: dht#1
     SSID: SFR-5458
     Password not shown
     Port: 1883
     Base topic: devices/
     Auth? no
     SSL? no
     Enabled? yes
     Port: 80
     Path: /ota
     SSL? no
 Attempting to connect to Wi-Fi...
 Wi-Fi connected
Triggering HOMIE_WIFI_CONNECTED event...
 Attempting to connect to MQTT...
Sending initial information...  OK
Subscribing to topics...  OK
 MQTT ready
Triggering HOMIE_MQTT_CONNECTED event...
Calling setup function...
Sending Wi-Fi signal quality (100%)...  OK
Sending uptime (8s)...  OK

And on HA web-ui I know have test humidity and test temperature sensors showing up on the top row.


And that's it!

21 août 2016 12:39:25 -- tags [ esp8266 , dht22 , home-assistant , homie ]


esp8266 with Homie and PlatformIO

As I talked about in previous posts, I got some ESP8266 devices I wanted to play with, one has been setup on a breadboard for testing for a while, able to boot, but never got to try anything more on it yet.

Protocol and firmware base choice

As my goal is to do some connected devices for home automation, I wanted to make firmwares for these that could talk to Home-Assistant. Even through HA supports quite a lot of way to talk to devices, there were 2 main options I considered:

  • http: simply having a http on the esp which exposes status, and can get commands via POST for example
  • mqtt: it is meant for IoT, the main drawback was I actually didn't want to run a broker

That's when I realized HA MQTT component has an embedded broker. Which apparently is not getting much love as everyone is using mqtt with HA for owntracks, and the embedded broken is incompatible with it. But given I don't plan on using owntracks myself for now, that should be sufficient for my need.

A large part of my fiddling for home automation has been triggered by Jan-Piet Mens and he actually loves MQTT, and played around with clients and told a few times (on twitter if my memory is any good) that Homie for esp8266 was well thought and stable, as I generally find his choices, advices and tastes to match pretty well mine, I decided to go for this.

So here we are, if all goes well, my esp8266 will be running a Homie based firmware to talk to home-assistant via MQTT.


Then the question of the tools arised, most of the things I read about esps were all talking about Arduino IDE, which I found comments before even trying it, saying to sum it up, as working, but not great. For those who know me, you know I don't like UI, graphical interfaces, and mouse compliant interfaces. I was not trilled to have to use this.

There is quite some talk about esptool which apparently is a great command line tool written in python to flash you esps. That could have done it, but as a complete beginner in all this, I had no clue how to build a project to create the firmware before flashing it with esptool.

Homie github README talks about PlatformIO. I had no clue what this was, and one of the first thing you notice on their webpage is the IDE word, and screenshot of Atom sporting a toolbar to interact with PlatformIO. But after discading it in the first place, and coming back to their website and taking some time to actually read things, I noticed: PlatformIO is an open source ecosystem for IoT development. There is an IDE, but it is more an ecosystem, and everything can be done from the command line, and that was a lot more appealing to me, so I went for it.


Being a python project, you can install it via pip, some python -c directly from curl as suggested in the Super-Quick Installation Guide.

I went with archlinux's AUR package, there also is a -git one, if need arises later on.

Once installed I went through this page and followed their example:

platformio init --board nodemcu

The nodemcu are based on ESP12s, as I own an esp12f, and I read somewhere it was generally working pretty fine with nocemcu as a board, I decided to go for it.

The previous command create an .ini and a lib and src folders. There also is a .pioenvs folder that contains everything PlatformIO will need to build your project, libs, the output binaries etc.

Then as explained on Homie's getting started guide I installed the lib, given I'm a bit curious I looked it up before typing what the doc suggests.

platformio lib search homie
platformio lib install 555

I then dropped the so called Bare mininum sketch

#include <Homie.h>

void setup() {

void loop() {

in src/test.c, and tried to compile using the command:

platformio run

But that failed because of missing PubSubClient.h in Homie includes, as PlatformIO is said to handle depedancies, that surprised me but well, whatever:

platformio lib search pubsubclient
platformio lib install 89

Then tried to compile again... this time a real compilation error arised:

In file included from .pioenvs/nodemcu/Homie_ID555/Homie/MqttClient.hpp:3:0,
from .pioenvs/nodemcu/Homie_ID555/Homie.hpp:3,
from .pioenvs/nodemcu/Homie_ID555/Homie.h:4,
from src/test.c:1:
.pioenvs/nodemcu/ESP8266WiFi/ESP8266WiFi.h:27:8: error: expected identifier
or '(' before string constant
extern "C" {

Given the presence of .hpp in the error, and the extern "C" section I guess I should not be assume I'm writing C, so I move src/test.c to src/test.cpp, launched the compilation, and this time it built fine.

If you have your FTDI adapter plugged in, and your ESP in flash mode, you can add --target upload to the run command line, and PlatformIO will flash your device for you.

I simply wired GPIO0 to GND and rebooted the esp, launch the run with upload flag, and my esp was flashed. I then rewired GPIO to VCC and rebooted, and the AP from Homie's configuration mode showed up in my phone wifi... I didn't yet took the time to configure it via json and test it further.

But that's already a good starting point, and that's it for now!

18 août 2016 22:59:54 -- tags [ esp8266 , wifi , homie , platformio , IoT , MQTT ]


Home Automation (part 3)

My current Home-Assistant setup

Currently my HA homepage looks like this:


Let's first go over the various parts of the UI, and how my setup is currently working befor diving into the configuration file details.

The web UI

  • At the top, we have the status of various sensors
    • Update notification
    • Sun status, useful sensor to automate things based on sunset/sunrise
    • Device tracker status: My wife's and my phone are both at home
    • Z-Wave embedded switch current power consumption
    • Current Weather
    • Speedtest: download, ping, upload
    • Z-Wave embedded switch power consumption sum: so we know since we started using HA the living room light consumed 5.62kWh
    • Transmisison torrent download and upload speed
  • Weblink section are custom links to whatever URL you want, here my transmission webui
  • Input Select shows a custom dropdown I created: Mode, I am using it to select the current working mode of my HA setup
    • Jour (day): when switching to it, switch off the living room light, no automation
    • Soir (evening): light set to a medium dimming level (90/255), media center playing will dim the light to 30/255 for better atmosphere
    • Soirée (party): As for soir setting, light 90/255, but no automation
    • Nuit (night): set living room light to very very low level (10/255)
  • Switch: as you may have guessed, that section list the switches that are not for light control
    • PC raton is a wake-on-lan switch to wake my computer up, unfortunately not working as the FreeBSD jail that runs HA won't let the WOL packet out...
  • Light: the switches to control lights, for dimming you can click on the name and get a slider to adjust level.
    • Lampadaire is my wall plug on which I plug a standing light
    • Salon is the living room wall switch controlled lights
  • Kodi: is my Openelec installation, my main media center
  • Ratoncast: is a chromecast, that we mainly use for netflix

By default there can be a lot of other things showing up, with pretty awful names, for example the Fibaro Z-Wave switch has various sensors and parameters, they all appear in there but you can rename them to friendly names and hide the unused ones in your configuration.

The Automation

I have only a few automation for now, but they are actually pretty handy, most of them are based on the custom Input Select.

  • At sunset - 2.5h set the mode to evening: That switch the living room on, and enable the media centers automation
  • At 1:30am switch the mode to night: light to minimum in living room and no automation based on media centers
  • At sunrise set the mode to day: switch off the living room if needed and no automation based on media centers

If the automation based on media centers is enable there is: when a media player starts playing dim the light when paused or stop, raise the light to its usual level

It may sound like I didn't setup much, but just that is actually really nice, we actually don't use the wall switch anymore, and only on a very few occasion change settings manually, it just works and we don't have to do anything, that's pretty awesome.

The configuration

Now for the ugly, if you just wanted to know what I did with HA quickly, that's about it, you can stop reading here :).

The configuration isn't very hard to understand, but it still does require a bit of fiddling and tweaking before getting things right.

Most of the components pages explains pretty well what to do and how to configure you devices. In addition there is the Examples or Cookbook page where you can find configuration from other people and snippets of automation for some commonly used cases, like dimming the lights when playing a movie.

The configuration is in YAML and can be splitted in multiple files using !include, I didn't do it myself yet, but will probably have to go this way if it gets any more complex than it currently is.


  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: 48.80569
  longitude: 2.130699
  # C for Celsius, F for Fahrenheit
  temperature_unit: C
  # Pick yours from here:
  time_zone: Europe/Paris

# Discover some devices automatically

# Allows you to issue voice commands from the frontend

# View all events in a logbook

# Enables the frontend

# Track the sun

# Prediction of weather
   platform: yr

# Show links to resources in log and frontend

# Enables support for tracking state changes over time.

# Checks for available updates

You give your setup a name, a location (to fetch sunrise/sunset), unit for temperature and your timezone.

Then in my case: enable discovery (that's how the chromecast is found), disable conversation: you can give voice command to HA from the webui, but I'm not a native english speaker, so I don't like talking to my phone in english, plus I do hope to do a better voice control system (always on) at some point. enable logbook, frontend, history and update checker enable sun tracking, based on location, that will create the sensor on top of the web UI and allow automation based on sunset and sunrise times * disable introduction: that is a nice to have at first when learning HA, but after that it mostly get annoying to me.

When using Z-Wave, you have to configure your dongle, as explained on the Z-Wave page:

  usb_path: /dev/cuaU0
  config_path: /usr/local/lib/python3.4/site-packages/libopenzwave-0.3.1-py3.4-freebsd-10.3-RELEASE-amd64.egg/config
  polling_interval: 60000


As explained earlier, by default, all sensors, switches, and their various settings will show up once HA detects them, the annoying part is that can create a lot of clutter and be pretty ugly as the default names of some devices are pretty awful.

To solve this HA provides the customize: section, where you can hide some devices, and change the friendly_name too. In the configuration you will still have to reference the original name, but at least for the UI you'll get a nice name.

Note that this is a subsection of the homeassistant: section, this one would be a good candidate for the first !include in my configuration file.

      friendly_name: Total Salon
      friendly_name: Instant. Salon
      friendly_name: Meteo
      hidden: true
      hidden: true
      hidden: true
      hidden: true
      hidden: true
      hidden: true
      hidden: true
      hidden: true
      hidden: true
      hidden: true

      friendly_name: Lampadaire
      friendly_name: Salon
      hidden: true
      hidden: true
      hidden: true

I don't think it is worth going over every part here, it is pretty clear, and definetily won't match anyone else setup, it is just here as a reference.


Here I use the sensor word in as a generic term, I'll include quite a list of various things within it, first the device tracker: I currently don't use it but it can be a great way to automate things based on weither you're home or not.

  platform: nmap_tracker
  home_interval: 10
  track_new_devices: no

There are other possibilities as you can see on the Presence Detection section of the components, one of the most popular being owntracks. But I went for a simple nmap on my local subnet, this will create a known_devices.yaml containing the list of the known hosts. As you can see I used the track_new_devices: no option, to avoid having any new device showing up, a friend who borrow your wifi for example etc, by default they will all show at the top of your homepage, that's why I used this option and the track: no option in known_devices.yaml file to limit to only tracking our two phones.

I also added speedtest to test our link speed periodically, and transimission to see if torrents are actually doing anything:

sensor 2:
  platform: speedtest
  minute: 30
    - 0
    - 6
    - 12
    - 18
    - ping
    - download
    - upload

sensor 3:
  platform: transmission
  port: 9091
    - 'download_speed'
    - 'upload_speed'

And that's it.

Media Players

The chromecast is automatically detected via the discover: module and there is no configuration entry related to it, except for the automation which is another part of this post.

So I only had to configure our Openelec media center, which is based on Kodi, which have its own component:

  platform: kodi
  host: http://192.168.0.X
  port: 80
  name: Kodi

Custom Switches

I have 2 switches added manually in the configuration, the wake-on-lan for my computer:

switch 1:
  platform: wake_on_lan
  mac_address: "XX:XX:XX:XX:XX:XX"
  name: PC raton
  host: "192.168.0.X"

The host ip is used to check if it is up for the switch in the web UI.

The second one is the input select to choose mode:

    name: "Mode"
      - Jour
      - Soir
      - Soiree
      - Nuit
    initial: Jour
    icon: mdi:power-settings

It creates a current_mode device you can refer to in the automation, you simply list the options and the initial setting to use when restarting HA, I added an icon, but that's optional.

You can also add sliders or boolean input, they are listed in the Automation components page.


If like me you use HA as your browser homepage, it can be useful to add links to some often used website or services, this is where you can use the weblink component, I simply added my transmission url:

    - name: Torrents
      url: http://192.168.0.X:9091
      icon: mdi:download


That's it about the devices and sensors we can now dive into the automation, I'm not sure it is really worth to detail everything, it can help getting a grasp of how it works, but every setup will be different, and most Examples will cover this better than I'll be able to. So what I'll do is simply explain what part of the automation I'm talking about, and paste the configuration part related to it.

All the automation rules will reside in the automation: section (who whould have guessed?)

Mode Switching Events

So, most of my automation is based on the mode the setup is running at, and when switching from one to another changing the light accordingly.

Switching to day mode, turn off all the lights. Currently we only have 2 lights both in the living room, so it make sense to simply use the service light witouth parameter so it act on all the light:

  - alias: Jour
      - platform: state
        entity_id: input_select.current_mode
        state: "Jour"
      service: light.turn_off

Switching to evening or party mode, turn the living room above and behing tv to 90/255 setting, only difference is that the media playing automation won't trigger if mode isn't evening:

  - alias: Soir
      - platform: state
        entity_id: input_select.current_mode
        state: "Soir"
      service: light.turn_on
        entity_id: light.fibaro_system_fgd212_dimmer_2_level_3
        brightness: 90

  - alias: Soiree
      - platform: state
        entity_id: input_select.current_mode
        state: "Soiree"
      service: light.turn_on
        entity_id: light.fibaro_system_fgd212_dimmer_2_level_3
        brightness: 90

Switching to night mode, set the light to the lowest level, we generally leave it running overnight, at this level, the consumption is about 2W, which really isn't much:

  - alias: Nuit
      - platform: state
        entity_id: input_select.current_mode
        state: "Nuit"
      service: light.turn_on
        entity_id: light.fibaro_system_fgd212_dimmer_2_level_3
        brightness: 10

Mode Switching Triggers

To automate switching from one mode to another, we have 3 time triggers, one for the evening, starting 2.5 hours before sunset

  - alias: "evening time"
      - platform: sun
        event: sunset
        offset: "-02:30:00"
      service: input_select.select_option
        entity_id: input_select.current_mode
        option: "Soir"

One for the night, with a hard limit set at 1:30am:

  - alias: "night time"
      - platform: time
        hours: 1
        minutes: 30
        seconds: 0
      service: input_select.select_option
        entity_id: input_select.current_mode
        option: "Nuit"

And one to go back to daytime mode, using the sunrise time:

  - alias: "day time"
      - platform: sun
        event: sunrise
      service: input_select.select_option
        entity_id: input_select.current_mode
        option: "Jour"

Now we have the mode switching automatically, let's handle the dimming of lights when watching movies!

Media Playing

Now based of if we are in the evening mode or not, we want the media players to trigger dimming the light. I tried to add a check on the content type for kodi, to avoid the dimming when listening to music, but it apparently dosen't work, and I didn't find out why yet.

For kodi:

  - alias: "Kodi paused/stopped"
      - platform: state
        entity_id: media_player.kodi
        from: 'playing'
      - condition: state
        entity_id: input_select.current_mode
        state: Soir
      - condition: template
        value_template: '{{ states.media_player.kodi.media_content_type != "video" }}'
      service: light.turn_on
      entity_id: light.fibaro_system_fgd212_dimmer_2_level_3
        brightness: 90

  - alias: "Kodi playing"
      - platform: state
        entity_id: media_player.kodi
        to: 'playing'
      - condition: state
        entity_id: input_select.current_mode
        state: Soir
      - condition: template
        value_template: '{{ states.media_player.kodi.media_content_type != "video" }}'
      service: light.turn_on
      entity_id: light.fibaro_system_fgd212_dimmer_2_level_3
        brightness: 30

For chromecast:

  - alias: "chromecast paused/stopped"
      - platform: state
        entity_id: media_player.ratoncast
        from: 'playing'
      - condition: state
        entity_id: input_select.current_mode
        state: Soir
      service: light.turn_on
      entity_id: light.fibaro_system_fgd212_dimmer_2_level_3
        brightness: 90

  - alias: "chromecast playing"
      - platform: state
        entity_id: media_player.ratoncast
        to: 'playing'
      - condition: state
        entity_id: input_select.current_mode
        state: Soir
      service: light.turn_on
      entity_id: light.fibaro_system_fgd212_dimmer_2_level_3
        brightness: 30


Currently one thing I would like to automate is running a script that would switch off the tv/amp when both media player have been idling for 30 minutes and if my wife computer isn't on.

For the light, I would like to replace the modes/timeslots in most cases except the night by a lux detection, and adjust brightness based on how much light there is in the living room.

I want to add other light in this room for other situation, I would love to have some sensors for temperature and humidity both inside and outside, automate the curtains too, and add some kind opened door/window detection too.

The possibilities are endless, only money, and ability in diy is the limits!

And that wraps it up for this serie of post on home automation, I'll try to keep posting the changes and hopefully my DIY sensors/controller based on ESP8266 in the near future.

31 juillet 2016 12:42:45 -- tags [ home-automation , home-assistant , light , dimming , configuration , yaml ]


Home Automation (part 2)

Note: Part 1 is here

Electrical setup for living room light

Our new place doesn't have any ceiling lights in the living room, but one switch activated wall plug. It is driven by a 2 way setup of 2 switches, and I decided to put the fibaro dimmer in one of them, and replace the standard switch by a push button.

The fibaro dimmer2 has 2 switch inputs, so I took a 2 button ones, and set it up in place of one of the switches, and use a single for the other one.

Currently it works like this:

  • entrance: single switch
  • near tv: double switch
  • single push: light on, same level of dimming as last use, another push to power off
  • double push: light on, full brightness
  • long press: starts at lower power, and slowly rise to allow control of dimming

Using the z-wave grouping, the second switch controls the dimming plug, where I have a floor lamp plugged for now.


First thing first, the usb z-wave dongles are generally seen simplay as usb serial ports, so it will work on almost any system, as long as the software you wan to use runs on it too.

Current setup

I currently plugged the dongle on my NAS, running FreeNAS, in a jail, this caused some issues I'll explain later when detailing the softwares I tested. The computer is an AMD-E350 which is a dual core 1.6GHz, with 8GB of ram.

Likely futur setup

I will probably move the home automation to a separate machine, a lot of people use raspberrypi for this, but I will probably take an odroid-c2, The reason being I do plan on adding voice control, and latency will play a big part in the feeling of it, faster cores will ilkely help a lot in this. We will see.

Testing the softwares

There are various open source options out there to setup your own home automation box:

  • OpenHAB: v1 and v2 (in beta), a pretty huge beast, in java, but very powerful, with a lot of features
  • Domoticz: written in C, it was pretty appealing to me at first (see below)
  • Home-Assistant: written in python, pretty easy to start with
  • Jeedom: opensource but limited if you don't subscribe

I didn't try Jeedom as I wasn't fond of the subscription thing but I did try, or at least try to try, the others.

NOTE: There are other options, I didn't search everything, and didn't had time to try them all, so I only mention the ones I planned to test at first.


OpenHAB is kinda the go to home automation software, it is well known, well supported, have a lot of features, the version2 has better way to handle things from the UI instead of fiddling with the configuration file, but the issue was that I run FreeBSD/FreeNAS: it comes with packages lib/modules/whatever the java world called them, including the one that handles talking to the serial port.

Unfortunately, there is a bug on FreeBSD with the shipped version, and I didn't dare rebuild everything myself just to patch this lib, so after a while I gave up for now. I still plan to give it a shot if things get easier to get it to work.

If you happen to have more motivation than I and want to try, there is a page about making openhab working on FreeBSD.


Domoticz was the second one I tried, mostly because it is written in C and I felt like it would be easier to fiddle inside it if needed. And I did. I had to fiddle in the code almost straight away, as the USB serial port showed up as /dev/ttyU0 but also as /dev/cuaU0. For some reason, the ifdefs in domoticz code are choosing ttyU for FreeBSD, but on my setup, this one wasn't working, wehereas the cuauU one worked, so I patched it and went with this.

I did set it up, and started using it. One nice thing it had too, is the z-wave network tools, monitongi, inclusion mode etc. That is pretty nice.

I was pretty happy until about 24h later, when for some reason it became pretty slow, like it took about 30 seconds to turn the lights on. So after a quick search, and not finding anything obvious, I went to try another one.

Home Assistant

Home Assistant (HA) is the one I still run today, it is pretty slick, setting it up was fairly easy and I didn't had to patch it. But it requires that you install the python-OpenZWave pacakge, but everything is explained on the getting-started/z-wave page, and it wasn't too hard to do. It has some services about the z-wave network that you can call, but it isn't as integrated as it is in domoticz, I actually didn't had to play with it, as the z-wave itself was already setup at this stage, I simply enable the z-wave component in HA, configured the serial port, and fire it up, and it detected the devices and they worked like a charm.

At first, it is a bit hard to understand what goes where in the configuration, or what the services/stats things in the web-ui are, but once you get a grasp of the basic principles, it is pretty easy to understand, and quite powerful.

Python-lovers will be confortable with the yaml configuration and the use of templates (as in jinja-templates) for some advanced usage.

Let's stop here for this part, the third one will talk about my current HA configuration.

16 juillet 2016 13:32:21 -- tags [ home-automation , z-wave , openhab , domoticz , home-assistant ]


Home Automation begins (part 1)


So we moved, again, back to Versailles. I had the bad idea to start looking into home automation before we even moved, and started wondering what I wanted to have or to do.

Defining the need

The thing I was interested in at first was simply controlling the lights, I wanted to be LED based and dimmable. I like LED strips, but 12 or 24v things means you need a converter, and the dimming should be done at this level, which kind of make the controlling remotely in software harder to achieve.

Wandering online I discovered there now are 220v LED strips, you can't cut them anywhere like most 12v, but that was fine by me, so I got a 10m of it, and a few connectors and stuff to go with it.

I then looked at the various protocol used, and pretty quickly found that there currently is more devices available for z-wave and decided I'd go with it.

At first I was about to buy an already made box, and realized there were pretty amazing open source software available and could be installed on a computer or a raspberrypi with an usb dongle.

Looking online, I then realized all the things you could do with the so called Home Automation, and ended up with a kinda crazy goal:

  • light control
  • temperature and humidity monitoring
  • heaters control
  • power consumption monitoring
  • door and windows opening monitoring
  • alarm
  • smoke detector

The biggest drawback on z-wave is its price, every module is expensive, when you sum up all the thing I listed before, the bill was fairly high and I decided to start small instead of getting everything right away.

First order

So I ordered 4 things:

Once I received this and the LED strip, I started playing with it, simply with the plug as we didn't move yet.

It does flicker (the bypass was for the fibaro module, not the plug), but it works, first test was encouraging, first step completed.

Continue to Part 2

15 juillet 2016 12:48:03 -- tags [ home-automation , z-wave , dimmer , dimmable , led ]


Decipher ESP packets with Scapy

I'm currently working on implementing support for hardware cryptographic engine on a board we support at work. Currently it is not working and I was trying to debug it, thus trying to see what is happening. I tcpdumped the packets received, and tried to have a look at it. Sadly, for some reason, the SAs I added in wireshark do not match and he doesn't want to use them.

I then discovered that scapy now has support for that, and it was actually added by a colleague... Should have started there! I only found the issues on bitbucket showing how to use it so I thought it would be worth it to write a quick how-to, once got it down it is pretty straightforward, but knowing what to look for may not be obvious in the first place.

So here is a an example usage for an AES-CBC with no auth packet:

pkts = rdpcap("path/to/file.pcap")

Here is the ESP packet:

###[ Ethernet ]###
  dst= 56:1e:7d:aa:e0:b3
  src= 68:05:ca:15:d1:99
  type= IPv4
###[ IP ]###
     version= 4L
     ihl= 5L
     tos= 0x0
     len= 140
     id= 0
     flags= DF
     frag= 0L
     ttl= 64
     proto= esp
     chksum= 0x2544
###[ ESP ]###
        spi= 0x22
        seq= 1
        data= '\xa7\[...]'

Then create the SA and decrypt:

sa = SecurityAssociation(ESP, spi=0x22, crypt_algo='AES-CBC', crypt_key='your_key')
res = sa.decrypt(pkts[0].getlayer(IP))

The result showing the icmp packet:

###[ IP ]###
  version= 4L
  ihl= 5L
  tos= 0x0
  len= 104
  id= 0
  flags= DF
  frag= 0L
  ttl= 64
  proto= ipv4
  chksum= 0x2596
###[ IP ]###
     version= 4L
     ihl= 5L
     tos= 0x0
     len= 84
     id= 36470
     flags= DF
     frag= 0L
     ttl= 63
     proto= icmp
     chksum= 0x9805
###[ ICMP ]###
        type= echo-request
        code= 0
        chksum= 0x7cd1
        id= 0x9df
        seq= 0x1
###[ Raw ]###
       load= '\x9fTMW[...]'

And here you go.

31 mai 2016 13:35:34 -- tags [ scapy , python , IPsec , cipher , crypto ]



As a reference, here is my current .vimrc file, stripped from a few things specific to my current job:

" ---------------
" Start Plug
" ---------------
call plug#begin('~/.vim/plugged')

" git
Plug 'tpope/vim-fugitive'

" utils
Plug 'rking/ag.vim'         " ag fro grepping in vim
Plug 'tpope/vim-commentary' " gcc gc etc for commenting
Plug 'tpope/vim-surround'   " change surrounding chars
Plug 'tpope/vim-unimpaired' " [<space> and more
Plug 'tpope/vim-sensible'   " default settings that makes sense
"Plug 'tpope/vim-rsi'        " shell/emacs binding in insert mode
Plug 'tpope/vim-tbone'      " tmux integration in vim
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'

" wiki + tw
Plug 'vimwiki/vimwiki'
Plug 'tbabej/taskwiki'

" appearance
Plug 'powerline/powerline'
Plug 'noah/fu'

" Add plugins to &runtimepath
call plug#end()

" ---------------
" Behavior
" ---------------
syntax on
set nobackup
set undodir=~/etc/vim/undo
set history=1000
set undolevels=1000
set wildignore=*.swp,*.bak,*.pyc,*.class
set hidden " allow changing buffer from unsaved

set ai
set cinoptions=(0
set cindent
set nocompatible
set ruler
set hlsearch
set wildmode=list:longest "bash like completion

set mouse=vn
set clipboard=unnamed

filetype plugin on
if has("autocmd")
  autocmd BufRead *.txt set tw=78 sw=2 ts=2 et
  autocmd BufRead *.py set tw=80 sw=4 ts=4 et
  autocmd BufRead *.sh set ts=4 sw=4 et noai nocin
  autocmd Bufread *.wiki set tw=80 ts=4 sw=4 et ai

" spellcheck in rst
autocmd FileType rst setlocal spell spelllang=en_us
autocmd Filetype rst set tw=80 sw=2 ts=2 et

" ---------------
" Appearance
" ---------------
set term=xterm-256color
set t_Co=256
set background=dark
set listchars=tab:▸\ ,eolcolorscheme fu

set laststatus=2
set cursorline
set nocursorcolumn
set colorcolumn=80
highlight ColorColumn ctermbg=16
highlight CursorLine ctermbg=23 term=bold cterm=bold

" Syntax error hilighting, this should be after colorscheme
syntax match Error "\(^\t.*\n\)\@<= \+"
syntax match Error "\(^ \+.*\n\)\@<=\t\+"
autocmd FileType c* syntax match Error '\%>80v.\+'

" enable powerline
set rtp+=/home/bleader/.vim/plugged/powerline/powerline/bindings/vim

" enable fzf
set rtp+=~/.fzf

" ---------------
" Binds
" ---------------

" use , for map leader, feel easier than \
let mapleader=","

set pastetoggle=<F9>

" edit/reload vimrc quickly
nmap <silent> <leader>ev :e $MYVIMRC<CR>
nmap <silent> <leader>sv :so $MYVIMRC<CR>

" allow use of ; in place of :
nnoremap ; :

" clear search hilight
nmap <silent> <leader>/ :nohl<CR>

" Error list binds
nnoremap <leader>n :cn<CR>zz
nnoremap <leader>p :cp<CR>zz

" compute line through rcpy
map <F4> yypV:!rcpy<CR>

" enable/disable list
imap <F1> <C-o>:set nolist!<CR>
map <F1> <ESC>:set nolist!<CR>

" moving around centering
nnoremap n nzz
nnoremap N Nzz
nnoremap * *zz
nnoremap # #zz
nnoremap g* g*zz
nnoremap g# g#zz
nnoremap [[ [[zz
nnoremap ]] ]]zz
nnoremap {{ {{zz
nnoremap }} }}zz

" debug printf in C
map <leader>dd oprintf("%s:%d\n", __func__, __LINE__);<Esc>==

" paste selection to haste
vnoremap Y <esc>:'<,'>:w !haste<CR>

" ---------------
" ---------------
nnoremap <leader>gd :Gvdiff<CR>
nnoremap <leader>gs :Gstatus<CR>
nnoremap <leader>gl :Glog --<CR>:copen<CR>
nnoremap <leader>gL :Git l<CR>
nnoremap <leader>gg :Ggrep <cword><CR>
nnoremap <leader>ag :Ag <cword><CR>
nnoremap <leader>gc :Gcommit -s -v<CR>
nnoremap <leader>gS :Git show<CR>
nnoremap <leader><leader>gS :Git show <cword><CR>
autocmd BufReadPost fugitive://* set bufhidden=delete

let g:git_branch_status_head_current=1 
let g:git_branch_status_text="branch "
let g:git_branch_status_nogit="no git"
let g:git_branch_status_around="{}"

fu! GitReview()
    Glog --reverse origin/master.. -- .
command Greview call GitReview()
nnoremap <leader>gr :Greview<CR>

" ---------------
" Cscope bindings and configuration (autoload of cscope.out)
" ---------------
nmap <leader>cs :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <leader><leader>cs :vert scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <leader>cg :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <leader><leader>cg :vert scs find g <C-R>=expand("<cword>")<CR><CR>
nmap <leader>cc :cs find c <C-R>=expand("<cword>")<CR><CR>
nmap <leader><leader>cc :vert scs find c <C-R>=expand("<cword>")<CR><CR>
nmap <leader>ct :cs find t <C-R>=expand("<cword>")<CR><CR>
nmap <leader><leader>ct :vert scs find t <C-R>=expand("<cword>")<CR><CR>
nmap <leader>ce :cs find e <C-R>=expand("<cword>")<CR><CR>
nmap <leader><leader>ce :vert scs find e <C-R>=expand("<cword>")<CR><CR>
nmap <leader>cf :cs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <leader><leader>cf :vert scs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <leader>ci :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <leader><leader>ci :vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <leader>cd :cs find d <C-R>=expand("<cword>")<CR><CR>
nmap <leader><leader>cd :vert scs find d <C-R>=expand("<cword>")<CR><CR>

if has("cscope") && filereadable("/usr/bin/cscope")
   set csprg=/usr/bin/cscope
   set csto=0
   set cst
   set nocsverb
   " add any database in current directory
   if filereadable("cscope.out")
      cs add cscope.out
   " else add database pointed to by environment
   elseif $CSCOPE_DB != ""
      cs add $CSCOPE_DB
   set csverb

fu! CscopeReload()
   cscope kill 0
   !cscope -R -b
   cscope add cscope.out

nmap <leader>cR :call CscopeReload()<CR>

" edit .configs
map <leader>uu ^i# <esc>$xxA is not set<esc>
map <leader>ss ^xxEld$a=y<esc>

let g:fzf_command_prefix="Fzf"
nnoremap <leader>f :FzfFiles<cr>
nnoremap <leader>b :FzfBuffers<cr>
inoremap <expr> <c-x><c-k> fzf#vim#complete#word({'left': '15%'})

30 décembre 2015 13:26:14 -- tags [ vim , configuration ]