Can't connect ESP8266 to Arduino IDE - c++

I am doing a project where the goal is to collect temperatures with Dht22 and send them in .json through Esp8266. For this I am using Arduino products. Today I was able to connect Arduino Uno and Dht22 to Arduino Ide and collect values by the sensor. The next step is to take care of the network. I followed several tutorials but I always get an error.
I'm using this (see bellow) code copied in create.arduino.cc:
#include <DHT.h>;
#define DHTPIN 7 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
float temp; //Stores temperature value
void setup()
{
Serial.begin(9600);
dht.begin();
}
void loop()
{
temp= dht.readTemperature();
Serial.print("Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
delay(10000); //Delay 2 sec.
}
The Board is configured with "Arduino/Genuino Uno" and the port is COM4. I can get the temperature values this way.
Now to configure Esp8266, from what I realized, I have to change the board to Generic ESP8266 Module. I added the URL
http://arduino.esp8266.com/stable/package_esp8266com_index.json
in File>Preferences>Settings. The problem is that I always get this error:
esptool.py v2.6
2.6
esptool.py v2.6
Serial port COM4
Connecting.....______....._______....._______Traceback(most recent call last):
File "C:\Arduinol5\packages\esp8266\hardware\esp8266\2.5.52\tools\upload.py", line 25, in <module> esptool.main(fakeargs)
File "C:/Arduinol5/packages/esp8266/hardware/esp8266/2.5.52/tools\esptool.py", line 2653, in main esp.connect(args.before)
File "C:/Arduinol5/packages/esp8266/hardware/esp8266/2.5.52/tools\esptool.py", line 468, in connect raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))
esptool.FatalErro: Failed to connect to ESP266: Timed out waiting for packed header
esptool.FatalErro: Failed to connect to ESP266: Timed out waiting for packed header
I've done several Internet searches to find a solution but nowhere to find an answer. For this reason I resort to asking here. If you have any advice I would appreciate it. I hope to have clearly stated the subject. Thank you!

For esp module to boot, reset or wake up, you need to keep IO2 high and IO15 pulled down.
Pull down IO0 to boot into flashing mode.

Related

How can I fix this error of ESP8266: "warning: espcomm_sync failed error: espcomm_open failed error: espcomm_upload_mem failed"?

