mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
130 lines
3.1 KiB
C++
130 lines
3.1 KiB
C++
#include <Wire.h>
|
|
#include <SPI.h>
|
|
#include <Adafruit_Sensor.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <PubSubClient.h>
|
|
|
|
|
|
const int gasPin = A0; //GAS sensor output pin to Arduino analog A0 pin
|
|
const char* ssid = "Xiaomi_Router";
|
|
const char* password = "UYQQMTHF";
|
|
|
|
const char* topic = "3DPrinterSensors";
|
|
const int mqtt_port = 1883;
|
|
const char* mqtt_server = "myhomie.be";
|
|
const char* topicLed = "3DPrinterLed";
|
|
const char* mqtt_client = "ESP8266Client-3DPrinter-Sensors";
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient client(espClient);
|
|
|
|
int redPin = 14;
|
|
int greenPin = 12;
|
|
int bluePin = 13;
|
|
|
|
unsigned long startMillis; //some global variables available anywhere in the program
|
|
unsigned long currentMillis;
|
|
const unsigned long period = 30000; //the value is a number of milliseconds => number of ms within every message
|
|
|
|
char message_buff[100];
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
Serial.println(F("Water test"));
|
|
|
|
WiFi.softAPdisconnect(true);
|
|
WiFi.begin(ssid, password);
|
|
|
|
bool status;
|
|
|
|
// Wait for connection
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.print("Connected to ");
|
|
Serial.println(ssid);
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
client.setServer(mqtt_server, mqtt_port);
|
|
client.setCallback(callback);
|
|
|
|
startMillis = millis(); //initial start time
|
|
|
|
Serial.println();
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
|
if(WiFi.status() != WL_CONNECTED){
|
|
Serial.println("Trying to reconnect !");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.println("Connected !");
|
|
}
|
|
|
|
if (!client.connected()) {
|
|
reconnect();
|
|
}
|
|
else
|
|
{
|
|
printValues();
|
|
}
|
|
client.loop();
|
|
}
|
|
|
|
|
|
|
|
void printValues() {
|
|
/*Serial.print("Humidity = ");
|
|
Serial.print(bme.readHumidity());
|
|
Serial.println(" %");*/
|
|
|
|
Serial.println(analogRead(gasPin));
|
|
//Serial.print("MQ-2 = ");
|
|
|
|
//Serial.println(" value");
|
|
delay(1000);
|
|
//Serial.println();
|
|
}
|
|
|
|
|
|
void callback(char* topic, byte* payload, unsigned int length) {
|
|
Serial.print("Message arrived [");
|
|
Serial.print(topic);
|
|
Serial.print("] ");
|
|
String payloadString;
|
|
for (int i = 0; i < length; i++) {
|
|
Serial.print((char)payload[i]);
|
|
payloadString += (char)payload[i];
|
|
}
|
|
Serial.println("");
|
|
}
|
|
|
|
void reconnect() {
|
|
// Loop until we're reconnected
|
|
while (!client.connected()) {
|
|
Serial.print("Attempting MQTT connection...");
|
|
// Attempt to connect
|
|
if (client.connect(mqtt_client, "thomas", "MyCore,1")) {
|
|
Serial.println("connected");
|
|
client.subscribe(topicLed);
|
|
String ipString = "{\"Name\":\"3DPrinterSensor\",\"IpAddress\":\""+ WiFi.localIP().toString()+"\"}";
|
|
ipString.toCharArray(message_buff, ipString.length()+1);
|
|
client.publish("IpAddress",message_buff);
|
|
} else {
|
|
Serial.print("failed, rc=");
|
|
Serial.print(client.state());
|
|
Serial.println(" try again in 5 seconds");
|
|
// Wait 5 seconds before retrying
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|