Skip to content

Commit 702f950

Browse files
authored
Merge pull request #3 from tomcarter868/main
Arduino .ino file changes for 2025 textbook update
2 parents 56daad2 + ccc833d commit 702f950

File tree

12 files changed

+760
-155
lines changed

12 files changed

+760
-155
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#include "MQ135.h" // MQ-135 library
2+
#include <WiFiNINA.h> // WiFi library
3+
#include <SPI.h> // SPI library
4+
5+
const int ANALOGPIN = 0; // Pin to read the MQ-135
6+
const float RZERO = 200.24; // Define the calibration setting
7+
const char* MY_SSID = "SECRET"; // Your WiFi SSID
8+
const char* MY_PWD = "SECRET"; // Your wiFi password
9+
10+
const int SSLPORT = 443; // The port for secure connections
11+
const char WEBSITE[] = "script.google.com"; // Server address to connect to
12+
const char* DEPLOYMENTID = "AKfycbzwlCsp9KRCtJC56aTbOananTum3Y_JATzTkKWB3vB3_HcplhH7UMG8-SwEDc2vXL_9";
13+
// Deployment ID from your script
14+
15+
MQ135 gasSensor = MQ135(ANALOGPIN, RZERO); // Initialise the MQ-135
16+
17+
WiFiSSLClient client; // Initialise the client
18+
19+
int status = WL_IDLE_STATUS; // The WiFi status
20+
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
21+
22+
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
23+
24+
25+
void setup() {
26+
Serial.begin(9600); // Sets the serial port to 9600
27+
while (!Serial) {
28+
; // Wait for serial. Remember - this blocks so you might prefer a delay!
29+
}
30+
31+
if (WiFi.status() == WL_NO_MODULE) {
32+
Serial.println("Communication with WiFi module failed!");
33+
while (true);
34+
}
35+
36+
while (status != WL_CONNECTED) {
37+
Serial.print("Attempting to connect to WPA SSID: ");
38+
Serial.println(MY_SSID);
39+
status = WiFi.begin(MY_SSID, MY_PWD);
40+
delay(10000);
41+
}
42+
Serial.println("Connected");
43+
printCurrentNet();
44+
printWiFiData();
45+
}
46+
47+
void loop() {
48+
if (millis() - lastConnectionTime > postingInterval) {
49+
float ppm = gasSensor.getPPM(); // Read the parts per million
50+
Serial.println(ppm); // Output the parts per million
51+
// Build the URL string
52+
String URL = (String) "/macros/s/"+DEPLOYMENTID+"/exec?reading="+ (String) ppm;
53+
httpRequest(URL);
54+
}
55+
}
56+
57+
// this method makes a HTTP connection to the server:
58+
void httpRequest(String URL) {
59+
// close any connection before sending a new request.
60+
// This will free the socket on the Nina module
61+
client.stop();
62+
// if there's a successful connection:
63+
if (client.connect(WEBSITE, SSLPORT)) {
64+
Serial.println("connecting...");
65+
// send the HTTP PUT request:
66+
client.println("GET "+URL+" HTTP/1.1");
67+
client.println("Host: "+(String) WEBSITE);
68+
client.println("User-Agent: ArduinoWiFi/1.1");
69+
client.println("Connection: close");
70+
client.println();
71+
Serial.println("Data sent.");
72+
// note the time that the connection was made:
73+
lastConnectionTime = millis();
74+
} else {
75+
// if you couldn't make a connection:
76+
Serial.println("connection failed");
77+
}
78+
}
79+
80+
void printWiFiData() {
81+
82+
// print your WiFi shield's IP address:
83+
84+
IPAddress ip = WiFi.localIP();
85+
86+
Serial.print("IP Address: ");
87+
88+
Serial.println(ip);
89+
90+
Serial.println(ip);
91+
92+
// print your MAC address:
93+
94+
byte mac[6];
95+
96+
WiFi.macAddress(mac);
97+
98+
Serial.print("MAC address: ");
99+
100+
printMacAddress(mac);
101+
102+
}
103+
104+
void printCurrentNet() {
105+
106+
// print the SSID of the network you're attached to:
107+
108+
Serial.print("SSID: ");
109+
110+
Serial.println(WiFi.SSID());
111+
112+
// print the MAC address of the router you're attached to:
113+
114+
byte bssid[6];
115+
116+
WiFi.BSSID(bssid);
117+
118+
Serial.print("BSSID: ");
119+
120+
printMacAddress(bssid);
121+
122+
// print the received signal strength:
123+
124+
long rssi = WiFi.RSSI();
125+
126+
Serial.print("signal strength (RSSI):");
127+
128+
Serial.println(rssi);
129+
130+
// print the encryption type:
131+
132+
byte encryption = WiFi.encryptionType();
133+
134+
Serial.print("Encryption Type:");
135+
136+
Serial.println(encryption, HEX);
137+
138+
Serial.println();
139+
}
140+
141+
void printMacAddress(byte mac[]) {
142+
143+
for (int i = 5; i >= 0; i--) {
144+
145+
if (mac[i] < 16) {
146+
147+
Serial.print("0");
148+
149+
}
150+
151+
Serial.print(mac[i], HEX);
152+
153+
if (i > 0) {
154+
155+
Serial.print(":");
156+
157+
}
158+
159+
}
160+
161+
Serial.println();
162+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// from https://docs.arduino.cc/tutorials/mkr-wifi-1010/scan-networks/
2+
#include <SPI.h>
3+
#include <WiFiNINA.h>
4+
5+
void setup() {
6+
//Initialize serial and wait for port to open:
7+
Serial.begin(9600);
8+
while (!Serial) {
9+
; // wait for serial port to connect. Needed for native USB port only
10+
}
11+
12+
// check for the WiFi module:
13+
if (WiFi.status() == WL_NO_MODULE) {
14+
Serial.println("Communication with WiFi module failed!");
15+
// don't continue
16+
while (true);
17+
}
18+
19+
String fv = WiFi.firmwareVersion();
20+
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
21+
Serial.println("Please upgrade the firmware");
22+
}
23+
24+
// print your MAC address:
25+
byte mac[6];
26+
WiFi.macAddress(mac);
27+
Serial.print("MAC: ");
28+
printMacAddress(mac);
29+
}
30+
31+
void loop() {
32+
// scan for existing networks:
33+
Serial.println("Scanning available networks...");
34+
listNetworks();
35+
delay(10000);
36+
}
37+
38+
void listNetworks() {
39+
// scan for nearby networks:
40+
Serial.println("** Scan Networks **");
41+
int numSsid = WiFi.scanNetworks();
42+
if (numSsid == -1) {
43+
Serial.println("Couldn't get a wifi connection");
44+
while (true);
45+
}
46+
47+
// print the list of networks seen:
48+
Serial.print("number of available networks:");
49+
Serial.println(numSsid);
50+
51+
// print the network number and name for each network found:
52+
for (int thisNet = 0; thisNet < numSsid; thisNet++) {
53+
Serial.print(thisNet);
54+
Serial.print(") ");
55+
Serial.print(WiFi.SSID(thisNet));
56+
Serial.print("\tSignal: ");
57+
Serial.print(WiFi.RSSI(thisNet));
58+
Serial.print(" dBm");
59+
Serial.print("\tEncryption: ");
60+
printEncryptionType(WiFi.encryptionType(thisNet));
61+
}
62+
}
63+
64+
void printEncryptionType(int thisType) {
65+
// read the encryption type and print out the title:
66+
switch (thisType) {
67+
case ENC_TYPE_WEP:
68+
Serial.println("WEP");
69+
break;
70+
case ENC_TYPE_TKIP:
71+
Serial.println("WPA");
72+
break;
73+
case ENC_TYPE_CCMP:
74+
Serial.println("WPA2");
75+
break;
76+
case ENC_TYPE_NONE:
77+
Serial.println("None");
78+
break;
79+
case ENC_TYPE_AUTO:
80+
Serial.println("Auto");
81+
break;
82+
case ENC_TYPE_UNKNOWN:
83+
default:
84+
Serial.println("Unknown");
85+
break;
86+
}
87+
}
88+
89+
void printMacAddress(byte mac[]) {
90+
for (int i = 5; i >= 0; i--) {
91+
if (mac[i] < 16) {
92+
Serial.print("0");
93+
}
94+
Serial.print(mac[i], HEX);
95+
if (i > 0) {
96+
Serial.print(":");
97+
}
98+
}
99+
Serial.println();
100+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "GY521.h"
2+
3+
float x = 0, y = 0, z = 0; // x,y and z readings initialised to 0
4+
5+
GY521 sensor(0x68);
6+
7+
void setup() {
8+
Serial.begin(9600); // Try to connect to the serial monitor
9+
delay(5000); // Wait for the serial monitor
10+
Serial.println("Serial started");
11+
Wire.begin(); //Connect to the accelerometer bus
12+
while (1) { // Keep trying
13+
if (sensor.wakeup()) { // Is there an accel connection?
14+
Serial.println("Accellerometer connected");
15+
break; //and leave the loop
16+
} else {
17+
Serial.println("Accellerometer is not connected");
18+
}
19+
}
20+
delay(2000); //Wait 2s for everythign to settle down
21+
sensor.setAccelSensitivity(0); // 2g
22+
}
23+
24+
void loop() {
25+
sensor.read(); // Read the sensor data
26+
x = sensor.getAccelX(); // Get X acceleration
27+
y = sensor.getAccelY(); // Get Y acceleration
28+
z = sensor.getAccelZ(); // Get Z acceleration
29+
Serial.print(x); // Output the data on a single line
30+
Serial.print(" | ");
31+
Serial.print(y);
32+
Serial.print(" | ");
33+
Serial.print(z);
34+
Serial.print(" | ");
35+
Serial.println();
36+
delay(500); //Wait before checking again.
37+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "GY521.h"
2+
3+
float x = 0, y = 0, z = 0; // x,y and z readings initialised to 0
4+
float old_x = 0, old_y = 0, old_z = 0; // old x, y and z readings to find change
5+
String x_diff = "", y_diff = "", z_diff = ""; // x, y and z differences
6+
GY521 sensor(0x68);
7+
8+
void setup() {
9+
Serial.begin(9600); // Try to connect to the serial monitor
10+
delay(5000); // Wait for the serial monitor
11+
Serial.println("Serial started");
12+
Wire.begin(); //Connect to the accelerometer bus
13+
while (1) { // Keep trying
14+
if (sensor.wakeup()) { // Is there an accel connection?
15+
Serial.println("Accellerometer connected");
16+
break; //and leave the loop
17+
} else {
18+
Serial.println("Accellerometer is not connected");
19+
}
20+
}
21+
delay(2000); //Wait 2s for everythign to settle down
22+
sensor.setAccelSensitivity(0); // 2g
23+
}
24+
25+
void loop() {
26+
sensor.read(); // Read the sensor data
27+
old_x = x; // Store old X acceleration
28+
old_y = y; // Store old Y acceleration
29+
old_z = z; // Store old Z acceleration
30+
x = sensor.getAccelX(); // Get X acceleration
31+
y = sensor.getAccelY(); // Get Y acceleration
32+
z = sensor.getAccelZ(); // Get Z acceleration
33+
x_diff = String(x - old_x); // Calculate X difference
34+
y_diff = String(y - old_y); // Calculate Y difference
35+
z_diff = String(z - old_z); // Calculate Z difference
36+
if (x_diff.length() == 4) {
37+
x_diff = " "+x_diff;
38+
}
39+
if (y_diff.length() == 4) {
40+
y_diff = " "+y_diff;
41+
}
42+
if (z_diff.length() == 4) {
43+
z_diff = " "+z_diff;
44+
}
45+
46+
Serial.print("\t");
47+
Serial.print(x);
48+
Serial.print("\t");
49+
Serial.print(x_diff); // Output the data on a single line
50+
Serial.print("\t|\t");
51+
Serial.print(y);
52+
Serial.print("\t");
53+
Serial.print(y_diff);
54+
Serial.print("\t|\t");
55+
Serial.print(z);
56+
Serial.print("\t");
57+
Serial.print(z_diff);
58+
Serial.print("\t|\t");
59+
Serial.println();
60+
delay(50); //Wait before checking again.
61+
}

0 commit comments

Comments
 (0)