I've been trying to combine these two scripts for two days however, cannot for the life of me just get the dustDensity to save to my database both scripts work absolutely fine individually.
This script connects to my local wifi and saves data to a local database
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
int Led_OnBoard = 2; // Initialize the Led_OnBoard
const char* ssid = "Lots of Security"; // Your wifi Name
const char* password = "Password"; // Your wifi Password
const char *host = "192.168.1.64"; //Your pc or server (database) IP, example : 192.168.0.0 , if you are a windows os user, open cmd, then type ipconfig then look at IPv4 Address.
void setup() {
// put your setup code here, to run once:
delay(1000);
pinMode(Led_OnBoard, OUTPUT); // Initialize the Led_OnBoard pin as an output
Serial.begin(115200);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(Led_OnBoard, LOW);
delay(250);
Serial.print(".");
digitalWrite(Led_OnBoard, HIGH);
delay(250);
}
digitalWrite(Led_OnBoard, HIGH);
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.println("Connected to Network/SSID");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
}
void loop() {
// put your main code here, to run repeatedly:
HTTPClient http; //Declare object of class HTTPClient
String LdrValueSend, postData;
int ldrvalue=analogRead(A0); //Read Analog value of LDR
LdrValueSend = String(ldrvalue); //String to interger conversion
//Post Data
postData = "ldrvalue=" + LdrValueSend;
http.begin("http://192.168.1.64/Nodemcu_db_record_view/InsertDB.php"); //Specify request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCode = http.POST(postData); //Send the request
String payload = http.getString(); //Get the response payload
//Serial.println("LDR Value=" + ldrvalue);
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
Serial.println("LDR Value=" + LdrValueSend);
http.end(); //Close connection
delay(4000); //Here there is 4 seconds delay plus 1 second delay below, so Post Data at every 5 seconds
digitalWrite(Led_OnBoard, LOW);
delay(1000);
digitalWrite(Led_OnBoard, HIGH);
}
script 2 which reads my sensor
int dustPin = A0; // dust sensor
int ledPin = D2;
float voltsMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
void setup()
{
Serial.begin(57600);
pinMode(ledPin,OUTPUT);
}
void loop()
{
digitalWrite(ledPin,LOW); // power on the LED
delayMicroseconds(280);
voltsMeasured = analogRead(dustPin); // read the dust value
delayMicroseconds(40);
digitalWrite(ledPin,HIGH); // turn the LED off
delayMicroseconds(9680);
//measure your 5v and change below
calcVoltage = voltsMeasured * (3.3 / 1024.0);
dustDensity = 0.17 * calcVoltage - 0.1;
Serial.println("GP2Y1010AU0F readings");
Serial.print("Raw Signal Value = ");
Serial.println(voltsMeasured);
Serial.print("Voltage = ");
Serial.println(calcVoltage);
Serial.print("Dust Density = ");
Serial.println(dustDensity); // mg/m3
Serial.println("");
delay(1000);
}
Instead of saving the ldrvalue value to my database i'd like to save the dustDensity. Can anyone help please?
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
int ledPin = 12; // Initialize the Led Pin
int dustPin = A0; // dust sensor pin
float voltsMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
const char* ssid = "Lots of Security"; // Your wifi Name
const char* password = "Password"; // Your wifi Password
const char *host = "192.168.1.64"; //Your pc or server (database) IP, example : 192.168.0.0 , if you are a windows os user, open cmd, then type ipconfig then look at IPv4 Address.
void setup() {
// put your setup code here, to run once:
delay(1000);
pinMode(ledPin, OUTPUT); // Initialize the Led_OnBoard pin as an output
Serial.begin(115200);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(ledPin, LOW);
delay(250);
Serial.print(".");
digitalWrite(ledPin, HIGH);
delay(250);
}
digitalWrite(ledPin, HIGH);
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.println("Connected to Network/SSID");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(ledPin,LOW); // power on the LED
delayMicroseconds(280);
voltsMeasured = analogRead(dustPin); // read the dust value
delayMicroseconds(40);
digitalWrite(ledPin,HIGH); // turn the LED off
delayMicroseconds(9680);
//measure your 5v and change below
calcVoltage = voltsMeasured * (3.3 / 1024.0);
dustDensity = 0.17 * calcVoltage - 0.1;
Serial.println("GP2Y1010AU0F readings");
Serial.print("Raw Signal Value = ");
Serial.println(voltsMeasured);
Serial.print("Voltage = ");
Serial.println(calcVoltage);
Serial.print("Dust Density = ");
Serial.println(dustDensity); // mg/m3
Serial.println("");
delay(1000);
HTTPClient http; //Declare object of class HTTPClient
String LdrValueSend, postData;
int ldrvalue=analogRead(A0); //Read Analog value of LDR
LdrValueSend = String(ldrvalue); //String to interger conversion
//Post Data
postData = "ldrvalue=" + LdrValueSend;
http.begin("http://192.168.1.64/Nodemcu_db_record_view/InsertDB.php"); //Specify request destination
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
int httpCode = http.POST(postData); //Send the request
String payload = http.getString(); //Get the response payload
//Serial.println("LDR Value=" + ldrvalue);
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
Serial.println("LDR Value=" + LdrValueSend);
http.end(); //Close connection
delay(4000); //Here there is 4 seconds delay plus 1 second delay below, so Post Data at every 5 seconds
digitalWrite(ledPin, LOW);
delay(1000);
digitalWrite(ledPin, HIGH);
}
For the two sketches, add a New Tab using the little down-arrow menu on the right end of the tabs bar. I named them "Sketch1.ino" and "Sketch2.ino". Copy your two sketches into those new tabs. In "Sketch1.ino" change setup() to setup1() and loop() to loop1(). Similar for "Sketch2.ino". Now the combined sketch will compile.
There is one compile warning but it probably existed before the sketches were combined:
/Users/john/Documents/Arduino/sketch_oct12a/Sketch1.ino: In function 'void loop1()':
/Users/john/Documents/Arduino/sketch_oct12a/Sketch1.ino:57:71: warning: 'bool HTTPClient::begin(String)' is deprecated (declared at /Users/john/Library/Arduino15/packages/esp8266/hardware/esp8266/2.5.2/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h:155) [-Wdeprecated-declarations]
http.begin("http://192.168.1.64/Nodemcu_db_record_view/InsertDB.php"); //Specify request destination
Of course if either sketch is written poorly so that, for example, loopX() doesn't return quickly, you may find that your combined sketch is not responsive. Also, if you need data to travel between sketches you may need to use a global variable or have one sketch call functions in the other sketch.