Last active
November 22, 2018 19:45
-
-
Save Ajimi/bb747c693fa75f3c07d0ebab7f5d2747 to your computer and use it in GitHub Desktop.
Contains all what you need for getting sensor data & interacting with Analog and Digital PINS & making network request
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
rgb_lcd lcd; | |
const int PIN_SOUND = A0; | |
const int PIN_TEMP = A1; | |
const int PIN_LIGHT = A2; | |
const int PIN_BUTTON = 6; | |
const int PIN_LED = 13; | |
const int PIN_BUZZER = 1; | |
bool isButtonActive = true; | |
// Temperature Related | |
const int B = 3975; | |
#pragma region UTILS | |
#pragma region BUZZER ACTIVATION | |
int length = 15; // the number of notes | |
char notes[] = "aaafbfbf "; // a space represents a rest | |
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; | |
int tempo = 200; | |
void playTone(int tone, int duration) { | |
for (long i = 0; i < duration * 1000L; i += tone * 2) { | |
digitalWrite(PIN_BUZZER, HIGH); | |
delayMicroseconds(tone); | |
digitalWrite(PIN_BUZZER, LOW); | |
delayMicroseconds(tone); | |
} | |
} | |
void playNote(char note, int duration) { | |
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; | |
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; | |
// play the tone corresponding to the note name | |
for (int i = 0; i < 8; i++) { | |
if (names[i] == note) { | |
playTone(tones[i], duration); | |
} | |
} | |
} | |
void activateBuzzer(){ | |
Serial.println("Buzzer toggle"); | |
for (int i = 0; i < 10; i++){ | |
playNote('g', beats[i] * tempo); | |
} | |
} | |
/*--END BUZZER ACTIVATION*/ | |
#pragma endregion | |
#pragma region LED ACTIVATION | |
void ledBlinking() { | |
int i; | |
for(i = 0 ; i < 5 ; i++) { | |
turnOnLed(); | |
delay(50); | |
turnOffLed(); | |
} | |
} | |
void turnOnLed(){ | |
digitalWrite(PIN_LED, HIGH); | |
} | |
void turnOffLed(){ | |
digitalWrite(PIN_LED, LOW); | |
} | |
#pragma endregion | |
int toInt(String str){ | |
int len = str.length(); | |
int i; | |
int dec = 0; | |
for(i=0;i <len ;i++) { | |
dec = dec * 10 + (str[i] - '0'); | |
} | |
return dec; | |
} | |
void setLcdMessage(String msg){ | |
lcd.clear(); | |
lcd.print(msg); | |
lcd.display(); | |
} | |
void toggleButton(){ | |
if(digitalRead(PIN_BUTTON)){ | |
isButtonActive = !isButtonActive; | |
} | |
} | |
float getTemperature(){ | |
int val = analogRead(PIN_TEMP); | |
// Determine the current resistance of the thermistor based on the sensor value. | |
float resistance = (float)(1023-val)*10000/val; | |
// Calculate the temperature based on the resistance value. | |
return 1/(log(resistance/10000)/B+1/298.15)-273.15; | |
} | |
int getSound(){ | |
return analogRead(PIN_SOUND); | |
} | |
int getLight(){ | |
return analogRead(PIN_LIGHT); | |
} | |
void initLcd(){ | |
lcd.begin(16,2); | |
} | |
void initEthernet(){ | |
system("ifup eth0"); | |
if (Ethernet.begin(mac) == 0) { | |
Serial.print("Failed to configure Ethernet using DHCP"); | |
// no point in carrying on, so do nothing forevermore: | |
for(;;) | |
; | |
} else { | |
Serial.println("connection succsessufl"); | |
} | |
} | |
void initPins(){ | |
pinMode(PIN_BUTTON, INPUT); | |
pinMode(PIN_LED, OUTPUT); | |
pinMode(PIN_BUZZER, OUTPUT); | |
} | |
/**-- END UTILS DATA */ | |
#pragma endregion |
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
void setup(){ | |
Serial.begin(9600); | |
delay(2000); | |
initLcd(); | |
initEthernet(); | |
initPins(); | |
} | |
void loop() { | |
executeWithButtonCheck(); | |
// execute(); | |
} | |
void executeWithPowerCheckFromServer(){ | |
updateTogglableValues(); | |
if(isButtonActive == true){ | |
execute(); | |
} else { | |
turnOff(); | |
} | |
} | |
void execute(){ | |
update(); | |
} | |
void update(){ | |
// Get Data From Server | |
// && update Gloabl Variables | |
} | |
void turnOff(){ | |
setLcdMessage("OFF"); | |
turnOffLed(); | |
} |
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
/** | |
* Example Of usage | |
* String url = "/client?type=temp&value=15"; | |
* int result = getIntFromServer(url); // Pass an url to the function and it will return an integer | |
* sendRequest(url); // | |
*/ | |
// Change PORT Number | |
const int PORT = 3000; | |
// Change IP ADDRESS | |
IPAddress server(172, 16, 197, 90); | |
// If you want to send data | |
void sendRequest(String url){ | |
getMessageFromServer(url, false); | |
} | |
// Return Int From Server | |
int getIntFromServer(String url){ | |
int number = 0; | |
String message = getMessageFromServer(url, true); | |
// ---> THIS LINE Uncoment if an error occurs | |
// number = toInt(message); | |
number = message.toInt(); | |
return number; | |
} | |
// Return String from Server | |
String getStringFromServer(String url) { | |
return getMessageFromServer(url, false); | |
} | |
// Helper functions | |
String getMessageFromServer(String url, boolean isNumber){ | |
EthernetClient client; | |
String result = ""; | |
if(client.connect(server,PORT)) { | |
client.println(url); | |
client.println(); | |
if(client.available()){ | |
delay(500); | |
if(isNumber){ | |
result = getIntMessage(client); | |
} else { | |
result = getStringMessage(client); | |
} | |
} | |
} else { | |
Serial.println("Client connection Failed"); | |
} | |
client.stop(); | |
return result; | |
} | |
String getIntMessage(EthernetClient client){ | |
String result = ""; | |
while(client.available()) { | |
// If an errors occurs uncoment this win and comment the line below | |
// also make sure to uncoment toInt() function in getIntFromServer | |
// result += client.read() - '0'; | |
result += (char)client.read(); | |
} | |
return result; | |
} | |
String getStringMessage(EthernetClient client){ | |
String result = ""; | |
while(client.available()) { | |
result += client.read(); | |
} | |
return result; | |
} | |
int toInt(String str){ | |
int len = str.length(); | |
int i; | |
int dec = 0; | |
for(i=0;i <len ;i++) { | |
dec = dec * 10 + (str[i] - '0'); | |
} | |
return dec; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you !! =D