I'm doing a project with ESP8266-01. This is how I connected it with PL2303:
PL2303 ---> ESP8266
3.3V ---> VCC, CH_PD
TXD ---> RX
RXD ---> TX
GND ---> GPIO0, GND
Next, I opened Arduino IDE (version 1.8.19) and added this code to my sketch (I'm using esp8266 library version 2.1.0):
#include <ESP8266WiFi.h>
const char *ssid = "ESP AP";
const char *password = "12345";
void setup() {
Serial.begin(115200);
WiFi.softAP(ssid, password);
Serial.print("Access Point: ");
Serial.print(ssid);
Serial.println(" started");
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
}
void loop() {}
These are my selections on the Tool tab:
Board: "Generic ESP8266 Module"
Upload Speed: "115200"
CPU Frequency: "80 MHz"
Flash Size: "512K(64K SPIFFS)"
Flash Mode: "DIO"
Flash Frequency: "40 MHz"
Upload Using: "Serial"
Reset Method: "ck"
Debug port: "Disabled"
Debug Level: "None"
Port: "COM13" (my PL2303 COM port)
Programmer: No programmers available for this board
Then I connected my PL2303 to my computer (Windows 10) and hit Upload. It compiled just fine, but I got this logged on my console:
Sketch uses 233067 bytes (53%) of program storage space. Maximum is 434160 bytes.
Global variables use 31920 bytes (38%) of dynamic memory, leaving 50000 bytes for local
variables.
Maximum is 81920 bytes.
warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed
error: espcomm_upload_mem failed
What should I do now to fix this error?
Update: I switched ESP8266v1 with NodeMCU-01 CH340. I'm still using the above sketch to upload to my board. It uploads without error, but I can't see any results like the wifi network created by that sketch (I mean the one with the SSID "ESP AP" but I only see a weird wifi network named "FaryLink_(with a few numbers)" and I think that maybe the wifi network of NodeMCU but that doesn't what I really want) or stuff printed on Serial Monitor. On the Tool tab, I set things up like this (esp8266 library version 3.0.2, Arduino version 1.8.18):
Board:"NodeMCU 1.0 (ESP-12E Module)"
Builtin Led:"2"
Upload Speed:"115200"
CPU Frequency:"80 MHz"
Flash Size:"4MB(FS:2MB OTA:~1019)"
Debug port:"Serial"
Debug Level:"None"
IwIP Variant:"v2 Lower Memory"
VTables:"Flash"
C++ Exceptions:"Disabled (new aborts on oom)"
Stack Protection:"Disabled"
Erase Flash:"Only Sketch"
SSL Support:"All SSL ciphers (most compatible)"
MMU:"32KB cache + 32KB IRAM (balanced)"
Non-32-Bit Access:"Use pgm_read macros for IRAM/PROGMEM"
Port:"COM14"
Anyone can help me solving my problem?
Finally, I decided to change the Arduino version to 1.8.8, esp library version 2.5.0; I've tested with another the sketch and it WORKED!!! I will share my selection on the Tool tab, hope it'll help someone:
Board:"NodeMCU 1.0 (ESP-12E Module)"
Upload Speed:"115200"
CPU Frequency:"80 MHz"
Flash Size:"4MB(3M SPIFFS)"
Debug port:"Serial"
Debug Level:"None"
IwIP Variant:"v2 Lower Memory"
VTables:"Flash"
Exceptions:"Disabled"
Erase Flash:"Only Sketch"
Port:"COM14"
Programmer:"Arduino as ISP"
The sketch I used:
const char* ssid = "name_of_wifi"; // The SSID (name) of the Wi-Fi
network you want to connect to
const char* password = "password_of_wifi"; // The password of the Wi-Fi network
void setup() {
Serial.begin(115200); // Start the Serial communication to send
messages to the computer
delay(10);
Serial.println('\n');
WiFi.begin(ssid, password); // Connect to the network
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266
to the computer
}
void loop() { }
In the circuit the GPIO0 pin is connected to VCC through a 10 k resistor. The voltage on the reset pin is 1.97V. in normal operation.
I removed this resister and connected the GPIO0 pin manually.
Normal operation--> GPIO0 not connected
Program mode --> GPIO0 to GND--> switch on VCC --> GPIO0 to VCC --> not connected (to normal operation)
I tried it with 5 esp8266 modules which all were functioning properly after this modification and did not before.
I did it with the blink example
But when I loaded another program in the arduino, it failed uploading to the esp/ Why?
The only difference was the upload speed of the serial monitor.
the arduino file:
void setup() {
//Serial.begin(115200);
When i commented this lline, the upload to the esp worked. ( //Serial.begin(115200); )
when I uncommented it, ( Serial.begin(115200); ) the upload to the esp failed.
So as soon as the Arduino sketch has a serial begin, the communication to the ESP fails.

Arduino USB Serial communication is throwing error, "avrdude: ser_open(): can't set com-state for "\\.\COM3"

I am learning to program Arduino Uno board. To be honest, I am an absolute beginner to it and I just started learning it today. I have finished my first lesson on blinking an LED light. That went well without any issues.
Now, I am trying to send data from my Arduino board to another device through USB serial port communication. For now, I am following this tutorial to get the serial port software working. https://www.arduino.cc/en/Tutorial/LibraryExamples/SoftwareSerialExample.
So I am trying to compile the following code in the IDE before uploading to the Arduino board.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Native USB only
}
Serial.println("Goodnight moon!");
// set the data rate for the SoftwareSerial port
mySerial.begin(38400);
mySerial.println("Hello, world?");
}
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}
When I compiled it, I am getting the following error.
An error occurred while uploading the sketch
avrdude: ser_open(): can't set com-state for "\\.\COM3"
What is wrong with my code and how can I fix it?

Arduino Nano BLE 33 Sense and DS18B20 not working

My issue is related to Arduino Nano Sense BLE and DS18B20 sensor (waterproof version) are not working together.
What I tried so far. I performed a test on UNO to isolate possible powering and sensor fault. Test looked as follows:
Connection of DS18B20
Black > GDN
Red > 3V
Yellow > D2
Last two connected via 2k2 resistor (2k2 instead 4k7 as I use 3V).
Then, to exclude possible coding mistakes I used ready example:
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/*
* The setup function. We only start the sensors here
*/
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
}
/*
* Main function, get and show the temperature
*/
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
delay(1000);
}
Result? works perfectly fine.
Then I switched to Nano Sense BLE board. without disconnecting the sensor I just switched connection on board's end and attached GDN, 3.3V and D2.
Result, -127. When trying to find DS18B20 address the result is none. I suspect board's pins order issue or Dallas/OneWire lib issue.
I also tried other libs to handle DS18B20, none works, and I tried like 3-4 of them. I noticed there is few topics around internet regarding nano series and none are resolved. I found also IoT and Every has the same problem.
The issue of getting -127 from DS18B20 is because sensor might be disconnected or didn't get 3.3V or 300mA current properly from board. So make sure your pins connected correctly. you can test it with you DMM by checking short between pin and wires. Sometime it might be a library issue. you need to install the latest version of onewire and Dallas temperature library.

