Http and MQTT feeds in the same sketch

I am running emoncms on a raspberry pi (emonpi) with a emontxshield feeding power data to emoncms locally.
I am also sending temp and humidity data to the emoncms locally. I’ve created separate sketches to load ESP8266 DHT22 data up to the emonpi/emoncms using http feeds:
client.print(String("GET “) + url + " HTTP/1.1\r\n” +
"Host: " + host + “\r\n” +
“Connection: close\r\n\r\n”);
and another one that sends the data via mqtt:
Serial.print(“New temperature:”);
Serial.println(String(temp).c_str());_
client.publish(temperature_topic, String(temp).c_str(), true);

I’m am looking to combine the two delivery mechanisms into one sketch. My goal is to load the temp/humdity data onto the emonpi, and then use a mqtt protocol to load the data onto a separate raspberry pi (all from within one sketch).

I’m not sure if it can be done, or how to snip elements from one sketch to put into the other.

I’m familiar with using node-red and Openhab to create the mqtt scripts, but would preferably like to deploy these through the one process.
Any help would be appreciated.

Yes, I have a sketch running on an ESP8266 which publishes to emoncms and MQTT. Basically it manages my heat bank, turning on the boiler as required to supplement heating from solar thermal and xx. It manages the temperatures to two levels depending on whether the CH is on or not.

It also has a simple http server with a main page that allows you to turn on the heating and also do things like override the boiler when we are on vacation. I’ve made this available through our ISP router so that we can turn on the heating when we’re away at night or when we’re on vacation so that the house is warm before we get home.

There’s lots more in the attached file than you need and I’ve not included the web pages, so if you either pick out the bits you need or delete the bits you don’t need I’m sure you’ll get there.

And the file is probably a bit messy but it works and one day I’ll clean it up… :wink:

Simonesp_MQTT_emoncms_DS18B20_CHControl_V5.ino (43.6 KB)

EmonESP has also got MQTT and Emoncms HTTP running on anESP8266

Hi Glyn, I got EmonESP loaded using Platformio and like how simple the web interface is, while additional layers can be loaded presumably on the back end. This leads to my question. Once EmonESP is up and running how are additional sensor sketches added, for instance esp_dht22_emoncms.ino? If I load that with the platformio.ini file within the esp-dht22_emoncms folder it erases the EmonESP program, or at least makes it not functional anymore. Does that sketch contents have to be added to another file? If so which one?
Also, although I see that the OTA file has been processed during the upload, it’s not clear if it is functioning. The Arduino IDE shows a virtual port when a node has OTA functionality. Any help on that topic would be appreciated as well.

Thanks for all your group has put into this project.

Uploading a new/different sketch with the EmonESP will overwrite the current sketch running on the device. Recommend sticking with the standard EmonESP sketch and modifying (e.g. add DHT22 support) as required:

In looking at the various files in the platformio EmonESP directory it is not totally clear which file to insert the relevant DHT22 data into. I could tried to cram it into the src.ino file and create a data string that gets inserted into the String input = “”, but I’m wondering if the 'input_get(input) line in that file looks for data in the input.cpp or input.h files based on the #include call.
If inserting the relevant parts of the esp_dht22_emoncms code into the src file is the right thing to do, what form should the string be? I’ve tried testing
String input = "http://192.168.1.227/input?string=“CT1:3935,CT2:325,T0:20.5”
and
String input = “CT1:3935,CT2:325”
and neither produces data in my emoncms, or mqtt sniffer.
I have tested entering http://192.168.1.227/input?string=“CT1:3935,CT2:325,T0:20.5” into a browser and it does inject the data into both my emoncms and mqtt server/subscriber so I know that is at least working. It also shows up in the Latest Data section of the EmonESP UI (which is very cool).