it is about smart street lights using solar tracker and connect with IOT
but i could not figur how can i used arduino mega wifi++esp8266
#include<SoftwareSerial.h>//include softwareserial library
#include <Servo.h> // include Servo library
#include <WiFi.h> // include wifi library
#include<ThingSpeak.h> // include thingSpeak library to connect
#include<WiFiClient.h> // include wificlient library
Servo horizontal; // horizontal servo
int servoh = 90; // stand horizontal servo
Servo vertical; // vertical servo
int servov = 90; // stand vertical servo
//..........Enter WiFi Details .......
const char* ssid = "**********";//user name SSID
const char* password = "*****************"; //password
//....................................
WiFiClient client;
//.........Thingspeak Detail .........
unsigned long myChannelNumber = 2333171;// write channel number
const int ChannelField1 = 1; // for slot 1
const int ChannelField2 = 2; // for slot 2
const int ChannelField3 = 3; // for slot 3
const int ChannelField4 = 4; // for slot 4
const char* myWriteAPIKey = "5B1379YQUZX4QCQ3";//write API key
//....................................
int led1 = 6; //choose pin for LED
int led2 = 5;
int led3 = 4;
int led4 = 7;
int irSensor1 = 2;//choose pin for infrared sensor IR
int irSensor2 = 3;
int irSensor3 = 8;
int irSensor4 = 11;
int sensorPin = A5; // LDR pin for light
int ldrlt = A0; //LDR top left
int ldrrt = A1; //LDR top rigt
int ldrld = A2; //LDR down left
int ldrrd = A3; //LDR down rigt
int sensorValue = 0; // variable to save value of LDR Pin A5
int s1 ,s2 ,s3 ,s4 ;//variable for reading the pin status
void setup()
{
Serial.begin(9600);
pinMode(led1, OUTPUT);// declared LED as output
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(irSensor1, INPUT );// declared IR as input
pinMode(irSensor2, INPUT );
pinMode(irSensor3, INPUT );
pinMode(irSensor4, INPUT );
int upload();
pinMode(sensorPin, INPUT ); //declared as input for arduino
pinMode(ldrlt,INPUT);//declared as input for arduino
pinMode(ldrrt,INPUT);
pinMode(ldrld,INPUT);
pinMode(ldrrd,INPUT);
horizontal.attach(9); // connect SERVO 1 with pin 9
vertical.attach(10);// connect SERVO 2 with pin 10
}
void loop()
{
//.........solar tracker..................
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down rigt
int dtime = analogRead(4)/20; // read potentiometers
int tol = analogRead(5)/4; // read tolerance (زاوية الانحراف المعياري )
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dvert = avt - avd; // check the diffirence of up and down
int dhoriz = avl - avr;// check the diffirence og left and rigt
if (-1*tol > dvert || dvert > tol) // check if the diffirence is in the tolerance else change vertical angle
{
if (avt > avd) // check the diffirance between average value top and average value down
{
servov = ++servov;//
if (servov > 180)
{
servov = 180;
}
}
else if (avt < avd)
{
servov= --servov;
if (servov < 0)
{
servov = 0;
}
}
vertical.write(servov);
}
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance (زاوية ميلان لتقرب زاوية سقوط الاشعاع الشمسي من العمودية ) else change horizontal angle
{
if (avl > avr) // check the diffirence between avarage lift and avarage right
{
servoh = --servoh;
if (servoh < 0)
{
servoh = 0;
}
}
else if (avl < avr)
{
servoh = ++servoh;
if (servoh > 180)
{
servoh = 180;
}
}
else if (avl == avr)
{
// nothing
}
horizontal.write(servoh);
}
delay(dtime);
//...............................
//.........WiFi setup ...........
WiFi.begin(ssid, password);
ThingSpeak.begin(client);
Serial.println ();
Serial.print("Connecting to");
Serial.println(ssid);
WiFi.begin(ssid, password);
if (WiFi.status() != WL_CONNECTED){
Serial.print("Attempling to Connect to SSID :");
Serial.println(ssid);
}
while (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid,password);
Serial.print(".");
delay(500);
}
Serial.println("\nConnected");
sensorValue = analogRead(sensorPin);//read analog sensorPin value (LDR Pin A5) and store in sensorValue variable
Serial.print(sensorValue);//print on serial monitor
delay(100);
if ( sensorValue<300){ // if it was less than 300 ( it mean that it still be sunny outside )
analogWrite(led1, 0);
analogWrite(led2, 0);
analogWrite(led3, 0);
analogWrite(led4, 0);
}
else {
Serial.println("it's dark now");
//IR sensor 1 code
s1 = digitalRead(irSensor1);
if(s1==0)//read IR sensor 1 value
{
digitalWrite(led1,HIGH);
delay(100);//microsecond
}
else{
digitalWrite(led1,HIGH);
analogWrite(led1,255/6);
delay(50);}
// read sensor 2 value
s2 = digitalRead(irSensor2);
if(s2==0)
{
digitalWrite(led2,HIGH);
delay(100);}
else{
digitalWrite(led2,HIGH);
analogWrite(led2,255/6);
delay(50);}
//IR sensor3 code
s3 = digitalRead(irSensor3);
if(s3==0)//read ir sensor3 value
{
digitalWrite(led3,HIGH);
delay(100);}
else{
digitalWrite(led4,HIGH);
analogWrite(led4,255/6);
delay(50);}
//read ir sensor4 value
s4 = digitalRead(irSensor4);
if(s4==0)
{
digitalWrite(led4,HIGH);
delay(100);}
else{
digitalWrite(led4,HIGH);
analogWrite(led4,255/6);
delay(50);}
delay(500);
//data send to thingspeak
/*ThingSpeak.writeField(myChannelField,ChannelField1,s1,myWriteAPIKey);
ThingSpeak.writeField(myChannelField,ChannelField2,s2,myWriteAPIKey);
ThingSpeak.writeField(myChannelField,ChannelField3,s3,myWriteAPIKey);
ThingSpeak.writeField(myChannelField,ChannelField4,s4,myWriteAPIKey);*/
}
ThingSpeak.writeField(myChannelNumber,ChannelField1,s1,myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber,ChannelField2,s2,myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber,ChannelField3,s3,myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber,ChannelField4,s4,myWriteAPIKey);
}
This part of the code is code that would run
inside
the ESP8266 when the ESP8266 is used
stand anlone
Things will become much easier if you reduce your system-complexity from using a
Arduino Mega plus ESP8266 to a
single ESP32
that does it all.
Or as second best to use an ESP8266 nodeMCU
stand-alone
with external ADC-board like ADS1115 and an IO-expander-board like MCP23017
If you would like to continue with the Arduino Mega and ESP8266 you have to use either
this very ugly hard to use AT-Software inside the ESP8266
or
to write code for the Arduino-Mega
and
write code for the ESP8266
where these both codes must organise serial data-exchange.
This is the reason why I recommend to use a single ESP32 nodeMCU with ADS115 and MCP23017
one microcontroller one code
instead of two micro-controllers
best regards Stefan
I have a single ESP8266 and i tried to used with arduino mega but i did not know which
one should i upload the code for it first
so i decide to use arduino mega wifi +esp8266 but it did not work
This post does
not
help at all
all
details what you really did
- sketch
- wiring
are missing.
Help us to help you by providing much more detailed information
welcome to the arduino-forum.
I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.
Directly after registering you got presented informations how to speed up solving your problem.
You should really read it.
best regards Stefan
rather vauge error report!
have a look at post flashing-arduino-mega2560-onboard-esp8266
also Mega-WiFi_R3_ATmega2560_ESP8266
however, having to program two microconntrollers with the overhead of interprocessor communication increases the task complexity by an order of magnitude
as @StefanL38 suggested it would be simpler to move to a single microcontroller such as the ESP32
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.