Hello,
I want to open my garage from HASS thanks to a power relay and verify the status with a hall sensor.
If the code is properly working with a ESP8266, I would prefer to use an ESP32.
So instead of using library #include <ESP8266WiFi.h> I include #include “WiFi.h” the sensor for open/close works but not the power relay when I click on the button in HASS the ESP32 reboot with the following message :
Rebooting…
ets Jun 8 2016 00:22:57rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:9280
load:0x40080400,len:5848
entry 0x40080698
Guru Meditation Error: Core 0 panic’ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x400d98b8: 4fe237fe 5250450c 00000000
Core 0 register dump:
PC : 0x400d98be PS : 0x00060130 A0 : 0x800d99e0 A1 : 0x3ffe3ac0
A2 : 0x00000000 A3 : 0x0000000e A4 : 0x00000000 A5 : 0x00000004
A6 : 0x00000002 A7 : 0x00000002 A8 : 0x3f402df4 A9 : 0x3f402df4
A10 : 0x00000002 A11 : 0xbaad5678 A12 : 0x3ffaff3c A13 : 0x3ffb64a8
A14 : 0x00000008 A15 : 0x3ffe3bd0 SAR : 0x0000000a EXCCAUSE: 0x00000000
EXCVADDR: 0x00000000 LBEG : 0x4000c2e0 LEND : 0x4000c2f6 LCOUNT : 0x00000000Backtrace: 0x400d98be:0x3ffe3ac0 0x400d99dd:0x3ffe3ae0 0x400d9b68:0x3ffe3b20 0x400d9db9:0x3ffe3b60 0x4012144a:0x3ffe3b90 0x40121507:0x3ffe3bb0 0x400db1d1:0x3ffe3bd0 0x4008272e:0x3ffe3bf0 0x400829ac:0x3ffe3c20 0x40078f93:0x3ffe3c40 0x40078ff9:0x3ffe3c70 0x40079004:0x3ffe3ca0 0x400791a3:0x3ffe3cc0 0x400806ca:0x3ffe3df0 0x40007c31:0x3ffe3eb0 0x4000073d:0x3ffe3f20
Rebooting…
ets Jun 8 2016 00:22:57
`
This is the code :
// ------------Lib for ESP8266------------
//#include <ESP8266WiFi.h>
// ------------Lib for ESP32------------
#include “WiFi.h”
// ------------Lib for MQTT------------
#include <PubSubClient.h>// ------------WiFi credentials------------
const char* ssid = “xxxxxxxxxxxxxxxxx”;
const char* password = “xxxxxxxxxxx”;// ------------MQTT credentials------------
const char* mqtt_server = “xxxxxxxxxxxxxx”;
const char* mqtt_user = “xxxxxxxxxxx”;
const char* mqtt_password = “xxxxxxxxxxxxxxxx”;// ------------MQTT topics------------
#define garage_status_topic “ha/garage/door-status”
#define garage_command_topic “ha/garage/door-command”//------------PINS------------
#define pin_garage_command 12
#define pin_garage_status 13WiFiClient espClient;
PubSubClient client(espClient);String switch1;
String strTopic;
String strPayload;
const char* OPENING_ON = “OPEN”;
const char* OPENING_OFF = “CLOSED”;
int door_state = LOW;
int door_value = 0;// ---------------------------------------------------------------------------
// -----------------------------PROGRAM---------------------------------------
// ---------------------------------------------------------------------------// ------------Connect to WiFi------------
void setup_wifi() {
delay(10);
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(”.”);
}Serial.println(“”);
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}//Read when a MQTT message arrive
void onMessageReceived(char* topic, byte* payload, unsigned int length) {
payload[length] = ‘\0’;
strTopic = String((char*)topic);
if(strTopic == “ha/garage/door-command”)
{
switch1 = String((char*)payload);
if(switch1 == “ON”)
{
Serial.println(“ON”);
digitalWrite(pin_garage_command, HIGH);
delay(1500);
Serial.println(“OFF”);
digitalWrite(pin_garage_command, LOW);
}
else
{
Serial.println(“Invalid command”);
}
}
}//Publish MQTT message (from loop)
void publishDoorSensorState() {
if (door_state) {
client.publish(garage_status_topic, OPENING_OFF, HIGH);
} else {
client.publish(garage_status_topic, OPENING_ON, LOW);
}
}// ------------Connect to MQTT server------------
void setup_Mqtt(){
client.setServer(mqtt_server, 1883);
client.setCallback(onMessageReceived);
}// Reconnect to MQTT server if disconnected
void reconnect() {
while (!client.connected()) {
Serial.print(“Attempting to connect to MQTT server…”);
if (client.connect(“GarageOpenningDevice”, mqtt_user, mqtt_password)) {
Serial.println(“connected”);
client.subscribe(garage_command_topic);
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}// ------------SETUP & LOOP------------
void setup()
{
Serial.begin(115200);
setup_wifi();
setup_Mqtt();pinMode(pin_garage_command, OUTPUT);
digitalWrite(pin_garage_command, LOW);
pinMode(pin_garage_status, INPUT_PULLUP);
}
long lastMsg = 0;
bool doorOpen = false;void loop()
{
//to be sure that we are well connected
if (!client.connected()) {
reconnect();
}
client.loop();
bool sent = false;
// read the sensor once per sec
long now = millis();
if (now - lastMsg > 1000) {
lastMsg = now;
bool newDoorOpen = digitalRead(pin_garage_status);
if (doorOpen && !newDoorOpen) {
doorOpen = false;
Serial.println(“Door open”);
client.publish(garage_status_topic, “open”, true);
sent = true;
} else if (!doorOpen && newDoorOpen) {
doorOpen = true;
Serial.println(“Door closed”);
client.publish(garage_status_topic, “closed”, true);
sent = true;
}}
}
What’s wrong ?
Thanks
Lionel