Why Serial Communications from Arduino stops after some time

I'm building a growbox/terrarium with arduino uno as the temperature controller. Simple sketch for arduino: if DS18B20 sensor giv less than 25'C than turn on relay, which the heating cable is connected to. Loop 30s, every time Serial.print(temperature) to the PC, where I'm collecting data and make timelapse photos. ---> Here is the problem.
After some time (from 15 min up to 4 hours). Serial communication with PC stops. When I'm trying to upload a new sketch to arduino I got an error msg:
avrdude: ser_open(): can't set com-state for "\.\COM3"
I need to unplug and plug in again USB cable (or turn it off and on in Windows Device Manager), also restart Python app that is collecting data. (Very unsatisfactory fix).
So my questions are:
1. Why?
2. How to fix it?
3. Or how to make some workaround, for example reset COM port from code (desirably python2.7)
PS. Example of what I'm doing and how it works(and do not works) here: Life of pepper
PS2. My goal is to make controllable habitate for plants, where I can see behaviours differs depending on the temp, day-duration, light-intensity, humidity.
Please help me :-) .
ARDUINO UNO SKETCH
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the A
rduino
#define ONE_WIRE_BUS 2
#define PIN_HEATING 6
float temperatura = 30.0;
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
pinMode(PIN_HEATING, OUTPUT);
}
void loop(void)
{
delay(30000);
if(temperatura<25.0){
digitalWrite(PIN_HEATING, HIGH);
}
else{
digitalWrite(PIN_HEATING, LOW);
}
sensors.requestTemperatures(); // Send the command to get temperatures
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
temperatura = sensors.getTempCByIndex(0);
Serial.print("#");
Serial.print(temperatura);
}
PYTHON2.7 RECEIVING PART
import serial #pySerial
ser = serial.Serial()
ser.port = "COM3"
ser.open()
data = ser.read_all().split("#")
datasize = len(data)
if datasize>1:
temp = data[datasize-1]
tempstr = " / " + str(temp) + "'C"
else:
tempstr=" / ----- "
I've experienced a similar problem in the past where the serial port would become unresponsive. The solution in my case wasn't very intuitive but worked nonetheless. Here's what might help you too:
Make sure the USB port you're using to connect the arduino isn't shared with other devices (for example using a USB multipoint hub). If you're using a desktop with multiple USB ports (usually found at the back of the desktop case), unplug all other USB devices (keyboard, mouse etc) and reconnect them to a different USB. Use the fastest USB port available available on your computer (USB 3.0 if present) and have it dedicated for communication with the arduino. The reason the communication port goes unresponsive sometimes is because of the momentary fluctuations in the voltage and/or current if you have several devices connected to it.
Make sure you have a stable power supply for you computer, voltage drifts due to heavy loading can sometimes cause PC peripherals to disconnect/reconnect.
If this doesn't work, try this quick software fix. Just before the delay(30000) statement in your code, add the following:
if(!Serial) { //check if Serial is available... if not,
Serial.end(); // close serial port
delay(100); //wait 100 millis
Serial.begin(9600); // reenable serial again
}
This might at least ensure continued operation operation of your application with out requiring your intervention.
So in my case the problem was solved by connecting my computer through proper surge supressor, formerly i had connectected it to wall with a simple cable, and I have old 2 wire, non-grounded, electric installation in my flat.

