Hello everyone,
I’m a complete beginner, doing a project for uni which is a very basic weather station. I have never worked with APIs and cloud stuff before.
I have 2 sensors connected (gas sensor for air quality and DHT-22 for temperature and humidity) and trying to get the readings for pressure and wind speed from Openweather API.
Additionally, I then have an integration with Google Cloud to send it to the database.
I set up a webhook for the API as per the instructions and tried to edit the code so that the pressure and wind speed are published separately and after hours of trying various approaches - it just does not work as expected.
What am I doing wrong in this code? Would really appreciate your help!
(apologies for the messy code but I’ve been working on it for so many days now and had no time to clean it up properly - just really wanting to get the API to work as expected first)
// This #include statement was automatically added by the Particle IDE.
#include <OpenWeatherMap.h>
// This #include statement was automatically added by the Particle IDE.
#include <MQ135.h>
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define INTERVAL 4000 // number of milliseconds between checks
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a
// very slow sensor)
// Connect pin 1 (on the left) of the sensor to +3.3V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialise an object to represent the DHT22 sensor
DHT dht(DHTPIN, DHTTYPE);
#define PIN_MQ135 A1
void subscriptionHandler(const char *event, const char *data);
SYSTEM_THREAD(ENABLED);
const char *EVENT_NAME = "GetWeatherData";
enum {
STATE_WAIT_FOR_CONNECTED,
STATE_WAIT_TO_PUBLISH,
STATE_IDLE
};
int state = STATE_WAIT_FOR_CONNECTED;
unsigned long stateTime;
// Declare globa variables for the readings and the time last checked
double temp;
double hum;
unsigned long lastCheck = 0;
int number = 0;
MQ135 gasSensor = MQ135(PIN_MQ135);
int led = D7;
float rzero = gasSensor.getRZero();
int ppm = gasSensor.getPPM();
String quality = "";
// Function that can be called from the Particle console
void readSensor() {
hum = dht.getHumidity();
temp = dht.getTempCelcius();
}
// Callback function to allow the Particle console access to readSensor()
int checkHandler(String command) {
readSensor();
return 1;
}
void subscriptionHandler(const char *event, const char *data) {
JSONValue outerObj = JSONValue::parseCopy(data);
JSONObjectIterator iter(outerObj);
while(iter.next()) {
if (iter.name() == "hourly") {
JSONArrayIterator iter2(iter.value());
for(size_t ii = 0; iter2.next(); ii++) {
Log.info("hourly array index %u", ii);
JSONObjectIterator iter3(iter2.value());
while(iter3.next()) {
if (iter3.name() == "ws") {
double windSpeed = iter3.value().toDouble();
Particle.publish("windSpeed", String(windSpeed), PRIVATE);
}
if (iter3.name() == "pres") {
double pressure = iter3.value().toDouble();
Particle.publish("pressure", String(pressure), PRIVATE);
}
}
}
}
}
}
void setup() {
dht.begin();
// Declare variables that can be seen in the Particle console
Particle.variable("temperature", temp);
Particle.variable("humidity", hum);
Particle.function("readSensor", checkHandler);
Particle.variable("ppm", ppm);
Particle.variable("air quality", quality);
// {{{PARTICLE_DEVICE_ID}}}/{{{PARTICLE_EVENT_NAME}}}
// String subscriptionName = String::format("%s/%s/", System.deviceID().c_str(), EVENT_NAME);
// Particle.subscribe(subscriptionHandler, MY_DEVICES);
// Log.info("subscribing to %s", subscriptionName.c_str());
}
void loop() {
if (lastCheck + INTERVAL < millis()) {
lastCheck = millis();
readSensor();
// Check if any reads failed and exit early (to try again).
if (isnan(hum) || isnan(temp)) {
Particle.publish("Failed to read from DHT sensor!");
return;
}
delay(40000);
Particle.publish("temperature", String(temp), PRIVATE);
// Particle.publish("my_event", "temperature", PRIVATE);
Particle.publish("humidity", String(hum), PRIVATE);
// Particle.publish("my_event", "humidity", PRIVATE);
delay(3000);
int ppm = gasSensor.getPPM();
if (ppm <= 300) {
quality = "Excellent";
}
else if (ppm <= 700) {
quality = "Good";
}
else if (ppm <= 900) {
quality = "Fair";
}
else if (ppm <= 1100) {
quality = "Mediocre";
}
else {
quality = "Bad";
}
Particle.publish("ppm", String(ppm), PRIVATE);
// Particle.publish("my_event", "ppm", PRIVATE);
Particle.publish("airquality", quality, PRIVATE);
// Particle.publish("my_event", "quality", PRIVATE);
void subscriptionHandler(const char *event, const char *data);
}
/*
switch(state) {
case STATE_WAIT_FOR_CONNECTED:
if (Particle.connected()) {
state = STATE_WAIT_TO_PUBLISH;
stateTime = millis();
}
break;
case STATE_WAIT_TO_PUBLISH:
if (millis() - stateTime >= 5000) {
Particle.publish(EVENT_NAME, "", PRIVATE);
state = STATE_IDLE;
}
break;
case STATE_IDLE:
break;
}
*/
}