Hi guys,
I'm doing a small project. I use Intel Edison as a server, recieve incoming message and print it out. And nodeMCU ESP12E as a client to periodly send message to server. The connection is already etablished, and all transmitted message is recieved. But when the server print the message, it's not like what the client sent.
Client : Hello? Any body there?
Server (print out):
H
e
l
l
o
?
A
n
y
b
o
d
y
t
h
e
r
e
?
And here is my code:
Server:
#include <WiFi.h>
#include <SPI.h>
char ssid[] = "Truong"; // your network SSID (name)
char pass[] = "12345678"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(500);
void setup() {
Serial.begin(9600);
Serial.print("Connecting to Access Point");
WiFi.begin(ssid,pass);
while (WiFi.status()!=WL_CONNECTED){
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to Access Point!");
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
// wait for a new client:
WiFiClient client = server.available();
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
//print what recieve to Serial Monitor
Serial.println(thisChar);
//}
}
}
and client:
#include <ESP8266WiFi.h>
const char* ssid = "Truong";
const char* password = "12345678";
const char* host = "192.168.1.109";
const char* streamId = "....................";
const char* privateKey = "....................";
WiFiClient client;
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 = 500;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
else Serial.println("Server connected!");
}
void loop(){
client.write("Hello? Any body there?");
Serial.println("Hello? Any body there?");
delay(2000);
}
I have change the code
In the Client, I use "client.write("Hello? Anybody there?\n");
and in the server I use "Serial.print(thisChar);"
The problem is solve! Thanks alot guys, but may I ask a question? What is the diffrent between the "print" and "write"? I have read the reference but I don't understand.
I read from Arduino.cc/Reference. It's said that:
"Serial.Write(): Writes binary data to the serial port. This data is sent as a byte or series of bytes."
But why when I use this function, the serial still write the real data, not binary code? Is there some conversion?
"Serial.print(): Prints data to the serial port as human-readable ASCII text." But sometime, I don't know what mistake cause the server print the array of numbers instead of the text (the text that I recieve from client, and tell the server to print out.)
But why when I use this function, the serial still write the real data, not binary code? Is there some conversion?
Try this in a simple sketch:
byte b = 10;
Serial.println("Sending b as binary data");
Serial.write(b);
Serial.println("----------");
Serial.println("Sending b as ASCII data");
Serial.print(b);
Serial.println("----------");
Open the Serial Monitor application, and let us know what you see.
Text is stored as numbers, each character is a byte. So, when you write() the binary that was defined as text, it sends the bytes as they are and are received and converted back to text by print().
If you define a byte such as b=10, if you send this via write(), it gets send as a single byte of value 10, if you print() it, it will be converted to 2 bytes for the '1' and '0'.
Here's a table showing text characters and their byte equivalents:
What happened? I thougth the write() function should print out "10"????
The print() function DID. The write() function sent a 10 - which the Serial Monitor application interpreted as a line feed. Look at an ASCII table to understand why.