-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfgc-2021.ino
166 lines (148 loc) · 4.1 KB
/
fgc-2021.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <SPI.h>
#include <WiFi101.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Arduino_MKRENV.h>
#include <ArduCAM.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include "memorysaver.h"
#include <Seeed_HM330X.h>
WiFiSSLClient client;
char ssid[] = "SSID"; // your network SSID (name)
char pass[] = "PASSWORD"; // your network password
char server[] = "api.z3db0y.com"; // Your api server that will handle the uploaded data
char apiToken[] = "J_!6tkh0s_Ts|!GU!b5V1U0|FKy4^T@A19WQfpuxcMaN8RfuV?n&7*9ey_T!IuOsYBU#VTwjrznPgISnKxlcg_qGO*101lv12TA3EE|6C|xBv!pm_5u6nDD4*KB-?eXn"; // Not operating anymore don't even try to use this
int status = WL_IDLE_STATUS;
const int CS = 7;
#define SD_CS 9
// TEMPERATUSE SENSOR PORT.
#define TEMPERATURE_SENSOR_PIN 2
OneWire oneWire(TEMPERATURE_SENSOR_PIN);
DallasTemperature dallasSensors(&oneWire);
HM330X particle;
void setup()
{
Serial.begin(9600);
// while(!Serial)
// {
if (!ENV.begin()) {
Serial.println("Failed to initialize MKR ENV shield!");
while (1);
}
//}
setupWiFi(ssid, pass);
dallasSensors.begin();
}
//==================================
//
// IMPORTANT!
// MAIN LOOP CODE!
//
//==================================
void loop()
{
String temp = String((readTemp() + readEnvShieldTemp())/2);
String sound = String(readVolume());
String uv = String(readUV());
String humidity = String(readEnvShieldHumidity());
String pressure = String(readEnvShieldPressure());
String illuminance = String(readEnvShieldIlluminance());
uploadAPIdata(temp, sound, uv, humidity, pressure, illuminance);
}
//==================================
//
// Sensor read code. SET PORTS HERE
//
//==================================
float readTemp()
{
dallasSensors.requestTemperatures();
float temp = dallasSensors.getTempCByIndex(0);
if(temp == DEVICE_DISCONNECTED_C)
{
return 0;
} else
{
return temp;
}
}
float readUV()
{
// UV SENSOR PORT.
return analogRead(A6);
}
float readVolume()
{
// SOUND SENSOR (Envelope) PORT.
return analogRead(A5);
}
float readEnvShieldTemp(){
// Temprature sensor on the shield.
float temperature = ENV.readTemperature();
return temperature;
}
float readEnvShieldHumidity(){
// Humidity sensor on the shield.
float humidity = ENV.readHumidity();
return humidity;
}
float readEnvShieldPressure(){
// Pressure sensor on the shield.
float pressure = ENV.readPressure();
return pressure;
}
float readEnvShieldIlluminance(){
// Illuminance sensor on the shield.
float illuminance = ENV.readIlluminance();
return illuminance;
}
//==================================
//
// INTERNAL WiFi CODE!
// Only modify tokens, addresses.
//
//==================================
// Setup WiFi
void setupWiFi(char ssid[], char password[])
{
// Check for shield.
if(WiFi.status() == WL_NO_SHIELD) {
Serial.println("No WiFi shield connected on the arduino board.");
return;
}
// Code partially copied from example.
int status = WL_IDLE_STATUS;
Serial.print("Attempting to connect to" + ssid+": ");
Serial.println(ssid);
// Try connect to WiFi network.
while(status != WL_CONNECTED)
{
status = WiFi.begin(ssid, password);
delay(2000);
}
Serial.println("Connected to " + String(ssid));
}
boolean uploadAPIdata(String temp, String sound, String uv, String humidity, String pressure, String illuminance)
{
// Enter API token here.
// you need to flash this website on your arduino board or else it won't work.
if(client.connect(server, 443))
{
Serial.println(temp + " " + sound + " " + uv + " " + humidity + " " + pressure + " " + illuminance);
String endpoint = "POST /v1/cubesat?temp=" + temp + "&sound=" + sound + "&uv=" + uv + "&humidity=" + humidity + "&pressure=" + pressure + "&illuminance=" + illuminance + " HTTP/1.1";
Serial.println(endpoint);
client.println(endpoint);
client.println("Host: "+server);
client.println("Authorization: Bearer " + String(apiToken));
client.println("Connection: close");
client.println("");
return true;
}
else
{
Serial.println("Failed to connect to "+server);
return false;
}
}