voltage sensor via esp and mqtt
-
hello every one ,
i’m trying to make a smart voltage meter to measure voltage and send the readings to a mobile phone using MQTT and openhab .hardware and circuit diagram :
ESP8266-12E wifi module
voltage sensor
i have a problem in the code that have to be burned to the esp module .
i have a pre-prepared code of a smart fire sensor that sends a notification when fire detected . i’m trying to modify this code to work with my voltage meter .
those are the two codes :
complete fire sensor code :/* Device Title: MK-FireSensor V2 Device Description: MQTT Fire Sensor Device Explanation: This device connects to your existing fire detectors and when they go off this device sends an MQTT message to the server Device information: https://www.MK-SmartHouse.com/fire-sensor Author: Matt Kaczynski Website: http://www.MK-SmartHouse.com Code may only be distrbuted through http://www.MK-SmartHouse.com any other methods of obtaining or distributing are prohibited Copyright © 2016-2018 Note: After flashing the code once you can remotely access your device by going to http://HOSTNAMEOFDEVICE.local/firmware obviously replace HOSTNAMEOFDEVICE with whatever you defined below. The user name and password are also defined below. */ /* ---------- DO NOT EDIT ANYTHING IN THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING---------- */ #include <FS.h> //this needs to be first, or it all crashes and burns… #include <Servo.h> #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino #include <MQTTClient.h> //https://github.com/256dpi/arduino-mqtt #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager #include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson #include <ESP8266mDNS.h> #include <ESP8266HTTPUpdateServer.h> //define your default values here, if there are different values in config.json, they are overwritten. char host[34]; char mqtt_server[40]; char mqtt_port[6] = “1883”; char mqtt_topic[50]; char mqtt_isAuthentication[7] = “FALSE”; char mqtt_username[40]; char mqtt_password[40]; char update_username[40]; char update_password[40]; char update_path[34] = “/firmware”; //Unique device ID const char* mqttDeviceID; //Form Custom SSID String ssidAP = “MK-FireSensor” + String(ESP.getChipId()); //flag for saving data bool shouldSaveConfig = false; //callback notifying us of the need to save config void saveConfigCallback () { shouldSaveConfig = true; } //the time when the sensor outputs a low impulse long unsigned int lowIn; //the amount of milliseconds the sensor has to be low //before we assume all detection has stopped long unsigned int pause = 100; //sensor variables boolean lockLow = true; boolean takeLowTime; //the digital pin connected to the fire sensor’s output int sensorPin = 13; ESP8266WebServer httpServer(80); ESP8266HTTPUpdateServer httpUpdater; WiFiClient net; MQTTClient client; unsigned long lastMillis = 0; void connect(); void setup() { if (SPIFFS.begin()) { if (SPIFFS.exists("/config.json")) { //file exists, reading and loading File configFile = SPIFFS.open("/config.json", “r”); if (configFile) { size_t size = configFile.size(); // Allocate a buffer to store contents of the file. std::unique_ptr<char[]> buf(new char[size]); configFile.readBytes(buf.get(), size); DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject(buf.get()); json.printTo(Serial); if (json.success()) { strcpy(host, json["host"]); strcpy(update_username, json["update_username"]); strcpy(update_password, json["update_password"]); strcpy(mqtt_isAuthentication, json["mqtt_isAuthentication"]); strcpy(mqtt_username, json["mqtt_username"]); strcpy(mqtt_password, json["mqtt_password"]); strcpy(update_path, json["update_path"]); strcpy(mqtt_server, json["mqtt_server"]); strcpy(mqtt_port, json["mqtt_port"]); strcpy(mqtt_topic, json["mqtt_topic"]); } else { } } } } else { } //end read // The extra parameters to be configured (can be either global or just in the setup) // After connecting, parameter.getValue() will get you the configured value // id/name placeholder/prompt default length WiFiManagerParameter custom_text0(“ Select your wifi network and type in your password, if you do not see your wifi then scroll down to the bottom and press scan to check again. ”); WiFiManagerParameter custom_text1(“ Hostname/MQTT ID ”); WiFiManagerParameter custom_text2(“ Enter a name for this device which will be used for the hostname on your network and identify the device from MQTT. ”); WiFiManagerParameter custom_host(“name”, “Device Name”, host, 32); WiFiManagerParameter custom_text3(“ MQTT ”); WiFiManagerParameter custom_text4(“ Enter the details of your MQTT server and then enter the topic for which the device sends Fire Detector State MQTT messages to. If your server requires authentication then set it to True and enter your server credentials otherwise leave it at false and keep the fields blank. ”); WiFiManagerParameter custom_mqtt_server(“server”, “MQTT Server IP”, mqtt_server, 40); WiFiManagerParameter custom_mqtt_port(“port”, “MQTT Server Port”, mqtt_port, 5); WiFiManagerParameter custom_mqtt_topic(“topic”, “MQTT Out Topic”, mqtt_topic, 50); WiFiManagerParameter custom_mqtt_isAuthentication(“isAuthentication”, “MQTT Authentication?”, mqtt_isAuthentication, 7); WiFiManagerParameter custom_mqtt_username(“userMQTT”, “Username For MQTT Account”, mqtt_username, 40); WiFiManagerParameter custom_mqtt_password(“passwordMQTT”, “Password For MQTT Account”, mqtt_password, 40); WiFiManagerParameter custom_text5(“ Web Updater ”); WiFiManagerParameter custom_text6(“ The web updater allows you to update the firmware of the device via a web browser by going to its ip address or hostname /firmware ex. 192.168.0.5/firmware you can change the update path below. The update page is protected so enter a username and password you would like to use to access it. “); WiFiManagerParameter custom_update_username(“user”, “Username For Web Updater”, update_username, 40); WiFiManagerParameter custom_update_password(“password”, “Password For Web Updater”, update_password, 40); WiFiManagerParameter custom_device_path(“path”, “Updater Path”, update_path, 32); WiFiManagerParameter custom_text7(“ To reset device settings restart the device and quickly move the jumper from RUN to PGM, wait 10 seconds and put the jumper back to RUN. ”); WiFiManagerParameter custom_text8(””); //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; //set config save notify callback wifiManager.setSaveConfigCallback(saveConfigCallback); //set static ip //wifiManager.setSTAStaticIPConfig(IPAddress(10,0,1,99), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); //add all your parameters here wifiManager.setCustomHeadElement(".c{text-align: center;} div,input{padding:5px;font-size:1em;} input{width:95%;} body{text-align: center;font-family:oswald;} button{border:0;background-color:#313131;color:white;line-height:2.4rem;font-size:1.2rem;text-transform: uppercase;width:100%;font-family:oswald;} .q{float: right;width: 65px;text-align: right;} body{background-color: #575757;}h1 {color: white; font-family: oswald;}p {color: white; font-family: open+sans;}a {color: #78C5EF; text-align: center;line-height:2.4rem;font-size:1.2rem;font-family:oswald;}"); wifiManager.addParameter(&custom_text0); wifiManager.addParameter(&custom_text1); wifiManager.addParameter(&custom_text2); wifiManager.addParameter(&custom_host); wifiManager.addParameter(&custom_text3); wifiManager.addParameter(&custom_text4); wifiManager.addParameter(&custom_mqtt_server); wifiManager.addParameter(&custom_mqtt_port); wifiManager.addParameter(&custom_mqtt_topic); wifiManager.addParameter(&custom_mqtt_isAuthentication); wifiManager.addParameter(&custom_mqtt_username); wifiManager.addParameter(&custom_mqtt_password); wifiManager.addParameter(&custom_text5); wifiManager.addParameter(&custom_text6); wifiManager.addParameter(&custom_update_username); wifiManager.addParameter(&custom_update_password); wifiManager.addParameter(&custom_device_path); wifiManager.addParameter(&custom_text7); wifiManager.addParameter(&custom_text8); //reset settings - for testing //wifiManager.resetSettings(); //set minimu quality of signal so it ignores AP’s under that quality //defaults to 8% //wifiManager.setMinimumSignalQuality(); //sets timeout until configuration portal gets turned off //useful to make it all retry or go to sleep //in seconds wifiManager.setTimeout(120); //fetches ssid and pass and tries to connect //if it does not connect it starts an access point with the specified name //here “AutoConnectAP” //and goes into a blocking loop awaiting configuration if (!wifiManager.autoConnect(ssidAP.c_str())) { delay(3000); //reset and try again, or maybe put it to deep sleep ESP.reset(); delay(5000); } //read updated parameters strcpy(host, custom_host.getValue()); strcpy(mqtt_server, custom_mqtt_server.getValue()); strcpy(mqtt_port, custom_mqtt_port.getValue()); strcpy(mqtt_topic, custom_mqtt_topic.getValue()); strcpy(mqtt_isAuthentication, custom_mqtt_isAuthentication.getValue()); strcpy(mqtt_username, custom_mqtt_username.getValue()); strcpy(mqtt_password, custom_mqtt_password.getValue()); strcpy(update_username, custom_update_username.getValue()); strcpy(update_password, custom_update_password.getValue()); strcpy(update_path, custom_device_path.getValue()); //save the custom parameters to FS if (shouldSaveConfig) { DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.createObject(); json[“host”] = host; json[“mqtt_server”] = mqtt_server; json[“mqtt_port”] = mqtt_port; json[“mqtt_topic”] = mqtt_topic; json[“mqtt_isAuthentication”] = mqtt_isAuthentication; json[“mqtt_username”] = mqtt_username; json[“mqtt_password”] = mqtt_password; json[“update_username”] = update_username; json[“update_password”] = update_password; json[“update_path”] = update_path; File configFile = SPIFFS.open("/config.json", "w"); if (!configFile) { } json.printTo(Serial); json.printTo(configFile); configFile.close(); //end save } delay(5000); if(digitalRead(0) == LOW || String(host).length() == 0 || String(mqtt_server).length() == 0 || String(mqtt_topic).length() == 0 || String(update_username).length() == 0 || String(update_password).length() == 0) { wifiManager.resetSettings(); ESP.reset(); } mqttDeviceID = host; client.begin(mqtt_server, atoi(mqtt_port), net); client.onMessage(messageReceived); connect(); MDNS.begin(host); httpUpdater.setup(&httpServer, update_path, update_username, update_password); httpServer.on("/", { if(!httpServer.authenticate(update_username, update_password)) return httpServer.requestAuthentication(); httpServer.send(200, “text/plain”, "Hostname: " + String(host) + "\nMQTT Server: " + String(mqtt_server) + "\nMQTT Port: " + String(mqtt_port) + "\nMQTT Authentication: " + String(mqtt_isAuthentication) + "\nMQTT Out Topic: " + String(mqtt_topic) + “\nTo update firmware go to: http://”+ String(host) + “.local” + String(update_path) + “\nTo reset device settings restart the device and quickly move the jumper from RUN to PGM, wait 10 seconds and put the jumper back to RUN.”); }); httpServer.begin(); MDNS.addService(“http”, “tcp”, 80); } void connect() { while (WiFi.status() != WL_CONNECTED) { delay(1000); } //If authentication true then connect with username and password if(String(mqtt_isAuthentication).equalsIgnoreCase(“TRUE”)) { while (!client.connect(mqttDeviceID, mqtt_username, mqtt_password)) { delay(1000); } } else { while (!client.connect(mqttDeviceID)) { delay(1000); } } client.subscribe(mqtt_topic); } void loop() { client.loop(); delay(10); if(!client.connected()) { //connect(); client.disconnect(); connect(); } httpServer.handleClient(); //Sensor Detection if(digitalRead(sensorPin) == HIGH) { if(lockLow) { //makes sure we wait for a transition to LOW before any further output is made: lockLow = false; client.publish(String(mqtt_topic), “No Fire Detected”); delay(50); } takeLowTime = true; } if(digitalRead(sensorPin) == LOW) { if(takeLowTime) { lowIn = millis(); //save the time of the transition from high to LOW takeLowTime = false; //make sure this is only done at the start of a LOW phase } //if the sensor is low for more than the given pause, //we assume that no more detection is going to happen if(!lockLow && millis() - lowIn > pause) { //makes sure this block of code is only executed again after //a new detection sequence has been detected lockLow = true; client.publish(String(mqtt_topic), “FIRE DETECTED!”); delay(50); } } } void messageReceived(String &topic, String &payload) { //This sensor does not recieve anything from MQTT Server so this is blank }
voltage meter code :
float correctionfactor = 26; // 77 int analogInput = A0; float vout = 0; int vin = 0; float R1 = 30000 float R2 = 7500; // 7.5 k int value = 0; void loop() { Voltage(); } void Voltage() { // read the value at analog input value = analogRead(analogInput); vout = (value * 5.0) / 1023.0; // see text vin = vout / (R2/(R1+R2)); vin = vin - correctionfactor; }