Uruchomiłem ten czujnik na Arduino UNO i działa bez problemu. Mam dokładnie ten czujnik i kupiony z tego sklepu Ultradźwiękowy czujnik odległości ME007YS Botland - Sklep dla robotyków
Edit:
Dodałem wszystkie twoje sugestie kod wygląda obecnie tak:
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// WIFI
const char* ssid = "ssid";
const char* password = "pass";
WiFiClient espClient;
// MQTT
const char* mqtt_server = "192.168.99.11";
const char* mqtt_username = "uid";
const char* mqtt_password = "pass";
PubSubClient client(espClient);
//*****scheduler*************************************
unsigned long previousMillis = 0;
const long sec = 1000;
const uint8_t timeout1 = 1; //time proces1 [sec]
//const uint8_t timeout2 = 250; //time proces2 [sec]
boolean proces1 = false;
//boolean proces2 = false;
uint8_t time1 = 0;
//uint8_t time2 = 0;
// CZUJNIK
const int trigPin = 12;
const int echoPin = 14;
//define sound velocity in cm/uS
#define SOUND_VELOCITY 0.034
#define CM_TO_INCH 0.393701
long duration;
float distanceCm;
int laststate;
int newstate;
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());
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
// Serial.print("Message arrived [");
// Serial.print(topic);
// Serial.print("] ");
// for (int i = 0; i < length; i++) {
// Serial.print((char)payload[i]);
// }
// Serial.println();
//
// // Switch on the LED if an 1 was received as first character
// if ((char)payload[0] == '1') {
// digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// // but actually the LED is on; this is because
// // it is active low on the ESP-01)
// } else {
// digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
// }
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
//clientId += String(random(0xffff), HEX);
// Attempt to connect
//if (client.connect(clientId.c_str())) {
if (client.connect(clientId.c_str(),mqtt_username,mqtt_password)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void Scheduler() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis < sec) {
return;
}
previousMillis = currentMillis;
if (time1 < timeout1) {
time1 += 1;
}
else {
proces1 = true;
}
// if (time2 < timeout2) {
// time2 += 1;
// }
// else {
// proces2 = true;
// }
}
void setup()
{
//pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
laststate = 0;
newstate = 0;
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}
void loop()
{
if (!client.connected())
{ reconnect(); }
client.loop();
Scheduler();
if (proces1 == true) {
odczyt();
time1 = 0;
proces1 = false;
}
}
void odczyt()
{
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_VELOCITY/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
if (distanceCm > 80)
{
newstate = 0;
}
if (distanceCm < 75)
{
newstate = 1;
}
if (newstate != laststate)
{
//Serial.println(newstate);
//Serial.println(laststate);
DynamicJsonDocument doc(1024);
if (newstate == 0)
{
doc["occupancy"] = "off";
}
else
{
doc["occupancy"] = "on";
}
// Sending the request
doc["dystans"] = distanceCm;
char buffer[256];
serializeJson(doc, buffer);
Serial.println(buffer);
if (client.publish("DIY/CzujnikKibelek", buffer) == true)
{
Serial.println("Success sending message");
}
else
{
Serial.println("Error sending message");
}
laststate = newstate;
}
//delay(1000);
}
Jak dla mnie działa bardzo dobrze (fragment logu)
...........WiFi connected
IP address:
192.168.99.253
Attempting MQTT connection...connected
Distance (cm): 145.50
Distance (cm): 143.79
Distance (cm): 143.80
Distance (cm): 143.80
Distance (cm): 143.80
Distance (cm): 143.80
Distance (cm): 143.80
Distance (cm): 11.80
{"occupancy":"on","dystans":11.79800034}
Success sending message
Distance (cm): 145.03
{"occupancy":"off","dystans":145.0269928}
Success sending message
Distance (cm): 11.20
{"occupancy":"on","dystans":11.20300007}
Success sending message
Distance (cm): 143.36
{"occupancy":"off","dystans":143.3609924}
Success sending message
Distance (cm): 144.60
Distance (cm): 143.36
Distance (cm): 143.79
Distance (cm): 145.49
Distance (cm): 143.36
Distance (cm): 14.47
{"occupancy":"on","dystans":14.46700001}
Success sending message
Distance (cm): 145.04
{"occupancy":"off","dystans":145.0440063}
Success sending message
Distance (cm): 143.79
Distance (cm): 144.21
Distance (cm): 13.16
{"occupancy":"on","dystans":13.15799999}
Success sending message
Distance (cm): 14.94
Distance (cm): 14.88
Distance (cm): 15.28
Distance (cm): 15.54
Distance (cm): 17.49
Distance (cm): 145.04
{"occupancy":"off","dystans":145.0440063}
Success sending message
Distance (cm): 145.04
Distance (cm): 143.36
Distance (cm): 143.79
Distance (cm): 143.77