I'm trying to send readings from an ADC to an MQTT broker via pubsub but I can't compile the code. Where am I wrong?
I am attaching all the code.
Thanks.
type o
//This is a simple code to handle payload as String.
#include "ADC.h"
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Global variables for module
static int16_t speed_mode = SLOW; //!< The ADC Speed Mode settings. Change it to "FAST" for higher data rate.
static float adc_vref = 2.5; //!< The ADC reference voltage
static uint8_t rejection_mode = ADC_R50; //!< The ADC rejection mode
static uint8_t i2c_address = 0x24; //!< I2C address in 7 bit format for part
static uint16_t eoc_timeout = 300; //!< Timeout in ms
static float adc_offset = 0;
#define C0 2
#define ssid "TP-LINK_09CE"
#define password "120120120"
#define mqtt_server "192.168.0.130"
#define client_name "NodeMCU 8CH"
const char* inTopic = "home/living";
const char* msgTopic = "home/msg";
const char* msgpH = "pool/pH";
char msgIN;
WiFiClient espClient;
PubSubClient client(espClient);
///////////////////////////////////////////////////////
void setup_wifi()
{
delay(100);
Serial.println("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.print(WiFi.localIP());
Serial.println();
}
///////////////////////////////////////////////////////
void reconnect()
{
while (!client.connected())
{
Serial.println("Attempting MQTT connection...");
if (client.connect(client_name))
{
client.publish(msgTopic, "MQTT is connected");
//client.publish(msgpH, "Just test");
Serial.println("connected");
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 callback(char* topic, byte* payload, unsigned int length)
{
String msgIN = "";
for (int i = 0; i < length; i++)
{
msgIN += (char)payload[i];
}
String msgString = msgIN;
Serial.println(msgString);
//Message as String
if (msgString == "CH1ON")
{
Serial.println("Channel1 is ON");
client.publish(msgTopic, "Channel1 is ON");
digitalWrite(C0, HIGH);
} else if (msgString == "CH1OFF")
{
Serial.println("Channel1 is OFF");
client.publish(msgTopic, "Channel1 is OFF");
digitalWrite(C0, LOW);
}
}
/////////////////////////////////////////////////////
void setup()
{
Wire.begin(4, 5); //(SDA,SCL)wake up I2C bus
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(C0, OUTPUT);
digitalWrite(C0, LOW);
}
////////////////////////////////////////////////////
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
//from sourse
uint8_t acknowledge = 0;
acknowledge |= read_adc();
if (acknowledge)
Serial.println(F("***** I2C ERROR *****"));
delay(1000);
//end sourse
if (!client.loop())
{
client.connect("ESP8266Client");
}
}
/////////////////////////////////////////////////////
//! @return 0 if successful, 1 is failure
int8_t read_adc()
{
uint8_t adc_command;
int32_t adc_code = 0;
float adc_voltage = 0;
uint8_t ack = 0;
adc_command = rejection_mode | speed_mode;
ack |= adc_read( i2c_address, adc_command, &adc_code, eoc_timeout );
adc_voltage = adc_code_to_voltage( adc_code, adc_vref ) * 1000 + adc_offset; //!< Convert the ADC code to mV voltage
if ( !ack )
{
Serial.print( F("*************************\n" ) );
Serial.print( "BNC Input Voltage: " );
Serial.print( adc_voltage, 1 );
Serial.print( F( "mV\n" ) );
float pH_raw = adc_voltage;
client.publish("pool/pH", String(pH_raw));
//client.publish("sensors", packet.c_str());
//client.publish( pH_raw);
//client.publish(msgpH, String(pH_raw));
}
else
{
Serial.print( F( "Error in read" ) );
return 1;
}
return ( 0 );
}r paste code here