-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
22 changed files
with
4,078 additions
and
0 deletions.
There are no files selected for viewing
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,115 @@ | ||
#include <ESP8266WiFi.h> | ||
#include <PubSubClient.h> | ||
|
||
#define AIR_PIN 14 | ||
|
||
const char* ssid = " "; | ||
const char* password = " "; | ||
const char* mqtt_server = "13.209.250.27"; | ||
const char* topic = "speak_topic"; | ||
|
||
WiFiClient espClient; | ||
PubSubClient client(espClient); | ||
|
||
long lastMsg = 0; | ||
char msg[50]; | ||
int value = 0; | ||
|
||
void setup_wifi() { | ||
delay(10); | ||
Serial.println(); | ||
Serial.print("Connecting to "); | ||
Serial.println(ssid); | ||
|
||
WiFi.begin(ssid, password); | ||
|
||
while (WiFi.status() != WL_CONNECTED) { | ||
delay(500); | ||
Serial.print("."); | ||
} | ||
|
||
randomSeed(micros()); | ||
|
||
Serial.println(""); | ||
Serial.println("WiFi connected"); | ||
Serial.println("IP address: "); | ||
Serial.println(WiFi.localIP()); | ||
} | ||
|
||
void callback(char* topic, byte* payload, unsigned int length) { | ||
String final_string; | ||
|
||
Serial.print("Message arrived ["); | ||
Serial.print(topic); | ||
Serial.print("] "); | ||
for (int i = 0; i < length; i++) { | ||
Serial.print((char)payload[i]); | ||
final_string += (char)payload[i]; | ||
} | ||
|
||
Serial.println(); | ||
Serial.println("-----------------------------------------"); | ||
Serial.println(final_string); | ||
// Switch on the LED if an 1 was received as first character | ||
// fix it - magiceco// | ||
if (final_string == "1") { | ||
//digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level | ||
digitalWrite(AIR_PIN, LOW); | ||
// but actually the LED is on; this is because | ||
// it is acive low on the ESP-01) | ||
} | ||
else if(final_string == "2") { | ||
//digitalWrite(BUILTIN_LED, HIGH);// Turn the LED off by making the voltage HIGH | ||
digitalWrite(AIR_PIN, HIGH); | ||
} | ||
|
||
} | ||
|
||
void reconnect() { | ||
// Loop until we're reconnected | ||
while (!client.connected()) { | ||
Serial.print("Attempting MQTT connection..."); | ||
// Create a random client ID | ||
String clientId = "ESP8266Client-"; | ||
clientId += String(random(0xffff), HEX); | ||
// Attempt to connect | ||
if (client.connect(clientId.c_str())) { | ||
Serial.println("connected"); | ||
// Once connected, publish an announcement... | ||
client.publish("outTopic", "hello world"); | ||
// ... and resubscribe | ||
client.subscribe(topic); | ||
} else { | ||
Serial.print("failed, rc="); | ||
Serial.print(client.state()); | ||
Serial.println(" try again in 5 seconds"); | ||
// Wait 5 seconds before retrying | ||
delay(5000); | ||
} | ||
} | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
pinMode(AIR_PIN, OUTPUT); | ||
digitalWrite(AIR_PIN, LOW); | ||
setup_wifi(); | ||
client.setServer(mqtt_server, 1883); | ||
client.setCallback(callback); | ||
} | ||
|
||
void loop() { | ||
if (!client.connected()) { | ||
reconnect(); | ||
} | ||
client.loop(); | ||
long now = millis(); | ||
if (now - lastMsg > 2000) { | ||
lastMsg = now; | ||
++value; | ||
snprintf (msg, 75, "hello world #%ld", value); | ||
Serial.print("Publish message: "); | ||
Serial.println(msg); | ||
client.publish("outTopic", msg); | ||
} | ||
} |
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,115 @@ | ||
// | ||
// FILE: FinedustMonitorWithGPS.ino | ||
// AUTHOR: Jaewoong Mun ([email protected]) | ||
// CREATED: November 19, 2019 | ||
// | ||
// Released to the public domain | ||
// | ||
|
||
#include <TinyGPS++.h> | ||
#include <SoftwareSerial.h> | ||
#include "RunningMedian.h" | ||
|
||
RunningMedian pm25s = RunningMedian(19); | ||
RunningMedian pm10s = RunningMedian(19); | ||
|
||
char* ssid = "HappyBono-GalaxyNote10+"; | ||
char* password = "06020688"; | ||
String api_key = "*2446388DA8B643EE7AA752DB64A42926444CDE2A"; | ||
#define PLAIVE_SERVER_ENABLE | ||
//#define THINGSPEAK_SERVER_ENABLE | ||
|
||
boolean wifi_ready; | ||
float map_x, map_y; | ||
String s_map_x, s_map_y; | ||
|
||
TinyGPSPlus gps; | ||
SoftwareSerial ss(12, 13); | ||
SoftwareSerial dust(D1, D0, false, 256); | ||
|
||
void got_dust(int pm25, int pm10) { //formula for dust sensor just use!! | ||
pm25 /= 10; | ||
pm10 /= 10; | ||
pm25s.add(pm25); | ||
pm10s.add(pm10); | ||
do_oled(pm25, pm10); //print pm25, pm10 in oled | ||
} | ||
|
||
|
||
//서버에 보내기(send server) | ||
void do_interval() { | ||
if (wifi_ready){ | ||
#ifdef PLAIVE_SERVER_ENABLE | ||
do_server_plaive(api_key,int(pm25s.getMedian()), int(pm10s.getMedian()), get_temperature(), s_map_x, s_map_y); | ||
#else | ||
#ifdef THINGSPEAK_SERVER_ENABLE | ||
do_server_thingspeak(api_key,int(pm25s.getMedian()), int(pm10s.getMedian()),get_temperature()); | ||
#else | ||
do_server_default(api_key,int(pm25s.getMedian()), int(pm10s.getMedian()),get_temperature()); | ||
#endif | ||
#endif | ||
} | ||
//wifi is ok | ||
} | ||
|
||
unsigned long mark = 0; | ||
boolean got_interval = false; | ||
|
||
//초기 세팅 | ||
void setup() { | ||
Serial.begin(115200); | ||
dust.begin(9600); | ||
ss.begin(9600); | ||
setup_oled();//oled setting | ||
wifi_ready = connect_ap(ssid, password); //wifi connection 유무 | ||
|
||
if (!wifi_ready) nowifi_oled();//wifi no connection | ||
delay(5000); | ||
Serial.println("\nDust Sensor Box V1.2, 2019/11/24 HappyBono"); | ||
} | ||
|
||
//아두이노가 반복적으로 작동하는 부분 | ||
void loop() { | ||
if(ss.available()<=0){ | ||
Serial.println("SIGNAL STATUS : WEAK"); | ||
s_map_x = String(map_x,6); | ||
s_map_y = String(map_y,6); | ||
} | ||
else{ | ||
while(ss.available()>0){ | ||
Serial.println("SIGNAL STATUS : GREAT"); | ||
if(gps.encode(ss.read())){ | ||
Serial.println("GPS READ"); | ||
Serial.println(ss.read()); | ||
if (gps.location.isValid()){ | ||
Serial.println("LOCATION : GREAT"); | ||
map_x = gps.location.lat(); | ||
map_y = gps.location.lng(); | ||
Serial.println(String(map_x,6)); | ||
Serial.println(String(map_y,6)); | ||
} | ||
} | ||
s_map_x = String(map_x,6); | ||
s_map_y = String(map_y,6); | ||
yield(); | ||
} | ||
} | ||
while (dust.available() > 0) { | ||
do_dust(dust.read(), got_dust); | ||
yield(); //loop 에서 while 문을 사용하는 경우 yield 를 포함해주어야 합니다. | ||
} | ||
//Serial.println(map_x); | ||
//Serial.print("pm 25 : "); | ||
//Serial.println(int(pm25s.getMedian())); | ||
|
||
if (millis() > mark) {//one minute(60000) interval | ||
mark = millis() + 60000; | ||
got_interval = true; | ||
} | ||
|
||
if (got_interval) { | ||
got_interval = false; | ||
do_interval(); | ||
} | ||
yield(); | ||
} |
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,123 @@ | ||
|
||
// FILE: RunningMedian.cpp | ||
// AUTHOR: Rob dot Tillaart at gmail dot com | ||
// VERSION: 0.1.04 | ||
// PURPOSE: RunningMedian library for Arduino | ||
|
||
#include "RunningMedian.h" | ||
|
||
RunningMedian::RunningMedian(uint8_t size) | ||
{ | ||
_size = constrain(size, MEDIAN_MIN_SIZE, MEDIAN_MAX_SIZE); | ||
// array's could be allocated by malloc here, | ||
// but using fixed size is easier. | ||
clear(); | ||
} | ||
|
||
RunningMedian::RunningMedian() | ||
{ | ||
_size = MEDIAN_DEF_SIZE; | ||
clear(); | ||
} | ||
// resets all counters | ||
void RunningMedian::clear() | ||
{ | ||
_cnt = 0; | ||
_idx = 0; | ||
_sorted = false; | ||
} | ||
|
||
// adds a new value to the data-set | ||
// or overwrites the oldest if full. | ||
void RunningMedian::add(float value) | ||
{ | ||
_ar[_idx++] = value; | ||
if (_idx >= _size) _idx = 0; // wrap around | ||
if (_cnt < _size) _cnt++; | ||
_sorted = false; | ||
} | ||
|
||
float RunningMedian::getMedian() | ||
{ | ||
if (_cnt > 0) | ||
{ | ||
if (_sorted == false) sort(); | ||
return _as[_cnt/2]; | ||
} | ||
return NAN; | ||
} | ||
|
||
#ifdef RUNNING_MEDIAN_ALL | ||
float RunningMedian::getHighest() | ||
{ | ||
if (_cnt > 0) | ||
{ | ||
if (_sorted == false) sort(); | ||
return _as[_cnt-1]; | ||
} | ||
return NAN; | ||
} | ||
|
||
float RunningMedian::getLowest() | ||
{ | ||
if (_cnt > 0) | ||
{ | ||
if (_sorted == false) sort(); | ||
return _as[0]; | ||
} | ||
return NAN; | ||
} | ||
|
||
float RunningMedian::getAverage() | ||
{ | ||
if (_cnt > 0) | ||
{ | ||
float sum = 0; | ||
for (uint8_t i=0; i< _cnt; i++) sum += _ar[i]; | ||
return sum / _cnt; | ||
} | ||
return NAN; | ||
} | ||
|
||
float RunningMedian::getAverage(uint8_t nMedians) | ||
{ | ||
if ((_cnt > 0) && (nMedians > 0)) | ||
{ | ||
if (_cnt < nMedians) nMedians = _cnt; // when filling the array for first time | ||
uint8_t start = ((_cnt - nMedians)/2); | ||
uint8_t stop = start + nMedians; | ||
sort(); | ||
float sum = 0; | ||
for (uint8_t i = start; i < stop; i++) sum += _as[i]; | ||
return sum / nMedians; | ||
} | ||
return NAN; | ||
} | ||
|
||
uint8_t RunningMedian::getSize() { return _size; }; | ||
|
||
uint8_t RunningMedian::getCount() { return _cnt; }; | ||
#endif | ||
|
||
void RunningMedian::sort() | ||
{ | ||
// copy | ||
for (uint8_t i=0; i< _cnt; i++) _as[i] = _ar[i]; | ||
|
||
// sort all | ||
for (uint8_t i=0; i< _cnt-1; i++) | ||
{ | ||
uint8_t m = i; | ||
for (uint8_t j=i+1; j< _cnt; j++) | ||
{ | ||
if (_as[j] < _as[m]) m = j; | ||
} | ||
if (m != i) | ||
{ | ||
float t = _as[m]; // PATCH from 0.1.05 (was long) | ||
_as[m] = _as[i]; | ||
_as[i] = t; | ||
} | ||
} | ||
_sorted = true; | ||
} |
Oops, something went wrong.