Hello, I'm facing a strange error when i combine my scripts which both work absolutely fine individually when joined my sensor data is maxed out.
Dust Sensor on its own (correct)
Raw Signal Value (0-1023): 590.00 - Voltage: 1.90 - Dust Density: 0.22
Raw Signal Value (0-1023): 360.00 - Voltage: 1.16 - Dust Density: 0.10
Raw Signal Value (0-1023): 316.00 - Voltage: 1.02 - Dust Density: 0.07
Raw Signal Value (0-1023): 418.00 - Voltage: 1.35 - Dust Density: 0.13
Raw Signal Value (0-1023): 377.00 - Voltage: 1.21 - Dust Density: 0.11
Raw Signal Value (0-1023): 364.00 - Voltage: 1.17 - Dust Density: 0.10
Raw Signal Value (0-1023): 316.00 - Voltage: 1.02 - Dust Density: 0.07
Raw Signal Value (0-1023): 273.00 - Voltage: 0.88 - Dust Density: 0.05
Raw Signal Value (0-1023): 364.00 - Voltage: 1.17 - Dust Density: 0.10
Raw Signal Value (0-1023): 394.00 - Voltage: 1.27 - Dust Density: 0.12
Networking + Dust Sensor Script result (wrong)
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
httpRequestData: &voMeasured=4095.00&calcVoltage=13.20&dustDensity=2.14
HTTP Response code: 200
Dust Sensor Script
int measurePin = 26;
int ledPower = 27;
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
void setup(){
Serial.begin(115200);
pinMode(ledPower,OUTPUT);
}
void loop(){
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin); // read the dust value
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(sleepTime);
// 0 - 3.3V mapped to 0 - 1023 integer values
// recover voltage
calcVoltage = voMeasured * (3.3 / 1024);
// linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
// Chris Nafis (c) 2012
dustDensity = 0.17 * calcVoltage - 0.1;
Serial.print("Raw Signal Value (0-1023): ");
Serial.print(voMeasured);
Serial.print(" - Voltage: ");
Serial.print(calcVoltage);
Serial.print(" - Dust Density: ");
Serial.println(dustDensity);
delay(1000);
}
Scripts Joined
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
#include <Wire.h>
int measurePin = 26;
int ledPower = 27;
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
// Replace with your network credentials
const char* ssid = "Lots of Security";
const char* password = "password";
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = "http://website.com/api";
void setup() {
Serial.begin(115200);
pinMode(ledPower,OUTPUT);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare your HTTP POST request data
digitalWrite(ledPower,LOW); // power on the LED
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin); // read the dust value
delayMicroseconds(deltaTime);
digitalWrite(ledPower,HIGH); // turn the LED off
delayMicroseconds(sleepTime);
// 0 - 3.3V mapped to 0 - 1023 integer values
// recover voltage
calcVoltage = voMeasured * (3.3 / 1024);
// linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/
// Chris Nafis (c) 2012
dustDensity = 0.17 * calcVoltage - 0.1;
String httpRequestData = "&voMeasured=" + String(voMeasured) + "&calcVoltage=" + calcVoltage + "&dustDensity=" + dustDensity;
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
//Send an HTTP POST request every 1 second
delay(1000);
}
I've broke this down and it seems to break as soon as i add this
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Any ideas would be grateful!