Итак, вот что у меня получилось:
Вложение:
			 motion_detector.png [ 261.11 Кб | Просмотров: 17533 ]
			motion_detector.png [ 261.11 Кб | Просмотров: 17533 ]
		
		
	 Цепочка C1, R2 и Q2 дёргают сброс wifi устройства. Q1 выполняет роль блокировки, до тех пор пока устройство не выполнит все функции и не перейдёт в режим сна. В ходе экспериментов спалил пару транзисторов Q1, пока не дошло, что R1 нужен обязательно, иначе импульсом пробивается сток-исток (а может и паразитный диод?).
R3 и R4 делит напряжение пропорционально до 3.3 вольта (максимум аналогового входа) при 4.2 на аккумуляторе и отправляет это значение по указанному адресу в скетче, он представлен ниже (не понял как под кат убрать):
Код:
#include <ESP8266WiFi.h>        // Include the Wi-Fi library
const byte holdPin = 12; // The pin which holds the ESP/NodeMCU in working state during data sends
const byte ledPin = 13; // Test pin for led
const int analogInPin = A0; // The pin where tests battery voltage
const char* ssid     = "SSID_NAME";         // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "password";     // The password of the Wi-Fi network
const char* host = "192.168.0.10"; // Either name or IP of the host to send data
const int httpPort = 10000; // The port
int sensorValue = 0;
int outputValue = 0;
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(holdPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  digitalWrite(holdPin, HIGH);
  sensorValue = analogRead(analogInPin);
  outputValue = map(sensorValue, 0, 1024, 0, 42); // e.g 4.2 Voltage for Li-Ion battery
  
  Serial.begin(115200);         // Start the Serial communication to send messages to the computer
  delay(10);
  Serial.println('\n');
  Serial.println();
  Serial.print("MAC: ");
  Serial.println(WiFi.macAddress()); // Send the MAC address of the ESP8266 to the computer
  Serial.println();
  Serial.print("Battery Voltage: ");
  Serial.print(outputValue);
  // Connect to the network
  Serial.println();
  WiFi.begin(ssid, password);             
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");
  int i = 0;
  while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print(++i); Serial.print(' ');
    if (i > 10) {
      Serial.println('\n');
      Serial.print("Connection failed to ");
      Serial.println(ssid);
      ESP.deepSleep(0);
    }
  }
  Serial.println('\n');
  Serial.println("Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer
  // Logging data to cloud
  Serial.println();
  WiFiClient client;
  Serial.print("Sending data to ");
  Serial.println(host);
  // Use WiFiClient class to create TCP connections
  int l = 0;
  while (!client.connect(host, httpPort)) {
    delay(1000);
    Serial.print(++l); Serial.print(' ');
    if (l > 10) {
      Serial.println('\n');
      Serial.print("Sending failed to ");
      Serial.println(host);
      ESP.deepSleep(0);
    }
  }
  // This will send the request to the server
  client.print("Host: " + WiFi.hostname() + " / Battery Voltage: " + outputValue + " \r\n");
  delay(500);
  client.stop();
  Serial.println("\r"); 
  Serial.println("Data sent, communication closed!");  
  delay(100);
  ESP.deepSleep(0); 
}
void loop() {
}
const char* ssid   - имя точки доступа
const char* password - пароль
const char* host - куда отправлять (имя или ip)
const int httpPort - порт
Далее я применил программу socat и шелл скрипт, который запускается каждый раз при получении данных от датчика:
Код:
socat TCP4-LISTEN:10000,fork,reuseaddr EXEC:./catch_motion_event.sh &
Код:
#!/bin/bash
while read line;
do
   beep
   echo "$(date +%F" "%H:%M:%S) / $line";
done >> catch_motion_event.log 
Каждое срабатывание регистрируется в лог и пикает спикер на сервере с запущеным socat.
Код:
# cat catch_motion_event.log 
2020-06-15 14:13:35 / Host: ESP-223311 / Battery Voltage: 42 
2020-06-15 14:14:07 / Host: ESP-223311 / Battery Voltage: 42 
2020-06-15 14:14:21 / Host: ESP-223311 / Battery Voltage: 42 
2020-06-15 14:16:06 / Host: ESP-223311 / Battery Voltage: 42 
2020-06-15 14:16:43 / Host: ESP-223311 / Battery Voltage: 42 
На этом пока всё.