int inPin=D3; int val = 0; // variable to store the read value int vals[]={0,0,0,0}; int mycounter=1; int readbits=0; int onecounter=0; int odotusaika=10; int barcode[100]; int barcodejarjestys; #include #define NEOPIXELPIN D8 // Datapin for the the built-in NeoPixel #define NUMPIXELS 6 //Amount of NeoPixels Adafruit_NeoPixel pixels(NUMPIXELS, NEOPIXELPIN, NEO_GRB + NEO_KHZ800); #define DELAYVAL 500 // Time (in milliseconds) to pause between loop #include // In U8g2 library // Seeed Expansion board uses SSD1306 U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset= */ U8X8_PIN_NONE); // Load Wi-Fi library #include #include // Replace with your network credentials const char* ssid = "Kalaksi"; const char* password = "testisalasana"; const char *ID = "lahettajaid"; // Name of our device, must be unique const char *TOPIC = "testTopic"; IPAddress broker(192,168,169,4); // IP address of your MQTT broker eg. 192.168.1.50 WiFiClient wclient; PubSubClient pclient(wclient); // Setup MQTT client // Set web server port number to 80 WiFiServer server(80); // Variable to store the HTTP request String header; // Current time unsigned long currentTime = millis(); // Previous time unsigned long previousTime = 0; // Define timeout time in milliseconds (example: 2000ms = 2s) const long timeoutTime = 2000; char Aarnebuffer[30]; char infoBuffer[30]; char infoBuffer2[30]; char webinfo[400]; char webinfo2[400]; char webinfo3[400]; char webinfo4[400]; //String Aarnestring; char printBuffer[30]; char printBuffer2[30]; char printBuffer3[30]; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(inPin, INPUT); // sets the digital pin 7 as input barcodejarjestys=0; pixels.begin(); u8g2.begin(); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } pclient.setServer(broker, 1883); // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); //Aarnestring=WiFi.localIP().toString(); //Serial.println(Aarnestring); IPAddress ip = WiFi.localIP(); sprintf(Aarnebuffer, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]); //from https://arduino.stackexchange.com/questions/30534/formatting-of-ipaddress-via-printf-family-similar-to-serial-object Serial.println(Aarnebuffer); //sprintf(Aarnebuffer,"%s",WiFi.localIP()); //Serial.println(Aarnebuffer); //Aarnestring=Aarnebuffer; //Serial.println(Aarnestring); server.begin(); sprintf(infoBuffer,"Welcome recycler!"); sprintf(infoBuffer2,"(c) Aarne Pohjonen"); sprintf(webinfo,"
"); sprintf(webinfo2,""); sprintf(webinfo4,"
"); for (int ee=0;ee<6;ee=ee+1) { pixels.setPixelColor(ee, pixels.Color(0, 0, 0)); } } // Reconnect to client void reconnect() { // Loop until we're reconnected while (!pclient.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (pclient.connect(ID)) { Serial.println("connected"); Serial.print("Publishing to: "); Serial.println(TOPIC); Serial.println('\n'); } else { Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } uint8_t passno; int dataatulee=0; void loop() { if (!pclient.connected()) // Reconnect if connection is lost { reconnect(); } pclient.loop(); // put your main code here, to run repeatedly: WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects, currentTime = millis(); previousTime = currentTime; Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected currentTime = millis(); if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // turns the GPIOs on and off /* if (header.indexOf("GET /26/on") >= 0) { Serial.println("GPIO 26 on"); output26State = "on"; digitalWrite(output26, HIGH); } else if (header.indexOf("GET /26/off") >= 0) { Serial.println("GPIO 26 off"); output26State = "off"; digitalWrite(output26, LOW); } else if (header.indexOf("GET /27/on") >= 0) { Serial.println("GPIO 27 on"); output27State = "on"; digitalWrite(output27, HIGH); } else if (header.indexOf("GET /27/off") >= 0) { Serial.println("GPIO 27 off"); output27State = "off"; digitalWrite(output27, LOW); }*/ // Display the HTML web page client.println(""); client.println(""); client.println(""); client.print(""); // CSS to style the on/off buttons // Feel free to change the background-color and font-size attributes to fit your preferences client.println(""); // Web Page Heading client.println("

Recycling info

"); client.println(webinfo); client.println(webinfo2); client.println(webinfo3); client.println(webinfo4); // Display current state, and ON/OFF buttons for GPIO 26 //client.println("

GPIO 26 - State " + output26State + "

"); // If the output26State is off, it displays the ON button /* if (output26State=="off") { client.println("

"); } else { client.println("

"); } */ // Display current state, and ON/OFF buttons for GPIO 27 /*client.println("

GPIO 27 - State " + output27State + "

"); // If the output27State is off, it displays the ON button if (output27State=="off") { client.println("

"); } else { client.println("

