0
votes

I've been trying to send JSON data from arduino mega by using ESP8266 as a wifi shield, and I've used node.js as a socket server. The problem is it seems that a server didn't receive any data. here is my code

   #include <ArduinoJson.h>
#include "SoftwareSerial.h"


String ssid ="ssid";
String password="pwd";
//SoftwareSerial esp(22,23);// RX, TX
String data;
String server = "server ip"; 
byte objlength;
String url = "";
String temp,hum,weight;
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
//String uri = "yourURI";

void reset() {

Serial2.println("AT+RST");
delay(1000);
if(Serial2.find("OK") ) Serial.println("Module Reset");


}
void connectWifi() {

String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
Serial2.println(cmd);
delay(4000);

if(Serial2.find("OK")) {
Serial.println("Connected!");
Serial2.println("AT+CIPSTATUS");
delay(300);
while(Serial2.available()){Serial.print(Serial2.read());}

}
else {
connectWifi();
Serial.println("Cannot connect to wifi"); }
}

void setup() {
delay(5000);
  // put your setup code here, to run once:
Serial2.begin(115200);
Serial.begin(115200);
reset();
connectWifi();

}


void loop() {
temp = 25.60;
hum = 65.3;
weight = 65.3;

root["weight"] = weight;
root["light"] = temp;
root["humid"] = hum;
objlength = root.measureLength();
senddata();
delay(10000);
}
void senddata() 
{
int objlength = root.measureLength();

Serial2.println("AT+CIPSTART=\"TCP\",\"" + server + "\",1336");//start a TCP connection.

delay(500);
if( Serial2.find("OK")) {
Serial.println("TCP connection ready");
} 
else
{
Serial.println("can't establish TCP connection");
}
String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.

Serial2.println(sendCmd);
delay(200);
Serial2.println(objlength);

delay(500);

if(Serial2.find(">")) 
{ 
Serial.println("Sending..");
root.printTo(Serial2);
root.printTo(Serial);
//Serial.println(postRequest);
  delay(2000);
  if( Serial2.find("SEND OK")) 
  { 
    Serial.println("Packet sent");
    delay(200);

    while (Serial2.available()) {
    String tmpResp = Serial2.readString();
    Serial.println(tmpResp);
  }
// close the connection

  }
 //delay(1000);
Serial2.print("+++");
delay(1200);
Serial2.println("AT+CIPCLOSE");
delay(50);
Serial.println("Closed");
}

}

Here's my node.js

var net = require('net');

var server = net.createServer(function(socket){
    socket.write('SEND OK');
//
    socket.pipe(socket);

socket.on('data',function(data){
    //if(typeof data != 'string'){
    var jsontest = JSON.parse(data);
    console.log('Received: ' + data);
    console.log(jsontest.weight);
    console.log(jsontest.light);
    console.log(jsontest.humid);
    //}
    });
socket.on('listening',function(){
    console.log(listen);
});
});
/*server.getConnections(function(err,count){
console.log(count);
});*/
server.listen(1336, '10.42.0.1');

I think that esp8266 can establish a connection with a server , but I don't know why the data won't show. Maybe it's about esp8266 respond time? screenshot As you can see from this screenshot, I run node.js server and arduino ,but data won't show on server side. Due to this,I'm not sure where are the problems that cause this.

1
I don't see any POST request being made. Does the JsonObject send it? What is your Arduino output? Where does it fail? - gre_gor
I didn't use http method to send the data. Data are sent through TCP socket between arduino and Node.js server after I use AT+CIPSEND command, but Esp8266 doesn't respond SEND OK. Plus node.js server doesn't print out the data. Even though arduino can connect to a server because it shows "TCP connection ready" in a serial monitor , but data don't show on a server. Thus I'm not really sure where to fix. - Ball Tha
Oh, I'm sorry. I think I forgot to change my function name. I used http method at first so my function name was httppost(). - Ball Tha
if we could see logs from both sides, that would help - leetibbett
For arduino,I only have output that show on serial monitor. Is that count? - Ball Tha

1 Answers

0
votes

I'm not an expert. I'm just learning how to do it but i'd say you are not giving enough time through connections. You are relying on delays rather than timeouts while waiting for a true response. There is a difference.

Add much more time or change your strategy by using serial.available() and read in a loop that last a prudential amount of time, and exit when you got data.

Sorry no pasting some code, but i hope you got the idea.

Also i'm facing a power problem. 3,3v pin in arduino uno could be weak in clone boards. But i dont think that's the case.