Meter shows 0.48V to 3.15V at the ESP-01 pin.
I have tried using a NodeMCU Lolin V3 with GPIO2 and I get the same behavior.. hangs on boot
I simply do a DigitalRead( ) on the pin and send it via MQTT.
Here is the code, but I do not think this is the issue. The built-in blue LED stays on right when I boot up, as there is a short on the circuitry....
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "xxxxxx";
const char* password = "xxxxxxx";
// Update these with the name of the sensor
const char* sensorname = "DEV1";
const char* mqtt_server = "xxxxxxxxxx"; /* "broker.mqtt-dashboard.com" test.mosquitto.org broker.hivemq.com */
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
int led = 0; // the pin that the LED is atteched to
int sensor = 3; // the pin that the sensor is atteched to (GPIO2 with the ESP-01S?)
int val = LOW; // variable to store the sensor status (value)
int lastval = LOW;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
}
void reconnect() {
digitalWrite(led, LOW); // turn LED OFF
delay(100); // delay 100 milliseconds
// Loop until we're reconnected
while (!client.connected()) {
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
byte willQoS = 0;
const char* willTopic = "lastwill";
const char* willMessage = sensorname;
boolean willRetain = true;
// Attempt to connect
if (client.connect(clientId.c_str(),"username", "password", willTopic, willQoS, willRetain, willMessage)) {
// Wait 2 seconds before retrying
delay(2000);
} else {
// Wait 2 seconds before retrying
delay(2000);
}
}
}
void setup() {
delay(1000);
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
val = digitalRead(sensor); // read sensor value
if (val != lastval) { // sensor see a change, we publish to the log
if (val==LOW) {
snprintf (msg, MSG_BUFFER_SIZE, "High electricity rate ends");
client.publish("electricity/log", msg); // publish
} else {
snprintf (msg, MSG_BUFFER_SIZE, "High electricity rate starts");
client.publish("electricity/log", msg); // publish
}
}
if (val == LOW){
snprintf (msg, MSG_BUFFER_SIZE, "low"); //
client.publish("electricity/state", msg); // publish the state
} else {
snprintf (msg, MSG_BUFFER_SIZE, "high"); //
client.publish("electricity/state", msg); // publish the state
}
delay(5000); // wait 60 senconds between measures
lastval = val;
}