Openhab + NodeMCU + HTTP + Arduino uno

Hello,

I am trying to send sensor value(temperature from DHT11) to openHAB over HTTP but i don’t know what should i put in this arduino code at "const char host = “http://localhost:8080”;" and here
http.begin(“http://localhost:8080/basicui/app”); . All code:
/

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include “DHTesp.h”

#define DHTpin 14 //D5 of NodeMCU is GPIO14

DHTesp dht;

const char *ssid = “ssid”;
const char *password = “password”;

const char *host = “http://localhost:8080”;

void setup() {
delay(1000);
Serial.begin(115200);
WiFi.mode(WIFI_OFF);
delay(1000);
WiFi.mode(WIFI_STA);

WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");

Serial.print(“Connecting”);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
Serial.println();
Serial.println(“Status\tHumidity (%)\tTemperature ©\t(F)\tHeatIndex ©\t(F)”);
dht.setup(DHTpin, DHTesp::DHT11);
}

//=======================================================================
// Main Program Loop
//=======================================================================
void loop() {
HTTPClient http; //Declare object of class HTTPClient
//delay(dht.getMinimumSamplingPeriod()); /* Delay of amount equal to sampling period */
delay(dht.getMinimumSamplingPeriod());
float temperature = dht.getTemperature();
String postData;

//Post Data
postData = String(temperature);

http.begin(“http://localhost:8080/basicui/app”); //Specify request destination
int httpCode = http.POST(postData); //Send the request
String payload = http.getString(); //Get the response payload

Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload

http.end(); //Close connection

delay(5000); //Post Data at every 5 seconds
}
//=======================================================================
And what should i pun in items, sitemaps and rule to prind this temperature value?
Can someone help me? Thanks

Hi @Ovidiu_Ionita,

Welcome to the openHAB forum. Hope you will get a great setup with the help of all the people on this forum. I’ll certainly try.

This is not how you can communicate with openHAB. You have a few options, the most obvious one being communicate using MQTT. There are a lot of topics about that in this forum. Just search for ‘mqtt 2.4 setup’. If you need more help, let us know.
The other way, is using the REST API. Install the REST API Documentation option and read the docs. Again, feel free to ask questions if you get stuck.

BTW, on the Arduino side you need the PubSubClient library.

Hi,

I already install REST API. But again i don’t know how to write arduino code. I am begginer so sorry for this questions.

Thank you for your answear.

I’ll try to find some example projects tomorrow, as I am off to bed now…:slightly_smiling_face:

Ok. I am waiting for that

If you have no programming skills, I would recommend something ready-made that can be configured via a webinterface like ESPEasy. The Wiki has a section on MQTT with openHAB.

I saw some tutorials with MQTT and openHAB, i create an mqtt broker step by step and my broker appear offline and i don’t know why, any idea?

Can you show me the openHAB configuration for the MQTT binding and the broker.

I have installed MQTT Binding 2.4.0

If you have an example of MQTT or HTTP or REST can I have it? I have programming skills but i have problem with those examples :frowning:

I don’t see anything about your MQTT binding and Things definition. Have you installed/configured the MQTT Binding as well or just the broker? You’ll need the binding too and create a Thing in OH with channels that represent the topics to have the ESP8266 subscribe to and publish it’s temperature data and whatever else you want to publish to MQTT. You can find more info here and here.

For the MQTT Broker, you can use the OH-internal broker, like you did, or an external program like the popular Mosquitto MQTT Broker.

I use Mosquitto as MQTT broker and file based configurations of OH instead of PaperUI, so I cannot give you the exact steps for PaperUI.

And for the ESP side of it: Do you want to stick with a small program like you already have and just add MQTT code to it, or go the ESPEasy way? I haven’t used ESPEasy myself but it should be mostly self-explanatory. If you want to modify your existing code I can give you some sample code to work with.
Some code snippits from a program I use:

#include <PubSubClient.h>`

/*------------------------------------------------------------------------------------------------*
 * ConnectMqtt: (Re)connect to the MQTT broker.
 *------------------------------------------------------------------------------------------------*
 *DESCRIPTION:
 *	Check if there is an active MQTT connection. If not try to re-establish a connection for 10
 *  seconds (retry 5 times, every other second), before quiting (to prevent stalling).
 *INPUT:
 *	None.
 *OUTPUT:
 *	(bool) true if a connection is established (within 10s), false if it failed.
 *------------------------------------------------------------------------------------------------*/
/*--- MQTT PubSub client handle/instance ---*/
PubSubClient hMqttClient;

bool ConnectMqtt(void)
{
    if (!hMqttClient.connected())
    {
        Serial.print("Setup MQTT...");

        /*--- Loop until we're (re)connected for 5 seconds ---*/
        for (int nLoop = 0; nLoop < 5; ++nLoop)
        {
            /*--- Attempt to connect ---*/
            if (hMqttClient.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PWD))
            {
                Serial.print("connected as Publish client with topic ");
                Serial.println(MQTT_TOPIC);
                return true; //We're done!
            }
            else
            {
#ifdef MQTT_DEBUG
                Serial.print("failed, rc=");
                Serial.print(hMqttClient.state());
                Serial.println("");
#else
                Serial.print(".");
#endif
                yield();
                delay(1000); //Wait at least 1s before retrying
            }
        }
        /*--- MQTT connection failed ---*/
#ifndef MQTT_DEBUG
        Serial.print("failed, rc=");
        Serial.print(hMqttClient.state());
        Serial.println("");
#endif
        return false;
    }
#ifdef MQTT_DEBUG
    Serial.println("MQTT connecting alive");
#endif
    return true;
}

/*------------------------------------------------------------------------------------------------*
 * PublishToTopic: Publish the data to MQTT topic.
 *------------------------------------------------------------------------------------------------*
bool PublishToTopic(char[] achData)
{
/*--- Publish the data to the MQTT topic ---*/
#ifdef MQTT_DEBUG
    Serial.print("MQTT topic: ");
    Serial.println(MQTT_TOPIC);
    Serial.print("MQTT message: ");
    Serial.println(achData);
#endif
    return hMqttClient.publish(MQTT_TOPIC, achData, true);
}

/*------------------------------------------------------------------------------------------------*
 * setup: The standard Arduino Framework one-time initialization routine.
 *------------------------------------------------------------------------------------------------*
void setup()
{
    Serial.begin(BAUDRATE); //Setup the serial console (USB) @115,200 baud

    Serial.print("\r\n \r\nBooting DHT MQTT Sensor, version ");
    Serial.println(SENSOR_VERSION); //Send welcome message to console

    SetupWiFi(); //Setup the WiFi connection

    SetupOTA(); //Setup OTA update service

    hMqttClient.setClient(hEspClient); //Initialzie the MQTT interface
    hMqttClient.setServer(MQTT_SERVER, MQTT_SERVER_PORT);
    (void)ConnectMqtt(); //Setup MQTT connection

    Serial.println("READY\r\n");
}

/*------------------------------------------------------------------------------------------------*
 * loop: The main program loop
 *------------------------------------------------------------------------------------------------*
void loop()
{
    /*--- Make sure we have an MQTT connection ---*/
    if (!hMqttClient.loop()) //Keep the MQTT connection alive
        (void)ConnectMqtt();

    /*--- Read & publish data ---*/
    //  Get your temparture value(s) here;
    (void)PublishToTopic(<YOUR DATA>);

    // Other stuff...
}

But first you should make sure the Broker and Binding are ready for it.