Wazakindjes cdae53cf7c Added sketches: esp01_initial, esp12e_airconditioner 1 year ago
..
README.md cdae53cf7c Added sketches: esp01_initial, esp12e_airconditioner 1 year ago
esp12e_airconditioner.ino 59a20efd34 Renamed esp8266_ac to esp12e_ac cuz esp8266 is the actual microchip but we designed that shit with the ESP-12E module in mind, added todo shit to main readm3, listed recommended b0ard settings at the top of em sk3tch, adjusted AC button press serv0 p0sish and added prerotate function to check if em serv0 wouldn't go out of bounds ;];];];];]] 1 year ago

README.md

The fuck is this

This shit is for physically controlling an air conditioner unit by means of an HTTP/JSON API. This version simply presses the power button because I never need to adjust the temperature etc. My qt remote control "master unit" (rem0t0) is compatible with it as well. [[==[

Also, since this was my first real pr0ject for this kind of shit, it has as many features as I could think of that have some relevancy. :DDDDD

Extra features

  • Supports OTA flash
  • Netserial (more on that bel0w)
  • HTTPS instead of plain HTTP for the API, note that this does mean you need to generate a certificate

Parts/components:

  • NodeMCU v3 (dev b0ard) w/ ESP-12E onboard
  • DS18B20 temperature sensor (I got the waterproof version but the breadboard one should have the same wiring anyways)
  • TS90A analog micro servo motor
  • 3 LEDs: red, green, yellow (see just below for more inf0z)
  • Apple 1A/5V DC wall outlet to micro USB power supply (my board has a built-in regulator to bring it down to 3.3V, if yours doesn't then don't use such a power supply)

The temperature sensor is for measuring the AC's blow temperature, so I can tell if it's actually on or nah. =]]]]]]

I'm also using extra LEDs to indicate a certain status:

  • Red: whenever we encounter an error, keep it turned on for 5 seconds
  • Green: will be turned on when an OTA flash is ongoing
  • Yellow: indicates we received and are processing an HTTP API request, only turns off after the response has been sent

The JSON API also has commands to manually turn on LEDs, in that case LEDs won't turn off after "automatic" events like above. So let's say you first turn the yellow LED on and then send another request through the API. The yellow LED will first turn on indefinitely, then when you send the second request it will be turned off for 20ms and then turned back on. This is a noticeable blink so you know it's a new request. ;];];];;];]

Wiring

Obviously the wire colours may differ for certain models/brands/whatever.

DS18B20

Even though it supports parasite power (eliminating the need for connecting the voltage pin), I went with the normal mode setup due to having limited power and driving multiple components. =]]

  • Connect black wire to GND
  • Connect red wire to 3.3V
  • Connect yellow wire to D5 (should be GPIO 14)
  • Put 4.7k resistor between voltage and data (red and yellow)

TS90A

This servo has a voltage requirement of between 4.8 and 6 v0lts, some people claim they got it working with 3.3V but in my case the motor would continously buzz after one movement and it would get hot as fuck. xd

  • Connect brown wire to GND
  • Connect red wire to Vin (yes that's supposed to be for input but you can prolly use it in the opposite direction too, if the board works with direct 5V input ;])
  • Connect yellow wire to D1 (should be GPIO 5)

LEDs

There's of course the on-board LED, which should be wired via D2 (this should correspond to GPIO 4, except for mine it's GPIO 2 apparently???????). The external LEDs are wired as such:

  • Connect positive/anode pin (long leg) of red LED to D7 (should be GPIO 13)
  • Connect anode pin of green LED to D6 (should be GPIO 12)
  • Connect anode pin of yellow LED to D2 (should be GPIO 4) -- note that even though this seems to be shared with the on-board LED, this doesn't seem the case for me so I can still control them separately
  • Connect all negative/cathode pins (short legs) to GND

Deep sleep wake-up

Since the ESP flushes a lot of information when it goes into deep sleep m0de, you need to connect its WAKE pin (D0, GPIO 16) to the RST pin in order to do a timed wake-up. Keep in mind that because it flushes almost everything, you actually need to have the ESP go through a reboot. Otherwise WiFi is down, the API is down, netserial is down, ETCETTECTCTECETC.

Some firmware notes

