Skip to content

Instantly share code, notes, and snippets.

@turnipsoup
Last active July 23, 2020 18:06
Show Gist options
  • Save turnipsoup/9ab4ab3e3643913f15df1b60e9942d53 to your computer and use it in GitHub Desktop.
Save turnipsoup/9ab4ab3e3643913f15df1b60e9942d53 to your computer and use it in GitHub Desktop.
Read from analog pin and generate HTTP POST message with data on an Arduino Nano 33 IOT
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
/*
Will take the reading on analog pin 0 and pass the data
via HTTP POST method to a server expecting it (Flask
on a Pi)
Original by Tom Igoe
*/
#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// Wifi Settings ///////
char ssid[] = YOUR_SSID;
char pass[] = YOUR_WIFI_PASSWORD;
char serverAddress[] = "192.168.1.18"; // server address
int port = 5000;
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
void setup() {
//Initialize serial and wait for port to open:
//Serial.begin(9600);
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
//Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
//Serial.println("Please upgrade the firmware");
}
// 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();
}
void loop() {
postRequest();
delay(1000);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("==========");
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");
Serial.print("==========");
Serial.print("");
}
void postRequest() {
// Here we read the data from pin 0
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3.3V):
float voltage = sensorValue * (3.3 / 1023.0);
// print out the value you read:
//Serial.println(voltage);
// Here we make the actual post request
//Serial.println("Making POST request");
String contentType = "application/x-www-form-urlencoded";
String postData = "raw=" + String(sensorValue) + "&voltage=" + String(voltage);
client.post("/", contentType, postData);
// read the status code and body of the response
int statusCode = client.responseStatusCode();
String response = client.responseBody();
//Serial.print("Status code: ");
//Serial.println(statusCode);
//Serial.print("Response: ");
//Serial.println(response);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment