Goal: Send two integer values from Arduino Nano to internet via ESP8266 using Arduino IDE
I am new to embedded programing and currently working on a project which sends some integer value from Arduino analog pins to an online database (IP address, port) via esp8266.
At this moment I know how to individually send data from ESP8266 to an IP keeping ESP in client mode. But I don't know how to transfer data generated at Arduno Nano to ESP8266.
#include <ESP8266WiFi.h>
#include<Wire.h>
const char *ssid = "SSID";
const char *password = "asdfghjkl";
const char* host = "192.222.43.1";
int portNum = 986;
WiFiClient client;
WiFiServer server(portNum);
void setup() {
Serial.begin(115200);
Wire.begin();
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("WIFI OK");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println("Connected to Wifi");
}
String message="";
void loop() {
message = "12,13"; // Message to be sent to ESP8266
if(!client.connected())
{
client.connect(host,portNum);
}
if(message.length()>0)
{
Serial.println(message);
client.println(message);
message="";
}
I can understand I would have to connect the TX-RX pin of Arduino - ESP to pass the data. But for some reason I am not able to make it work.
I would really appreciate if someone can help me understand the process with a simple example.
Thanks.
PS: The reason I had to use Arduino is because the sensor I am using need 2 analog Pins and ESP just have 1.
You connect Arduino Tx to Esp Rx
You connect ESP Tx to your serial device to your PC (so you can read the messages from the ESP in your Terminal window)
On the ESP you use the Wire library you have loaded.
You use the Serial object to listen for incoming data on the ESP's Rx pin.
void loop()
{
while (Serial.available())
{
Do something;
}
}
This works exactly the same as Arduino to Arduino serial comms and there is a nice tutorial here:
Arduino to Arduino comms
WARNING: ESPs use 3.3V and Arduinos use 5V on the Tx and Rx pins. You must not allow 5v to reach the pins of the ESP or it may burn out.
This tutorial shows a safe wiring diagram.
safe wiring diagram
1) Try this sample: simple sample that looks good
2) You have a logic problem in your loop function
a) Your message will be send out as fast as possible because after you leave the loop function you will enter the function again
b) You don't wait for incoming data
I hope the sample helps: I didn't try it because I directly used AT comands instead.
Related
I'm trying to send data from Arduino UNO to NodeMCU via UART.
What I want to do is that when Arduino UNO sends "on" String to the NodeMCU, NodeMCU lights up its builtin LED, when "off" - it turns off.
I send data from Arduino UNO via standard Serial.println (). On the NodeMCU I use the SoftwareSerial library. I assigned rx and tx to pins D7 and D8 accordingly. Serial ports on Arduino(standard) and NodeMCU(SoftwareSerial) are set at 9600 baud rate.
Standard Serial port (USB) of NodeMCU is set to 115200.
I send the string that I receive from the Arduino to the standard serial port of the node (connected to usb)
The question is:
On the standard port of NodeMCU, which I view through the Arduino IDE, messages coming from the arduino are displayed, and displayed correctly (those that were sent), but the NodeMCU does not want to accept them in conditional statements and light up my LED. Why?
At the same time, when I remap the virtual UART to its original pins (connected to USB, GPIO3 and GPIO1, and send the same messages via usb through the COM port view in the Arduino IDE, the LED turns on and off as I programmed it.
Do you have any ideas why this is happening?
By the way, I do not lower voltage coming from Arduino RX and TX pins from 5V to 3.3V, but since messages are recived coorectly, I don't think that this is causing a problem.
Here's my code:
Arduino:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("on");
delay(1000);
Serial.println("off");
delay(1000);
}
NodeMCU:
#include <SoftwareSerial.h>
SoftwareSerial s(D7,D8);//rx,tx
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
s.begin(9600);
pinMode(D4,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
String str = s.readStringUntil('\n');
Serial.println(str);
if(str == "on"){
digitalWrite(D4, HIGH);
}
if(str=="off"){
digitalWrite(D4, LOW);
}
}
Screenshot of COM4:
Screenshot of COM4:
UPD: I tried using sending 1 or 0 as int value via Serial.write() and s.read() and it works, maybe the prolem is in String type somehow
You Use İt Maybe Work
#include <SoftwareSerial.h>
String text = "";
SoftwareSerial s(D7,D8);//rx,tx
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
s.begin(9600);
pinMode(D4,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
String str = s.readStringUntil('\n');
text = str
Serial.println(text);
if(text == "on"){
digitalWrite(D4, HIGH);
}
if(text =="off"){
digitalWrite(D4, LOW);
}
}
I've been working on a project and want an Arduino uno with an HM-19 Bluetooth module to repeatedly send an AT command to the module then print the modules result in the serial monitor. I'm having trouble and it is only returning the number 53 and not the response 'OK'. Thanks for any insight you're able to give.
//import Software Serial to communicate over comms port
#include <SoftwareSerial.h>
// 0 and 1 are the tx and rx pins ans should be where the hm-19 is
// may be 0,1 instead
SoftwareSerial HM19(1,0);
void setup() {
// begin serial monitor and wait for something(?, found online)
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
//tell user it's started
Serial.println("Started");
//I believe this begins the monitor for the HM-19
HM19.begin(9600);
delay(1000);
}
void loop() {
// give user status report
delay(1000);
Serial.println("Sending an AT command...");
// need to find if this is how you send an AT command (currenty send 'AT' as a test)
HM19.write("AT\r");
delay(30);
//wait for the HM19 to respond then print it out
int HMresponse = 0;
if (HM19.available()){}
HMresponse = HM19.read();
Serial.println(HMresponse);
}
I am trying to get an Arduino Uno to send data via Sigfox. Using a Libelium Xbee Shield and Sigfox Module for Arduino (Cooking Hacks).
I tried to send a string using the example found in the Arduino library. The Arduino sketch is simple:
#include <Wire.h>
// Cooking API libraries
#include <arduinoClasses.h>
#include <arduinoUART.h>
#include <arduinoUtils.h>
#include <arduinoSigfox.h>
// Pin definition for Sigfox module error LED:
const int error_led = 13;
//////////////////////////////////////////////
uint8_t socket = SOCKET0; //Asign to UART0
//////////////////////////////////////////////
uint8_t error;
void setup()
{
Serial.begin(9600);
pinMode(error_led, OUTPUT);
//////////////////////////////////////////////
// 1. switch on
//////////////////////////////////////////////
error = Sigfox.ON(socket);
// Check status
if( error == 0 )
{
//"Switch ON OK"
digitalWrite(error_led, LOW);
Serial.println("Sigfox Switch ON -> SUCCES");
}
else
{
//"Switch ON ERROR"
digitalWrite(error_led, HIGH);
Serial.println("Switch Switch ON -> FAILED");
}
//////////////////////////////////////////////
// 2. send data
//////////////////////////////////////////////
// Send 12 bytes at most
error = Sigfox.send("000102030405060708090A0B");
// Check sending status
if( error == 0 )
{
//"Sigfox sending -> SUCCES"
digitalWrite(error_led, LOW);
Serial.println("Sigfox sending -> FAILED");
}
else
{
//"Sigfox packet sent ERROR"
digitalWrite(error_led, LOW);
Serial.println("Sigfox packet sent ERROR");
}
}
void loop()
{
//////////////////////////////////////////////
// 3. sleep
//////////////////////////////////////////////
}
The output on Serial port is the following:
AT
Sigfox Switch ON -> FAILED
AT$SF=000102030405060708090A0B
Sigfox sending -> FAILED
Connection between the Sigfox module and the board seems to be OK, because Sigfox.getID() is working, and the correct ID is retrieved. Also subscription of the device on the Sigfox platform seems to be OK.
How can I debug this? I have no idea how to start a diagnosis: something in the libraries? something in the sending? something in the hardware?. All help on this is appreciated.
Please double check Arduino TX is connected to Sigfox RX, and Arduino RX is connected to Sigfox TX
Check also, that the module has VCC on pin 1, and GND on pin 9.
If it still don't work, it's probably because there is something else connected to RX and TX lines. Remove it.
Personnaly, I put a logic analyser on these lines to check the dialog.
for the "ON": AT\r\n is sent, and "OK\r\n" is answered.
Hope this helps
The problem was relatively simple to solve. It turns out that it is not possible to run the combination Arduino/Xbee/Sigfox with the serial cable connected (I used it for power, and for sending debugging info to my computer).
All I had to do was:
put switch to USB
upload new code via serial cable
unplug the serial cable
put switch to Xbee
power the arduino via the 12V jacket (or other power input)
Then it works.
I have been playing around with an arduino ethernet shield, trying to get basic examples to work, to no avail. Here is my setup:
The Arduino Mega 2560 is connected to the computer via usb and the ethernet shield is stacked upon it. I have tried many variations of the examples that come with the arduino software, and none seemed to work properly.After lots of debugging with wireshark, I figured that:
I can't use DHCP, because it just hangs at the Ethernet.begin(mac) call.
When I try with a static ip, the Ethernet.localIP() function returns 0.0.0.0. However, I can ping my device from my computer using the ip I have set and the device seems to receive and send packets properly.The problem now is that for some reason it drops the tcp connections.E.g here is the code I run that comes the closest to working:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,27);
IPAddress server(192,168,2,52);
EthernetClient client;
void setup() {
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("a");
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 23)) {
Serial.println("connected");
}
else {
Serial.println("connection failed");
}
Serial.println(Ethernet.localIP());
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// as long as there are bytes in the serial queue,
// read them and send them out the socket if it's open:
while (Serial.available() > 0) {
char inChar = Serial.read();
if (client.connected()) {
client.print(inChar);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing:
while(true);
}
}
Its basically the Ethernet/TelnetClient example.
I have set up a telnet server on my computer. Now this is the arduino/computer exchange:
The arduino sends a RST packet, but my server goes on to send it the greeting and login prompt.
I have tried the same with an arduino uno, and have also tried disconnecting the usb and using another power supply.
So, what could be the issue?
The problem is in the connection to the shield, sometimes this shield if it is chinese version, It might have small short-circuit.
I tried disconnect the shield and connect with wire as like as arduino website indicate for arduino uno (the connections with arduino mega aren't correct, so you need connect like arduino uno)
If it don't work , try change the arduino shield. I have a similar problem with the same shield , and normally the problem is the connection to the arduino.
The example should work if the arduino is correctly connected
Strangely, I found that Ethernet fails to initialize if an SD card is inserted and not initialized. So, either take the SD card out, or initialize the SD card:
Sd2Card card;
card.init(speed, pinSelect);
I have some Chinese (SunFounder) version of Ethernet Shield, so it might not be relevant to cards from other manufacturers.
I have a ladyada xbee adapter on the computer side and an arduino xbeeshield which I am trying to communicate with over wireless. Both xbees are configured correctly in that I can receive data from the xbeeshield to the computer. However it doesn't work the other way i.e. xbeeshield does not echo a byte sent from the computer serially. Any idea what I might be doing wrong? (Note: When I connect the arduino board to the computer using USB cable, the echo program works just fine. It seems to be a problem in wireless mode only)
processing code
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
Serial.print((char) Serial.read());
delay(10);
}
}
I am just sending keystrokes from computer and waiting for a reply. I am not getting any.
I use the code I answered to the following question in regards to sending serial bytes from PC to Xbee/Arduino. It's been working fine for months. Ensure you've configured both your Xbee modules on the PC and Arduino side. Ensure your PAN ID's are the same as well.
Arduino making decision according to a packet received from serial port
What version of the Xbee modules are you using? My code works with Series 1 but should work with newer versions as well.
Try using softwareSerial library and connecting Tx and Rx to pin 4 and 2. Run the following sketch and tell me what happens. Change the Baudrate value to match your own
#include <SoftwareSerial.h>
uint8_t pinRx = 2 , pinTx = 4; // the pin on Arduino
long BaudRate = 57600; // Please set your Baudrate. It should match the one in XC-TU
char GotChar, getData;
// Xbee SoftwareSerial initialization
SoftwareSerial xbee(pinRx, pinTx); // RX, TX
void setup()
{
Serial.begin(9600);
Serial.println( "Welcome to the XBee Communication Test" );
Serial.print("BaudRate:");
Serial.println(BaudRate);
Serial.print(" Rx Pin#");
Serial.println(pinRx,DEC);
Serial.print(" Tx Pin#");
Serial.println(pinTx,DEC);
// set the data rate for the SoftwareSerial port
xbee.begin( BaudRate );
xbee.println("Setup Completed!");
}
void loop()
{
if (Serial.available())
{
GotChar = Serial.read();
xbee.print(GotChar);
Serial.print(GotChar);
}
while (xbee.available()>0)
{
Serial.println("Ohohoh");
getData = xbee.read();
Serial.print(" Received: );
Serial.print(getData);
Serial.println();
if(getData == 'a')
{
Serial.println(" sbam");
}
else if(getData == 'b')
{
Serial.println(" sbo");
}
}
}
Upload the program and open the serial monitor. Do you get the 'Setup completed' message on the computer? What happens if you send 'a' or 'b' from the Pc to the Arduino?