Triggering Gotify with an ESP8266/ESP32/Arduino

(deutschsprachige Version hier)


as of: 2020-02-11

What's it all about?

Gotify is a service written in Go and running on Linux. It issues push notifications to mobile phones and browsers. It is a self-hosted open source project and can be added to a SBC like a Raspberry Pi that already exists for other purposes and can access open ports to the Internet. Gotify has its own web server and push notifications can be triggered by using its HTTP REST API.

An example usage is to forward alarms (temperature, opened doors, ...) via Gotify to your mobile phone. Fortunately, the corresponding app is available google-free on the F-Droid store.

As a drawback, there are no (cloud-based) Gotify services on the net. So self-hosting is the only possibility. Similar services exist, though. But expect to spend some money for using these services or to use a Google account to log in.


The Problem

I needed a couple of attempts to successfully trigger Gotify from my ESP8266 via HTTP-POST, because Gotify is somewhat strict in parsing JSON messages. Therefore I decided to publish a quick-and-dirty proof-of-concept for the communication. In this example, when a RXB6 radio module receives the ASK modulated signal of a door sensor (433 MHz), Gotify gets triggered. See code above. For every other use case, the RXB6 stuff can be dismissed, i.e., the RCSwitch and mySwitch lines. The communication with Gotify takes place in void post only.


Some Words About the RXB6 Radio Module

Each module uses its own fixed ID which has a length of up to seven digits (decimal). On opening and closing the sensor transmits a different ID where the ID sent when closing is (according to my observations) lower by the value of 4. The coverage of the signal is about one wall farther than WiFi. But usually does not reliably exceed 20 metres within a building. I did not test the outside coverage. Also, a good antenna at the receiver is likely to increase the coverage.


Code

/*
  simple example for receiving. Modification by Thomas Rudolph.
  Source: https://github.com/sui77/rc-switch/
*/
#include <RCSwitch.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>


RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  Serial.println("\r\nMoin!");
  
  mySwitch.enableReceive(4);
  wifiInit();
}

void wifiInit() {
  WiFi.mode(WIFI_STA);
  WiFi.begin("yourSSID","yourWiFiKey");
  Serial.print("Verbinde mit WLAN ");

  uint8_t temp = 0;

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    temp++;
    if (temp % 7 == 0) Serial.print(".");
    if (temp > 100) ESP.restart(); // reboot if no connection can be established
  }
  Serial.println(WiFi.localIP());
}

void post(bool status) {
  HTTPClient http;
  http.begin("http://yourgotifyserver:90/message?token=yourgotifytoken");

  http.addHeader("Content-Type", "application/json");
  
  uint16_t httpResponseCode;

  if (status) httpResponseCode =
    http.POST("{\"message\": \"is open\",\"title\": \"Door\",\"priority\": 5}");
  if (!status) httpResponseCode =
    http.POST("{\"message\": \"is closed\",\"title\": \"Door\"}");

  if(httpResponseCode>0) {

    String response = http.getString();

    Serial.println(httpResponseCode);
    Serial.println(response);

  } else {

    Serial.print("Error on sending POST: ");
    Serial.println(httpResponseCode);
  }

  http.end();
}


void loop() {
  if (mySwitch.available()) {
    
    Serial.print("Received ");
    Serial.print( mySwitch.getReceivedValue() );
    Serial.print(" / ");
    Serial.print( mySwitch.getReceivedBitlength() );
    Serial.print("bit ");
    Serial.print("Protocol: ");
    Serial.println( mySwitch.getReceivedProtocol() );

    if (mySwitch.getReceivedValue() == 4410158) post(1);
    if (mySwitch.getReceivedValue() == 4410154) post(0);

    mySwitch.resetAvailable();
    delay(1000);
  }
}

Gotify web surface
Website of the Gotify server

Über mich und Kontakt

Sie finden hier Kontaktmöglichkeiten und weitere Informationen über mich.



     
Impressum, Schlichtungsstelle und Datenschutzerklärung