instead of getting 2 values, the order is changed as shown in pic, i am using parseFloat() here, i want to save the float values into a variable with array, how do i do it so that those two values are alone saved in the array?? sometimes 2 values come, sometimes 3 values come,
the first is humidity and the other is temperature.
P.S baud rate is 9600 b/w nodemcu to arduino
parse code
void loop() {
// put your main code here, to run repeatedly:
while (NodeSerial.available() > 0)
{
delay(1000);
float val = NodeSerial.parseFloat();
//Data.concat(val);
if (NodeSerial.read() == '\n');
//delay(1000);
}
/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
#include <SoftwareSerial.h>
SoftwareSerial NodeSerial(D2, D3); // RX | TX
const char* ssid = "Jerald";
const char* password = "9443110000";
WiFiClient client;
unsigned long myChannelNumber = 441486;
const char * myWriteAPIKey = "5QEPJGGHNVKRGPR6";
uint8_t temperature, humidity;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
NodeSerial.begin(9600);
delay(10);
// 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");
// Print the IP address
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop() {
// put your main code here, to run repeatedly:
static boolean data_state = false;
float val1[3]; //char character;
while (NodeSerial.available() > 0)
{
delay(1000);
float val = NodeSerial.parseFloat();
//Data.concat(val);
if (NodeSerial.read() == '\n');
Serial.println(val);
}
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
if( data_state )
{
ThingSpeak.writeField(myChannelNumber, 1, temperature, myWriteAPIKey);
data_state = false;
}
else
{
ThingSpeak.writeField(myChannelNumber, 2, humidity, myWriteAPIKey);
data_state = true;
}
// } /
//delay(30000); // ThingSpeak will only accept updates every 15 seconds.
/*NodeSerial.print(10);
NodeSerial.print("\n");
delay(100);*/
}
arduino send code
#include <SoftwareSerial.h>
#include <dht.h>
#define dht_apin 7
dht DHT;
float temp;
float hum;
SoftwareSerial ArduinoSerial(3, 2); // RX | TX
void setup() {
// put your setup code here, to run once:
pinMode(3, INPUT);
pinMode(2, OUTPUT);
Serial.begin(115200);
ArduinoSerial.begin(9600);
delay(500);//Delay to let system boot
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);//Wait before accessing Sensor
}
void loop() {
// put your main code here, to run repeatedly:
//delay(2000); //sensor boot-up delay
//Read data and store it to variables hum and temp
DHT.read11(dht_apin);
hum = DHT.humidity;
temp = DHT.temperature;
ArduinoSerial.print("Current humidity = ");
ArduinoSerial.print(hum);
ArduinoSerial.print("% ");
Serial.print(DHT.humidity);
ArduinoSerial.print("temperature = ");
ArduinoSerial.print(temp);
ArduinoSerial.println("C ");
Serial.print(DHT.temperature);
ArduinoSerial.print("\n");
}
It is a waste of time printing the text as you do not use it on the receiving side but it would be a good idea to format the data better so that receiving and parsing it was easier.
You have no delays in the sending loop and " delay(1000);" in the receiving loop. I suspect your inout buffer is overflowing. Try putting the delay in the sending and NOT in the receiving. The "parseFloat()" will wait for the float to arrive.
johnwasser:
You have no delays in the sending loop and " delay(1000);" in the receiving loop. I suspect your inout buffer is overflowing. Try putting the delay in the sending and NOT in the receiving. The "parseFloat()" will wait for the float to arrive.
thanks much, its working now thanks all guys all your help is implemented
all are working except the below error i want the parsed data to enter the array, but this error happens
char array1[2] = {'0','0'};
void loop() {
while (NodeSerial.available() > 0)
{
delay(1000); // receive delay working as expected
float val = NodeSerial.parseInt();
Serial.println(val);
for(j = 0; j < 2; j++) //// here i get error 'j' was not used in this scope
{
array1[j] = val;
Serial.println(array1);
}
}
omg, yes thanks, another issue is, according my loop code 1st loop gets temperature 2nd loop gets humidity and recurs i need to store temp and humidity in 2 different array(array1[0],array1[1]), how's that possible
current code
void loop() {
while (NodeSerial.available() > 0)
{
delay(1000);
float val = NodeSerial.parseInt();
Serial.println(val);
for (int i=0; i < 2 ; i++)
{
array1[i] = val;
}
Serial.println(array1[0]);
Serial.println(array1[1]);
}