Openweathermap API unsure how to publish properly

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

#define PIN_MQ135 A1

void subscriptionHandler(const char *event, const char *data);


const char *EVENT_NAME = "GetWeatherData";

enum {
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) {
    return 1;

void subscriptionHandler(const char *event, const char *data) {
    JSONValue outerObj = JSONValue::parseCopy(data);
    JSONObjectIterator iter(outerObj);
    while( {
        if ( == "hourly") {
            JSONArrayIterator iter2(iter.value());
            for(size_t ii = 0;; ii++) {
      "hourly array index %u", ii);
                JSONObjectIterator iter3(iter2.value());
                while( {
                    if ( == "ws") {
                       double windSpeed =  iter3.value().toDouble();
                        Particle.publish("windSpeed", String(windSpeed), PRIVATE);
                    if ( == "pres") {
                        double pressure = iter3.value().toDouble();
                        Particle.publish("pressure", String(pressure), PRIVATE);

void setup() {

    // 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);
   // String subscriptionName = String::format("%s/%s/", System.deviceID().c_str(), EVENT_NAME);
   // Particle.subscribe(subscriptionHandler, MY_DEVICES);
   //"subscribing to %s", subscriptionName.c_str());

void loop() {
    if (lastCheck + INTERVAL < millis()) {
        lastCheck = millis();

        // Check if any reads failed and exit early (to try again).
        if (isnan(hum) || isnan(temp)) {
            Particle.publish("Failed to read from DHT sensor!");

        Particle.publish("temperature", String(temp), PRIVATE);
       // Particle.publish("my_event", "temperature", PRIVATE);
        Particle.publish("humidity", String(hum), PRIVATE);
       // Particle.publish("my_event", "humidity", PRIVATE);
        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) {
            if (Particle.connected()) {
                state = STATE_WAIT_TO_PUBLISH;
                stateTime = millis();

            if (millis() - stateTime >= 5000) {
                Particle.publish(EVENT_NAME, "", PRIVATE);
                state = STATE_IDLE;

        case STATE_IDLE:


What exactly is happening with your code? Are you getting any errors at all? An error log would be helpful to see what is going on.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.