Cannot connect Wifly to Arduino

I am just an Arduino beginner. I bought an Arduino Uno and a Wifly shield yesterday and I am not able to run the Wifly_Test example program come with WiFlySerial library.
When I look at Serial Monitor, I only saw 2 lines are printed out
1.Starting WiFly Tester.
2.Free memory:XXXX
How can I know that the Wifly Sheild that I bought is not faulty?
I soldered the heard ping to Wifly Shield and stacked it to Aurduino Uno and I can see the LEDs blinking on the Wifly Shield.
Do I need to reset the Wifly Sheild? How do I reset it?
Please point me to the most simple example on how to connect to the router.
I have also bought the shield and had trouble to start with.
If you have soldered the pins to the shield that should be fine but make sure you check they all have a connection and that they don't have solder running down the legs of the pins as this causes the shield to be temperamental.
Run the code below which is from the WiFly library (alpha 2 version) that can be found here:
http://forum.sparkfun.com/viewtopic.php?f=32&t=25216&start=30
Once you see that the shield has connected it will ask for an input, type $$$ and press enter... you have now entered the command line and CMD will be displayed.
If you do not know your network settings type scan and this will display them.
Then set your authentication by typing set wlan auth 3 (Mixed WPA1 & WPA2-PSK) or set wlan auth 4 (WPA2-PSK) this depends on the type of authentication you ise so pick the write one for your network.
Then type set wlan phrase YourPharsePhrase (Change YourPharsePhrase to whatever your WPA key is)
Then type join YourSSIDName (Change YourSSIDName to whatever your network name is)
You see something like this:
join YourSSIDName
Auto-Assoc YourSSIDName chan=1 mode=MIXED SCAN OK
Joining YourSSIDName now..
<2.15> Associated!
DHCP: Start
DHCP in 1234ms, lease=86400s
IF=UP
DHCP=ON
IP=10.0.0.116:2000
NM=255.255.255.0
GW=10.0.0.1
Listen on 2000
you are now connected to your network.
Hopefully this will get you up and running.
N.B. REMEMBER TO CAREFULLY CHECK YOUR PINS! I had great trouble with mine because only a small amount of solder is needed but enough to get a good connection, the balance of this was minute but enough that it wouldn't work. I used a magnifying to check mine in the end.
#include "WiFly.h" // We use this for the preinstantiated SpiSerial object.
void setup() {
Serial.begin(9600);
Serial.println("SPI UART on WiFly Shield terminal tool");
Serial.println("--------------------------------------");
Serial.println();
Serial.println("This is a tool to help you troubleshoot problems with the WiFly shield.");
Serial.println("For consistent results unplug & replug power to your Arduino and WiFly shield.");
Serial.println("(Ensure the serial monitor is not open when you remove power.)");
Serial.println();
Serial.println("Attempting to connect to SPI UART...");
SpiSerial.begin();
Serial.println("Connected to SPI UART.");
Serial.println();
Serial.println(" * Use $$$ (with no line ending) to enter WiFly command mode. (\"CMD\")");
Serial.println(" * Then send each command followed by a carriage return.");
Serial.println();
Serial.println("Waiting for input.");
Serial.println();
}
void loop() {
// Terminal routine
// Always display a response uninterrupted by typing
// but note that this makes the terminal unresponsive
// while a response is being received.
while(SpiSerial.available() > 0) {
Serial.write(SpiSerial.read());
}
if(Serial.available()) { // Outgoing data
//SpiSerial.print(Serial.read(), BYTE);
SpiSerial.write(Serial.read());
}
}
Sorry I forgot to mention , you reset the shield by going to the WiFly library and going to: WiFly/tools/HardwareFactoryReset
Then open the serial monitor and type in any character and this will start the reset.
Thanks everyone who tried to answer me. I finally solved my problem by using Arduino 0023 instead of 1.0.