Hello everyone,
I am a newbie and trying to send HC sensor data and HX711 data to thingspeak server using arduino and esp 8266 and my esp hardly connects to internet by doing many times plug in and plug out. Getting correct data on serial monitor but while sending data to thingspeak HC sensor's data is correct as seen on serial monitor and HX711 sensor's data is consistently 3.0 .
Kindly help me I stuck at this place and don't know how to proceed further.
#include<SoftwareSerial.h>
#include <stdlib.h>
#define LDRpin A0 // pin where we connected the LDR and the resistor
int LDRValue = 0; // result of reading the analog pin
#include "HX711.h" //You must have this library in your arduino library folder
#define DOUT 3
#define CLK 2
HX711 scale(DOUT, CLK);
//Change this calibration factor as per your load cell once it is found you many need to vary it in thousands
float calibration_factor = -106600; //-96650; //-106600 worked for my 40Kg max scale setup
#define echoPin 11 // Echo Pin
#define trigPin 10 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
/*-----------------ESP8266 Serial WiFi Module---------------*/
#define SSID "SSID-WiFiname"
#define PASS "password"
#define IP "184.106.153.149"// thingspeak.com ip
String msg = "GET /update?key=XXXXXXXXXXXXXXXX"; //change it with your key...
/*-----------------------------------------------------------*/
// result of reading the analog pin
float temp;
int error;
String tempC;
float tempW;
void setup() {
Serial.begin(9600); // sets serial port for communication
Serial.println("Press T to tare");
scale.set_scale(-96650); //Calibration Factor obtained from first sketch
scale.tare(); //Reset the scale to 0
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.println("AT");
delay(4000);
if(Serial.find("OK")){
connectWiFi();
}
}
void loop() {
start: //label
error=0;
Serial.print("Weight: ");
Serial.print(scale.get_units(), 3); //Up to 3 decimal points
Serial.println(" kg"); //Change this to kg and re-adjust the calibration factor if you follow lbs
tempW = (scale.get_units(), 3);
if(Serial.available())
{
char temp = Serial.read();
if(temp == 't' || temp == 'T')
scale.tare(); //Reset the scale to zero
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println(NULL);//("-1");
digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}
/* temp = analogRead(LDRpin); // read the value from the LDR
Serial.println(LDRValue); // print the value to the serial port
delay(3000); // wait a little
*/
char buffer[10];
// there is a useful c function called dtostrf() which will convert a float to a char array
//so it can then be printed easily. The format is: dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
tempC = dtostrf(tempW, 4, 1, buffer);
updateTemp();
//Resend if transmission is not completed
if (error==1){
goto start; //go to label "start"
}
delay(10000);
}
void updateTemp(){
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
Serial.println(cmd);
delay(2000);
if(Serial.find("Error")){
return;
}
cmd = msg ;
cmd += "&1="; //field 1 for height
cmd += distance;
cmd += "&2="; //field 2 for humidity
cmd += tempW;//String(hum);
cmd += "\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">")){
Serial.print(cmd);
}
else{
Serial.println("AT+CIPCLOSE");
//Resend...
error=1;
}
}
boolean connectWiFi(){
Serial.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial.println(cmd);
delay(5000);
if(Serial.find("OK")){
return true;
}else{
return false;
}
}
arduino_hc_hx711.ino (4.01 KB)