Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
1,597 Views

Send data from nodeMCU ESP12 v01 to NodeJS server Edison

Hi guys,

I'm getting stuck in sending data to Server that running nodeJS (intel Edison) with GET method.

The client is the ESP8266 NodeMCU v1.0: http://www.ebay.com/itm/V1-0-NodeMcu-LUA-WiFi-Networking-ESP8266-ESP-12E-Development-Board-/39139337... V1.0 NodeMcu LUA WiFi Networking ESP8266 ESP-12E Development Board

The client is connected with DHT22 Temperature and Humidity sensor, to collect data and send to Intel Server.

The code in server-side:

var express = require('express');

var app = express();

app.listen(3000);

app.get('/', function (req,res) {

console.log('connected');

})

app.get('/sensor/:temp/:hmdt', function(req, res) {

var t = req.params.temp;

var h = req.params.hmdt;

console.log(t);

console.log(h);

});

On ESP, I use Arduino IDE to code with ESP8266WiFi.h library:

# include

# include

# define DHTPIN 5

# define DHTTYPE DHT22

const char* ssid = "xxxxxxx";

const char* password = "xxxxxxx";

const char* host = "xxxxxxx";

const char* streamId = "....................";

const char* privateKey = "....................";

WiFiClient client;

DHT dht(DHTPIN,DHTTYPE);

void setup() {

Serial.begin(115200);

delay(10);

// We start by connecting to a 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");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

Serial.print("Connecting to ");

Serial.println(host);

// Use WiFiClient class to create TCP connections

const int httpPort = 3000;

if (!client.connect(host, httpPort)) {

Serial.println("connection failed");

return;

}

else Serial.println("Server connected!");

dht.begin();

}

void loop(){

client.close();

float t = dht.readTemperature();

float h = dht.readHumidity();

int temp = (int) (t*100);

int humid = (int) (h*100);

String tempchar = String(temp);

String humidchar = String(humid);

String packet = "GET /sensor/" + tempchar+ "/" + humidchar + " HTTP/1.1";

client.println(packet);

Serial.println(packet);

delay(2000);

}

The server is running correctly because I've tried to send data with the browser, but when I tried the ESP, the server did not responsed anything even the ESP said that it's connected to the server.

Please help me.

Tags (1)
0 Kudos
6 Replies
Highlighted
Community Manager
177 Views

Hello NewMech,

Thank you for interest in the Intel® Edison Breakout Board.

I understand that you are trying to send data collected by a couple of sensors to an Intel server. I have been doing some research but I need more time before I come up with information that you may find useful.

Thank you for your patience, I'll be contacting you as soon as I have relevant information.

If you have any updates, please post them here.

Regards,

 

Andres
0 Kudos
Highlighted
Beginner
177 Views

That's great Andres!

If you need more infomation, please tell me.

0 Kudos
Highlighted
Community Manager
177 Views

Hello NewMech,

 

 

From what I understand, the issue seems to be related to the "sending" part. You mentioned that the server (Edison, Node.js) is running correctly because you sent data with a browser. What kind of test did you performed in order to see if the ESP module is transmitting the data obtained by the sensors?

 

 

You may want to check this link http://www.instructables.com/id/Programming-ESP8266-ESP-12E-NodeMCU-Using-Arduino-/, because it mentions some hardware guidelines that might help us find if the issue is in fact software related (please review the pin mapping). Also, there is a LED blink example and a series of helpful links.

 

 

I'll be waiting for your response.

 

 

Regards,

 

Andres
0 Kudos
Highlighted
Beginner
177 Views

Hi Andres,

Thanks for reply, I've read the topic you gave me, but it is not contain what I need to read.

This is my test for server:

First, I run the server on my own PC (the same way as we run the server in Intel Edison and I've already checked). Then, I open the browser (Google Chrome), and type: "localhost:3000/25/30". The terminal printout 25 and 30. By the way, I had tried to use another computer (in the same local network) to send data the same way, but a little different, I type: "192.168.1.102:3000/26/31", and the result is still the same (192.168.1.102 is the IP address of the server PC in the network).

This is the test for ESP (client):

I have 2 nodeMCU ESP-12. The first one is "coded" as master, and the second one is "slave". The slave collect the data from the sensor, then mix the data in to the only one string, and print out what it sent to "master". (Ex: 28504500, mean temperature is 28.50 Celcius deg. and humidity is 45%). The master can receive all the data, so that I believe that the data is collected correctly. But it's so wierd. The 2 ESPs is communicating using string, and the data is just a string. But what I type on the browser to ask the browser to send is the different one, and I think it's the true HTTP request, and what the ESP sending is not. I think it's just a line of string even it look the same as what I type on browser.

The broblem is only on the "sending" part. How can I ask the ESP to send the true HTTP request to server, not some lines of string?

Thanks.

0 Kudos
Highlighted
Beginner
177 Views

Oh, Andres, I've found the way.

What I've told the ESP to send is not enough for a HTTP request. The full HTTP request contain many things like: method, path, protocol, header.

This is the fixed code:

# include

# include

# define DHTPIN 5

# define DHTTYPE DHT22

const char* ssid = "xxxxxxx";

const char* password = "xxxxxxx";

const char* host = "xxxxxxx";

const char* streamId = "....................";

const char* privateKey = "....................";

WiFiClient client;

DHT dht(DHTPIN,DHTTYPE);

void setup() {

Serial.begin(115200);

delay(10);

// We start by connecting to a 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");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

Serial.print("Connecting to ");

Serial.println(host);

// Use WiFiClient class to create TCP connections

const int httpPort = 3000;

if (!client.connect(host, httpPort)) {

Serial.println("connection failed");

return;

}

else Serial.println("Server connected!");

dht.begin();

}

void loop(){

client.close();

float t = dht.readTemperature();

float h = dht.readHumidity();

int temp = (int) (t*100);

int humid = (int) (h*100);

String tempchar = String(temp);

String humidchar = String(humid);

String packet = "/sensor/" + tempchar+ "/" + humidchar;

client.print(String("GET ") + packet + " HTTP/1.1\r\n" +

"Host: " + host + "\r\n" +

"Connection: close\r\n\r\n");

Serial.println(packet);

delay(2000);

}

0 Kudos
Highlighted
Community Manager
177 Views

Hello NewMech,

 

 

I'm glad that you could find a solution to your issue.

 

 

Thank you very much for sharing your solution with the community, we really appreciate it.

 

 

Regards,

 

Andres
0 Kudos