Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TIES504
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
joalhelk
TIES504
Commits
db52f63e
Commit
db52f63e
authored
3 years ago
by
joalhelk
Browse files
Options
Downloads
Patches
Plain Diff
add arduino code
parent
a30895be
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
arduinoSensor/arduinoSensor.ino
+155
-0
155 additions, 0 deletions
arduinoSensor/arduinoSensor.ino
with
155 additions
and
0 deletions
arduinoSensor/arduinoSensor.ino
0 → 100644
+
155
−
0
View file @
db52f63e
/*
Repeating WiFi Web Client
This sketch connects to a a web server and makes a request
using a WiFi equipped Arduino board.
created 23 April 2012
modified 31 May 2012
by Tom Igoe
modified 13 Jan 2014
by Federico Vanzati
http://www.arduino.cc/en/Tutorial/WifiWebClientRepeating
This code is in the public domain.
*/
#include
<SPI.h>
#include
<WiFiNINA.h>
#include
<ArduinoHttpClient.h>
#include
<ArduinoJson.h>
#include
"arduino_secrets.h"
#include
<Wire.h>
#include
<Adafruit_Sensor.h>
#include
<Adafruit_BME280.h>
#include
"Adafruit_TSL2591.h"
// Initialize sensors
Adafruit_BME280
bme
;
// BME sensor which has pressure, humidity and temperature
Adafruit_TSL2591
tsl
=
Adafruit_TSL2591
(
2591
);
// Luminosity sensor
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char
ssid
[]
=
SECRET_SSID
;
// your network SSID (name)
char
pass
[]
=
SECRET_PASS
;
// your network password (use for WPA, or use as key for WEP)
int
keyIndex
=
0
;
// your network key index number (needed only for WEP)
int
status
=
WL_IDLE_STATUS
;
int
temp
,
humidity
,
luminosity
;
boolean
api_success
=
false
;
// server address:
char
server
[]
=
"192.168.0.8"
;
IPAddress
server_ip
(
192
,
168
,
0
,
8
);
int
port
=
8080
;
// Initialize the WiFi client library
WiFiClient
client
;
HttpClient
http
=
HttpClient
(
client
,
server
,
port
);
unsigned
long
lastConnectionTime
=
0
,
lastConnectionTime2
=
0
,
lastMovementTime
=
0
;
// last time you connected to the server, in milliseconds
const
unsigned
long
postingInterval
=
60L
*
30L
*
1000L
;
// delay (1 800 000 ms = 30 min) between updates, in milliseconds
void
setup
()
{
//Initialize serial and wait for port to open:
Serial
.
begin
(
9600
);
while
(
!
Serial
)
{
;
// wait for serial port to connect. Needed for native USB port only
}
// Initialize pins
bme
.
begin
(
0x76
);
tsl
.
begin
();
tsl
.
setGain
(
TSL2591_GAIN_MED
);
tsl
.
setTiming
(
TSL2591_INTEGRATIONTIME_300MS
);
// check for the WiFi module:
if
(
WiFi
.
status
()
==
WL_NO_MODULE
)
{
Serial
.
println
(
"Communication with WiFi module failed!"
);
// don't continue
while
(
true
);
}
// attempt to connect to WiFi network:
while
(
status
!=
WL_CONNECTED
)
{
Serial
.
print
(
"Attempting to connect to SSID: "
);
Serial
.
println
(
ssid
);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status
=
WiFi
.
begin
(
ssid
,
pass
);
// wait 10 seconds for connection:
delay
(
10000
);
}
// you're connected now, so print out the status:
printWifiStatus
();
}
// MAIN LOOP
void
loop
()
{
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
while
(
client
.
available
())
{
char
c
=
client
.
read
();
Serial
.
write
(
c
);
}
// Send the data
// If 30 minutes has been passed since last posting, then take measurements and post again
if
(
millis
()
-
lastConnectionTime
>
postingInterval
)
{
temp
=
bme
.
readTemperature
();
humidity
=
bme
.
readHumidity
();
luminosity
=
tsl
.
getLuminosity
(
TSL2591_VISIBLE
);
httpRequest
();
}
}
// Lähetetään pilveen sensori1:lle sensoreiden tiedot
void
httpRequest
()
{
// close any connection before send a new request.
// This will free the socket on the NINA module
Serial
.
println
(
"making POST request"
);
String
contentType
=
"application/x-www-form-urlencoded"
;
String
postData
=
"temperature="
;
postData
+=
temp
;
postData
+=
"&humidity="
;
postData
+=
humidity
;
postData
+=
"&luminosity="
;
postData
+=
luminosity
;
postData
+=
"&parent_id="
;
postData
+=
1
;
http
.
post
(
"/api/v1/nodeData"
,
contentType
,
postData
);
// read the status code and body of the response
int
statusCode
=
http
.
responseStatusCode
();
String
response
=
http
.
responseBody
();
Serial
.
print
(
"Status code: "
);
Serial
.
println
(
statusCode
);
Serial
.
print
(
"Response: "
);
Serial
.
println
(
response
);
// note the time that the connection was made:
lastConnectionTime
=
millis
();
}
// Tulostetaan wifin tiedot
void
printWifiStatus
()
{
// print the SSID of the network you're attached to:
Serial
.
print
(
"SSID: "
);
Serial
.
println
(
WiFi
.
SSID
());
// print your board's IP address:
IPAddress
ip
=
WiFi
.
localIP
();
Serial
.
print
(
"IP Address: "
);
Serial
.
println
(
ip
);
// print the received signal strength:
long
rssi
=
WiFi
.
RSSI
();
Serial
.
print
(
"signal strength (RSSI):"
);
Serial
.
print
(
rssi
);
Serial
.
println
(
" dBm"
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment