/*
WiFi 101 ThingSpeak Data Uploader
Hardware Required:
* Arduino Zero or Uno Board
* Arduino Wifi Shield 101
* Photocell
* Temperature Sensor (This example uses a TMP36)
* 10K Ohm Resistor
created Sept 2015
by Helena Bisby <[email protected]>
This example code is in the public domain
http://arduino.cchttps://www.arduino.cc/en/Tutorial/WiFi101ThingSpeakDataUploader
*/
#include <string.h> //Biblioteka za rad sa string-ovima, koristi se za strcmp funkcijum
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "Wokwi-GUEST"; // your network SSID (name)
char 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)
String SendATCmdAndGetResponse(String str)
{
Serial1.println(str);
String response = "";
if(Serial1.available() > 0)
{
response = Serial1.readString();
}
return response;
}
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String APIKey = "1G4LCJ8KFWS2UDGD"; // enter your channel's Write API Key
const int updateThingSpeakInterval = 20 * 1000; // 20 second interval at which to update ThingSpeak
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
// Initialize Arduino Ethernet Client
//WiFiClient client;
void setup() {
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
Serial1.begin(9600); //Serial for at command
Serial.println("Connecting with WiFi 'Wokwi-GUEST'...");
Serial.print(SendATCmdAndGetResponse("AT+CWJAP=\"Wokwi-GUEST\",")); //connect to wifi
printWifiStatus();
}
void loop() {
// read values from pins and store as strings
String light = String(analogRead(A0), DEC); // read light value
// find temp value
float voltage = analogRead(A1) * (3.3 / 1024); // convert 0-1023 range to 3.3V range
int tempVal = (voltage - 0.5) * 100; // convert voltage to temperature in *C
String temp = String(tempVal);
// Disconnect from ThingSpeak
if (strchr(SendATCmdAndGetResponse("AT+CWSTATE?"), '2') != NULL && lastConnected) {
Serial.println("...disconnected");
Serial.println(SendATCmdAndGetResponse("AT+CWQAP"));
}
// Update ThingSpeak
if (strchr(SendATCmdAndGetResponse("AT+CWSTATE?"), '2') != NULL && (millis() - lastConnectionTime > updateThingSpeakInterval)) {
updateThingSpeak("\"field1: " + light + "\",\"field2: " + temp + "\"");
Serial.println(light);
Serial.println(temp);
}
lastConnected = strchr(SendATCmdAndGetResponse("AT+CWSTATE?"), '2') != NULL;
}
void updateThingSpeak(String tsData) {
String httpCommand = "AT+HTTPCPOST=\"https://api.thingspeak.com/update.json\",5\
[\"Host: api.thingspeak.com\",\"Connection: close\",\"Content-Type: application/x-www-form-urlencoded\",\
\"api_key: " + APIKey + "\",\
\"Content-Type: application/x-www-form-urlencoded\"," + tsData + "\",\"channel_id:1724077\"]";
Serial.println("--SENDING TO THINKSPEAK--");
Serial.println(httpCommand);
Serial.println("--STATUS--");
Serial.println(SendATCmdAndGetResponse(httpCommand));
/*
if (client.connect(thingSpeakAddress, 80)) {
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + APIKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected()) {
Serial.println("Connecting to ThingSpeak...");
Serial.println();
}
}*/
}
void printWifiStatus() {
String cwState = SendATCmdAndGetResponse("AT+CWSTATE?");
if(strchr(cwState, '2') != NULL){
Serial.print("SSID: ");
Serial.println( (cwState.split(',')[1]).split(' ')[0] );
} else {
Serial.println(cwState);
}
String cwLif = SendATCmdAndGetResponse("AT+CWLIF");
Serial.print("IP Address: ");
Serial.println((cwLif.split(':')[1]).split(',')[0]);
String cwLap = SendATCmdAndGetResponse("AT+CWLAP");
Serial.print("Ostali podaci o WiFi signalu: ");
Serial.println(cwLap);
}