Code: Select all
// V2
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Esp.h>
#define machineId 1
const char *ssid = "Wifi ssid";
const char *password = "Wifi password";
const char *serverUrl = "my api";
const int relayPin = 2;
bool cutDetected = false;
WiFiClient wifiClient;
void ICACHE_RAM_ATTR countCut() {
static unsigned long lastInterruptTime = 0;
unsigned long currentDebounceTime = millis();
// Debounce
if (currentDebounceTime - lastInterruptTime > 2000) {
lastInterruptTime = currentDebounceTime;
cutDetected = true;
}
}
void setup() {
Serial.begin(115200);
delay(10);
pinMode(relayPin, INPUT_PULLUP);
// Connect to WiFi
Serial.println();
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP().toString());
// Attach interrupt
attachInterrupt(digitalPinToInterrupt(relayPin), countCut, FALLING);
delay(5000);
}
void loop() {
if (cutDetected) {
cutDetected = false;
// JSON Payload
String json = "{";
json += "\"id\": \"" + String(machineId) + "\"";
json += "}";
// Setup HTTP request
HTTPClient http;
http.begin(wifiClient, serverUrl);
http.addHeader("Content-Type", "application/json");
// Setup HTTP request
int httpResponseCode = http.POST(json);
if (httpResponseCode > 0) {
Serial.print("HTTP Response Code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("HTTP POST request failed. Error code: ");
Serial.println(httpResponseCode);
Serial.println(http.errorToString(httpResponseCode));
}
http.end();
}
}
load 0x4010f000, len 3424, room 16
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8
tail 0
chksum 0x2b
csum 0x2b
v00046d40
~ld
����n�r��n|��l�rlc��|r�l�n��n�l`��r�l�l��
And then connects to wifi again.
I even tried using a second relay, to isolate the signal. But when I connect one of the relay pins to ground, it sometimes starts restarting again.