Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ESP8266 NodeMCU AJAX Demo
- * Updates and Gets data from webpage without page refresh
- */
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #ifndef UNIT_TEST
- #include <Arduino.h>
- #endif
- #include <IRremoteESP8266.h>
- #include <IRsend.h>
- #define LED 2 //On board LED
- #define IR_LED 4 // ESP8266 GPIO pin to use. Recommended: 4 (D2).
- #include "index.h" //Our HTML webpage contents with javascripts
- IRsend irsend(IR_LED); // (pin, not inverted, and use modulation) Set the GPIO to be used to sending the message.
- String readText;
- unsigned int khz = 38; // 38kHz carrier frequency for infoglobe
- unsigned char header[] = {0x04}; //0x01: static, 0x04: scrolling
- unsigned char message[35] = {"Thank you very much Crankyoldgit"};
- //SSID and Password of your WiFi router
- const char* ssid = "xxxxx";
- const char* password = "xxxxx";
- ESP8266WebServer server(80); //Server on port 80
- //===============================================================
- // This routine is executed when you open its IP in browser
- //===============================================================
- void handleRoot() {
- String s = MAIN_page; //Read HTML contents
- server.send(200, "text/html", s); //Send web page
- }
- void handleReadMessage() {
- server.send(200, "text/plane", readText); //Send ADC value only to client ajax request
- }
- void handleMessage() {
- readText = server.arg("textbox1"); //Refer xhttp.open("GET", "setLED?LEDstate="+led, true);
- Serial.print("Current Message: ");
- Serial.println(readText);
- server.send(200, "text/plane", readText); //Send web page
- memset(&message[0], 0, sizeof(message));
- for (int j = 0; j < readText.length(); ++j) {
- message[j] = readText[j];
- }
- for (int i = 0; i < sizeof(message); ++i) {
- if (message[i] == 0x00) {
- Serial.println("null char");
- }
- else {
- Serial.println(message[i]);
- }
- }
- sendHexRaw(header, 1, 38); //header at 38kHz
- sendHexRaw(message, readText.length(), 38); // Send a raw data capture at 38kHz.
- digitalWrite(IR_LED, LOW);
- delay(100);
- }
- //==============================================================
- // SETUP
- //==============================================================
- void setup(void){
- readText = "Thank you very much Crankyoldgit";
- Serial.begin(115200);
- WiFi.begin(ssid, password); //Connect to your WiFi router
- Serial.println("");
- //Onboard LED port Direction output
- pinMode(LED,OUTPUT);
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- //If connection successful show IP address in serial monitor
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP()); //IP address assigned to your ESP
- server.on("/", handleRoot); //Which routine to handle at root location. This is display page
- server.on("/setMessage", handleMessage);
- server.on("/readMessage", handleReadMessage);
- server.begin(); //Start server
- Serial.println("HTTP server started");
- irsend.begin();
- Serial.begin(115200, SERIAL_8N1);
- readText = "Thank you very much Crankyoldgit"; //current implementation has this match 'message'
- }
- //==============================================================
- // LOOP
- //==============================================================
- void loop(void){
- server.handleClient(); //Handle client requests
- }
- void sendHexRaw(unsigned char *sigArray, unsigned int sizeArray, unsigned int khz) {
- /* HEADER - the byte determines the transition effect
- When debugging note that first four bits of header should always be '0'
- 00 - loads message to buffer for transition effect
- 01 - Static message - blanks without message
- 02 - Flashing static message
- 03 - matches static/scroll of previous message
- 04 - scrolling (blanks without message)
- 05 - Overwrite portion of existing message
- 06 - Toggles scrolling when sent without a message
- MESSAGE - 35 characters max - if sending a message each byte is an ASCII character.
- characters not supported: % & + ; @ [ \ ] ^ _ ` { | } ~
- see http://hanixdiy.blogspot.com/2010/10/hacking-infoglobe-part-3.html for more details
- */
- uint32_t sigTime = micros();
- uint32_t delayTime;
- irsend.enableIROut(khz,33);
- for (unsigned int i = 0; i < sizeArray; i++) { //iterate thru each byte in sigArray
- register uint8_t bitMask = 0x80; //starting value of bitmask fo each Hex byte
- while (bitMask) { //do 8 times for each bit of the 8 bit byte
- sigTime += 1000; //Time 1ms after our last operation (or start)
- delayTime = sigTime - micros(); //The difference between current time and 1ms after our last bit
- if (bitMask & sigArray[i]) { //its a One bit
- irsend.space(delayTime); // LED off for 1000 usec = 1msec
- //Serial.print('1'); //for debug - ruins timing but ensures you use the correct bits
- }
- else { // its a Zero bit
- irsend.mark(delayTime);
- //Serial.print('0'); //for debug - ruins timing but ensures you use the correct bits
- }
- bitMask = (unsigned char) bitMask >> 1; // shift mask bit along until it reaches zero > exit the loop
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement