Commit c4622f57 authored by Federico Meloda's avatar Federico Meloda

added main.cpp and other stuff

parent 4bd6dcef
.pio
.pioenvs
.piolibdeps
.clang_complete
.gcc-flags.json
.DS_Store
.AppleDouble
.LSOverride
._*
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
\ No newline at end of file
# Continuous Integration (CI) is the practice, in software
# engineering, of merging all developer working copies with a shared mainline
# several times a day < https://docs.platformio.org/page/ci/index.html >
#
# Documentation:
#
# * Travis CI Embedded Builds with PlatformIO
# < https://docs.travis-ci.com/user/integration/platformio/ >
#
# * PlatformIO integration with Travis CI
# < https://docs.platformio.org/page/ci/travis.html >
#
# * User Guide for `platformio ci` command
# < https://docs.platformio.org/page/userguide/cmd_ci.html >
#
#
# Please choose one of the following templates (proposed below) and uncomment
# it (remove "# " before each line) or use own configuration according to the
# Travis CI documentation (see above).
#
#
# Template #1: General project. Test it using existing `platformio.ini`.
#
# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# install:
# - pip install -U platformio
# - platformio update
#
# script:
# - platformio run
#
# Template #2: The project is intended to be used as a library with examples.
#
# language: python
# python:
# - "2.7"
#
# sudo: false
# cache:
# directories:
# - "~/.platformio"
#
# env:
# - PLATFORMIO_CI_SRC=path/to/test/file.c
# - PLATFORMIO_CI_SRC=examples/file.ino
# - PLATFORMIO_CI_SRC=path/to/test/directory
#
# install:
# - pip install -U platformio
# - platformio update
#
# script:
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp01_1m]
platform = espressif8266
board = esp01_1m
framework = arduino
/*
SunRise main code
The program connects to mqtt server, and subscrive to sunRise
If it receives a message "1" a servo will start to move a sun up to a maximum
If 0 it will reduce the angle up to zero.
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Arduino.h>
#include <Servo.h>
//NETWORK STUFF
const char* ssid = "Mittelab_Members_Legacy"; // The SSID (name) of the Wi-Fi network you want to connect to. cause everybody knows what ssid is and people do not read long comments "dickbutt"
const char* password = "******"; // The password of the Wi-Fi network
const char* mqtt_server = " mqtt.hq.mittelab.org";
const char* clientID = "ESPsun";
WiFiClient ESPclient;
PubSubClient client(ESPclient);
// SUN ACTIVATION STUFF
bool sunFlag = false;
unsigned long previousMillis=0;
//SERVO STUFF
int servoPin = 2; // Declare the Servo pin
Servo Servo1; // Create a servo object
unsigned int maxAng = 90; // Servo max angle
unsigned int servoAng = 0; // Current angle in degree for servo
unsigned int servoWaitUp = 1000; //the time needed to move 1 degree up in milliseconds
unsigned int servoWaitDown = 100; //the time needed to move 1 degree down in milliseconds
void setup_wifi() {
WiFi.disconnect();
delay(10);
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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
client.subscribe("sunRise");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
char receivedChar = (char)payload[i];
Serial.print(receivedChar);
if (receivedChar == '1')
sunFlag=true;
if (receivedChar == '0')
sunFlag=false;
}
Serial.println();
}
void sunRise() {
if (servoAng<maxAng){
servoAng++;
Servo1.write(servoAng);
}
}
void sunFall() {
if (servoAng>0){
servoAng--;
Servo1.write(servoAng);
}
}
void setup() {
Serial.begin(9600);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Servo1.attach(servoPin);
}
void loop() {
unsigned long currentMillis = millis();
if (!client.connected()) {
reconnect();
}
client.loop();
if (sunFlag==true) {
if ((unsigned long)(currentMillis - previousMillis) >= servoWaitUp) {
sunRise();
previousMillis = currentMillis;
}
} else {
if ((unsigned long)(currentMillis - previousMillis) >= servoWaitDown) {
sunFall();
previousMillis = currentMillis;
}
}
}
Markdown is supported
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