"); }*/ client.println(""); // The HTTP response ends with another blank line client.println(); // Break out of the while loop break; } else { // if you got a newline, then clear currentLine currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // Clear the header variable header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); } if(millis()%1000==0) { //snprintf(printBuffer, sizeof(printBuffer), " Pass %3d", ++passno); snprintf(printBuffer, sizeof(printBuffer), Aarnebuffer); snprintf(printBuffer2, sizeof(printBuffer2), infoBuffer); snprintf(printBuffer3, sizeof(printBuffer3), infoBuffer2); u8g2.clearBuffer(); // clear the internal memory u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font //u8g2.drawStr(0, 10, " Greetings from Aarne!"); //u8g2.drawStr(0, 10, Aarnestring); //u8g2.drawStr(0, 10, WiFi.localIP()); //u8g2.drawStr(0, 10, " Hello XIAO RP2040!"); u8g2.drawStr(0, 10, printBuffer); u8g2.drawStr(0, 20, printBuffer2); u8g2.drawStr(0, 30, printBuffer3); //u8g2.drawStr(0, 63, "davekw7x"); u8g2.sendBuffer(); // transfer internal memory to the display } if(dataatulee==0&&millis()%(odotusaika/10)==0){ //delay(odotusaika/10); val=digitalRead(inPin); if(val==1) { dataatulee=1; delay(odotusaika/2-odotusaika/10); } } while(dataatulee==1){ val=digitalRead(inPin); //if(readbits==0){ //Serial.print(val); //} mycounter=mycounter+1; vals[mycounter-1]=val; if((val==1)&&(readbits==0)) { onecounter=onecounter+1; //Serial.println("lisattiin yksi"); if(onecounter==4){ readbits=1; onecounter=0; mycounter=0; Serial.println("Start reading"); } } if((val==0)&&(readbits==0)) { onecounter=0; } if((readbits==1)&&(mycounter==4)) { // Serial.println("aaaa"); // for(int kk=0;kk<=3;kk=kk+1){ // Serial.print(vals[kk]); // } // Serial.println("bbbb"); if(vals[0]==0&&vals[1]==0&&vals[2]==0&&vals[3]==1) barcode[barcodejarjestys]=1;//Serial.println("got 1"); if(vals[0]==0&&vals[1]==0&&vals[2]==1&&vals[3]==0) barcode[barcodejarjestys]=2;//Serial.println("got 2"); if(vals[0]==0&&vals[1]==0&&vals[2]==1&&vals[3]==1) barcode[barcodejarjestys]=3;//Serial.println("got 3"); if(vals[0]==0&&vals[1]==1&&vals[2]==0&&vals[3]==0) barcode[barcodejarjestys]=4;//Serial.println("got 4"); if(vals[0]==0&&vals[1]==1&&vals[2]==0&&vals[3]==1) barcode[barcodejarjestys]=5;//Serial.println("got 5"); if(vals[0]==0&&vals[1]==1&&vals[2]==1&&vals[3]==0) barcode[barcodejarjestys]=6;//Serial.println("got 6"); if(vals[0]==0&&vals[1]==1&&vals[2]==1&&vals[3]==1) barcode[barcodejarjestys]=7;//Serial.println("got 7"); if(vals[0]==1&&vals[1]==0&&vals[2]==0&&vals[3]==0) barcode[barcodejarjestys]=8;//Serial.println("got 8"); if(vals[0]==1&&vals[1]==0&&vals[2]==0&&vals[3]==1) barcode[barcodejarjestys]=9;//Serial.println("got 9"); if(vals[0]==1&&vals[1]==0&&vals[2]==1&&vals[3]==0) barcode[barcodejarjestys]=0;//Serial.println("got 0"); barcodejarjestys=barcodejarjestys+1; if(vals[0]==0&&vals[1]==0&&vals[2]==0&&vals[3]==0) { Serial.println("Stop reading"); readbits=0; dataatulee=0; Serial.println(barcodejarjestys); for(int uu=0;uu"); sprintf(webinfo2,""); sprintf(webinfo4,""); pclient.publish(TOPIC, "0001 5702071381464"); pclient.publish(TOPIC, "0002 5702071381464"); } else if(array_cmp(barcode, verrattava2, merkkienlkm, lenArray2)){ Serial.println("sama kuin 2"); ledit[0]=1; sprintf(infoBuffer,"Wrapping: Plastic C"); sprintf(infoBuffer2,""); sprintf(webinfo,"
"); sprintf(webinfo2,""); sprintf(webinfo4,"
"); pclient.publish(TOPIC, "0003 6411200112600"); //ledit[2]=1; } else { Serial.println("ei mikaan noista"); } R=0; G=255; B=0; pixels.clear(); for (int ee=0;ee<9;ee=ee+1) { if(ledit[ee]==1) { pixels.setPixelColor(ee, pixels.Color(R, G, B)); Serial.println(ee);} } pixels.show(); // Send the updated pixel colors to the hardware. delay(DELAYVAL); // wait between pixels.clear(); //for (int oo=0;oo<9;oo=oo+1) { // Serial.println(ledit[oo]); // } /*issame=0; if(merkkienlkm-1==lenArray1){ int issame=1; for(int rr=0;rr