I'm testing the Serial Communication with my Arduino and NodeMCU, and how I can send one data to another. Here is the Code.
For the Arduino Uno
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //RX, TX
float i = 10;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
Serial.print("Number ");
Serial.print(i);
Serial.println("");
mySerial.write(i);
delay(1000);
}
And here is for my ESP8266 NodeMCU
#include <SoftwareSerial.h>
SoftwareSerial myNode(D1, D2); //RX, TX
float git;
void setup() {
Serial.begin(115200);
myNode.begin(9600);
}
void loop() {
git = Serial.read();
Serial.print("The Number is ");
Serial.print(git);
Serial.println("");
delay(1000);
}
The result should be that in the Serial Monitor it should say "The Number is 10". But instead it says -1.00 enter image description here
Am I doing something wrong?
Ah, I finally figured it out. Apparently I need to initialize the NodeMCU Pin's as so:
#include <SoftwareSerial.h>
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
SoftwareSerial myNode(D1, D2); //RX, TX
float git;
void setup() {
Serial.begin(115200);
myNode.begin(9600);
}
void loop() {
git = Serial.read();
Serial.print("The Number is ");
Serial.print(git);
Serial.println("");
delay(1000);
}
Try
git = myNode.read();
instead of
git = Serial.read();
And check myNode.available() before reading
Related
I, a complete beginner, was doing a project with ESP8266 where I interfaced with a relay and two sensors. The sensors are working fine but the relay is not getting actuated by the Blynk button when I press it. Below is the code where I got proper output for the sensors but not the relay.
I get all the values of sensors in the Blynk app but not the relay actuation where I connected it to a motor and in the D2 pin. Thanks in advance.:)
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "my_template"
#define BLYNK_DEVICE_NAME "my_device name"
#define BLYNK_AUTH_TOKEN "Auth token"
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#define BLYNK_PRINT Serial
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
char auth[] = "Authtoken";
char ssid[] = "my_ssid";
char pass[] = "my_pass";
#define sensorPin D3
int sensorState = 0;
int lastState = 0;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
Blynk.virtualWrite(V5, h); //hum
Blynk.virtualWrite(V6, t); //temp
}
void setup()
{
pinMode(D2,OUTPUT); //these two lines are the one which use for actuating the relay
digitalWrite(D2, HIGH);//
Blynk.begin(auth, ssid, pass);
pinMode(sensorPin, INPUT);
dht.begin();
timer.setInterval(1000L, sendSensor);
Blynk.begin(auth, ssid, pass);
sensors.begin();
}
int sensor = 0;
void sendTemps()
{
sensor = analogRead(A0);
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
Blynk.virtualWrite(V1, temp);
Blynk.virtualWrite(V2, sensor);
delay(1000);
}
void loop()
{
Blynk.run();
timer.run();
sendTemps();
sensorState = digitalRead(sensorPin);
if (sensorState == 1 && lastState == 0) {
lastState = 1;
delay(1000);
}
else if (sensorState == 1 && lastState == 1) {
delay(1000);
}
else {
lastState = 0;
delay(1000);
}
delay(100);
}
I am using 2 Arduino, 1 Esp01,433 MHz 1 receiver and 1 433 MHz transmitter. The transmtiter sending uint_8 "85648217". When i write below the code.I can receive the messages and i can get response from api.
#include <ArduinoJson.h>
#include <RH_ASK.h>
#include <SPI.h>
#define RX 0
#define TX 1
using namespace std;
unsigned char rxBuf[512];
RH_ASK rf_driver;
void setup() {
rf_driver.init();
Serial.begin(9600);
}
void loop() {
{
uint8_t buf[8];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen))
{
Serial.println("https://example.com/example/example/"+(String((char*)buf))+";headers\n");
//(String((char*)buf) is number from transmitter
String message = Serial.readString();
const size_t capacity = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(capacity);
// DeserializationError error = deserializeJson(doc, message);
//if (error) {
// Serial.print(F("deserializeJson() failed: "));
//Serial.println(error.f_str());
// return;
// }
Serial.println(doc["attribute"].as<const char*>());
Serial.println(message);
}
}
}
When i write code like this.I can't receive message.I am receving "07413943⸮" or "07413943⸮ " .I don't know why is this happening.Does anyone know how can i fix this?
#include <ArduinoJson.h>
#include <RH_ASK.h>
#include <SPI.h>
#define RX 0
#define TX 1
using namespace std;
unsigned char rxBuf[512];
RH_ASK rf_driver;
void setup() {
rf_driver.init();
Serial.begin(9600);
}
void loop() {
{
uint8_t buf[8];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen))
{
Serial.println("https://example.com/example/example/"+(String((char*)buf))+";headers\n");
//(String((char*)buf) is number from transmitter
String message = Serial.readString();
const size_t capacity = JSON_OBJECT_SIZE(1) + 20;
DynamicJsonDocument doc(capacity);
DeserializationError error = deserializeJson(doc, message);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
Serial.println(doc["attribute"].as<const char*>());
Serial.println(message);
}
}
}
I found the solution.It seems like i need to convert string this (String((char*)buf)) but not like this.Here is how i solved.
uint8_t buflen = sizeof(buf)
if (rf_driver.recv(buf, &buflen))
{
rf_driver.printBuffer("Got:",buf,buflen);
String rcv;
for (int i=0; i<buflen;i++){
rcv+=(char)buf[i];
}
Serial.print("example.com/example"+rcv+";header");
String input = Serial.readString();
StaticJsonDocument<32> doc;
DeserializationError error = deserializeJson(doc,input);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
float attribute= doc["attribute"]; // 428.5
Serial.println(float(attribute));
Serial.println(input);
I am new to this community, and I look forward to your help with this issue.
I have communication between two ESP8266 modules. One of these as a Server/Access-Point and the other as a Client. The configuration and connection between these two modules are satisfactory, and part of the communication as well. However, after the Client makes the first request (Begin and GET), and the server responds, for the second Client/Server request, the Client restarts with the next message or exception.
Exception (9):
epc1=0x40207204 epc2=0x00000000 epc3=0x00000000 excvaddr=0x0036006a depc=0x00000000
That is, the first request to the server's root is answered, but the second to /data causes the mentioned restart on the Client.
After reviewing the stack exception decoder, I find that the code (9) means 9: LoadStoreAlignmentCause Load or store to an unaligned address.
Then, I've tried different solutions for this issue
erase all flash content
http.end()
change the macro delay() for delayMicroseconds()
and a few more
But nothing works yet.
Please, help me with this issue. The codes of the server and Client are:
The client:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define LED_PIN 2
const char* ssid = "Mi_Servidor";
const char* password = "";
const char* host = "http://192.168.4.1";
int counter=1, httpCode=0;
HTTPClient http;
void peticion (int httpCode){
String mensaje;
if(httpCode == 200) {
mensaje = http.getString();
Serial.println(mensaje);
}
else {
Serial.print("[HTTP] GET... failed, no connection or no HTTP server\n");
}
}
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
delay(10);
Serial.print("Conectando a ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi conectada");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(){
counter++;
int modulo=counter%2;
if(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
else{
Serial.print("[HTTP] begin...\n");
if(modulo==0) http.begin("192.168.4.1", 80); //HTTp
else http.begin("192.168.4.1", 80, "/data"); //HTTp
httpCode = http.GET();
Serial.print("httpCode: "); //JJ
Serial.println(httpCode); //JJ
peticion(httpCode);
delay(20);
}
Serial.println("End Loop!");
}
And the server:
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define LED_PIN 2
void handle_datos();
const char WiFiClave[] = ""; //Sin clave
const char AP_Nombre[] = "Mi_Servidor";
int val;
String MiMessage = "Message to transmit:\n\r";
ESP8266WebServer server(80);
void handle_data() {
server.send(200, "text/plain", MiMessage);
delay(1000);
}
void handle_root() {
server.send(200, "text/plain", "Mi_root");
delay(1000);
}
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
WiFi.mode(WIFI_AP);
WiFi.softAP(AP_Nombre, WiFiClave);
server.on("/data", handle_data);
server.on("/", handle_root);
server.begin();
// for (int i = 0; i <= 3999; i++) {
// MiMessage.concat(String(i));
// MiMessage.concat(";");
// }
Serial.println();
Serial.println(MiMessage);
Serial.println("\nServidor ready... ");
}
void loop() {
server.handleClient();
delay(100);
}
Thanks in advance
TRANSMITTER.ino (COM8 port)
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include<ArduinoJson.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[6] = {"00001"};
void setup() {
radio.begin();
radio.openWritingPipe(addresses);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
DynamicJsonBuffer jBuffer;
JsonObject& root = jBuffer.createObject();
root["North"] = "true";
root["South"] = "false";
root["East"] = "true";
root["West"] = "true";
radio.write(&root, sizeof(root));
delay(1000);
}
RECEIVER.ino (COM9 port)
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include<ArduinoJson.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[6] = {"00001"};
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, addresses);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if ( radio.available()) {
unsigned char data[1024];
radio.read(&data, sizeof(data));
StaticJsonBuffer<1024> jsonBuffer;
JsonObject& toor = jsonBuffer.parseObject(data);
String n = toor["North"];
String s = toor["South"];
String e = toor["East"];
String w = toor["West"];
Serial.println(n);
Serial.println(s);
Serial.println(e);
Serial.println(w);
delay(1000);
}
}
I am trying to transmit a JSON data over NRF24L01, and printing it to the serial monitor, but i dont see any output in my serial monitor(COM9 serial monitor). what is the mistake am i doing here?
I am using arduino JSON 5.13.5 version
#include "DHT.h"
#define DHTTYPE DHT11
#define dht_dpin 0
const int analog_ip = A0;
int inputVal = 0;
DHT dht(dht_dpin, DHTTYPE);
void setup() {
dht.begin();
Serial.begin(9600);
Serial.println("Humidity sensor \n\n");
delay(1000);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("current humidity = ");
Serial.print(h);
Serial.println("% ");
Serial.print("\ncurrent temperature = ");
Serial.print(t);
Serial.println("C ");
inputVal = analogRead (analog_ip);
Serial.print("\ncurrent heartbeat = ");
Serial.println (60000/inputVal);
delay(1000);
}
This is my working code. I want to send data from these sensors to the database. How can I do this using variables of my code? I am using Node MCU as I/O processor.
visit this githup page
Github MySQL Connector with Samples
and download the lib.
There are many samples how to use MySQL / MariaDB.