Help with newbie code
Posted: Mon Jan 29, 2018 2:29 am
Hi, I have a couple of issues. It should be simple for the experienced..
1) I don't know how to convert a stream of serial bytes to a string variable.
I have the following code:
I'm trying to get the value in Serial.write (msg, size) into a string variable, but I don't know how to do it. I've tried the options above but none of them work.
2) Serial Monitor
My serial monitor is flaky. Sometimes it works, sometimes it doesn't. I actually don't know how or when to use it.
a) does it only work when uploading via USB?
b) could it also read serial output of the ESP without re-uploading the code? In other words, if I find an ESP-01 on the ground, can I plug it in my USB adapter and read its serial output?
c) How does it work, do I have to open the Serial Monitor before uploading for it to read the serial output, or can I open it any time?
d) Do I have to open the Serial Monitor before plugging in the ESP into a USB CH340 adapter or do I open it after the device is plugged in to the USB adapter?
e) Some of my codes work and some doesn't.
WIth this code, only "LED Example." is written on the serial monitor. But the "LED On" and "LED Off" messages are never seen on the serial monitor. The LED on the ESP does blink blue on and off every second though.
This one returns only garbage in the serial monitor:
hksum 0x2d
csum 0x2d
v4ceabea9
~ld
I don't see any of my Serial.prints on the serial monitor. But the two double blinks does work every time it boots up. Just that there are no serial messages coming in.
3) COM Error
I get a java error about the com port every time I try to upload. I have to press upload twice to get the code to upload. This happens even if I click Verify/Compile first or just straight Upload.
Thanks for reading this far.
I'm sure these are actually stupid questions but I really don't know how to figure it out and I don't even know what the right Google search terms to type, I've tried all that I could think off.
Cheers
aledrus
1) I don't know how to convert a stream of serial bytes to a string variable.
I have the following code:
Code: Select all
int size;
while ((size = client.available()) > 0) {
uint8_t* msg = (uint8_t*)malloc(size);
size = client.read(msg, size);
Serial.write(msg, size);
//String chineseSurname = msg.toString(); // error: request for member 'toString' in 'msg', which is of non-class type 'uint8_t* {aka unsigned char*}'
//String chineseSurname = String(msg);
//String chineseSurname = String(msg,size);
String chineseSurname.concat(msg, size);
free(msg);
}
I'm trying to get the value in Serial.write (msg, size) into a string variable, but I don't know how to do it. I've tried the options above but none of them work.
2) Serial Monitor
My serial monitor is flaky. Sometimes it works, sometimes it doesn't. I actually don't know how or when to use it.
a) does it only work when uploading via USB?
b) could it also read serial output of the ESP without re-uploading the code? In other words, if I find an ESP-01 on the ground, can I plug it in my USB adapter and read its serial output?
c) How does it work, do I have to open the Serial Monitor before uploading for it to read the serial output, or can I open it any time?
d) Do I have to open the Serial Monitor before plugging in the ESP into a USB CH340 adapter or do I open it after the device is plugged in to the USB adapter?
e) Some of my codes work and some doesn't.
Code: Select all
void setup() {
Serial.begin(115200);
Serial.println("LED Example.");
delay(1000); // Wait for a second
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
Serial.println("LED On!");
delay(1000); // Wait for a second
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
Serial.println("LED Off!");
delay(2000); // Wait for two seconds (to demonstrate the active low LED)
}
WIth this code, only "LED Example." is written on the serial monitor. But the "LED On" and "LED Off" messages are never seen on the serial monitor. The LED on the ESP does blink blue on and off every second though.
Code: Select all
#include <ESP8266WiFi.h>
const char* ssid = "Hotspot";
const char* password = "00000000";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO1
pinMode(1, OUTPUT);
digitalWrite(1, 1); delay(1000);
digitalWrite(1, 0); delay(200); digitalWrite(1, 1); delay(200); digitalWrite(1, 0); delay(200);
digitalWrite(1, 1); delay(2000);
digitalWrite(1, 0); delay(200); digitalWrite(1, 1); delay(200); digitalWrite(1, 0); delay(200);
digitalWrite(1, 1);
// 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");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
WiFiClient client;
// Check if a client has connected
client = server.available();
if (!client) {
Serial.println("waiting for connection.");
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
Serial.println("waiting for data.");
delay(1);
}
}
This one returns only garbage in the serial monitor:
hksum 0x2d
csum 0x2d
v4ceabea9
~ld
I don't see any of my Serial.prints on the serial monitor. But the two double blinks does work every time it boots up. Just that there are no serial messages coming in.
3) COM Error
I get a java error about the com port every time I try to upload. I have to press upload twice to get the code to upload. This happens even if I click Verify/Compile first or just straight Upload.
Code: Select all
Uploading to I/O Board...
java.io.IOException: jssc.SerialPortException: Port name - COM3; Method name - setEventsMask(); Exception type - Can't set mask.
at processing.app.Serial.dispose(Serial.java:166)
at processing.app.SerialMonitor.close(SerialMonitor.java:116)
at processing.app.AbstractMonitor.suspend(AbstractMonitor.java:90)
at processing.app.Editor$DefaultExportHandler.run(Editor.java:2160)
at java.lang.Thread.run(Thread.java:748)
Caused by: jssc.SerialPortException: Port name - COM3; Method name - setEventsMask(); Exception type - Can't set mask.
at jssc.SerialPort.setEventsMask(SerialPort.java:279)
at jssc.SerialPort.removeEventListener(SerialPort.java:1064)
at jssc.SerialPort.closePort(SerialPort.java:1090)
at processing.app.Serial.dispose(Serial.java:163)
... 4 more
Thanks for reading this far.
I'm sure these are actually stupid questions but I really don't know how to figure it out and I don't even know what the right Google search terms to type, I've tried all that I could think off.
Cheers
aledrus