Trying to send data from a DHT11 sensor to InfluxDB using a NodeMCU.
Been trying the following 2 codes but not able to do it, someone please help.
Also just for code purposes, I am generating a random value which will be passed on as a temperature value but I am not able to connect to InfluxDB, but my nodemcu is connecting to the wifi successfully
CODE 1
#include <ESP8266WiFi.h> // Include the Wi-Fi library
#include <SPI.h>
//#include <WiFi.h>
WiFiClient client;
byte server = { 192, 168, 56, 101 };
//int port = 8086;
//const int bufferSize= 2048;
//char buf[bufferSize] = {‘\0’};
char* ssid = “test”; // The SSID (name) of the Wi-Fi network you want to connect to
char* password = “siemenss”; // The password of the Wi-Fi network
void setup() {
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println(‘\n’);
WiFi.begin(ssid, password); // Connect to the network
Serial.print(“Connecting to “);
Serial.print(ssid); Serial.println(” …”);
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i);
Serial.print(’ ');
}
Serial.println(‘\n’);
Serial.println(“Connection established!”);
Serial.print(“IP address:\t”);
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
}
void loop() {
String line, temperature;
temperature = String(random(1,101));
line = String(“temperature, device=NodeMCU value=” + temperature);
Serial.print(line + “\n”);
send_data(line);
}
//void send_data(char* line, int linesize) {
//first we need to connect to InfluxDB server
void send_data(String line) {
int conState = client.connect(server, 8086);
if(conState <= 0) { //check if connection to server is stablished
Serial.print(“Could not connect to InfluxDB Server, Error #”);
Serial.println(conState);
return;
}
/*
//Send HTTP header and buffer
client.println(“POST /write?db=new HTTP/1.1”);
client.println(“Host: localhost:8086:”);
client.println(“User-Agent: NodeMCU”);
client.println(“Connection: close”);
client.println(“Content-Type: application/x-www-form-urlencoded”);
//client.print("Content-Length: ");
//client.println(linesize);
client.println();
client.println(line);
delay(50); //wait for server to process data
//Now we read what server has replied and then we close the connection
Serial.println(“Reply from InfluxDB”);
while(client.available()) { //receive char
Serial.print((char)client.read());
}
Serial.println(); //empty line
client.stop();
*/
}