Hi for All i found some codes source on Net about iot and control over web using arduino so i modified it : the 1st source code its for send sensors data(DHT11,ULTRASONIC ) to ThingSpeak (open IoT platform) the 2nd its for controling over web page( relay , Led , servo ) the 2 sources codes work 100 % i want to make it in just one code so how is that I tried but its not worknig i neeeeeeed HELP
1st source code for sending sensors data to Thingspeak.com
#include <ThingSpeak.h>
#include <DHT.h> // You have to download DHT11 library
#include <SPI.h>
#include <Ethernet.h>
#ifdef SPARK
#include "ThingSpeak/ThingSpeak.h" // You have to download ThingSpeak library
#else
#include "ThingSpeak.h"
#endif
#define WRITE_DELAY_FOR_THINGSPEAK 15000
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define echoPin 9 // Echo Pin
#define trigPin 8 // Trigger Pin
long duration, distance; // Duration used to calculate distance
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE,0xED};
IPAddress ip(192,168,8,20); //Check your router IP
EthernetClient client;
unsigned long mySensorsChannelNumber = xxxxx; //Replase this code with your thingSpeak channel ID
unsigned long myCommandsChannelNumber = xxxx; //Replase this code with your thingSpeak channel ID
const char * myWriteAPIKey_sensors = "xxxxxxxxxx"; //Replace with your channel Write Key
const char * myWriteAPIKey_commands = "xxxxxxxxxx"; //Replace with your channel Write Key. This key is not used in this example
//Use this key from android device
long lastWriteTime=0;
long lastReadTime=0;
float responseValue=0;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
ThingSpeak.begin(client);
dht.begin();
pinMode(7,OUTPUT);
pinMode(8,OUTPUT); //Trigger FW
pinMode(9,INPUT); //Echo
Serial.println("Setup completed");
delay(1000);
}
void loop() {
//-------- Example 1 write DHT11 sensors' values to thingSpeak channel sensors
long duration1, distance1;
if (millis()-lastWriteTime>20000) { // every 20 secs. Must be >15000.
Serial.println("Read sensors' values...");
// FW 1 pin distance calculation
digitalWrite(8,LOW);
delayMicroseconds(2);
digitalWrite(8,HIGH);
delayMicroseconds(10);
digitalWrite(8,LOW);
duration1 = pulseIn(9,HIGH);
distance1 = (duration1/2)/29.1;
delay(500);
float u = distance1 ;
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
}
else {
Serial.println("Temp="+String(t)+" *C");
Serial.println("Humidity="+String(h)+" %");
Serial.println("distance="+String(u)+" cm");
ThingSpeak.setField(1,t);
ThingSpeak.setField(2,h);
ThingSpeak.setField(3,u); // This is the value of commands channel
ThingSpeak.writeFields(mySensorsChannelNumber, myWriteAPIKey_sensors); // write two values
lastWriteTime=millis(); // store last write time
}
}
delay(8000);
}
the 2nd code its for controling ( relay , Led , servo motor) over page web
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
int led = 4;
Servo microservo;
int pos = 0;
const byte mainlightPin = 5;
const byte chandelierPin = 6;
const byte fanPin = 8;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 8, 20 };
byte gateway[] = { 192, 168, 8, 20 };
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);
String readString;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
pinMode(led, OUTPUT);
microservo.attach(7);
pinMode(mainlightPin, OUTPUT);
pinMode(chandelierPin, OUTPUT);
pinMode(fanPin, OUTPUT);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100) {
readString += c;
}
if (c == '\n') {
Serial.println(readString);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Control over web page</H1>");
client.println("<hr />");
client.println("<a href=\"/?button1off\"\">OFF Light</a>");
client.println("<a href=\"/?button1on\"\">ON light</a>
");
client.println("
");
client.println("
");
client.println("<a href=\"/?button5on\"\">OFF Motor 2</a>");
client.println("<a href=\"/?button5off\"\">ON Motor 2</a>
");
client.println("
");
client.println("
");
client.println("<a href=\"/??button6on\"\">OFF Motor 3</a>");
client.println("<a href=\"/??button6off\"\">ON Motor 3</a>
");
client.println("
");
client.println("
");
client.println("<a href=\"/?button7on\"\">OFF Motor 4</a>");
client.println("<a href=\"/?button7off\"\">ON Motor 4</a>
");
client.println("
");
client.println("
");
client.println("<a href=\"/?button2on\"\">Rotate Left</a>");
client.println("<a href=\"/?button2off\"\">Rotate Right</a>
");
client.println("
");
client.println("</BODY>");
client.println("
");
client.println("</HTML>");
delay(1);
client.stop();
if (readString.indexOf("?button1on") >0){digitalWrite(led, HIGH);}
if (readString.indexOf("?button1off")>0){digitalWrite(led, LOW);}
if (readString.indexOf("?button5on") >0) {digitalWrite(mainlightPin, HIGH);}
if (readString.indexOf("?button5off")>0 ){ digitalWrite(mainlightPin, LOW);}
if (readString.indexOf("?button6on")>0 ) {digitalWrite(chandelierPin, HIGH);}
if (readString.indexOf("?button6off")>0 ) {digitalWrite(chandelierPin, LOW);}
if (readString.indexOf("?button7on")>0 ) {digitalWrite(fanPin, HIGH);}
if (readString.indexOf("?button7off")>0) {digitalWrite(fanPin, LOW);}
if (readString.indexOf("?button2on") >0)
{
for(pos = 0; pos < 180; pos += 3)
{
microservo.write(pos);
delay(15);
}
}
if (readString.indexOf("?button2off") >0){
for(pos = 180; pos>=1; pos-=3)
{
microservo.write(pos);
delay(15);
}
}
readString="";
}
}
}
}
}