Giter VIP home page Giter VIP logo

Comments (1)

whatislove23 avatar whatislove23 commented on August 10, 2024

#include "WiFiEsp.h"
#include "SoftwareSerial.h"
#include <SPI.h>
#include <SD.h>
#define R_PIN 5
#define G_PIN 6
#define B_PIN 7
#define BUTTON_PIN 8
#define SD_ChipSelectPin 4
uint8_t btn_prev;
char ssid[] = "Home"; // your network SSID (name)
char pass[] = "23042002"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "192.168.31.160";
const int CHUNK_SIZE = 64; // Adjust the chunk size as needed
char mybuffer[CHUNK_SIZE];
WiFiEspClient client;
SoftwareSerial Serial1(3, 2); // RX, TX

void setRGBColor(int mode) {
switch (mode) {
case 0:
analogWrite(R_PIN, 0);
analogWrite(G_PIN, 0);
analogWrite(B_PIN, 0);
break;
case 1:
// Set color for mode 1 (e.g., red)
analogWrite(R_PIN, 255);
analogWrite(G_PIN, 0);
analogWrite(B_PIN, 0);
break;

case 2:
  // Set color for mode 2 (e.g., green)
  analogWrite(R_PIN, 0);
  analogWrite(G_PIN, 255);
  analogWrite(B_PIN, 0);
  break;

case 3:
  // Set color for mode 3 (e.g., blue)
  analogWrite(R_PIN, 0);
  analogWrite(G_PIN, 0);
  analogWrite(B_PIN, 255);
  break;

  // Add more cases as needed for different modes
case 4:
  // Set color for mode 4 (e.g., purple)
  analogWrite(R_PIN, 255);
  analogWrite(G_PIN, 0);  // Adjust green value for orange
  analogWrite(B_PIN, 255);
  break;
default:
  // Default case (e.g., turn off)
  analogWrite(R_PIN, 0);
  analogWrite(G_PIN, 0);
  analogWrite(B_PIN, 0);
  break;

}
}

void setup() {
setRGBColor(3);
pinMode(R_PIN, OUTPUT);
pinMode(G_PIN, OUTPUT);
pinMode(B_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
Serial.begin(115200);
Serial1.begin(9600);
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (true) setRGBColor(1);
}
Serial.println("initialization done.");
WiFi.init(&Serial1);
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true) setRGBColor(1);
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
}
setRGBColor(2);
}

void saveAudioToFile() {
File audioFile = SD.open("audio.wav", FILE_WRITE );

if (audioFile) {
while (client.available()) {
int bytesRead = client.readBytes(mybuffer, CHUNK_SIZE);
audioFile.write(mybuffer, bytesRead);
Serial.print(".");
}
Serial.println("Audio saved to SD card.");
audioFile.close();
setRGBColor(2);

} else {
Serial.println("Error opening audio file for saving!");
}
}
void loop() {
uint8_t btn = digitalRead(BUTTON_PIN);

if (btn == LOW && btn_prev == HIGH) {
setRGBColor(4);
/* RECORD SPEECH/ SAVE TO SD/ SEND FROM SD TO SERVER
TO DO
*/
if (client.connect(server, 5000)) {
Serial.println("Connected to server");
int value = 2.5; // an arbitrary value for testing
String content = "{"JSON_key": " + String(value) + "}";
client.println("POST /post HTTP/1.1");
client.println("Host: 192.168.31.160:5000");
client.println("Accept: /");
client.println("Content-Length: " + String(content.length()));
client.println("Content-Type: application/json");
client.println();
client.println(content);
}
if (client.connect(server, 5000)) {
Serial.println("Connected to server");
client.println("GET / HTTP/1.1");
client.println("Host: 192.168.31.160:5000");
client.println("Connection: close");
client.println();
}
}
if(client.available()){
Serial.println(1);
saveAudioToFile();
}
btn_prev = digitalRead(BUTTON_PIN);
}

from wifiesp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.