Calculate Resistance Using Audrino

/*****************************************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point *********************************/


//#define WLAN_SSID       "Thom"
//#define WLAN_PASS       "thom1111"
#define WLAN_SSID       "MySpectrumWiFi64-2G"
#define WLAN_PASS       "narrowdaisy835"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "MaxMaeder"
#define AIO_KEY         "3754a5c50ec647ab99ae315fcfab09c5"

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish waterHardness = Adafruit_MQTT_Publish(&mqtt, "MaxMaeder" "/feeds/salty.grains-per-gallon");

Adafruit_MQTT_Publish ppm = Adafruit_MQTT_Publish(&mqtt, "MaxMaeder" "/feeds/salty.parts-per-million");

Adafruit_MQTT_Publish conductivity = Adafruit_MQTT_Publish(&mqtt, "MaxMaeder" "/feeds/salty.conductivity");

Adafruit_MQTT_Publish alerts = Adafruit_MQTT_Publish(&mqtt, "MaxMaeder" "/feeds/salty.alerts");

/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).

int saltLevel = 0;
int dataTransfer = 1;
int flash = 0;
float res = 0;
float conduct = 0;

void MQTT_connect();

void setup() {

  digitalWrite(12, HIGH); 
  
  Serial.begin(115200);
  delay(10);

  Serial.println(F("Please wait..."));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print(F(" Connecting to "));
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();

  Serial.println(F("WiFi connected! "));
  Serial.println(F("IP address: ")); Serial.println(WiFi.localIP());

  // Setup MQTT subscription for onoff feed.
}

uint32_t x=0;

void loop() {
  
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  String wifiAlerts = "";
  int newSubscription = 0;
  float raw = analogRead(0);
  Serial.println(raw);
  if (raw) {
  float buff = raw * 3.3;
  float vout = (buff)/1024.0;
  buff = (3.3/(vout)) -1;
  res = 10000UL * buff;
  conduct = 1/res;
  saltLevel= conduct * 2;
  saltLevel = saltLevel * 17.1;
  Serial.println(res);
  }
    
  MQTT_connect();

Serial.println();
Serial.print("Data transfer #");
Serial.println(dataTransfer);
dataTransfer++;


  // Now we can publish stuff!
  Serial.print(F("Sending Grains Per Gallon Value... "));
  if (! waterHardness.publish(saltLevel)) {
    Serial.println(F("Failed "));
  } else {
    Serial.println(F("Sent! "));
  }

  Serial.print(F("Sending Parts Per Million Value... "));


   Serial.print(F("Sending uS/CM Value... "));
  if (! conductivity.publish(conduct)) {
    Serial.println(F("Failed "));
  } else {
    Serial.println(F("Sent! "));
  }

    Serial.print(F("Sending Resistance Value... "));
  if (! ppm.publish(res)) {
    Serial.println(F("Failed "));
  } else {
    Serial.println(F("Sent! "));
  }
  

  if (saltLevel > 1000) {
    wifiAlerts = "Hard Water Alert";
    if (flash == 1) {
    digitalWrite(0, LOW); 
    flash = 0;
    } else {
    digitalWrite(0, HIGH); 
    flash = 1;  
    }
  } else {
    wifiAlerts = "No Alerts To Display";
    if (WiFi.status() != WL_CONNECTED) {
    if (flash == 1) {
    digitalWrite(0, LOW); 
    flash = 0;
    } else {
    digitalWrite(0, HIGH); 
    flash = 1;  
    }     
    } else {
    digitalWrite(0, HIGH); 
  }
  }

Serial.print(F("Sending Alerts Data ("));
Serial.print(wifiAlerts);
Serial.print(F(")... "));
if (! alerts.publish(wifiAlerts.c_str())) {
  Serial.println(F("Failed "));
} else {
  Serial.println(F("Sent! "));
}

  // ping the server to keep the mqtt connection alive
  // NOT required if you are publishing once every KEEPALIVE seconds
  /*
  if(! mqtt.ping()) {
    mqtt.disconnect();
  }
  */
delay(500);  
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print(F("Connecting to MQTT... "));

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println(F("Retrying MQTT connection in 5 seconds... "));
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println(F("MQTT Connected! "));
}