I would think 192.168.1.32, which is the IP address of the ESP8266
IPAddress local_IP(192, 168, 1, 32);
// Code written in C++ for Aruduino. GreyThree1
// Poll an ultrasonic sensor module JSN-SR04T and send the distance read to an MQTT broker at 192.168.1.30
// ctl-shift M to open console
//GreyOrchard
//GreyLowEast
//GreyLowWest
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP.h>
#define GreyOrchardTrigPin D1
#define GreyOrchardEchoPin D2
#define GreyEastTrigPin D3
#define GreyEastEchoPin D4
#define GreyWestTrigPin D5
#define GreyWestEchoPin D6
IPAddress local_IP(192, 168, 1, 32);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 1, 0);
const char* ssid = "myssid"; // Enter your WiFi name
const char* password = "mypassword"; // Enter WiFi password
const char* Server = "192.168.1.30";
const int Port = 1883;
long GreyOrchardDuration, GreyOrchardDistance; // Duration used to calculate distance
long GreyEastDuration, GreyEastDistance; // Duration used to calculate distance
long GreyWestDuration, GreyWestDistance; // Duration used to calculate distance
char msg[50]; // Set msg variable for conversion for
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet))
{
Serial.println("STA Failed to configure");
}
// Connect to WiFi
WiFi.mode(WIFI_STA);
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Port defaults to 8266
ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("GreyThree");
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
ArduinoOTA.onStart( {
Serial.println("Start");
});
ArduinoOTA.onEnd( {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Connected to the WiFi networkdddd");
// Setup I/O pins for ultrasound.
pinMode(GreyOrchardTrigPin,OUTPUT);
pinMode(GreyOrchardEchoPin,INPUT);
pinMode(GreyEastTrigPin,OUTPUT);
pinMode(GreyEastEchoPin,INPUT);
pinMode(GreyWestTrigPin,OUTPUT);
pinMode(GreyWestEchoPin,INPUT);
// Connect to the broker.
client.setServer(Server, Port);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266Client")) {
Serial.println("connected");
}
else {
Serial.print("failed with state ");
Serial.println(client.state());
delay(2000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
// This could be a way to have MQTT from the web page, eg read the level
// client.subscribe("tank1");
void loop() {
// GreyOrchard Read
digitalWrite(GreyOrchardTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(GreyOrchardTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(GreyOrchardTrigPin, LOW);
GreyOrchardDuration = pulseIn(GreyOrchardEchoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
GreyOrchardDistance = GreyOrchardDuration/5.82;
Serial.print(GreyOrchardDistance);
Serial.println(" GreyOrchardDistance mm");
//We sometimes get zero readings for some reason, take them out
if(GreyOrchardDistance > 0) {
//Publish the variable "GreyOrchardDistance" to topic "GreyOrchard" on the broker.
sprintf(msg, "%ld", GreyOrchardDistance);
client.publish("GreyOrchard", msg); //Topic name, input message
}
// GreyEast Read
digitalWrite(GreyEastTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(GreyEastTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(GreyEastTrigPin, LOW);
GreyEastDuration = pulseIn(GreyEastEchoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
GreyEastDistance = GreyEastDuration/5.82;
Serial.print(GreyEastDistance);
Serial.println(" GreyEastDistance mm");
//We sometimes get zero readings for some reason, take them out
if(GreyEastDistance > 0) {
//Publish the variable "GreyEastDistance" to topic "GreyEast" on the broker.
sprintf(msg, "%ld", GreyEastDistance);
client.publish("GreyEast", msg); //Topic name, input message
}
// GreyWest Read
digitalWrite(GreyWestTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(GreyWestTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(GreyWestTrigPin, LOW);
GreyWestDuration = pulseIn(GreyWestEchoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
GreyWestDistance = GreyWestDuration/5.82;
Serial.print(GreyWestDistance);
Serial.println(" GreyWestDistancccccccccccccccccccce mm");
//We sometimes get zero readings for some reason, take them out
if(GreyWestDistance > 0) {
//Publish the variable "GreyWestDistance" to topic "GreyWest" on the broker.
sprintf(msg, "%ld", GreyWestDistance);
client.publish("GreyWest", msg); //Topic name, input message
}
delay(2000);
client.loop();
}