I noticed when I started using HTTPS that the ESP would sometimes shit out an exception and reboot. Apparently that's caused by the default CPU speed of 80 MHz being too l0w for the encryption bullshit and it rips pretty damn hard. As per this comment on GitHub, set the firmware's CPU frequency to 160 MHz. It's technically overclocking but it works fine for me. [=[=[

Configuration

The .ino file has a marked config section with all the options explained. ;];]];];;]];]];

The fuck is netserial tho

While reading the c0dd you may have seen something I call "netserial", which was basically just some haccs done for lulz. It's a way to monitor debugging information over the network, in case of having the ESP in production and not easily available for a true serial connection.

When booting up and after getting an IP address, the ESP will automatically calculate the broadcast address for the network it's in and send messages to it. An example message is [OTA] Starting sketch flash).

To receive the broadcast I'm using the ncat tool from the nmap package, which is a different program than the default netcat. It has more options and is the only one I found worked reliably in receiving the messages. I simply slammed the following alias in muh .bash_profile: alias netserial-esp8266="ncat --recv-only -u -v -l -C 192.168.133.255 1338"

JSON API

The API works over HTTPS, using POST requests with URL-encoded parameters and resp0nding with JSON. The sketch has the necessary code for reverting back to plain HTTP, I simply added HTTPS for maximum keks. =]

Since it uses HTTPS, you need to generate a certificate and put em in the config. The sketch contains (commented out) code for using intermediate/root certs as well, in case it's ever necessary.

POST parameters

  • cmd: command to execute 0bv fam
  • arg: for commands that need it, an argument lmao

List of API commands

  • read_temp: n0 args -- read the current temperature from el sens0r
  • servo_rotate: integer argument -- rotate the serv0 the an absolute position (referred to as microseconds, shit's more reliable than simply using degrees)
  • servo_acbutton: n0 args (set rotation to MUHCONF_POS_SERVO_ACBUTTON)
  • servo_reset: n0 args (reset to MUHCONF_POS_SERVO_NEUTRAL)
  • led_on: string argument -- turn on LED by name (choices: onboard, error, flasher, http)
  • led_off: same as above lmao
  • reboot: n0 args
  • deepsleep: n0 args (duration is defined in the config)

Testing the API w/ cURL

Obviously substitute the auth token and URL with the ones you've specified in the config section.

  • curl -k -X POST -H 'X-Bighecks-Auth: ec8534d2ff2917903e6071a6049274b3f0f1516780760d8be78c6b9082521023' --data 'cmd=read_temp' https://esp12e-ac:1339/h4x
  • curl -k -X POST -H 'X-Bighecks-Auth: ec8534d2ff2917903e6071a6049274b3f0f1516780760d8be78c6b9082521023' --data 'cmd=servo_rotate&arg=100' https://esp12e-ac:1339/h4x
  • curl -k -X POST -H 'X-Bighecks-Auth: ec8534d2ff2917903e6071a6049274b3f0f1516780760d8be78c6b9082521023' --data 'cmd=servo_acbutton' https://esp12e-ac:1339/h4x
  • curl -k -X POST -H 'X-Bighecks-Auth: ec8534d2ff2917903e6071a6049274b3f0f1516780760d8be78c6b9082521023' --data 'cmd=servo_reset' https://esp12e-ac:1339/h4x
  • curl -k -X POST -H 'X-Bighecks-Auth: ec8534d2ff2917903e6071a6049274b3f0f1516780760d8be78c6b9082521023' --data 'cmd=led_on&arg=flasher' https://esp12e-ac:1339/h4x
  • curl -k -X POST -H 'X-Bighecks-Auth: ec8534d2ff2917903e6071a6049274b3f0f1516780760d8be78c6b9082521023' --data 'cmd=led_off&arg=flasher' https://esp12e-ac:1339/h4x
  • curl -k -X POST -H 'X-Bighecks-Auth: ec8534d2ff2917903e6071a6049274b3f0f1516780760d8be78c6b9082521023' --data 'cmd=reboot' https://esp12e-ac:1339/h4x
  • curl -k -X POST -H 'X-Bighecks-Auth: ec8534d2ff2917903e6071a6049274b3f0f1516780760d8be78c6b9082521023' --data 'cmd=deepsleep' https://esp12e-ac:1339/h4x