Testing ordered components
Update sensor node shopping- Bought Arduino Pro Mini 3,3v (Altmega 328 16Mhz) boards
- Bought FTDI-Adapter
- Bought ESP8266-1 boards
- Bought BMP180, BH1750, temperature sensors
I used the Arduino IDE to upload a sketch that reads the analog input A0.
Testing the ESP8266 board
First I wanted to check the ESP8266-Firmware:I connected the ESP8266 directly to the FTDI-Adapter. My ESP8266 hat a default baud rate of 115200.
Update: After I bought a breadboard power supply |
ESP8266 -> FTDI-Programmer (has to be common GND)
CH_PD -> GND
VCC -> VCC
TX0 -> RX0
RX0 -> TX0
Output:
AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
Ai-Thinker Technology Co. Ltd.
Jun 13 2016 11:29:20
OK
More: AT+Commands: Cheat sheet
Then I connected the Arduino with the ESP8266
Wiring Arduino/ESP8266/FTDI-Programmer |
- Used nRLF24+ radio chips as ESP8266 modules had not arrived
- Connected first FTDI and ESP8266 to hardware serial due to communication problems on software serial, thats why you will see me use different pins
- Programmed Arduino board with FTDI adapter, then disconnected and attached ESP. Worked but really bad when debugging.
Code to communicate between Arduino and ESP
Here we simply just just have a bridge between both serial (Programmer to Arduino to ESP) connections: #include <SoftwareSerial.h>
SoftwareSerial wifiSerial(2, 3); // TX-Blau-Weiss-2-RX, RX-Violett-1K&1,5K-Grün-3-TX Spannungsteiler</pre>
void setup()
{
Serial.begin(9600); // Seriel Port auf 9600 Baud starten für FTDI Anschluß an PC
Serial.println("Bitte Befehl Eingeben");
wifiSerial.begin(115200); // Software Seriel Port auf 9600 Baud starten für ESP8266
}
void loop() // Endloss Programschleife
{
//Wenn vom ESP Informationen kommen zum FTDI weiterleiten
while (wifiSerial.available()>0 ){
Serial.write(wifiSerial.read());
}
// Wenn vom PC Befehle kommen an ESP weiter senden
while (Serial.available()>0 ){
wifiSerial.write(Serial.read());
delay(100);
}
}
Code to read A0 from Arduino and send it over UDP (via ESP8266 board)
Using my sketch from before just with minor changes. NanoESP to Ardunio + ESP8266
/*
UDP Button Send.
No change necessary.
*/
#define DEBUG true
#include <SoftwareSerial.h>
SoftwareSerial esp8266(11, 12); // RX, TX
void setup() {
Serial.begin(19200);
esp8266.begin(19200);
//Long Timeout needed for RST and settings of WLAN-Data
esp8266.setTimeout(5000);
if (sendCom("AT+RST", "ready")) {
debug("RESET OK");
}
if (configAP()) {
debug("AP ready");
}
if (configUDP()) {
debug("UDP ready");
}
//shorter Timeout for faster wrong UPD-Comands handling
esp8266.setTimeout(1000);
}
void loop() {
if (esp8266.available())
{
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(100);
/* constraint function will limit the values we get so we can work better with map
* since I only need values in the bottom limit of 300, will make it the outer limit and 1023 the other limit
*/
sensorValue = constrain (sensorValue, 300,1023);
// print the values returned by the sensor
//Serial.println(sensorValue);
// create a map with the values
// You can think we have the 100% to 300 and 0 % to 1023 wrong, but no !
// 300 - or 100 % - is the target value for moisture value in the soil
// 0% of humidity - or moisture - is when the soil is dry
int moisture = map (sensorValue, 300, 1023, 100, 0);
String output = String(moisture) + "%";
Serial.println(output);
sendUDP(output);
}
}
//-----------------------------------------Config ESP8266------------------------------------
boolean configAP()
{
boolean succes = true;
succes &= (sendCom("AT+CWMODE=2", "OK"));
succes &= (sendCom("AT+CWSAP=\"NanoESP\",\"\",5,0", "OK"));
return succes;
}
boolean configUDP()
{
boolean succes = true;
succes &= (sendCom("AT+CIPMODE=0", "OK"));
succes &= (sendCom("AT+CIPMUX=0", "OK"));
succes &= sendCom("AT+CIPSTART=\"UDP\",\"192.168.4.255\",55005,55006", "OK"); //UDP Bidirectional and Broadcast
return succes;
}
//-----------------------------------------------Controll ESP-----------------------------------------------------
boolean sendUDP(String Msg)
{
boolean succes = true;
succes &= sendCom("AT+CIPSEND=" + String(Msg.length() + 2), ">"); //+",\"192.168.4.2\",90", ">");
if (succes)
{
succes &= sendCom(Msg, "OK");
}
return succes;
}
boolean sendCom(String command, char respond[])
{
esp8266.println(command);
if (esp8266.findUntil(respond, "ERROR"))
{
return true;
}
else
{
debug("ESP SEND ERROR: " + command);
return false;
}
}
String sendCom(String command)
{
esp8266.println(command);
return esp8266.readString();
}
//-------------------------------------------------Debug Functions------------------------------------------------------
void serialDebug() {
while (true)
{
if (esp8266.available())
Serial.write(esp8266.read());
if (Serial.available())
esp8266.write(Serial.read());
}
}
void debug(String Msg)
{
if (DEBUG)
{
Serial.println(Msg);
}
}
No comments:
Post a Comment