Connecting NodeMCU to TCP protocol
Posted:
Thu Dec 29, 2016 11:36 am
by WisterDesigns
Hey everyone,
I have created a TCP server with a use of NodeJS and Express framework. I would like to use the NodeMCU as a client and the TCP as the server, I don't have any idea about how to connect the NodeMCU to the server. Can I have an example of code or something in this case?
Thanks!
Re: Connecting NodeMCU to TCP protocol
Posted:
Thu Dec 29, 2016 3:54 pm
by NOVAGENIO
Please, check the examples that you have in the nodemcu instalations. You have a lot of examples, specificly you have a httpclient -> basic httpclient...
regards
/**
* BasicHTTPClient.ino
*
* Created on: 24.05.2015
*
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial
ESP8266WiFiMulti WiFiMulti;
void setup() {
USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("SSID", "PASSWORD");
}
void loop() {
// wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
//http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
http.begin("http://192.168.1.12/test.html"); //HTTP
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(10000);
}
Re: Connecting NodeMCU to TCP protocol
Posted:
Fri Dec 30, 2016 9:51 am
by martinayotte
The above example is an HTTPClient, but you can also use the WiFiClient to connect as plain TCP client, such as a telnet client.