搜索
Currently i am able to send the 2 decimal data to thingspeak field. But i stuck on how to write 3 decimal places data to that field. Hope u guys can help.Here is my coding. #include PubSubClient.h // library used to publish the data through mqtt broker #include WiFiEsp.h // library for ESP8266 wifi #include EEPROM.h // library that GravityTds use #include GravityTDS.h // library that GravityTds use #include OneWire.h // Include the libraries for temperature #include DallasTemperature.h // Include the libraries for temperature #include SoftwareSerial.h // library for software serial to display output on the serial monitor
#define TdsSensorPin A1 // set TdsSensorPin at pin A1 #define temperaturePin 2 // temperature sensor is connected into pin 2 on the Arduino
OneWire oneWire(temperaturePin); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) DallasTemperature tempsensor(&oneWire); // Pass our oneWire reference to Dallas Temperature. GravityTDS gravityTds; // define the gravityTds SoftwareSerial Esp8266(5, 6); // Rx, Tx set communication pin for esp8266 to uno WiFiEspClient Esp8266wifi; // set the esp8266 for the mqtt broker connnection PubSubClient client(Esp8266wifi); // set the esp8266 for the mqtt broker connnection
unsigned long previousMillis1 = 0; // used to delay without delay the system for sensor data unsigned long previousMillis2 = 0; // used to delay without delay the system for the send data to thingspeak float temperature; // variable to store temperature value int tdsValue; // variable to store tds value float EC; // variable to store EC value
char ssid[] = ""; // your network SSID (name) char pass[] = ""; // your network password int status = WL_IDLE_STATUS; // the Wifi radio's status String clientName="ESP-Thingspeak";
//char* topic="channels/<channelID/publish/<channelAPI> char* topic = "channels/1244171/publish/FOZYSFXXS4P6DE5N"; // topic to be published to the thingspeak with channel id and api code char* mqtt_server = "mqtt.thingspeak.com"; // set the server for the mqtt of thingspeak
void setup()
{
Serial.begin(9600); // 9600 is the baud rate for the data to display to the serial monitor Esp8266.begin(9600); //9600 is the baud rate for the srial data connection of esp8266 WiFi.init(&Esp8266);
client.setServer(mqtt_server, 1883); // set mqtt broker server and port tempsensor.begin(); // begin temperature sensor library gravityTds.setPin(TdsSensorPin); // set pin tds at declared variable TdsSensorPin gravityTds.setAref(5.0); //reference voltage on ADC, default 5.0V on Arduino UNO gravityTds.setAdcRange(1024); //1024 for 10bit ADC;4096 for 12bit ADC // arduino uno adc level is a 10-bit ADC gravityTds.begin(); //initialization
InitWiFi(); //call function to intialize wifi
Serial.print("Connecting to "); // display on serial monitor Serial.print(mqtt_server); Serial.print(" as "); Serial.println(clientName);
if (client.connect((char*) clientName.c_str())) // connect to the thingspeak with the client name { Serial.println("Connected to MQTT broker");
if (client.publish(topic, "Test send data")) // Test send data to the thingspeak { Serial.println("Testing send data succes"); } else { Serial.println("Testing send data failed"); } } else { Serial.println("MQTT connect failed"); Serial.println("Will reset and try again..."); abort(); }
}
//this function is only for the initialization of the esp8266 to connect to the network & mqtt and initialize the sensor
void loop(){ unsigned long currentMillis = millis(); //timer start
checkconnection(); // calling function to check the connection
if (currentMillis - previousMillis1 >= 2000) //delay without causing system delay {
previousMillis1 = currentMillis;
sensordata(); // calling fucntion of the sensor data }
if (currentMillis - previousMillis2 >= 2000) //delay without causing system delay {
previousMillis2 = currentMillis;
senddata(); // calling fucntion to send data // send one set of data in 2 seconds }
}
void sensordata() { tempsensor.requestTemperatures(); // get temperatures temperature = tempsensor.getTempCByIndex(0); // set variable to the temperature sensor output gravityTds.setTemperature(temperature); // set the temperature and execute temperature compensation gravityTds.update(); //sample and calculate tdsValue = gravityTds.getTdsValue(); // then get the value of TDS EC = gravityTds.getEcValue()/1000; // get EC value Serial.println(temperature); // display sent data Serial.println(tdsValue); Serial.println(EC,3); Serial.println("Data Sent"); }
void senddata() {
String payload="field1="; // payload to send to thingspeak using mqtt broker payload+=temperature; // field1=temperature&field2=tdsValue&field3=EC&status=MQTTPUBLISH payload+="&field2="; //// field1=26.9&field2=120&field3=0.2&status=MQTTPUBLISH payload+=tdsValue; payload+="&field3="; //// field1=26.9&field2=120&field3=0.2&status=MQTTPUBLISH payload+=EC,3; payload+="&status=MQTTPUBLISH";
if (client.connected()) // if client connected { Serial.println("Sending data ");
if (client.publish(topic, (char*) payload.c_str())) // send data to the thingspeak, topic (the channel id and channel api) -- payload is the data from sensor that will be sent to the thingspeak { Serial.println(temperature); // display sent data Serial.println(tdsValue); Serial.println(EC,3); Serial.println("Data Sent"); } else { Serial.println("Publish failed"); } } }
void checkconnection() /// check connection if disconnected try to reconnect wifi and mqtt broker {
if ( status != WL_CONNECTED) Serial.println("Wifi Disconnected"); { while ( status != WL_CONNECTED) { InitWiFi(); // call function to start wifi // reconnect wifi } }
if (!client.connected()) // if mqtt broker disconnected from thingspeak { Serial.println("MQTT Disconnected"); while (!client.connected()) // while mqtt broker disconnected from thingspeak { if (client.connect((char*) clientName.c_str())) // reconnect to the server { Serial.println("MQTT connected"); } else { Serial.println("MQTT connect failed"); Serial.println("Will reset and try again...");
} } }
} void InitWiFi() { // check for the presence of the module if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WiFi module not present"); // don't continue // don't continue while (true); //keep looping until it detect esp8266 module }
// attempt to connect to WiFi network while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to WPA SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network status = WiFi.begin(ssid, pass); }
// print your WiFi ESP8266 IP address IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip);
// print the SSID of the network you're attached to Serial.print("SSID: "); Serial.println(WiFi.SSID());
// print the received signal strength long rssi = WiFi.RSSI(); Serial.print("Signal strength (RSSI): "); Serial.println(rssi); delay (5000); }