The use of the ESP8266 in the world of IoT

User avatar
By stewecar
#45943 Hello!
i am a newbie in esp8266 and arduino bit i have to launch a project about monitoring (and possibly put the datas in real time on Thingspeak) dust particles in my city in a few days..and the fastest way to do it is by arduino and esp8266..
So i bought a nodemcu (esp12-e) that i would like to connect to the sensor and the arduino mega2560..
I copied the project by an interesting blog (http://www.shadowandy.net/2015/06/arduino-dust-sensor-with-esp8266.htm) about air pollution but i am stuck in uploading the sketch..
I know that some parameters have to be adjusted (like the different gpio's) but i don't know how to modify the sketch to make the think working.
Thanks for any help!
stefano


#include <ESP8266WiFi.h>

const char ssid[] = "fill in your wireless SSID";
const char pass[] = "fill in your wireless pass";
const char thingSpeakAddress[] = "api.thingspeak.com";
const char thingSpeakAPIKey[] = "fill in your thingspeak API write key";

#define PM25 0
#define PM10 1
int pin[] = {2, 0};
unsigned long starttime;
unsigned long sampletime_ms = 30000;
unsigned long triggerOn[2];
unsigned long triggerOff[2];
unsigned long lowpulseoccupancy[] = {0, 0};
float ratio[] = {0, 0};
float count[] = {0, 0};
boolean value[] = {HIGH, HIGH};
boolean trigger[] = {false, false};

void setup() {
connectWiFi();
pinMode(pin[PM25], INPUT); //Listen at the designated PIN
pinMode(pin[PM10], INPUT); //Listen at the designated PIN
starttime = millis(); //Fetching the current time
ESP.wdtEnable(WDTO_8S); // Enabling Watchdog
}

void loop() {
value[PM25] = digitalRead(pin[PM25]);
value[PM10] = digitalRead(pin[PM10]);

if (value[PM25] == LOW && trigger[PM25] == false) {
trigger[PM25] = true;
triggerOn[PM25] = micros();
}
if (value[PM25] == HIGH && trigger[PM25] == true) {
triggerOff[PM25] = micros();
lowpulseoccupancy[PM25] += (triggerOff[PM25] - triggerOn[PM25]);
trigger[PM25] = false;
}
if (value[PM10] == LOW && trigger[PM10] == false) {
trigger[PM10] = true;
triggerOn[PM10] = micros();
}
if (value[PM10] == HIGH && trigger[PM10] == true) {
triggerOff[PM10] = micros();
lowpulseoccupancy[PM10] += (triggerOff[PM10] - triggerOn[PM10]);
trigger[PM10] = false;
}
ESP.wdtFeed(); // Reset the WatchDog

if ((millis() - starttime) > sampletime_ms) //Checking if it is time to sample
{
ratio[PM25] = lowpulseoccupancy[PM25] / (sampletime_ms * 10.0);
count[PM25] = 1.1 * pow(ratio[PM25], 3) - 3.8 * pow(ratio[PM25], 2) + 520 * ratio[PM25] + 0.62;
ratio[PM10] = lowpulseoccupancy[PM10] / (sampletime_ms * 10.0);
count[PM10] = 1.1 * pow(ratio[PM10], 3) - 3.8 * pow(ratio[PM10], 2) + 520 * ratio[PM10] + 0.62;
count[PM25] -= count[PM10];

ESP.wdtFeed(); // Reset the WatchDog
// Begin mass concentration calculation
float concentration[] = {0, 0};
double pi = 3.14159;
double density = 1.65 * pow(10, 12);
double K = 3531.5;

ESP.wdtFeed(); // Reset the WatchDog
// PM10
double r10 = 2.6 * pow(10, -6);
double vol10 = (4 / 3) * pi * pow(r10, 3);
double mass10 = density * vol10;
concentration[PM10] = (count[PM10]) * K * mass10;

ESP.wdtFeed(); // Reset the WatchDog
// PM2.5
double r25 = 0.44 * pow(10, -6);
double vol25 = (4 / 3) * pi * pow(r25, 3);
double mass25 = density * vol25;
concentration[PM25] = (count[PM25]) * K * mass25;
// End of mass concentration calculation

ESP.wdtFeed(); // Reset the WatchDog

connectWiFi();
updateThingSpeak("1=" + String(concentration[PM10], DEC) + "&2=" + String(count[PM10], DEC) + "&3=" + String(concentration[PM25], DEC) + "&4=" + String(count[PM25], DEC));
// Resetting for next sampling
lowpulseoccupancy[PM25] = 0;
lowpulseoccupancy[PM10] = 0;
starttime = millis();
ESP.wdtFeed(); // Reset the WatchDog
}
}

void connectWiFi() {
if (WiFi.status() == WL_CONNECTED) {
return;
}
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}

void updateThingSpeak(String tsData) {
WiFiClient client;
if (!client.connect(thingSpeakAddress, 80)) {
return;
}
client.print(F("GET /update?key="));
client.print(thingSpeakAPIKey);
client.print(F("&"));
client.print(tsData);
client.print(F(" HTTP/1.1\r\nHost: api.thingspeak.com\r\n\r\n"));
client.println();
}