Commit 4a56b6ce authored by Zainab Temitope Olalekan's avatar Zainab Temitope Olalekan
Browse files

first commit

parents
//ThatsEngineering
//Sending Data from Arduino to NodeMCU Via Serial Communication
//Arduino code
//DHT11 Lib
#include <DHT.h>
//Arduino to NodeMCU Lib
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
//Initialise Arduino to NodeMCU (5=Rx & 6=Tx)
SoftwareSerial nodemcu(5, 6);
//Initialisation of DHT11 Sensor
#define DHTPIN 4
DHT dht(DHTPIN, DHT11);
float temp;
float hum;
void setup() {
Serial.begin(9600);
dht.begin();
nodemcu.begin(9600);
delay(1000);
Serial.println("Program started");
}
void loop() {
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& data = jsonBuffer.createObject();
//Obtain Temp and Hum data
dht11_func();
//Assign collected data to JSON Object
data["humidity"] = hum;
data["temperature"] = temp;
//Send data to NodeMCU
data.printTo(nodemcu);
jsonBuffer.clear();
delay(17000);
}
void dht11_func() {
hum = dht.readHumidity();
temp = dht.readTemperature();
Serial.print("Humidity: ");
Serial.println(hum);
Serial.print("Temperature: ");
Serial.println(temp);
}
//Include Lib for Arduino to Nodemcu
//#include <SoftwareSerial.h>
//#include <ArduinoJson.h>
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>
#define DHTPIN D4
#define RAINPIN D0
DHT dht(DHTPIN, DHT11);
float temp;
float hum;
int rain;
WiFiClient client;
////D6 = Rx & D5 = Tx
//SoftwareSerial nodemcu(D6, D5);
const char* ssid = "MST-GUEST";
const char* password = "miner2020";
const char* writeApiKey = "EGY1UXJ00D57GZKO";
unsigned long channelNumber = 1720766;
void setup() {
// Initialize Serial port
Serial.begin(9600);
// nodemcu.begin(9600);
dht.begin();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
ThingSpeak.begin(client);
while (!Serial) continue;
}
void loop() {
// StaticJsonBuffer<1000> jsonBuffer;
// JsonObject& data = jsonBuffer.parseObject(nodemcu);
// if (data == JsonObject::invalid()) {
// //Serial.println("Invalid Json Object");
// jsonBuffer.clear();
// return;
// }
// Serial.println("JSON Object Recieved");
// Serial.print("Recieved Humidity: ");
// float hum = data["humidity"];
// Serial.println(hum);
// Serial.print("Recieved Temperature: ");
// float temp = data["temperature"];
// Serial.println(temp);
// Serial.print("Recieved Rain Value: ");
// int rainVal = data["rain"];
// Serial.println(rainVal);
dht11_func();
rain_func();
// int rain = digitalRead(RAINPIN);
Serial.println("-----------------------------------------");
ThingSpeak.setField(1,temp);
ThingSpeak.setField(2, hum);
ThingSpeak.setField(3, rain);
int x = ThingSpeak.writeFields(channelNumber, writeApiKey);
if(x = 200) {
Serial.println("channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(20000);
}
void dht11_func() {
hum = dht.readHumidity();
temp = dht.readTemperature();
Serial.print("Humidity: ");
Serial.println(hum);
Serial.print("Temperature: ");
Serial.println(temp);
}
void rain_func(){
rain = digitalRead(RAINPIN);
Serial.print("Rain Status: ");
Serial.println(rain);
}
//ThatsEngineering
//Sending Data from Arduino to NodeMCU Via Serial Communication
//NodeMCU code
//Include Lib for Arduino to Nodemcu
#include <SoftwareSerial.h>
#include <ArduinoJson.h>
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
WiFiClient client;
//D6 = Rx & D5 = Tx
SoftwareSerial nodemcu(D6, D5);
const char* ssid = "MST-GUEST";
const char* password = "miner2020";
const char* writeApiKey = "EGY1UXJ00D57GZKO";
unsigned long channelNumber = 1720766;
void setup() {
// Initialize Serial port
Serial.begin(9600);
nodemcu.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
ThingSpeak.begin(client);
while (!Serial) continue;
}
void loop() {
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& data = jsonBuffer.parseObject(nodemcu);
// if (data == JsonObject::invalid()) {
// //Serial.println("Invalid Json Object");
// jsonBuffer.clear();
// return;
// }
Serial.println("JSON Object Recieved");
Serial.print("Recieved Humidity: ");
float hum = data["humidity"];
Serial.println(hum);
Serial.print("Recieved Temperature: ");
float temp = data["temperature"];
Serial.println(temp);
// Serial.print("Recieved Rain Value: ");
// int rainVal = data["rain"];
// Serial.println(rainVal);
Serial.println("-----------------------------------------");
ThingSpeak.setField(1,temp);
ThingSpeak.setField(2, hum);
// ThingSpeak.setField(3, rainVal);
int x = ThingSpeak.writeFields(channelNumber, writeApiKey);
if(x = 200) {
Serial.println("channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(20000);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment