-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
committed
Feb 26, 2018
0 parents
commit f96d025
Showing
17 changed files
with
1,168 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
ESP8266-01 | ||
o o o o | ||
GND RX | ||
TX 3.3 3.3 | ||
o o o o | ||
*/ | ||
|
||
#include <WiFiEspClient.h> | ||
#include <WiFiEsp.h> | ||
#include <WiFiEspUdp.h> | ||
#include <PubSubClient.h> | ||
#include "SoftwareSerial.h" | ||
|
||
#define WIFI_AP "YourWifFiName" // Replace with your WiFi name | ||
#define WIFI_PASSWORD "YourWifFiPassword" // Replace with your WiFi password | ||
|
||
#define server "IP_Address" // Replace with your broker Ip Address | ||
|
||
//ESP8266 TX is connected to Arduino Digital Pin 8 | ||
#define ESP8266_TX_PIN 8 | ||
|
||
//ESP8266 RX is connected to Arduino Digital Pin 7 | ||
#define ESP8266_RX_PIN 7 | ||
|
||
|
||
// Initialize the WiFi client object | ||
WiFiEspClient espClient; | ||
PubSubClient client(espClient); | ||
|
||
// Rx , Tx | ||
SoftwareSerial soft(ESP8266_TX_PIN,ESP8266_RX_PIN); | ||
|
||
int status = WL_IDLE_STATUS; | ||
|
||
// select the input pin for the potentiometer | ||
int sensorAnalogPin = A0; | ||
|
||
// variable to store the value coming from the sensor | ||
int moisture = 0; | ||
|
||
void setup() | ||
{ | ||
pinMode(sensorAnalogPin, INPUT); | ||
Serial.begin(9600); | ||
InitWiFi(); | ||
client.setServer( server, 1883 ); | ||
} | ||
|
||
void loop() | ||
{ | ||
|
||
if(WiFi.status() != WL_CONNECTED){ | ||
connectWiFi(); | ||
} | ||
|
||
if ( !client.connected()){ | ||
reconnect(); | ||
} | ||
|
||
// Call the function to publish to the Server | ||
publishToBroker(); | ||
|
||
// Update and send only after 2 seconds | ||
delay(2000); | ||
|
||
client.loop(); | ||
} | ||
|
||
|
||
void publishToBroker() | ||
{ | ||
// getting moisture value | ||
moisture = readSoilMoisture(sensorAnalogPin); | ||
|
||
// Prepare a JSON payload string | ||
String payload = "{"; | ||
payload += "\"moisture\":"; | ||
payload += moisture; | ||
payload += ","; | ||
payload += "\"area\":"; | ||
payload += "\"north\""; | ||
payload += "}"; | ||
|
||
|
||
char Data[200]; | ||
payload.toCharArray( Data, 200 ); | ||
|
||
// Send payload | ||
client.publish( "north/moisture", Data ); | ||
Serial.println( Data ); | ||
} | ||
|
||
void InitWiFi() | ||
{ | ||
// initialize serial for ESP module | ||
soft.begin(9600); | ||
// initialize ESP module | ||
WiFi.init(&soft); | ||
// check for the presence of the shield | ||
if (WiFi.status() == WL_NO_SHIELD) | ||
{ | ||
Serial.println("WiFi shield not present"); | ||
// don't continue | ||
while (true); | ||
} | ||
connectWiFi(); | ||
} | ||
|
||
void connectWiFi(){ | ||
|
||
while ( status != WL_CONNECTED) { | ||
Serial.print("Attempting to connect to "); | ||
Serial.print(WIFI_AP); | ||
Serial.print("."); | ||
// Connect to WPA/WPA2 network | ||
status = WiFi.begin(WIFI_AP, WIFI_PASSWORD); | ||
delay(500); | ||
} | ||
} | ||
|
||
void reconnect() | ||
{ | ||
// Loop until we're reconnected | ||
while (!client.connected()) | ||
{ | ||
Serial.print("Connecting to Broker..."); | ||
|
||
// Attempt to connect (clientId, username, password) | ||
if ( client.connect("Moisture Publisher ") ) | ||
{ | ||
Serial.println( "Connected" ); | ||
} | ||
else | ||
{ | ||
Serial.print( "Failed to connect with status = " ); | ||
Serial.print( client.state() ); | ||
Serial.println( " : retrying in 5 seconds" ); | ||
// Wait 5 seconds before retrying | ||
delay( 5000 ); | ||
} | ||
} | ||
} | ||
|
||
int readSoilMoisture(int sensorAnalogPin) | ||
{ | ||
|
||
|
||
// read the value from the sensor: | ||
int sensorValue = analogRead(sensorAnalogPin); | ||
|
||
// coverting value in Scale of 0 to 100 | ||
sensorValue = map(sensorValue,1023,0,0,100); | ||
|
||
Serial.print("Mositure : "); | ||
Serial.print(sensorValue); | ||
Serial.println("%"); | ||
return (int)sensorValue; | ||
|
||
} | ||
|
||
|
128 changes: 128 additions & 0 deletions
128
modules/North Module/TemperatureAndHumidity/TemperatureAndHumidity.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* Example of MQTT Publish on ThingsBoard API through ESP | ||
ESP Connection | ||
o o o o | ||
GND DHT_Data_Pin RX | ||
TX 3.3 3.3 | ||
o o o o | ||
DHT Wiring | ||
Pin Connection | ||
Data ESP GPIO 2 | ||
NC Not used | ||
GND GND | ||
VCC 5V | ||
*/ | ||
|
||
#include <ESP8266WiFi.h> | ||
#include <PubSubClient.h> | ||
#include <DHT.h> | ||
|
||
// Using DHT11 Sensor | ||
#define DHTTYPE DHT11 | ||
#define DHTPIN 2 | ||
|
||
WiFiClient espClient; | ||
PubSubClient client(espClient); | ||
|
||
DHT dht(DHTPIN, DHTTYPE); | ||
|
||
// Replace this with your WiFi | ||
// name and password | ||
const char* ssid = "YourWiFiName"; | ||
const char* password = "YourWiFiPassword"; | ||
|
||
// Replace with your Broker IP Address | ||
const char* mqtt_server = "IP_Address"; | ||
|
||
// Defining Global Variables for Temperature and Humidity | ||
float temperature; | ||
float humidity; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
|
||
Serial.print("Connecting to: "); | ||
Serial.println(ssid); | ||
|
||
// Connecting to WiFi Network | ||
WiFi.begin(ssid, password); | ||
|
||
while (WiFi.status() != WL_CONNECTED){ | ||
Serial.print("."); | ||
delay(500); | ||
} | ||
|
||
Serial.println("\nWifi Connected Successfully"); | ||
|
||
// Setting Mqtt Server | ||
client.setServer(mqtt_server, 1883); | ||
|
||
} | ||
|
||
void loop() { | ||
// if not connected to Broker | ||
if(!client.connected()){ | ||
connectToBroker(); | ||
} | ||
|
||
// Publising Data to Broker | ||
// with delay of 2 seconds | ||
publishData(); | ||
delay(5000); | ||
|
||
// Looping all the client Procedures | ||
client.loop(); | ||
|
||
} | ||
|
||
void connectToBroker(){ | ||
Serial.println("Connecting to Broker......"); | ||
|
||
// Connecting to MQTT broker | ||
if (client.connect("North DHT")) { | ||
Serial.println( "Done" ); | ||
} | ||
else | ||
{ | ||
Serial.print( "Failed with status: " ); | ||
Serial.print( client.state()); | ||
Serial.println( " : retrying in 5 seconds" ); | ||
// Wait 5 seconds before retrying | ||
delay( 5000 ); | ||
} | ||
} | ||
|
||
void publishData() | ||
{ | ||
|
||
humidity = dht.readHumidity(); // Read humidity (percent) | ||
temperature = dht.readTemperature(); // Read temperature (celsius) | ||
|
||
|
||
// Prepare a JSON payload string | ||
String payload = "{"; | ||
payload += "\"temperature\":"; | ||
payload += (int)temperature; | ||
payload += ","; | ||
payload += "\"humidity\":"; | ||
payload += (int)humidity; | ||
payload += ","; | ||
payload += "\"area\":"; | ||
payload += "\"north\""; | ||
payload += "}"; | ||
|
||
// Send payload | ||
char attributes[200]; | ||
payload.toCharArray( attributes, 200 ); | ||
client.publish( "north/temperatureAndHumidity", attributes ); | ||
Serial.println( attributes ); | ||
} | ||
|
Oops, something went wrong.