Embedotronics / How-to-send-DHT11-data-to-MySQL-server-using-NodeMCU

5 stars 12 forks source link

connection failed error #1

Open Vishalkargathara opened 4 years ago

Vishalkargathara commented 4 years ago

**### I am not able to connect with my localhost. below code is used

include

include

include

include

include

include

define DHTPIN D2

PZEM004Tv30 pzem(4, 5);

float volt; float amp; float watt; float watth; float hz; float angle; const char ssid = "vishal";// const char password = "vishal1723"; //WiFiClient client; char server[] = "localhost"; //eg: 192.168.0.222

WiFiClient client;

void setup() { Serial.begin(115200); delay(10); // Connect to WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); Serial.print(WiFi.localIP()); delay(1000); Serial.println("connecting..."); } void loop() { float volt = pzem.voltage(); float amp = pzem.current(); float watt = pzem.power(); float watth = pzem.energy(); float hz = pzem.frequency(); float angle = pzem.pf(); Sending_To_phpmyadmindatabase(); delay(2000); // interval }

void Sending_To_phpmyadmindatabase() //CONNECTING WITH MYSQL { if (client.connect(server, 80)) { Serial.println("connected"); // Make a HTTP request: Serial.print("GET /pzem/update.php?voltage="); client.print("GET /pzem/update.php?voltage="); //YOUR URL Serial.println(volt); client.print(volt); client.print("&current="); Serial.println("&current="); client.print(amp); Serial.println(amp); client.print(" "); //SPACE BEFORE HTTP/1.1 client.print("HTTP/1.1"); client.println(); client.println("Host: Your Local IP"); client.println("Connection: close"); client.println(); } else { // if you didn't get a connection to the server: Serial.println("connection failed"); } }

ayazurrehman786 commented 4 years ago

I am using this one and I am ale to connect to my local host.

`#include

include

include

char server[] = "192.168.1.131"; // Your local pc IP address , Go to CMD and type IPCONFIG char ssid[] = "dlink-1880"; // your SSID char pass[] = "Mohammad1"; // your SSID Password

// Initialize the client library WiFiClient client; float humidityData, temperatureData;

void setup() { Serial.begin(115200); WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED) {

delay(1000); Serial.println("Connecting...");

} Serial.println(); Serial.print("WIFI Local IP: "); Serial.print(WiFi.localIP());

if (WiFi.status() == WL_CONNECTED) { Serial.println(); Serial.println("Connected to wifi"); Serial.println("\nStarting connection..."); // if you get a connection, report back via serial:

} } //------------------------------------------------------------------------------

/ Infinite Loop / void loop(){ humidityData = 12.88; temperatureData = 98.4; Sending_To_phpmyadmindatabase(); delay(2000); // interval }

void Sending_To_phpmyadmindatabase() //CONNECTING WITH MYSQL { if (client.connect(server, 80)) { Serial.println("connected"); // Make a HTTP request: Serial.print("GET /Project2/dht_new.php?humidity="); client.print("GET /Project2/dht_new.php?humidity="); //YOUR URL client.print(humidityData); client.print("&temperature="); Serial.println("&temperature="); client.print(temperatureData); client.print(" "); //SPACE BEFORE HTTP/1.1 client.print("HTTP/1.1"); client.println(); client.println("Host: 192.168.1.151"); client.println("Connection: close"); client.println(); } else { // if you didn't get a connection to the server: Serial.println("connection failed"); } }`