The Oak by Digistump > Oak Projects

CO2 sensor hacking, MQTT messaging

(1/3) > >>

Pegman:
Some time ago I bought a Voltcraft CO2 sensor and after a spot of googling I found that it is not too difficult to tap into it and obtain CO2/temp/humidity measurements that way (reference).

My aim is to hook up the Oak to the Voltcraft, parse the measurements and send them to my Domoticz home domotica system, over MQTT (Mosquitto).

So far I have only a few explorations into the electronic part (figuring out which PCB connections to use, hooking up the logic analyzer), mostly waiting for the Oak to become 'mature'. But today I managed to hook up the Oak and send MQTT messages using the PubSubclient library.

I will share the MQTT connection code here, for others to use. Mind: this is not original work, it's an adaptation of a PubSubclient example, for use with the Oak. Basically removing Serial.print() statements and the WiFi initialization parts.


--- Code: ---/*
 Basic ESP8266 MQTT example.

 20 Mar 2016 Adapted for use on Digistump Oak by Pegman, Digistump Forums.

 This sketch demonstrates the capabilities of the pubsub library in combination
 with the ESP8266 board/library.

 It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic" every two seconds
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary
  - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
    else switch it off

 It will reconnect to the server if the connection is lost using a blocking
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 achieve the same result without blocking the main loop.

*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>


const char* mqtt_server = "192.168.178.xxx";    // raspberrypi, Domoticz
const unsigned int mqtt_port = 11883;
const char* connection_id = "ESP8266Client";
const char* client_name = "digistumpoak";
const char* client_password = "secretpassword";


WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
}


void callback(char* topic, byte* payload, unsigned int length) {

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    // Attempt to connect
    if (client.connect(connection_id, client_name, client_password)) {
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}


void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 75, "hello world #%ld", value);
    client.publish("outTopic", msg);
  }
}


--- End code ---

djflix:
Sounds awesome! Is this a personal project or related to work or a commercial product? Keep up the good work  8)

defragster:
Thanks for sharing - MQTT is something I wanted to see work.  I got as far as installing a server on my Windows machine. Also got a $5 Pi Zero that might work as a server I'm hoping.

Pegman:
I have Mosquitto running on my Pi 1. It does not have to do a lot of messaging but neither does it seem to cause a noticeable load increase.

Pegman:

--- Quote from: djflix on March 22, 2016, 12:15:22 pm ---Sounds awesome! Is this a personal project or related to work or a commercial product? Keep up the good work  8)

--- End quote ---

Thanks! I've been thinking about it for a long time, finally the Oak is ready for use.
 It's a personal project. I /wish/ I could do that stuff for work  :-[

Navigation

[0] Message Index

[#] Next page

Go to full version